// file: $isip_ifc/class/algo/Generator/gen_03.cc
// version: $Id: gen_03.cc 10391 2006-01-25 03:32:38Z prasad $
//

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

// ------------------------------------------------------------------
// these methods have to be in the same file so they can use the same
// static parser pointer
// ------------------------------------------------------------------

static SofParser* parser_d = (SofParser*)NULL;

// method: setParser
//
// arguments:
//  SofParser* parser: (input) sof file object
//
// return: a boolean value indicating status
//
// this method sets the parser from the parent object to be used in
// the next readData method.
//
boolean Generator::setParser(SofParser* parser_a) {

  // set the parser
  //
  parser_d = parser_a;

  // 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: a boolean indicating status
//
// this method has the object read itself from an Sof file according to
// the specified name and tag
//
boolean Generator::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 specified?
//  boolean nested: (input) is this nested?
//
// return: a boolean indicating status
//
// this method has the object read itself from an Sof file. it assumes
// that the Sof file is already positioned correctly.
//
boolean Generator::readData(Sof& sof_a, const String& pname_a,
				  long size_a, boolean param_a,
				  boolean nested_a) {

  // local variables
  //
  boolean status;
  
  // we need a parser
  // 
  if (parser_d == (SofParser*)NULL) {
    
    parser_d = new SofParser;
    
    if (nested_a) {
      parser_d->setNest();
    }
    
    // load the parse
    //
    if (!parser_d->load(sof_a, size_a)) {

      // delete the parser
      //
      delete parser_d;
      parser_d = (SofParser*)NULL;
      
      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::READ,
                           __FILE__, __LINE__, Error::WARNING);
    }           
  }

  // set the debug level of the parser
  //
  parser_d->setDebug(debug_level_d.getIndex());

  // get the signal algorithm
  //
  if (parser_d->isPresent(sof_a, PARAM_ALGORITHM)) {
    if (!ALGO_MAP.readElementData((long&)algorithm_d, sof_a, PARAM_ALGORITHM,
                                  parser_d->getEntry(sof_a, PARAM_ALGORITHM))) {

      // delete the parser
      //
      delete parser_d;
      parser_d = (SofParser*)NULL;
      
      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::READ,
                           __FILE__, __LINE__, Error::WARNING);
    }
  }  
  else {
    algorithm_d = DEF_ALGORITHM;
  }
  
  // get the implementation
  //
  if (parser_d->isPresent(sof_a, PARAM_IMPLEMENTATION)) {
    if (!IMPL_MAP.readElementData((long&)implementation_d, sof_a,
				  PARAM_IMPLEMENTATION,
				  parser_d->getEntry(sof_a,
						     PARAM_IMPLEMENTATION))) {

      // delete the parser
      //
      delete parser_d;
      parser_d = (SofParser*)NULL;
      
      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }
  }
  else {
    implementation_d = DEF_IMPLEMENTATION;
  }

  // read the number of channels of the signal
  //
  if (parser_d->isPresent(sof_a, PARAM_CHANNELS)) {
    if (!channels_d.readData(sof_a, PARAM_CHANNELS,
			      parser_d->getEntry(sof_a, PARAM_CHANNELS))) {

      // delete the parser
      //
      delete parser_d;
      parser_d = (SofParser*)NULL;
	
      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::READ, __FILE__,
			     __LINE__, Error::WARNING);
    }
  }
  else {
    channels_d.assign(DEF_CHANNELS);
  }

  // read the seed
  //
  if (parser_d->isPresent(sof_a, PARAM_SEED)) {
    if (!seed_d.readData(sof_a, PARAM_SEED,
			 parser_d->getEntry(sof_a, PARAM_SEED))) {
      
      // delete the parser
      //
      delete parser_d;
      parser_d = (SofParser*)NULL;
      
      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::READ, __FILE__,
			   __LINE__, Error::WARNING);
    }
  }
  else {  
    seed_d.assign(DEF_SEED);
  }   
  
  // read the generate sample frequency
  //
  if (parser_d->isPresent(sof_a, PARAM_GEN_SAMPLE_FREQUENCY)) {
    if (!gen_sample_freq_d.readData(sof_a, PARAM_GEN_SAMPLE_FREQUENCY,
			 parser_d->getEntry(sof_a, PARAM_GEN_SAMPLE_FREQUENCY))) {
      
      // delete the parser
      //
      delete parser_d;
      parser_d = (SofParser*)NULL;
      
      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::READ, __FILE__,
			   __LINE__, Error::WARNING);
    }
  }
  else {  
    gen_sample_freq_d.assign(AlgorithmBase::DEF_SAMPLE_FREQUENCY);
  }   

  // get the debug level
  //
  if (parser_d->isPresent(sof_a, PARAM_DBGL)) {
    if (!debug_level_d.readData(sof_a, PARAM_DBGL,
				parser_d->getEntry(sof_a, PARAM_DBGL))) {
      
      // delete the parser
      //
      delete parser_d;
      parser_d = (SofParser*)NULL;
      
      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::IO, __FILE__, __LINE__,
			   Error::WARNING);
    }
  }
  else {
    debug_level_d = Integral::DEF_DEBUG;
  }
  
  // check known algorithms: invoke the proper lower-level method
  //
  if ((algorithm_d == SINE) ||
      (algorithm_d == SQUARE) ||
      (algorithm_d == TRIANGLE) ||
      (algorithm_d == PULSE) ||
      (algorithm_d == GAUSSIAN) ||
      (algorithm_d == LORENTZ) ||
      (algorithm_d == ROSSLER)) {
    status = readDataCommon(sof_a, pname_a, size_a, param_a, nested_a);
  }
  
  // check unsupported algorithms
  //
  else {
    return Error::handle(name(), L"readData",
			 ERR_UNKALG, __FILE__, __LINE__);
  }
  
  // check that all parameters are accounted for
  //
  if (!parser_d->checkParams(sof_a)) {
    
    // delete the parser
    //
    delete parser_d;
    parser_d = (SofParser*)NULL;
    
    // return a warning message
    //
    return Error::handle(name(), L"readData", Error::IO, __FILE__, __LINE__,
                         Error::WARNING);
  }

  // delete the parser
  //
  delete parser_d;
  parser_d = (SofParser*)NULL;
  
  // exit gracefully
  //
  return status;
}

// method: readDataCommon
//
// arguments:
//  Sof& sof: (input) sof file object
//  const String& pname: (input) parameter name
//  long size: (input) size in bytes of object (or FULL_OBJECT)
//  boolean param: (input) is the parameter name in the file?
//  boolean nested: (input) are we nested?
//
// return: a boolean value indicating status
//
// this method assumes the algorithm has been read, and proceeds to read
// the rest of the object based on the value of the algorithm
//
boolean Generator::readDataCommon(Sof& sof_a, const String& pname_a,
			       long size_a, boolean param_a,
			       boolean nested_a) {

  // read parameters common to SINE, PULSE, TRIANGLE or SQUARE
  // algorithms
  //
  if ((algorithm_d == SINE) || (algorithm_d == PULSE) ||
      (algorithm_d == TRIANGLE) || (algorithm_d == SQUARE)) {

    // read the frequency of the signal
    //
    if (parser_d->isPresent(sof_a, PARAM_FREQUENCY)) {
      if (!frequency_d.readData(sof_a, PARAM_FREQUENCY,
				parser_d->getEntry(sof_a, PARAM_FREQUENCY))) {
	
	// delete the parser
	//
	delete parser_d;
	parser_d = (SofParser*)NULL;
	
	// return a warning message
	//
	return Error::handle(name(), L"readData", Error::READ, __FILE__,
			     __LINE__, Error::WARNING);
      }
    }
    else {
      frequency_d.assign(DEF_FREQUENCY);
     }
          
    // read the amplitude
    //
    if (parser_d->isPresent(sof_a, PARAM_AMPLITUDE)) {
      if (!amplitude_d.readData(sof_a, PARAM_AMPLITUDE,
				parser_d->getEntry(sof_a, PARAM_AMPLITUDE))) {
	
	// delete the parser
	//
	delete parser_d;
	parser_d = (SofParser*)NULL;
	
	// return a warning message
	//
	return Error::handle(name(), L"readData", Error::READ, __FILE__,
			     __LINE__, Error::WARNING);
      }
    }
    else {  
      amplitude_d.assign(DEF_AMPLITUDE);
    }

  // get the phase mode
  //
  if (parser_d->isPresent(sof_a, PARAM_PHMODE)) {
    if (!PHMODE_MAP.readElementData((long&)phmode_d, sof_a, PARAM_PHMODE,
				    parser_d->getEntry(sof_a, PARAM_PHMODE))) {

      // delete the parser
      //
      delete parser_d;
      parser_d = (SofParser*)NULL;
      
      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::READ,
                           __FILE__, __LINE__, Error::WARNING);
    }
  }  
  else {
    phmode_d = DEF_PHMODE;
  }
    
    // read the phase
    //
    if (parser_d->isPresent(sof_a, PARAM_PHASE)) {
      if (!phase_d.readData(sof_a, PARAM_PHASE,
			    parser_d->getEntry(sof_a, PARAM_PHASE))) {

	// delete the parser
	//
	delete parser_d;
	parser_d = (SofParser*)NULL;
	
	// return a warning message
	//
	return Error::handle(name(), L"readData", Error::READ, __FILE__,
			     __LINE__, Error::WARNING);
      }
    }
    else {  
      phase_d.assign(DEF_PHASE);
    }
    
    if (phmode_d == RANDOM) {
      setPhase();
    }
 
    // read the DC
    //
    if (parser_d->isPresent(sof_a, PARAM_BIAS)) {
      if (!bias_d.readData(sof_a, PARAM_BIAS,
			    parser_d->getEntry(sof_a, PARAM_BIAS))) {

	// delete the parser
	//
	delete parser_d;
	parser_d = (SofParser*)NULL;
	
	// return a warning message
	//
	return Error::handle(name(), L"readData", Error::READ, __FILE__,
			     __LINE__, Error::WARNING);
      }
    }
    else {  
      bias_d.assign(DEF_BIAS);
    }   
  }

  // read parameters common to PULSE, TRIANGLE or SQUARE
  // algorithms
  //
  if ((algorithm_d == PULSE) || (algorithm_d == TRIANGLE) ||
      (algorithm_d == SQUARE)) {

    // read the duty cycle
    //
    if (parser_d->isPresent(sof_a, PARAM_DUTY_CYCLE)) {
      if (!duty_cycle_d.readData(sof_a, PARAM_DUTY_CYCLE,
			 parser_d->getEntry(sof_a, PARAM_DUTY_CYCLE))) {

	// delete the parser
	//
	delete parser_d;
	parser_d = (SofParser*)NULL;
	
	// return a warning message
	//
	return Error::handle(name(), L"readData", Error::READ, __FILE__,
			     __LINE__, Error::WARNING);
      }
    }
    else {  
      duty_cycle_d.assign(DEF_DUTY_CYCLE);
    }
  }

  // read parameters needed for GAUSSIAN algorithm
  //
  if (algorithm_d == GAUSSIAN) {

    // read the mean and variance
    //
    if (parser_d->isPresent(sof_a, PARAM_MEAN)) {
      if (!mean_d.readData(sof_a, PARAM_MEAN,
			   parser_d->getEntry(sof_a, PARAM_MEAN))) {

	// delete the parser
	//
	delete parser_d;
	parser_d = (SofParser*)NULL;
	
	// return a warning message
	//
	return Error::handle(name(), L"readData", Error::READ, __FILE__,
			     __LINE__, Error::WARNING);
      }
    }
    else {  
      mean_d.assign(DEF_MEAN);
    }

    // read the variance
    //
    if (parser_d->isPresent(sof_a, PARAM_VARIANCE)) {
      if (!variance_d.readData(sof_a, PARAM_VARIANCE,
			       parser_d->getEntry(sof_a, PARAM_VARIANCE))) {

	// delete the parser
	//
	delete parser_d;
	parser_d = (SofParser*)NULL;
	
	// return a warning message
	//
	return Error::handle(name(), L"readData", Error::READ, __FILE__,
			     __LINE__, Error::WARNING);
      }
    }
    else {  
      variance_d.assign(DEF_VARIANCE);
    }
  }

   // read parameters needed for LORENTZ and ROSSLER algorithm
  //
  if ((algorithm_d == LORENTZ) || (algorithm_d == ROSSLER) ) {

    // read the system parameter 'a'
    //
    if (parser_d->isPresent(sof_a, PARAM_CHAOS_A)) {
      if (!chaos_a_d.readData(sof_a, PARAM_CHAOS_A,
			   parser_d->getEntry(sof_a, PARAM_CHAOS_A))) {

	// delete the parser
	//
	delete parser_d;
	parser_d = (SofParser*)NULL;
	
	// return a warning message
	//
	return Error::handle(name(), L"readData", Error::READ, __FILE__,
			     __LINE__, Error::WARNING);
      }
    }
    else {  
      chaos_a_d.assign(DEF_CHAOS_A);
    }

    // read the system parameter 'b'
    //
    if (parser_d->isPresent(sof_a, PARAM_CHAOS_B)) {
      if (!chaos_b_d.readData(sof_a, PARAM_CHAOS_B,
			   parser_d->getEntry(sof_a, PARAM_CHAOS_B))) {

	// delete the parser
	//
	delete parser_d;
	parser_d = (SofParser*)NULL;
	
	// return a warning message
	//
	return Error::handle(name(), L"readData", Error::READ, __FILE__,
			     __LINE__, Error::WARNING);
      }
    }
    else {  
      chaos_b_d.assign(DEF_CHAOS_B);
    }

    // read the system parameter 'c'
    //
    if (parser_d->isPresent(sof_a, PARAM_CHAOS_C)) {
      if (!chaos_c_d.readData(sof_a, PARAM_CHAOS_C,
			   parser_d->getEntry(sof_a, PARAM_CHAOS_C))) {

	// delete the parser
	//
	delete parser_d;
	parser_d = (SofParser*)NULL;
	
	// return a warning message
	//
	return Error::handle(name(), L"readData", Error::READ, __FILE__,
			     __LINE__, Error::WARNING);
      }
    }
    else {  
      chaos_c_d.assign(DEF_CHAOS_C);
    }

    // read the initial condition for x 'x0'
    //
    if (parser_d->isPresent(sof_a, PARAM_CHAOS_X0)) {
      if (!chaos_x0_d.readData(sof_a, PARAM_CHAOS_X0,
			   parser_d->getEntry(sof_a, PARAM_CHAOS_X0))) {

	// delete the parser
	//
	delete parser_d;
	parser_d = (SofParser*)NULL;
	
	// return a warning message
	//
	return Error::handle(name(), L"readData", Error::READ, __FILE__,
			     __LINE__, Error::WARNING);
      }
    }
    else {  
      chaos_x0_d.assign(DEF_CHAOS_X0);
    }

    // read the initial condition for y 'y0'
    //
    if (parser_d->isPresent(sof_a, PARAM_CHAOS_Y0)) {
      if (!chaos_y0_d.readData(sof_a, PARAM_CHAOS_Y0,
			   parser_d->getEntry(sof_a, PARAM_CHAOS_Y0))) {

	// delete the parser
	//
	delete parser_d;
	parser_d = (SofParser*)NULL;
	
	// return a warning message
	//
	return Error::handle(name(), L"readData", Error::READ, __FILE__,
			     __LINE__, Error::WARNING);
      }
    }
    else {  
      chaos_y0_d.assign(DEF_CHAOS_Y0);
    }

    // read the initial condition for z 'z0'
    //
    if (parser_d->isPresent(sof_a, PARAM_CHAOS_Z0)) {
      if (!chaos_z0_d.readData(sof_a, PARAM_CHAOS_Z0,
			   parser_d->getEntry(sof_a, PARAM_CHAOS_Z0))) {

	// delete the parser
	//
	delete parser_d;
	parser_d = (SofParser*)NULL;
	
	// return a warning message
	//
	return Error::handle(name(), L"readData", Error::READ, __FILE__,
			     __LINE__, Error::WARNING);
      }
    }
    else {  
      chaos_z0_d.assign(DEF_CHAOS_Z0);
    }      

    // read the upsampling factor for numerical integration 'chaos_integrate_upsample'
    //
    if (parser_d->isPresent(sof_a, PARAM_CHAOS_INTEGRATE_UPSAMPLE)) {
      if (!chaos_integrate_upsample_d.readData(sof_a, PARAM_CHAOS_INTEGRATE_UPSAMPLE,
			   parser_d->getEntry(sof_a, PARAM_CHAOS_INTEGRATE_UPSAMPLE))) {

	// delete the parser
	//
	delete parser_d;
	parser_d = (SofParser*)NULL;
	
	// return a warning message
	//
	return Error::handle(name(), L"readData", Error::READ, __FILE__,
			     __LINE__, Error::WARNING);
      }
    }
    else {  
      chaos_integrate_upsample_d.assign(DEF_CHAOS_INTEGRATE_UPSAMPLE);
    }  
  }

  // exit gracefully
  //
  return true;
}
