// file: $isip/class/pr/RegressionDecisionTreeNode/rdtnod_03.cc
// version: $Id: rdtnod_03.cc 9459 2004-04-19 16:06:08Z gao $
//

// isip include files
//
#include "RegressionDecisionTreeNode.h"

// method: read
//
// arguments:
//  Sof& sof: (input) sof file object
//  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 RegressionDecisionTreeNode::read(Sof& sof_a, long tag_a,
				       const String& name_a) {

  // read 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) size in bytes of object (or full_size)
//  boolean param: (input) is the parameter name in the file?
//  boolean nested: (input) are we 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 RegressionDecisionTreeNode::readData(Sof& sof_a, const String& pname_a,
					   long size_a, boolean param_a,
					   boolean nested_a) {

  SofParser parser;

  // ignore implicit parameter setting
  //
  
  // are we nested?
  //
  if (nested_a) {
    parser.setNest();
  }
  
  // load the parse
  //
  if (!parser.load(sof_a, size_a)) {
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }	    

  // set the debug level of the parser
  //
  parser.setDebug(debug_level_d.getIndex());
  
  // get the datapoints
  //
  if (!gaussian_models_d.readData(sof_a, PARAM_DATAPOINTS,
			     parser.getEntry(sof_a, PARAM_DATAPOINTS),
			     false, false)) {
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }	    

  // get the average mean
  //
  if (!average_mean_d.readData(sof_a, PARAM_AVERAGE_MEAN,
			       parser.getEntry(sof_a, PARAM_AVERAGE_MEAN),
				true, true)) {
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }

  // get the node cluster score
  //
  if (!cluster_score_d.readData(sof_a, PARAM_CLUSTER_SCORE,
				parser.getEntry(sof_a, PARAM_CLUSTER_SCORE),
				true, true)) {
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }

  // get the node cluster accumulate
  //
  if (!cluster_accumulate_d.readData(sof_a, PARAM_CLUSTER_SCORE,
		parser.getEntry(sof_a, PARAM_CLUSTER_ACCUMULATE),
		true, true)) {
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }

  // get the node number of components
  //
  if (!number_components_d.readData(sof_a, PARAM_NUMBER_COMPONENTS,
		parser.getEntry(sof_a, PARAM_NUMBER_COMPONENTS),
		true, true)) {
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }

  // get the node index
  //
  if (!node_index_d.readData(sof_a, PARAM_NODE_INDEX,
		parser.getEntry(sof_a, PARAM_NODE_INDEX),
		true, true)) {
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }

  // get the node average covariance
  //
  if (!average_covariance_d.readData(sof_a, PARAM_AVERAGE_COVARIANCE,
		parser.getEntry(sof_a, PARAM_AVERAGE_COVARIANCE),
		true, true)) {
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }

  // get the node speech flag
  //
  if (!speech_flag_d.readData(sof_a, PARAM_SPEECH_FLAG,
		parser.getEntry(sof_a, PARAM_SPEECH_FLAG),
		true, true)) {
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }

  // get the node split flag
  //
  if (!split_flag_d.readData(sof_a, PARAM_SPLIT_FLAG,
		parser.getEntry(sof_a, PARAM_SPLIT_FLAG),
		true, true)) {
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }

  // get the parent node index
  //
  if (!parent_node_index_d.readData(sof_a, PARAM_PARENT_NODE_INDEX,
		parser.getEntry(sof_a, PARAM_PARENT_NODE_INDEX),
		true, true)) {
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }

  // get the node transform flag
  //
  if (!transform_flag_d.readData(sof_a, PARAM_TRANSFORM_FLAG,
		parser.getEntry(sof_a, PARAM_TRANSFORM_FLAG),
		true, true)) {
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }

  // get the node stat models
  //
  if (!stat_models_d.readData(sof_a, PARAM_STAT_MODELS,
		parser.getEntry(sof_a, PARAM_STAT_MODELS),
		true, true)) {
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }

  // get the node w transform matrix
  //
  if (!w_transform_d.readData(sof_a, PARAM_W_TRANSFORM,
		parser.getEntry(sof_a, PARAM_W_TRANSFORM),
		true, true)) {
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }  

  // get the debug level
  //
  if (parser.isPresent(sof_a, PARAM_DBGL)) {
    if (!debug_level_d.readData(sof_a, PARAM_DBGL,
                                parser.getEntry(sof_a, PARAM_DBGL))) {
      
      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::IO,
                           __FILE__, __LINE__, Error::WARNING);
    }
  }
  else {
    debug_level_d = Integral::DEF_DEBUG;
  }

  // exit gracefully
  //
  return true;
}
