// file: $isip/class/pr/NGramNode/nnode_03.cc
// version: $Id: nnode_03.cc 8731 2002-09-29 17:56:46Z huang $
//

// isip include files
//
#include "NGramNode.h"
#include <Sof.h>

// method: read
//
// arguments:
//  Sof& sof: (input) sof file object
//  const long tag: (input) sof object instance tag
//  const String& name: (input) sof object instance name
//
// return: a boolean value indicating status
//
// this method has the object read itself from an Sof file
//
boolean NGramNode::read(Sof& sof_a, const long tag_a, const String& name_a) {

  // get the instance of the object from the Sof file
  //
  if (!sof_a.find(name_a, tag_a)) {
    return false;
  }

  // read the actual data from the sof file
  //
  if (!readData(sof_a)) {
    return false;
  }

  // exit gracefully
  //
  return true;
}

// method: readData
//
// arguments:
//  Sof& sof: (input) sof file object
//  const String& pname: (input) parameter name
//  long size: (input) number of bytes in file
//  boolean param: (input) is the parameter specified?
//  boolean nested: (input) is this nested?
//
// return: a boolean value indicating status
//
// this method has the object read itself from an Sof file. it assumes
// that the Sof file is already positioned correctly.
//
boolean NGramNode::readData(Sof& sof_a, const String& pname_a, long size_a,
			    boolean param_a, boolean nested_a) {

  // if ascii, parse the start bracket
  //
  SofParser parser;
    
  nested_a = true;
  
  if (nested_a) {
    parser.setNest();
  }

  // set the debug level
  //
  parser.setDebug(debug_level_d);
  
  //if (nested_a) {
  //  parser.setNest();
  //}

  // load the parse
  //
  if (!parser.load(sof_a, size_a)) {

    // return a warning message
    //
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }           

  // read the data
  //
  if (parser.isPresent(sof_a, PARAM_INDEX)) {
    if (!index_d.readData(sof_a, PARAM_INDEX,
				parser.getEntry(sof_a,
						PARAM_INDEX))) {
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }	      
  }
  else {
    index_d.assign(DEF_INDEX);
  }

  if (parser.isPresent(sof_a, PARAM_LM_SCORE)) {
    if (!lm_score_d.readData(sof_a, PARAM_LM_SCORE,
				parser.getEntry(sof_a,
						PARAM_LM_SCORE))) {
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }	      
  }
  else {
    lm_score_d.assign(DEF_LM_SCORE);
  }

  if (parser.isPresent(sof_a, PARAM_BACKOFF)) {
    if (!backoff_d.readData(sof_a, PARAM_BACKOFF,
				parser.getEntry(sof_a,
						PARAM_BACKOFF))) {
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }	      
  }
  else {
    backoff_d.assign(DEF_BACKOFF);
  }

  
  if (parser.isPresent(sof_a, PARAM_HASH)) {
    next_gram_d = new HashTable<Long, NGramNode>();
    if (!next_gram_d->readData(sof_a, PARAM_HASH,
			       parser.getEntry(sof_a, PARAM_HASH),
			       true, true)) {
      
	// return a warning message
	//
	return Error::handle(name(), L"readData", Error::READ,
			     __FILE__, __LINE__, Error::WARNING);
    }
    if ( next_gram_d->isEmpty() ){
      delete next_gram_d;
      next_gram_d = NULL;
    }
  }

  // make sure all coefficients have been accessed
  //
  if (!parser.checkParams(sof_a)) {
    return Error::handle(name(), L"readData", Error::IO, __FILE__, __LINE__,
			 Error::WARNING);
  }
  
  // exit gracefully
  //
  return true;
}
