// file: $isip/class/stats/MixtureModel/mm_03.cc
// version: $Id: mm_03.cc 8601 2002-08-15 03:20:19Z alphonso $
//

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

// method: readAccumulator
//
// arguments:
//  Sof& sof: (input) sof file object
//  long tag: (input) sof object instance tag
//  const String& name: (input) sof object instance name
//
// return: logical error status
//
// this method has the object read accumulators from an Sof file
//
boolean MixtureModel::readAccumulator(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 (!readAccumulatorData(sof_a)) {
    return false;
  }
    
  // exit gracefully
  //
  return true;  
}

// method: readAccumulatorData
//
// 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: logical error status
//
// this method has the object read itself from an Sof file. it assumes
// that the Sof file is already positioned correctly.
//
boolean MixtureModel::readAccumulatorData(Sof& sof_a, const String& pname_a,
				    long size_a, boolean param_a,
				    boolean nested_a) {

  Integral::DEBUG debug_level = Integral::NONE;

  SofParser parser;

  parser.setDebug(debug_level);
  
  // 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);
  }	    

  // save the state of the list
  //
  const_cast<SingleLinkedList<StatisticalModel>& >(models_d).setMark();
  
  boolean end_loop = (!const_cast<SingleLinkedList<StatisticalModel>& >(models_d).gotoFirst());
  
  // loop through the list
  //
  long i=0;
  while (!end_loop) {

    // read this element
    //
    if (!const_cast<SingleLinkedList<StatisticalModel>& >(models_d).getCurr()->readAccumulatorData(sof_a, PARAM_MODELS, parser.getEntry(sof_a, PARAM_MODELS, i++, 1), false, true)) {
      return Error::handle(name(), L"readAccumulatorData", Error::ARG, __FILE__, __LINE__);
    }

    // move to the next node
    //
    end_loop = (!const_cast<SingleLinkedList<StatisticalModel>& >(models_d).gotoNext());
  }

  // restore the state of the list
  //
  const_cast<SingleLinkedList<StatisticalModel>& >(models_d).gotoMark();

  // set the initialization flag
  //
  is_valid_d = true;
  
  // exit gracefully
  //
  return true;    
}

// 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: logical error status
//
// this method has the object read itself from an Sof file
//
boolean MixtureModel::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: logical error status
//
// this method has the object read itself from an Sof file. it assumes
// that the Sof file is already positioned correctly.
//
boolean MixtureModel::readData(Sof& sof_a, const String& pname_a,
			       long size_a, boolean param_a,
			       boolean nested_a) {

  Integral::DEBUG debug_level = Integral::NONE;

  SofParser parser;

  parser.setDebug(debug_level);
  
  // 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);
  }	    

  // get the vector of weights
  //
  if (!weights_d.readData(sof_a, PARAM_WEIGHTS,
			  parser.getEntry(sof_a, PARAM_WEIGHTS),
			  false, false)) {
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }	    
  
  // get the models
  //
  if (!models_d.readData(sof_a, PARAM_MODELS,
			 parser.getEntry(sof_a, PARAM_MODELS), false)) {
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }	    

  // set the initialization flag
  //
  is_valid_d = false;

  // exit gracefully
  //
  return true;
}

// method: readOccupancies
//
// arguments:
//  Sof& sof: (input) sof file object
//  long tag: (input) sof object instance tag
//  const String& name: (input) sof object instance name
//
// return: logical error status
//
// this method has the object read occupancies from accumulators in an Sof file
//
boolean MixtureModel::readOccupancies(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 (!readOccupanciesData(sof_a)) {
    return false;
  }
  
  // exit gracefully
  //
  return true;  
}

// method: readOccupanciesData
//
// 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: logical error status
//
// this method has the object read itself from an Sof file. it assumes
// that the Sof file is already positioned correctly.
//
boolean MixtureModel::readOccupanciesData(Sof& sof_a, const String& pname_a,
					  long size_a, boolean param_a,
					  boolean nested_a) {

  Integral::DEBUG debug_level = Integral::NONE;

  SofParser parser;

  parser.setDebug(debug_level);
  
  // 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);
  }	    

  // save the state of the list
  //
  const_cast<SingleLinkedList<StatisticalModel>& >(models_d).setMark();
  
  boolean end_loop = (!const_cast<SingleLinkedList<StatisticalModel>& >(models_d).gotoFirst());
  
  // loop through the list
  //
  long i=0;
  while (!end_loop) {

    // read this element
    //
    if (!const_cast<SingleLinkedList<StatisticalModel>& >(models_d).getCurr()->readOccupanciesData(sof_a, PARAM_MODELS, parser.getEntry(sof_a, PARAM_MODELS, i++, 1), false, true)) {
      return Error::handle(name(), L"readOccupanciesData", Error::ARG, __FILE__, __LINE__);
    }

    // move to the next node
    //
    end_loop = (!const_cast<SingleLinkedList<StatisticalModel>& >(models_d).gotoNext());
  }

  // restore the state of the list
  //
  const_cast<SingleLinkedList<StatisticalModel>& >(models_d).gotoMark();

  // set the initialization flag
  //
  is_valid_d = true;
  
  // exit gracefully
  //
  return true;    
}
