// file: $isip/class/pr/StatisticalModelAdaptation/StatisticalModelAdaptation.h
// version: $Id: StatisticalModelAdaptation.h 9470 2004-05-10 15:40:08Z gao $
//

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

// isip include files
//
#ifndef ISIP_REGRESSION_DECISION_TREE_NODE
#include <RegressionDecisionTreeNode.h>
#endif

#ifndef ISIP_REGRESSION_DECISION_TREE
#include <RegressionDecisionTree.h>
#endif

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

#ifndef ISIP_GAUSSIAN_MODEL
#include <GaussianModel.h>
#endif

#ifndef ISIP_MIXTURE_MODEL
#include <MixtureModel.h>
#endif

#ifndef ISIP_BIGRAPH_ARC
#include <BiGraphArc.h>
#endif

#ifndef ISIP_BIGRAPH_VERTEX
#include <BiGraphVertex.h>
#endif

// forward class definitions:

//  we must define the RegressionDecisionTreeNode and
//  RegressDecisionTree class here first because the header files
//  might be short-circuited by the ifndef.
//
class RegressionDecisonTreeNode;
class RegressionDecisonTree;

// StatisticalModelAdaptation: a class that adapts the statistical
// models by giving the regression decision tree.
//
class StatisticalModelAdaptation {

  //---------------------------------------------------------------------------
  //
  // public constants
  //
  //---------------------------------------------------------------------------
 public:
  
  // define the class name
  //
  static const String CLASS_NAME;
  
  //----------------------------------------
  //
  // other important constants
  //
  //----------------------------------------
  
  // define the algorithm choices
  //
  enum ALGORITHM { MLLR = 0, MAP, DEF_ALGORITHM = MLLR };
  
  // define the implementation choices
  //
  enum IMPLEMENTATION { MEAN = 0, VARIANCE, DEF_IMPLEMENTATION = MEAN };

  // define the static NameMap objects
  //
  static const NameMap ALGO_MAP;
  static const NameMap IMPL_MAP;

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

  static const String DEF_PARAM;
  static const String PARAM_ALGORITHM;
  static const String PARAM_IMPLEMENTATION;
  static const String PARAM_DBGL;

  //----------------------------------------
  //
  // other static constants
  //
  //----------------------------------------
  
  //----------------------------------------
  //
  // error codes
  //
  //----------------------------------------  
  
  static const long ERR = 00100750;
  static const long ERR_UNSUPM = 00100760;
  //---------------------------------------------------------------------------
  //
  // protected data
  //
  //---------------------------------------------------------------------------
protected:

  // define the structures
  //
  typedef BiGraphVertex<RegressionDecisionTreeNode> RTreeNode;
  
  // algorithm name
  //
  ALGORITHM algorithm_d;

  // implementation name
  //
  IMPLEMENTATION implementation_d;

  // define transformation matrix W (one row for each Gaussian
  // dimension)
  //
  Vector<VectorFloat> w_transform_d;
  
  // debugging parameters
  //
  DebugLevel debug_level_d;
  
  // static memory manager
  //
  static MemoryManager mgr_d;

  //---------------------------------------------------------------------------
  //
  // required public methods
  //
  //---------------------------------------------------------------------------
public:
    
  // method: name
  //
  static const String& name() {
    return CLASS_NAME;
  }

  // other static methods
  //
  static boolean diagnose(Integral::DEBUG debug_level);
  
  // debug methods:
  //
  boolean debug(const unichar* msg) const;

  // method: setDebug
  //
  boolean setDebug(Integral::DEBUG level) {
    debug_level_d = level;
    return true;
  }

  // method: destructor
  //
  ~StatisticalModelAdaptation() {
  }

  // method: default constructor
  //
  StatisticalModelAdaptation(ALGORITHM algorithm = DEF_ALGORITHM,
			     IMPLEMENTATION implementation =
			     DEF_IMPLEMENTATION) {
    algorithm_d = algorithm;
    implementation_d = implementation;
  }

  // method: copy constructor
  //
  StatisticalModelAdaptation(const StatisticalModelAdaptation& arg) {
    assign(arg);
  }

  // assign methods
  //
  boolean assign(const StatisticalModelAdaptation& arg);

  // method: operator=
  //
  StatisticalModelAdaptation& operator= (const StatisticalModelAdaptation& arg) {
    assign(arg);
    return *this;
  }
  
  // i/o methods
  //
  long sofSize() const;
  
  // method: read
  //
  boolean read(Sof& sof, long tag) {
    return read(sof, tag, name());
  }
  boolean read(Sof& sof, long tag, const String& name);

  // method: write
  //
  boolean write(Sof& sof, long tag) const {
    return write(sof, tag, name());
  }
  boolean write(Sof& sof, long tag, const String& name) const;

  // method: readData
  //
  boolean readData(Sof& sof, const String& pname = DEF_PARAM,
		   long size = SofParser::FULL_OBJECT, boolean param = true,
                   boolean nested = false);

  // method: writeData
  //
  boolean writeData(Sof& sof, const String& pname = DEF_PARAM) const;
   
  // equality methods
  //
  boolean eq(const StatisticalModelAdaptation& arg) 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);
  }

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

  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  set methods
  //
  //---------------------------------------------------------------------------

  // method: setAlgorithm
  //
  boolean setAlgorithm(ALGORITHM algorithm) {
    algorithm_d = algorithm;
    return true;
  }

  // method: setImplementation
  //
  boolean setImplementation(IMPLEMENTATION implementation) {
    implementation_d = implementation;
    return true;  
  }

  // method: set
  //
  boolean set(ALGORITHM algorithm = DEF_ALGORITHM,
	      IMPLEMENTATION implementation = DEF_IMPLEMENTATION) {
    algorithm_d = algorithm;
    implementation_d = implementation;
    return true;
  }

  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  get methods
  //
  //---------------------------------------------------------------------------

  // method: getAlgorithm
  //
  ALGORITHM getAlgorithm() const {
    return algorithm_d;
  }  

  // method: getImplementation
  //
  IMPLEMENTATION getImplementation() const {
    return implementation_d;
  }

  // method: getDebug
  //
  boolean getDebug(Integral::DEBUG & level) {
    level = debug_level_d; 
    return true;
  }

  // method: get
  //
  boolean get(ALGORITHM& algorithm, IMPLEMENTATION& implementation) {
    algorithm = algorithm_d;
    implementation = implementation_d;
    return true;
  }

  // method to adapt the model
  //
  boolean adapt(RegressionDecisionTree& rdt_a,
		Vector<StatisticalModel>& stat_models_a);

  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  computational methods
  //
  //---------------------------------------------------------------------------
  
  // method to set the parser
  //
  boolean setParser(SofParser* parser);
  
  //---------------------------------------------------------------------------
  //
  // private methods
  //
  //---------------------------------------------------------------------------
  
private:

  // find a terminal by given a regression decision tree and node
  // index
  //
  BiGraphVertex<RegressionDecisionTreeNode>* findTerminal(RTreeNode*& root_node_a, long index_a);

};

// end of include file
//

#endif
