// file: $isip/class/pr/LanguageModelJSGF/lmjsgf_08.cc
//

// isip include files
//
#include "LanguageModelJSGF.h"
#include <LanguageModelABNF.h>

// method: getRuleModel
//
// arguments:
//   none
// 
// return: a RuleModel corresponding to the XML grammar in memory
//
// this method initiates ABNF->BNF conversion
//
RuleModel LanguageModelJSGF::getRuleModel() {

  abnf_model_d.assign(getABNFRuleModel());
  
  // now we have an all ABNF RuleModel.  here's the
  // tricky part: we instantiate a LanguageModelABNF
  // object to perform the rest of the conversion to
  // BNF
  //
  LanguageModelABNF lm_abnf;
  lm_abnf.setABNFRuleModel(abnf_model_d);
  return lm_abnf.getRuleModel();  
}

// method: getRuleModel
//
// arguments:
//
// return:
//
RuleModel LanguageModelJSGF::getABNFRuleModel() {

  RuleModel rm;
  Vector<Vector<ProductionRuleSet> > abnf_grammars;

  // loop over levels
  //
  for (long  i = 0; i < grammars_d.length(); i++) {

    Vector<ProductionRuleSet> abnf_grammar;

    // loop over all graphs at this level
    //
    for (long j = 0; j < grammars_d(i).length(); j++) {

	abnf_grammar.concat(convertJSGFtoABNF(grammars_d(i)(j)));
	
    }
    
    abnf_grammars.concat(abnf_grammar);
  }

  rm.assign(abnf_grammars, hg_d);
  
  return rm;
  
}

// method: setRuleModel
//
// arguments:
//   RuleModel rm_a: a RuleModel corresponding to the BNF graph
// 
// return: boolean
//
// this method initiates BNF->JSGF conversion
//
boolean LanguageModelJSGF::setRuleModel(const RuleModel& rm_a) {

  rm_d.assign(rm_a);
  grammars_d.clear();
  Vector< Vector <ProductionRuleSet> > bnf_rules(rm_d.first());
  
  // loop over levels
  //
  for (long i = 0; i < bnf_rules.length(); i++) {

    Vector< Vector<JSGFToken> > graphs;

    // loop over graphs
    //
    for (long j = 0; j < bnf_rules(i).length(); j++) {

      graphs.concat(convertBNFtoJSGF(bnf_rules(i)(j)));     
    }
    grammars_d.concat(graphs);  
  }

  return true;
}
