// file: $isip/class/pr/LanguageModel/lm_02.cc
//

// ISIP include files
//
#include "LanguageModel.h"

// method: diagnose
//
// arguments:
//  Integral::DEBUG level: (input) debug level for diagnostics
//
// return: a boolean value indicating status
//
boolean LanguageModel::diagnose(Integral::DEBUG level_a) {

  //---------------------------------------------------------------------------
  //
  // 0. preliminaries
  //
  //---------------------------------------------------------------------------
  
  // output the class name
  //
  if (level_a > Integral::NONE) {
    SysString output(L"diagnosing class ");
    output.concat(CLASS_NAME);
    output.concat(L": ");
    Console::put(output);
    Console::increaseIndention();
  }
  
  //--------------------------------------------------------------------------
  //
  // 1. required public methods
  //
  //--------------------------------------------------------------------------

  // set indention
  //
  if (level_a > Integral::NONE) {
    Console::put(L"testing required public methods...\n");
    Console::increaseIndention();
  }
  
  // test constructor
  //
  LanguageModel lm;
  LanguageModel lm2;
  LanguageModel lm3;
  LanguageModel lm4;
  LanguageModel lm5;
    
  if (level_a > Integral::BRIEF) {
    Console::put(L"testing debug method...");
   }
  
  // load grammar file
  //
  Filename model_file_1;
  Filename model_file_2;
  Filename model_file_3;
  Filename model_file_4;  
  Filename model_file_5;
  model_file_1.assign(L"diagnose_grammar_ihd.sof"); 
  model_file_2.assign(L"diagnose_grammar.sof");
  model_file_3.assign(L"diagnose_grammar_out_1.sof"); 
  model_file_4.assign(L"diagnose_grammar.sof");
  model_file_5.assign(L"diagnose_grammar_out_2.sof"); 
 
  Sof lm_file_1;
  Sof lm_file_2;
  Sof lm_file_3;
  Sof lm_file_4;
  Sof lm_file_5;

  lm_file_1.open(model_file_1, File::READ_ONLY);
  lm_file_2.open(model_file_2, File::READ_ONLY);
  lm_file_3.open(model_file_3, File::WRITE_ONLY);
  lm_file_4.open(model_file_4, File::READ_ONLY);
  lm_file_5.open(model_file_5, File::WRITE_ONLY);
  
  // test read methods
  //
  if (level_a > Integral::BRIEF) {
    Console::put(L"testing read method...");
  }
    
  if(!lm.read(lm_file_1,0)) {
    return Error::handle(name(), L"read", Error::TEST, __FILE__, __LINE__);
  }
  lm_file_1.close();
  if(!lm2.read(lm_file_2,0)) {
    return Error::handle(name(), L"read", Error::TEST, __FILE__, __LINE__);
  }
  lm_file_2.close();    

  // test write method is tested in class-specific methods
  //

  // test assign
  //
  lm2.assign(lm);

  if (level_a > Integral::BRIEF) {
    Console::put(L"testing assign method...");
  }
  
  // test eq method
  //
  if (level_a > Integral::BRIEF) {
    Console::put(L"testing eq method...");
  }
  
  if(!lm2.eq(lm)) {
    return Error::handle(name(), L"eq", Error::TEST, __FILE__, __LINE__);
  }
  
  // test clear method
  //
  lm3.clear();
  
  if (level_a > Integral::BRIEF) {
    Console::put(L"testing clear method...");
  }
  
  // reset indention
  //
  if (level_a > Integral::NONE) {
    Console::decreaseIndention();
  }
  
  //--------------------------------------------------------------------------
  //
  // 2. class-specfic public methods:
  //
  //--------------------------------------------------------------------------
  
  // set indention
  //
  if (level_a > Integral::NONE) {
    Console::put(L"testing class public methods...\n");
    Console::increaseIndention();
  }

  // test other methods
  //
  lm2.setFormat(lm.getFormat());
  lm2.setRuleModel(lm.getRuleModel());
  
  if (level_a > Integral::BRIEF) {
    Console::put(L"testing other methods...");
  }
  
  // test write method
  //
  if (level_a > Integral::BRIEF) {
    Console::put(L"testing write method...");
  }

  if(!lm2.write(lm_file_3,0)) {
    return Error::handle(name(), L"write", Error::TEST, __FILE__, __LINE__);
  }
  lm_file_3.close();
  

  if(!lm4.read(lm_file_4,0)) {
    return Error::handle(name(), L"read", Error::TEST, __FILE__, __LINE__);
  }
  lm_file_4.close();

  // test convert method
  //
  lm4.convert(XML);
  lm4.write(lm_file_5,0);
  
  lm_file_5.close();
  
  // reset indendtion
  //
  if (level_a > Integral::NONE) {
    Console::decreaseIndention();
  }

  //--------------------------------------------------------------------------
  //
  // 3. Clean up.
  //
  //--------------------------------------------------------------------------

  // reset indention
  //
  if (level_a > Integral::NONE) {
    Console::decreaseIndention();
  }
  
  if (level_a > Integral::NONE) {
    SysString output(L"diagnostics passed for class ");
    output.concat(name());
    output.concat(L"\n");
    Console::put(output);
  }

  // indicate success
  //
  return true;
}
