// file: $isip/class/mmedia/TranscriptionDatabase/TranscriptionDatabase.h
// version: $Id: TranscriptionDatabase.h 9421 2004-03-10 20:11:33Z parihar $
//

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

#ifndef ISIP_STRING
#include <String.h>
#endif

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

#ifndef ISIP_ANNOTATION_GRAPH
#include <AnnotationGraph.h>
#endif

#ifndef ISIP_SDB
#include <Sdb.h>
#endif

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

// TranscriptionDatabase: a class that facilitates the interface
// between the transcription database and another applications or
// classes
//
class TranscriptionDatabase {
  
  //--------------------------------------------------------------------------
  //
  // public constants
  //
  //--------------------------------------------------------------------------
public:
  
  // define the class name
  //
  static const String CLASS_NAME;

  // i/o related constants
  //
  static const String DEF_PARAM;
  static const String PARAM_KEYS;
  static const String PARAM_NAME;  
  static const String PARAM_VALUES;

  //----------------------------------------
  //
  // error codes
  //
  //----------------------------------------  
  
  static const long ERR = 50500;

  //---------------------------------------------------------------------------
  //
  // protected data
  //
  //---------------------------------------------------------------------------
protected:

  // define the database name
  //
  String name_d;
  
  // define the container object
  //
  HashTable<String, AnnotationGraph> hash_d;
  HashTable<String, Long> hash_index_d;
  
  // define the database file pointer
  //
  Sof database_sof_d;
  
  // declare a static debug level for all class instantiations
  //
  static Integral::DEBUG debug_level_d;
  
  // a static memory manager
  //
  static MemoryManager mgr_d;
  
  //---------------------------------------------------------------------------
  //
  // required public methods
  //
  //---------------------------------------------------------------------------
public:
  
  // method: name
  //
  static const String& name() {
    return CLASS_NAME;
  }
  
  static boolean diagnose(Integral::DEBUG debug_level);
  
  // debug methods
  //
  boolean debug(const unichar* msg) const;

  // method: setDebug
  //
  static boolean setDebug(Integral::DEBUG arg) {
    debug_level_d = arg;
    return true;
  }
  
  // method: destructor
  //
  ~TranscriptionDatabase() {
    clear();
  }

  // method: default constructor
  //
  TranscriptionDatabase();

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

  // method: assign
  //
  boolean assign(const TranscriptionDatabase& arg);
  
  // method: sofSize
  //
  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);
  boolean readData(Sof& sof, const String& pname = DEF_PARAM, long size =
                   SofParser::FULL_OBJECT, boolean param = true,
                   boolean nested = false);

  // method: write
  //
  boolean write(Sof& sof, long tag) const {
    return write(sof, tag, name());
  }
  boolean write(Sof& sof, long tag, const String& name) const;
  boolean writeData(Sof& sof, const String& pname = DEF_PARAM) const;
  
  // method: eq
  //
  boolean eq(const TranscriptionDatabase& arg) const;

  // method: new
  //
  static void* operator new(size_t arg) {
    return mgr_d.get();
  }

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

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

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

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

  // method: clear
  //
  boolean clear(Integral::CMODE cmode = Integral::DEF_CMODE);

  //---------------------------------------------------------------------------
  //
  // class-specific public methods
  //
  //---------------------------------------------------------------------------
  
  // method: getDataBaseName
  //
  String& getDataBaseName() {
    return name_d;
  }

  // method: setDataBaseName
  //
  boolean setDataBaseName(String& arg) {
    return name_d.assign(arg);
  }  

  // method: getAnnotationGraphTable
  //
  HashTable<String, AnnotationGraph>& getAnnotationGraphTable() {
    return hash_d;
  }

  // method: getSymbolTable
  //
  HashTable<String, Long>& getSymbolTable() {
    return hash_index_d;
  }  
  
  // method: removeRecord
  //
  boolean removeRecord(String& identifier) {
    return hash_d.remove(identifier);
  }
  
  // method to insert a record into the database
  //
  boolean insertRecord(String& identifier, AnnotationGraph& graph);
  
  // method to retrieve the records form the database
  //
  boolean getRecord(String& identifier, AnnotationGraph& graph);
  boolean getRecord(String& identifier, String& feature, String& value,
		    Vector<String>& annotations,
		    VectorFloat& start_times, VectorFloat& end_times);
  boolean getRecord(String& identifier, String& feature,
		    String& value, String& type,
		    Vector<String>& annotations,
		    VectorFloat& start_times, VectorFloat& end_times);
  
  boolean getRecord(String& identifier, String& feature, String& value,
		    DoubleLinkedList<String>& records);
  boolean getRecord(String& identifier, String& feature, String& value,
		    DoubleLinkedList<Annotation>& records);
  boolean getRecord(String& identifier, String& feature,
		    Vector<String>& values, String& record);

  // database open and close method
  //
  boolean open(const Filename& filename);
  boolean close();
  
  // method to load a file to transcription database
  //
  boolean load(Filename& trans_file, String& name, String& level);
  boolean load(Sdb& sdb, Filename& trans_file);
  boolean load(Sdb& sdb, Filename& trans_file,
	       Filename& lexicon_file, boolean flag = true);
  boolean load(Sdb& sdb, Filename& trans_file,
	       Filename& lexicon_file, Filename& systactic_file);
  
  // method to store a database to a file
  //
  boolean store(Sof& db_sof, long tag);
  
  // method to store one transcription to a file
  //
  boolean storePartial(String& trans, String& name, String& level, long num,
		       Sof& db_sof, long tag);
  
  // method to sequencially read and store the transcriptions
  //
  boolean store(Filename& trans_file, String& name, String& level,
		Sof& db_sof, long tag);

  // method to store the database header to file
  //
  boolean storePartial(Sof& sof, long tag, Vector<String>& keys);
  boolean storePartial(Sof& sof, long tag, AnnotationGraph& graph);
  
  // method to retrieve the hypotheses in NIST_TRN format from the
  // database
  //
  boolean getHypothesesNistTrn(Sdb& ident_list_sdb, Sdb& exlude_symbols_sdb,
			       String& feature,
			       Vector<String>& nist_hypotheses);  
  
  // method to retrieve the alignments in HYP_ALIGN format from the
  // database
  //
  boolean getAlignmentsHypAlign(Sdb& ident_list_sdb, String& levels,
				Vector<String>& alignments);

  // method to get all the identifiers in the database
  //
  boolean getIdentifiers(Vector<String>& identifiers);

  //---------------------------------------------------------------------------
  //
  // private methods
  //
  //---------------------------------------------------------------------------
private:

  // method to parse the input string of level(s)
  //
  boolean parseLevels(const String& input, Vector<String>& levels);
};

//end of include file
//
#endif
