// file: dtn_question_1.cc
//

// system include files
//
#include <string.h>

// isip include files
//
#include "dt_node.h"
#include "dt_node_constants.h"

// method: ask_opt_question_cc
//
// arguments:
//   int_4 staet_index: (input) global state index
//   Hash_table* answer: (input) Hash_table with all the answer
//   int_4** l_context: (input) left context list
//   int_4** r_context: (input) right context list

// return: a logical_1 inidicating the answer is yes or no
//
// this method asks the optimal question for a state
//
logical_1 Dt_node::ask_opt_question_cc(int_4 state_index_a,
				       Hash_table* answer_a,
				       int_4** l_context_a,
				       int_4** r_context_a) {

  char_1* key_string = new char_1[ISIP_MAX_STRING_LENGTH];
  logical_1 tag_left;
  int_4 num = 0;
  char_1* attribute = (char_1*)NULL;
  int_4 phn;
  char_1* dire= new char_1[2];
  
  num = opt_question_d->get_num_cc();
  attribute = opt_question_d->get_attribute_cc();
  tag_left = opt_question_d->get_direction_cc();  
  
  if(tag_left==ISIP_TRUE) {
    phn = l_context_a[state_index_a][num];
    strcpy((char*)dire, "L");  
  }
  
  else{
    phn = r_context_a[state_index_a][num];
    strcpy((char*)dire, "R");  
  }
  
  // create the key string
  //
  sprintf((char*)key_string, "%ld", phn);
  strcat((char*)key_string, " ");
  strcat((char*)key_string, (char*)dire);
  strcat((char*)key_string, " ");
  strcat((char*)key_string, (char*)attribute);
  
  // loop up the answer
  //
  if((answer_a->hash_lookup_cc(key_string)) == (Hash_cell*)NULL) {
    
    // free memory
    //
    delete [] key_string;
    delete [] dire;
    
    // exit gracefully with the answer no
    //
    return ISIP_FALSE;
  }
  
  // free memory
  //
  delete [] key_string;
  delete [] dire;
  
  //exit gracefully with the answer yes
  //
  return ISIP_TRUE;
}
