
import jess.*;
import jess.examples.xfer.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;

class CustomerInt{
   public String attrib1;
   public String attrib2;
   public String attrib3;
   public String attrib4;

   CustomerInt(String s1, String s2,String s3, String s4)
   {
    attrib1=s1;
    attrib2=s2;
    attrib3=s3;
    attrib4=s4;
   }

}

class STCustomerInt extends CustomerInt {
  
public  String dervattrib[] = new String[10];

STCustomerInt(String s1, String s2, String s3, String s4, String s5)
   {
    super(s1,s2,s3,s4);
    dervattrib[0]=  s5;
   }

}




public class ug5 extends Frame implements ActionListener,FocusListener
{

    Button b1;
    Label l0,l1,l2,l3,l4,l5,l10,l7,l23,l32;
    Choice c1,c2,c3;
    TextField t1,t2;
    TextArea ta1; 
    int tokenCount;
    String fAtomArray[] = new String[25];
    void atomizeFreeFormText(String fft, String[] fArray)
    {
		StringTokenizer stringToken = new StringTokenizer(fft);
     
      int i = 0;
      while(stringToken.hasMoreTokens())
      {
        fArray[i]= stringToken.nextToken();
        i++;
      }
     tokenCount = i;
           
 }

    public static void main(String args [])
   {
     ug5  arm = new ug5("STCI from CI");
   }
 

 
    public ug5 (String pa)
    {

     super(pa);
     super.setSize(600,600);
     setLayout(null);
     Font ff2 = new Font ("Comic",Font.BOLD,12);
     setFont(ff2);
     l0 = new Label ("Customer Interface [CI]");
     l23 = new Label ("==================");
     l7 = new Label ("Strongly Type Customer Interface [STCI]");
     l32 = new Label ("================================");
     b1 = new Button("OK");
     l10 = new Label ("      ");
     l1 = new Label ("Company");
     l2 = new Label ("Complain - 1");
     l3 = new Label ("Complain - 2");
     l4 = new Label ("Free form Text ");
     l5 = new Label ("Derrived Attribute ");
     c1 = new Choice();
     c1.add("Company 1");
     c1.add("Company 2");
     c1.add("Company 3");
     c1.add("Company 4");
     c1.add("Company 5");


     c2 = new Choice();
     c2.add("Problem1 ");
     c2.add("Problem2 ");
     c2.add("Problem3 ");
     c2.add("Problem4 ");
     c2.add("Problem5 ");


     c3 = new Choice();
     c3.add("P1 ");
     c3.add("P2 ");
     c3.add("P3 ");
     c3.add("P4 ");
     c3.add("P5 ");

     t1 = new TextField(20);
     t2 = new TextField(20); 
     
     ta1 = new TextArea(10,30);


     l0.setBounds(10,30,220,30);
     l23.setBounds(10,50,220,30);

     l1.setBounds(10,70,160,30);
     c1.setBounds(230,70,250,30);
     l2.setBounds(10,120,160,30);
     c2.setBounds(230,120,250,30);
     l3.setBounds(10,170,160,30);
     c3.setBounds(230,170,250,30);
     l4.setBounds(10,220,200,30);
     t1.setBounds(230,220,250,30);
     b1.setBounds(10,270,50,25);
     l7.setBounds(10,315,250,25);
     l32.setBounds(10,335,250,25);

     ta1.setBounds(10,360,470,200);
     add(l0);
     add(l10);
     add(l1);
     add(c1);
     add(l2);
     add(c2);
     add(l3);
     add(c3);
     add(l4);
     add(t1);
     add(b1);
     add(l7);
     add(ta1);
     add(l23);
     add(l32);
     b1.addActionListener(this);
     MyWindowAdapter adapter = new MyWindowAdapter(this);
     addWindowListener(adapter);
     super.setVisible(true);
     
     }


     public void actionPerformed(ActionEvent ae)
     {
         
          String ciV1 = c1.getSelectedItem(); 
          String ciV2 = c2.getSelectedItem(); 
 	       String ciV3 = c3.getSelectedItem(); 
	       String ciV4 = t1.getText();   
          CustomerInt ci = new CustomerInt(ciV1,ciV2,ciV3,ciV4);  
          STCustomerInt stci = new STCustomerInt(ciV1,ciV2,ciV3,ciV4," ");
          atomizeFreeFormText(ciV4,fAtomArray);

          Man m = new Man();
          m.tomain(ci,stci,fAtomArray,tokenCount);
          ta1.setText(  stci.attrib1+"\n"
          					+stci.attrib2+"\n"
					         +stci.attrib3+"\n" 
					         +stci.attrib4+ "\n\n"
					         +stci.dervattrib);   


     }
     
     

     public void focusGained(FocusEvent fe)
     {                          
     }

     public void focusLost(FocusEvent fe)
     {                          
     }

}



///////////////////window adapter/////////


class MyWindowAdapter extends WindowAdapter{
     ug5 arm;
     public MyWindowAdapter(ug5 arm){
     this.arm = arm;
	}
public void  windowClosing(WindowEvent we){
     arm.dispose();
     System.exit(0);
     }


}

//'''''''''''''''''''''''''''''''''''''''''/////////////////



/////////////////////////JESS IMP??????????????????




class Man
{  
  public void tomain(CustomerInt ci, STCustomerInt stci, String[] fAtomArray,int tokenCount ) 
  {        
            
    try
      {
        // *************************************************************
        // Create engine; load in some extra functions

        Rete rete = new Rete();
        rete.addUserpackage(new PredFunctions());
        rete.executeCommand("(deffacts Knowledgebase"+"(Knowledge knowledge1)"+"(Knowledge not)"+"(Knowledge working))" ); 
        rete.reset();
        String name;

        for(int i=0;i<tokenCount;i++)

                		{  	        			    
                      try{
                            rete.executeCommand("(defrule assert_problems" +
                            " =>" +
                            "  (assert (Problem "+ fAtomArray[i] +")))");
                            rete.run();
                                 
                      }catch(JessException e)
                      {
                      System.out.println(e);
                      }      
     		              }
     System.out.println("Derived attributes are as follows");    
  		              
     rete.executeCommand(
     									"(defrule findderv" +
       								"(Knowledge ?x)"+
								      "(Problem ?x)"+
								      "=>"+
								      "(assert (derivedattrib ?x))"+
								      "(printout t  ?x crlf))" 
								 );
     rete.run();
     
     ///suggest some code in java such that we can store 
     ///the ?x of the frule findderv into an array of string defined
     ///in java ie., the string array derrvattrib[] defined in 
     ///STCustomerInt class.
     ///
     				
      }
    catch (JessException re) 
      {
        re.printStackTrace();
      }

  }
}