// file: $isip/class/pr/SupportVectorMachine/svm_03.cc
//

// isip include files
//
#include "SupportVectorMachine.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 according
// to the specified name and tag
//
boolean SupportVectorMachine::read(Sof& sof_a, 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
  //
  return readData(sof_a);
}

// 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_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 has the object read itself from an Sof file. it assumes
// that the Sof file is already positioned correctly
//
boolean SupportVectorMachine::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);
  }	    

  // read the algorithm
  //
  if (parser.isPresent(sof_a, PARAM_ALGORITHM)) {
    if (!ALGO_MAP.readElementData((long&)algorithm_d, sof_a, PARAM_ALGORITHM,
				  parser.getEntry(sof_a, PARAM_ALGORITHM))) {
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }       
  }
  else {
    algorithm_d = DEF_ALGORITHM;
  }

  // read the implementation
  //
  if (parser.isPresent(sof_a, PARAM_IMPLEMENTATION)) {
    if (!IMPL_MAP.readElementData((long&)implementation_d, sof_a,
				  PARAM_IMPLEMENTATION,
			      parser.getEntry(sof_a, PARAM_IMPLEMENTATION))) {
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }       
  }
  else {
    implementation_d = DEF_IMPLEMENTATION;
  }

  // read the penalty
  //
  if (parser.isPresent(sof_a, PARAM_PENALTY)) {
    if (!penalty_d.readData(sof_a, PARAM_PENALTY,
			    parser.getEntry(sof_a, PARAM_PENALTY))) {
      
      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
      
    }
  }

  // read the tolerance
  //
  if (parser.isPresent(sof_a, PARAM_TOLERANCE)) {
    if (!tol_d.readData(sof_a, PARAM_TOLERANCE,
		      parser.getEntry(sof_a, PARAM_TOLERANCE))) {
      
      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
      
    }
  }
  
  // read the epsilon
  //
  if (parser.isPresent(sof_a, PARAM_EPSILON)) {
    if (!eps_d.readData(sof_a, PARAM_EPSILON,
		      parser.getEntry(sof_a, PARAM_EPSILON))) {
      
      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
      
    }
  }    

  // read the polynomial degree
  //
  if (parser.isPresent(sof_a, PARAM_POLYNOMIAL_DEGREE)) {
    if (!polynomial_degree_d.readData(sof_a, PARAM_POLYNOMIAL_DEGREE,
		      parser.getEntry(sof_a, PARAM_POLYNOMIAL_DEGREE))) {
      
      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
      
    }
  }    

  // read the rbf gamma
  //
  if (parser.isPresent(sof_a, PARAM_RBF_GAMMA)) {
    if (!rbf_gamma_d.readData(sof_a, PARAM_RBF_GAMMA,
		      parser.getEntry(sof_a, PARAM_RBF_GAMMA))) {
      
      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
      
    }
  }    

  // read the sigmoid kappa
  //
  if (parser.isPresent(sof_a, PARAM_SIGMOID_KAPPA)) {
    if (!sigmoid_kappa_d.readData(sof_a, PARAM_SIGMOID_KAPPA,
		      parser.getEntry(sof_a, PARAM_SIGMOID_KAPPA))) {
      
      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
      
    }
  }    

  // read the sigmoid delta
  //
  if (parser.isPresent(sof_a, PARAM_SIGMOID_DELTA)) {
    if (!sigmoid_delta_d.readData(sof_a, PARAM_SIGMOID_DELTA,
		      parser.getEntry(sof_a, PARAM_SIGMOID_DELTA))) {
      
      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
      
    }
  }    

  // read the audio database file
  //
  if (parser.isPresent(sof_a, PARAM_AUDIO_DB)) {
    if (!audio_db_file_d.readData(sof_a, PARAM_AUDIO_DB,
			       parser.getEntry(sof_a,
					       PARAM_AUDIO_DB))) {
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }       
  }

  // read the output type
  //
  if (parser.isPresent(sof_a, PARAM_OUTPUT_TYPE)) {
    if (!TYPE_MAP.readElementData((long&)output_type_d, sof_a,
					 PARAM_OUTPUT_TYPE,
				  parser.getEntry(sof_a, PARAM_OUTPUT_TYPE))) {
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }       
  }

  // read the output file name
  //
  if (parser.isPresent(sof_a, PARAM_OUTPUT_FILE)) {
    if (!output_file_d.readData(sof_a, PARAM_OUTPUT_FILE,
			       parser.getEntry(sof_a,
						PARAM_OUTPUT_FILE))) {
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }       
  }
  
  // check that all parameters are accounted for
  //
  if (!parser.checkParams(sof_a)) {

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

  // exit gracefully
  //
  return true;
}
