/**
 *  bondInferenceStrategy.java
 *  @contains controls the inference engine. It contains
       an instance of the JESS inference engine.
       (See bond.agent.strategydb.bondInferenceEngine.java)
 *
 *  @author Kyungkoo Jun
 *  Bond group, CS, Purdue Univ.
 *  @date February 22, 1999
 */

package bond.agent.strategydb;

import bond.agent.*;
import bond.agent.interfaces.*;
import java.util.*;
import java.io.*;
import bond.core.util.*;

public class bondInferenceStrategy
extends bondDefaultStrategy
{
   // debugging
   //bondModel model;

   bondInferenceEngine infegn;
   private boolean inited = false;

   public bondInferenceStrategy() {
   }

   public void install(bondFiniteStateMachine fsm) {

      super.install(fsm);
      System.out.println("in Inference, first");
      if (!inited) // initialization should be done only once
         init_infegn();
      System.out.println("in StockWatcherGui, second");         
   }

   public long action(bondModel model, bondAgenda agenda) {

      if (!inited) {
         transition("default");
         return 0L;
      }

      Log.Debug(infegn.show_facts());
      infegn.run(1);

      String nxtste = (String)model.get("NextState");
      if (nxtste == null) {
         if ((nxtste = (String)getModel("Previous")) != null)
            transition(nxtste);
         else
            transition("default");
      }
      else
         transition(nxtste);
      setModel("NextState", "default");  // reset
      return 0L;
   }

   private void init_infegn() {

      // create Jess inference engine
      if (infegn == null) {
         infegn = new bondInferenceEngine();
         infegn.storeObject("Model", model);
         setModel("INFEGN", infegn);
      }

      String rulefilename = (String)getModel("RuleFileName");

      if (rulefilename != null) {

         try {
            RandomAccessFile f = new RandomAccessFile(rulefilename, "r");

            StringBuffer s = new StringBuffer("");
            String str;
            while ( (str = f.readLine()) != null)
               s = s.append(str+"\n");

            if (!infegn.load_kbase(s)) {
                  System.out.println("Rule Loading Error");
                  System.exit(1); // for debugging
                  return;
            }
         }
         catch (IOException e) {
            model.set("ErrorMessage", "IO exception while loading "+rulefilename);
            e.printStackTrace();
            System.exit(1); // for debugging
            return;
         }
      }

      inited = true;
   }

   public String getDescription() {
           return "Perform inference using Jess";
   }

   // debugging
   /*
   public static void main(String argv[]) {
      bondInferenceStrategy s = new bondInferenceStrategy();
      s.init_infegn();
      s.tmp();

   }
   public void tmp() {

     infegn.load_kbase(new StringBuffer("(deftemplate quoteobj (slot quoteName) (slot currentPrice))"+
     "(defglobal ?*model* = (fetch Model))"+
     "(deffunction setModel (?name ?value)"+
     "(call ?*model* set ?name (new java.lang.Float(?value))))"+
     "(defrule price"+
     "(quoteobj (quoteName ?n) (currentPrice ?c))"+
     "=>"+
     "(setModel ?n ?c)"+
     "(printout t ?n\" has price \"?c crlf))"));

     try {
     infegn.executeCmd("(batch stockrule.txt)");

     infegn.insert_fact("(quoteobj (quoteName \"IBM\") (currentPrice 145.4))");
     infegn.insert_fact("(quoteobj (quoteName \"DELL\") (currentPrice 45.4))");
     System.out.println(infegn.show_facts());

     //   infegn.executeCmd("(facts)");
     }
     catch (Exception e) {
        e.printStackTrace();
     }
     infegn.run(20);

     System.out.println(infegn.getResult());
     System.out.println("Value is "+getModel("IBM"));
     System.out.println("Value is "+getModel("DELL"));
     System.out.println("State is "+getModel("NextState"));
   } */
}
