/**
 *  bondInferenceGuiStrategy.java
 *  @contains provides a GUI for managing the inference
    engine, e.g., rule and fact loading.
 *
 *  @author Kyungkoo Jun
 *  Bond group, CS, Purdue Univ.
 *  @date February 22, 1999
 */
package bond.agent.strategydb;

import java.awt.event.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import bond.agent.*;
import bond.agent.interfaces.*;
import bond.core.gui.util.*;

public class bondInferenceGuiStrategy
extends bondGuiStrategy
{

   JTextArea cmdArea;
   JTextArea output;
   JButton exeCmd, facts, clear, quit;
   boolean inited = false;

   public bondInferenceGuiStrategy() {
   }
   public void install(bondFiniteStateMachine fsm) {
      super.fsm = fsm;
      model = fsm.agent.model;

      if (!inited) {
         init_gui();
         inited = true;
      }

   }

   public void uninstall() {
   }

   public long action(bondModel model, bondAgenda agenda) {

      transition("wait");
      return 0L;
   }

   private void init_gui() {

      cmdArea = addArea();
      exeCmd = addButton("Exec. Cmd");
      facts = addButton("Facts");
      clear = addButton("Clear");
      quit = addButton("Quit", last);
      output = addArea();

      w.setTitle("Inference Engine Control Panel");
      Display();
   }

   public void actionPerformed(ActionEvent e) {
      // respond to button press
      String com = e.getActionCommand();

      if (com.equals("Quit")) {
         System.exit(0);
      }
      else if (com.equals("Exec. Cmd")) {
         executeCommand();
      }
      else if (com.equals("Facts")) {
         try {
            output.append(((bondInferenceEngine)model.get("INFEGN")).show_facts());
         }
         catch (Exception e1) {
            output.append("Inference Engine Error\n");
            e1.printStackTrace();
         }
      }
      else if (com.equals("Clear")) {
         cmdArea.setText("");
         output.setText("");
      }

   }

   /*
   private void load_knowledgebase(String fname) {

      BufferedReader br;

      if (fname.length() < 1) {
         output.append("File Name Should be specified\n");
         return;
      }
      else { // load from file
         kbase.setText("");
         try {
            br = new BufferedReader(new FileReader(fname));
            do {
               kbase.append(br.readLine()+"\n");
            } while (br.ready());
            br.close();
         }
         catch (FileNotFoundException fexp) {
            output.append("File is not found\n");
            return;
         }
         catch (IOException ioexp) {
            output.append("IO error during file reading\n");
            return;
         }
      }
   }
   */

   private void executeCommand() {

      bondInferenceEngine infegn = (bondInferenceEngine)model.get("INFEGN");
      if (infegn != null ) {
         String cmd = cmdArea.getText();
         if (cmd.length() > 1 && infegn.executeCmd(cmd)) {
            output.append(cmd+" Executed\n");
         }
         else {
            output.append("Empty or improper command\n");
         }
         cmdArea.setText("");
      }
      else
         output.append("Inference Engine is not available\n");
   }

   public String getDescription() {
           return "Provide control panel for inference engine";
   }

   // For Testting & Debugging
   public static void main(String argv[]) {
      bondInferenceGuiStrategy gui = new bondInferenceGuiStrategy();
      gui.init_gui();
   }
}

