// file: $isip/class/search/TrainNode/TrainNode.h
//

// make sure definitions are only made once
//
#ifndef ISIP_TRAIN_NODE
#define ISIP_TRAIN_NODE

#ifndef ISIP_LONG
#include <Long.h>
#endif

#ifndef ISIP_VECTOR
#include <Vector.h>
#endif

#ifndef ISIP_VECTOR_FLOAT
#include <VectorFloat.h>
#endif

#ifndef ISIP_VECTOR_DOUBLE
#include <VectorDouble.h>
#endif

#ifndef ISIP_CONTEXT
#include <Context.h>
#endif

#ifndef ISIP_STATISTICAL_MODEL
#include <StatisticalModel.h>
#endif

// TrainNode: A class to store training information for each state in
// the search space.
//
class TrainNode {

  //---------------------------------------------------------------------------
  //
  // public constants
  //
  //---------------------------------------------------------------------------
public:

  // define the class name
  //
  static const String CLASS_NAME;

  //----------------------------------------
  //
  // i/o related constants
  //
  //----------------------------------------  

  static const String DEF_PARAM;

  //----------------------------------------
  //
  // other important constants
  //
  //----------------------------------------  
  
  //----------------------------------------
  //
  // default values and arguments
  //
  //----------------------------------------  

  // define the default time stamp
  //
  static const long DEF_TIMESTAMP = -1;
  
  //---------------------------------------
  //
  // error codes
  //
  //---------------------------------------
  
  //---------------------------------------------------------------------------
  //
  // protected data
  //
  //---------------------------------------------------------------------------
protected:
  
  // define the time instance (t)
  //
  long frame_d;
  
  // define the backward probability for the state at time (t)
  //  
  double beta_d;

  // define the forward probability for the state at time (t)
  //  
  double alpha_d;  

  // define the score associated with the  state at time (t)
  //  
  double score_d;
  
  // define a flag that tells us if the train node is reachable form
  // the valid hypothesis
  //
  boolean is_valid_d;
  boolean is_alpha_valid_d;
  boolean is_beta_valid_d;
  boolean is_accum_valid_d;
  
  // define the trace/instance reference pointer
  //
  Context* reference_d;

  // define the statistical model pointer
  //
  StatisticalModel* stat_model_d;
  
  // define a static debug level
  //
  static Integral::DEBUG debug_level_d;
  
  // define a static memory manager
  //
  static MemoryManager mgr_d;

  //---------------------------------------------------------------------------
  //
  // required public methods
  //
  //---------------------------------------------------------------------------
public:

  // method: name
  //
  static const String& name() {
    return CLASS_NAME;
  }

  // method: diagnose
  //
  static boolean diagnose(Integral::DEBUG debug_level);

  // method: debug
  //
  boolean debug(const unichar* message) const;

  // method: setDebug
  //
  static boolean setDebug(Integral::DEBUG debug_level) {
    debug_level_d = debug_level;
    return true;
  }
  
  // method: destructor
  //
  ~TrainNode() {
    if (debug_level_d >= Integral::ALL) {     
      fprintf(stdout, "Destructor of train_node: %p\n", this);
      fflush(stdout);
    }
  }

  // constructor(s)
  //
  TrainNode();
  TrainNode(const TrainNode& copy_node);
  
  // assign methods
  //
  boolean assign(const TrainNode& copy_node);

  // method: sofSize
  //
  long sofSize() const {
    return Error::handle(name(), L"sofSize", Error::ARG, __FILE__, __LINE__);
  }
  
  // method: read
  //
  boolean read(Sof& sof, long tag, const String& cname = CLASS_NAME) {
    return Error::handle(name(), L"read", Error::ARG, __FILE__, __LINE__);
  }

  // method: write
  //
  boolean write(Sof& sof, long tag, const String& cname = CLASS_NAME) const {
    return Error::handle(name(), L"write", Error::ARG, __FILE__, __LINE__);
  }

  // method: readData
  //
  boolean readData(Sof& sof, const String& pname = DEF_PARAM,
		   long size = SofParser::FULL_OBJECT, boolean param = true,
                   boolean nested = false) {
    return Error::handle(name(), L"readData", Error::ARG, __FILE__, __LINE__);
  }

  // method: writeData
  //
  boolean writeData(Sof& sof, const String& pname = DEF_PARAM) const {
    return Error::handle(name(), L"writeData", Error::ARG, __FILE__, __LINE__);
  }

  // equality method
  //
  boolean eq(const TrainNode& compare_node) const;
  
  // method: new
  //
  static void* operator new(size_t size) {
    return mgr_d.get();
  }

  // method: new[]
  //
  static void* operator new[](size_t size) {
    return mgr_d.getBlock(size);
  }

  // method: delete
  //
  static void operator delete(void* ptr) {
    mgr_d.release(ptr);
  }

  // method: delete[]
  //
  static void operator delete[](void* ptr) {
    mgr_d.releaseBlock(ptr);
  }

  // method: setGrowSize
  //
  static boolean setGrowSize(long grow_size) {
    return mgr_d.setGrow(grow_size);
  }

  // clear methods
  //
  boolean clear(Integral::CMODE ctype = Integral::DEF_CMODE);

  //---------------------------------------------------------------------------
  //
  // class-specific public methods
  //
  //---------------------------------------------------------------------------

  // method: setAlpha
  //
  boolean setAlpha(double arg) {
    return (alpha_d = arg);
  }

  // method: getAlpha
  //
  double getAlpha() const {
    return alpha_d;
  }

  // method: setBeta
  //
  boolean setBeta(double arg) {
    return (beta_d = arg);
  }

  // method: getBeta
  //
  double getBeta() const {
    return beta_d;
  }

  // method: setFrame
  //
  boolean setFrame(long arg) {
    return (frame_d = arg);
  }

  // method: getFrame
  //
  long getFrame() const {
    return frame_d;
  }

  // method: setScore
  //
  boolean setScore(double arg) {
    return (score_d = arg);
  }

  // method: getScore
  //
  double getScore() const {
    return score_d;
  }
  
  // method: setReference
  //
  boolean setReference(Context* arg) {
    return (reference_d = arg);
  }

  // method: getReference
  //
  Context* getReference() const {
    return reference_d;
  }

  // method: getStatisticalModel
  //
  StatisticalModel* getStatisticalModel() {
    return stat_model_d;
  }

  // method: setStatisticalModel
  //
  boolean setStatisticalModel(StatisticalModel* arg) {
    return (stat_model_d = arg);
  }
  
  // method: getValidModel
  //
  boolean getValidModel() const {
    if (stat_model_d != (StatisticalModel*)NULL) {
      return true;
    }
    return false;
  }

  // method: setValidNode
  //
  boolean setValidNode(boolean arg) {
    return (is_valid_d = arg);
  }

  // method: getValidNode
  //
  boolean getValidNode() const {
    return is_valid_d;
  }        

  // method: setAlphaValid
  //
  boolean setAlphaValid(boolean arg = true) {
    return (is_alpha_valid_d = arg);
  }

  // method: isAlphaValid
  //
  boolean isAlphaValid() const {
    return is_alpha_valid_d;
  }

  // method: setAccumulatorValid
  //
  boolean setAccumulatorValid(boolean arg = true) {
    return (is_accum_valid_d = arg);
  }

  // method: isAccumulatorValid
  //
  boolean isAccumulatorValid() const {
    return is_accum_valid_d;
  }  

  // method: setBetaValid
  //
  boolean setBetaValid(boolean arg = true) {
    return (is_beta_valid_d = arg);
  }

  // method: isBetaValid
  //
  boolean isBetaValid() const {
    return is_beta_valid_d;
  }          
  
  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  accumulate and update methods needed for training models
  //
  //---------------------------------------------------------------------------

  // method to update the models using the accumulators generated
  // during training
  //  
  boolean update(VectorFloat& varfloor, long min_model);
  
  // method to accumulate the statistics in training which are
  // used to update the model
  //
  boolean accumulate(double utter_prob, Vector<VectorFloat>& data, 
		     float min_mpd, float min_occupancy);
    
  //---------------------------------------------------------------------------
  //
  // private methods
  //
  //---------------------------------------------------------------------------
private:

};

// end of include file
//
#endif
