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

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

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

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

#ifndef ISIP_DOUBLE_LINKED_LIST
#include <DoubleLinkedList.h>
#endif

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

#ifndef ISIP_SEARCH_SYMBOL
#include <SearchSymbol.h>
#endif

#ifndef ISIP_INSTANCE
#include <Instance.h>
#endif

#ifndef ISIP_TRACE
#include <Trace.h>
#endif

// forward class definition(s):
//
class SearchLevel;
class LexicalTree;
class Instance;
class Trace;

// SearchNode: A class to specify a location in a search space.
//
class SearchNode {

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

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

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

  static const String DEF_PARAM;
  
  //----------------------------------------
  //
  // default values and arguments
  //
  //----------------------------------------  

  // define the default symbol id
  //
  static const long DEF_SYMBOL_ID = -1;

  // define the default model id
  //
  static const long DEF_MODEL_ID = -1;
  
  // define the default dummy symbol id
  //
  static const long DEF_DUMMY_SYMBOL_ID = -100;

  // define the default time stamp
  //
  static const long DEF_TIMESTAMP = -1;

  // define the inactive likelihood score
  //  
  static const float INACTIVE_SCORE = -11111111111111.0f;
  
  //---------------------------------------
  //
  // error codes
  //
  //---------------------------------------

  static const long ERR = (long)90300;
  
  //---------------------------------------------------------------------------
  //
  // protected data
  //
  //---------------------------------------------------------------------------
protected:

  // define the symbol that is represented by this node
  //
  Long symbol_id_d;

  // define the underlying statistical model for this node.
  // this could be a gaussian, or a laplacian, or any such statistical model.
  // if stat_model_d is not set then evaluations do not take place
  //
  long model_id_d;  
  StatisticalModel* stat_model_d;

  // define the level this node exists at in the main graph
  //
  SearchLevel* level_d;

  // define the level this node exists at in the main graph
  //
  LexicalTree* lex_tree_d;

  // define a set of traces at this node in the search
  //
  DoubleLinkedList<Trace> traces_d;

  // define a set of traces at this node in the search
  //
  DoubleLinkedList<Instance> instances_d;  

  // define the current time stamp for this node
  //
  long timestamp_d;

  // define the current score
  //
  float score_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
  //
  ~SearchNode() {
    if (debug_level_d >= Integral::ALL) { 
      fprintf(stdout, "Destructor of search_node: %p\n", this);
      fflush(stdout);
    }
  }

  // constructor(s)
  //
  SearchNode();
  SearchNode(const SearchNode& copy_node);
  
  // assign methods
  //
  boolean assign(const SearchNode& copy_node);
  
  // method: sofSize
  //
  long sofSize() const {
     return Error::handle(name(), L"sofSize", Error::NOT_IMPLEM, __FILE__,
			 __LINE__);
  }
  
  // method: read
  //
  boolean read(Sof& sof, long tag, const String& cname = CLASS_NAME) {
    return Error::handle(name(), L"read", Error::NOT_IMPLEM, __FILE__,
			 __LINE__);
  }

  // method: write
  //
  boolean write(Sof& sof, long tag, const String& cname = CLASS_NAME) const {
    return Error::handle(name(), L"write", Error::NOT_IMPLEM, __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::NOT_IMPLEM, __FILE__,
			 __LINE__);
  }

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

  // equality method
  //
  boolean eq(const SearchNode& 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: setModelId
  //
  boolean setModelId(const long arg) {
    return (model_id_d = arg);
  }

  // method: getModelId
  //
  long getModelId() const {
    return model_id_d;
  }
  
  // symbol id methods
  //
  boolean setSymbolId(long symbol_id);

  // method: getSymbolId
  //
  long getSymbolId() const {

    long index = symbol_id_d;
    
    // dummy node will have negative index
    //
    if ( index <= DEF_DUMMY_SYMBOL_ID ){
      index = DEF_DUMMY_SYMBOL_ID - index;
    }
    return index;
    
  }

  // method: isDummyNode
  //
  boolean isDummyNode() const {

    // dummy node will have negative index
    //
    return ( symbol_id_d <= DEF_DUMMY_SYMBOL_ID );
    
  }

  // symbol methods
  //
  boolean setSymbol(const SearchSymbol& symbol);
  boolean getSymbol(SearchSymbol& output_symbol) const;

  // method: setStatisticalModel
  //
  boolean setStatisticalModel(StatisticalModel* model) {
    stat_model_d = model;
    return true;
  }

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

  // method: setScore
  //
  boolean setScore(float score) {
    score_d = score;
    return true;      
  }

  // method: getScore
  //
  float getScore() const {
    return score_d;
  }

  // method: setSearchLevel
  //
  boolean setSearchLevel(SearchLevel* level) {
    level_d = level;
    return true;
  }

  // method: getSearchLevel
  //
  SearchLevel* getSearchLevel() const {
    return level_d;
  }

  // method: setLexicalTree
  //
  boolean setLexicalTree(LexicalTree* lex_tree_a) {
    lex_tree_d = lex_tree_a;
    return true;
  }

  // method: getLexicalTree
  //
  LexicalTree* getLexicalTree() const {
    return lex_tree_d;
  }
  
  // trace list manipulation methods
  //
  boolean addTrace(Trace* trace, long timestamp, Trace*& ptr);
  boolean mergeTrace(Trace* trace, long timestamp,
		     Trace*& ptr, long nsymbol_index, long nsymbol_order);
  
  boolean removeTrace(Trace* trace);
  boolean clearTraceList();

  // instance list manipulation methods
  //
  boolean addInstance(Instance* instance, long timestamp, Instance*& ptr);
  boolean insertInstance(Instance* instance, long timestamp, Instance*& ptr);
  boolean mergeInstance(Instance* instance, long timestamp,
			Instance*& ptr, long nsymbol_index, long nsymbol_order);
  
  boolean removeInstance(Instance* instance);
  boolean clearInstanceList();  

  // computational method
  //
  float evaluateData(VectorFloat& data, long timestamp);
  
  //---------------------------------------------------------------------------
  //
  // private methods
  //
  //---------------------------------------------------------------------------
private:

  // trace tracking methods
  //
  boolean addWithViterbi(Trace* trace, Trace*& ptr);
  boolean addWithBaumWelch(Trace* trace, Trace*& ptr);

  // instance tracking methods
  //
  boolean addWithViterbi(Instance* instance, Instance*& ptr);
  boolean insertWithViterbi(Instance* instance, Instance*& ptr);
  boolean addWithBaumWelch(Instance* instance, Instance*& ptr);    
};

// include the train node class here circumvent the circular dependency
//
#ifndef ISIP_SEARCH_LEVEL
#include <SearchLevel.h>
#endif

#ifndef ISIP_LEXICAL_TREE
#include <LexicalTree.h>
#endif

// end of include file
//
#endif
