// file: $isip/class/search/Instance/inst_05.cc
// version: $Id: inst_05.cc 9002 2003-01-24 15:26:13Z alphonso $
//

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

// method: printHistory
//
// arguments:
//   History* history: (input) history of the instance
//
// return: logical error status
//
// method print the current contents of the history stack
//
boolean Instance::printHistory(History* history_a) {

  // declare local variables
  //
  Context* symbol = (Context*)NULL;

  // when the history is null do  nothing
  //
  if (history_a == (History*)NULL) {
    return true;
  }

  // save the current state of the history
  //
  history_a->setMark();
  
  // print each level of the history
  //
  String output(L"printing each level of the history ");
  Console::put(output);
    
  for (boolean more = history_a->gotoFirst(); more;
       more = history_a->gotoNext()) {

    symbol = history_a->pop();      

    if (symbol != (Context*)NULL) {
      symbol->print();
    }
  }

  // return to the saved state
  //
  history_a->gotoMark();
  
  // exit gracefully
  //
  return true;  
}

// method: setActive
//
// arguments:
//  boolean is_active: (input) defines the instance as active or inactive
//
// return: a boolean value indicating status
//
// this method sets the instance to be either active or inactive
//
boolean Instance::setActive(boolean is_active_a) {

  if (!is_active_a) {
    score_d = INACTIVE_SCORE;
  }
  else {
    if (score_d == INACTIVE_SCORE) {
      score_d = DEF_SCORE;
    }
  }

  // exit gracefully
  //
  return true;
}

// method: setBackPointer
//
// arguments:
//  Instance* back_ptr: (input) the instance to point back to
//
// return: a boolean value indicating status
//
// this method sets the instance's back pointer
//
boolean Instance::setBackPointer(Instance* back_ptr_a) {

  if (back_ptr_d != (Instance*)NULL) {
    back_ptr_d->decrementRefCount();
  }
  
  // set the new back pointer
  //
  back_ptr_d = back_ptr_a;
  if (back_ptr_d != (Instance*)NULL) {
    back_ptr_d->incrementRefCount();
  }
  
  // exit gracefully
  //
  return true;
}

// method: decrementRefCount
//
// arguments:
//  ulong decrement: (input) the number to decrement by 
//
// return: the reference count after decrementing
//
// this method decrements the reference count
//
ulong Instance::decrementRefCount(ulong decrement_a) {

  if (decrement_a > reference_count_d) {
    reference_count_d = 0;
  }
  else {
    reference_count_d -= decrement_a;
  }

  // exit gracefully
  //
  return reference_count_d;
}
