import java.applet.*; import java.awt.*; import java.util.*; import java.awt.event.*; public class GSPlanner extends Applet implements ActionListener{ String[][][] operators={ {{"fry_eggs"}, {"have_eggs","have_pan"}, {"pan_is_clean","have_eggs"}, {"have_ommelette"}}, {{"clean_pan"}, {"have_water","have_pan"}, {}, {"pan_is_clean"}}, }; String[] initialState={"have_eggs","have_pan","have_water"}; String[] goalState={"pan_is_clean","have_ommelette"}; Vector goalStack=new Vector(); Vector currentPlan=new Vector(); Vector currentState=new Vector(); TextField currentStateT=new TextField("",62); List currentPlanL=new List(10); // {public Dimension getPreferredSize(){return getPreferredSize(20);}}; List goalStackL=new List(10); Label message=new Label(""); public void init(){ Button b; Panel p,p2; setLayout(new BorderLayout()); p=new Panel(); p.setLayout(new BorderLayout()); p.add("North",new Label("Current Plan: ")); p.add("South",currentPlanL); add("West",p); p=new Panel(); p.setLayout(new BorderLayout()); p.add("North",new Label( "Goal Stack: ")); p.add("South",goalStackL); add("East",p); p=new Panel(); p.setLayout(new BorderLayout()); p2=new Panel(); p2.setLayout(new BorderLayout()); p2.add("West",new Label("Current State:")); p2.add("East",currentStateT); p.add("North",p2); p.add("East",b=new Button("Reset")); b.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){reset();} }); p.add("West",b=new Button("Step")); p.add("Center",message); b.addActionListener(this); add("South",p); reset(); } public void reset(){ Vector t=new Vector(); goalStack.removeAllElements(); for(int i=0;igoalStack.size()) goalStackL.add(""); else goalStackL.add(conjunction((Vector)goalStack.elementAt(9-i))); } String conjunction(Vector v){ String t=new String(""); for(int i=0;i1){ //conjunction of goals message("Found conjunction of goals ( "+conjunction(t)+").",2000); message("Ignoring goals already in Current State ...",2000); for(int i=t.size();i-->0;){ if(stringInVector((String)t.elementAt(i),currentState)) t.removeElementAt(i); } if(t.size()>1){ message("... pushing resulting conjuntion ( "+conjunction(t) +") onto Goal Stack ...",2000); goalStack.addElement(t); //push a vector with the conjunction refresh(); message("... done.",2000); } if(t.size()>0){ message("... pushing individual goals ( "+conjunction(t) +") onto Goal Stack ...",2000); for(int i=t.size();i-->0;){ Vector push=new Vector(); push.addElement((String)t.elementAt(i)); goalStack.addElement(push); //push a vector of only one element } refresh(); message("... done.",2000); } if(t.size()==0){ message("... all goals where already in Current State.",2000); } return; } //only one in top, may be a goal or an operator String top=(String)t.elementAt(0); for(int i=0;i0;) if(stringInArray((String)currentState.elementAt(j),operators[i][2])) currentState.removeElementAt(j); refresh(); message("... done.",2000); message("Applying operator's add list ( "+conjunction(operators[i][3]) +")...",2000); for(int j=operators[i][3].length;j-->0;) currentState.addElement(operators[i][3][j]); refresh(); message("... done.",2000); return; } } message("Unknown element ("+top+") on top of stack.",2000); goalStack.addElement(t);//just put it as it was refresh(); //flow should never reach here } void message(String m, long w){ message.setText(" "+m); try{synchronized(this){wait(w*2);}}catch(InterruptedException e){} message.setText(""); } boolean stringInVector(String s, Vector v){ for(int i=0;i