View Javadoc

1   /*
2   * The contents of this file are subject to the BT "ZEUS" Open Source 
3   * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file 
4   * except in compliance with the Licence. You may obtain a copy of the Licence
5   * from $ZEUS_INSTALL/licence.html or alternatively from
6   * http://www.labs.bt.com/projects/agents/zeus/licence.htm
7   * 
8   * Except as stated in Clause 7 of the Licence, software distributed under the
9   * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or 
10  * implied. See the Licence for the specific language governing rights and 
11  * limitations under the Licence.
12  * 
13  * The Original Code is within the package zeus.*.
14  * The Initial Developer of the Original Code is British Telecommunications
15  * public limited company, whose registered office is at 81 Newgate Street, 
16  * London, EC1A 7AJ, England. Portions created by British Telecommunications 
17  * public limited company are Copyright 1996-9. All Rights Reserved.
18  * 
19  * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
20  */
21  
22  
23  
24  package zeus.visualiser.control;
25  
26  import java.util.*;
27  import java.awt.*;
28  import java.io.*;
29  import java.awt.event.*;
30  import javax.swing.*;
31  import javax.swing.border.*;
32  
33  import zeus.util.*;
34  import zeus.concepts.*;
35  // import zeus.concepts.rtn.IEngine;
36  import zeus.gui.*;
37  // import zeus.gui.visual.*;
38  import zeus.gui.dialog.*;
39  import zeus.gui.help.*;
40  import zeus.actors.*;
41  import zeus.visualiser.VisualiserModel;
42  import zeus.visualiser.basic.BasicTool;
43  
44  
45  
46  public class ControlTool extends BasicTool implements ActionListener /*,
47                                                        IBrowseGoal, IDoCommandUI, IEngine */
48  {
49    private static int count = 0;
50  
51    protected ControlMenuBar menubar;
52  
53    protected SingleSelectionDialog sa_goal = null;
54    //protected DoCommandUI cmdUI     = null;
55  
56    protected ControlToolBar toolbar;
57    protected File           file;
58    //protected GoalUI       goalUI = null;
59    //protected BrowseGoalUI   browseGoalUI = null;
60    protected SummaryTable   goalTable = new SummaryTable();
61    protected String         myName;
62    protected AgentContext   context;
63  
64    public ControlTool(AgentContext context, VisualiserModel model)
65  	{
66      super(context, model);
67      myName = context.whoami();
68      this.setTitle(myName + " -  Control Tool:" + (count++));
69      ImageIcon icon = new ImageIcon(SystemProps.getProperty("gif.dir") +
70  		                               File.separator + "visualiser" +
71  																	 File.separator + "control-icon.gif");
72      setIconImage(icon.getImage());
73      this.setBackground(Color.lightGray);
74      getContentPane().setLayout(new BorderLayout());
75  
76      menubar = new ControlMenuBar(this);
77      setJMenuBar(menubar);
78  
79      // Create and Add Toolbars
80      toolbar = new ControlToolBar(this);
81      ImageIcon im = new ImageIcon(SystemProps.getProperty("gif.dir") + File.separator + "visualiser" + File.separator + "bt.gif");
82      JLabel logo = new JLabel(im);
83      JPanel toolpane = new JPanel();
84      toolpane.setLayout(new BorderLayout());
85      toolbar.setBorder(new EmptyBorder(0,16,0,16));
86      toolpane.add(toolbar, BorderLayout.WEST);
87      logo.setBorder(new EmptyBorder(4,8,4,8));
88      toolpane.add(logo, BorderLayout.EAST);
89  
90      getContentPane().setLayout(new BorderLayout());
91      getContentPane().add(toolpane, "North");
92  
93      pack();
94      this.setVisible(true);
95    }
96  
97    public void addNotify()
98  	{
99      super.addNotify();
100     Dimension d = this.getPreferredSize();
101     this.setSize(d.width+150,d.height);
102   }
103 
104   public void actionPerformed(ActionEvent evt)
105 	{
106     // Object src = evt.getSource();
107     String cmd = evt.getActionCommand();
108 
109     if      (cmd.equals("Kill"))        KillAgents();
110     else if (cmd.equals("Help"))        showHelp();
111     else
112       JOptionPane.showMessageDialog(this, "Option not functional yet");
113 
114     /* if      (cmd.equals("AddGoal"))     AddGoal();
115     else if (cmd.equals("TuneAgent"))   TuneAgent();
116     else if (cmd.equals("AddFact"))     AddFact();
117     else if (cmd.equals("AddTask"))     AddTask();
118     else if (cmd.equals("AddRelation")) AddRelation();
119     else if (cmd.equals("AddStrategy")) AddStrategy(); */
120   }
121 
122   public void SendCommand()
123 	{
124     if ( !hubOK() ) return;
125 
126     String[] agents = model.getAgents();
127     /* if ( cmdUI == null )
128       cmdUI = new DoCommandUI(Util.getJFrame(this),"Command Interface",
129                               "Add Agent:", agents, this);
130     else
131 		{
132       String[] chosen = cmdUI.getData();
133       cmdUI.setListData(agents);
134       cmdUI.setData(chosen);
135     }
136     cmdUI.setVisible(true); */
137   }
138 
139   public void executeCommand(String cmd, String[] agents)
140 	{
141     /* if ( cmd != null ) cmd = cmd.trim();
142     if ( cmd.equals("") ) cmd = null;
143     model.addAgents(agents);
144 
145     if ( agents != null && agents.length > 0 && cmd != null )
146       achieve(cmd, agents, "?????"); */
147   }
148 
149   public void LoadOntology()
150 	{
151     File f = getFile(FileDialog.LOAD);
152     if ( f == null ) return;
153 
154     if ( !f.exists() )
155     {
156 		  JOptionPane.showMessageDialog(this, "File does not exist!");
157 			return;
158     }
159 
160     OntologyDb db = context.OntologyDb();
161     int status = db.openFile(f);
162     if ( (status & OntologyDb.ERROR_MASK) != 0 )
163     {
164       JOptionPane.showMessageDialog(this, "Ontology File I/O Error: " + db.getError());
165       return;
166     }
167     else if ( (status & OntologyDb.WARNING_MASK) != 0 )
168       JOptionPane.showMessageDialog(this, "Warning: " + db.getWarning());
169     else if ( (status & OntologyDb.WARNING_MASK) == 0 )
170       JOptionPane.showMessageDialog(this, "Now using ontology from: " + f.getName());
171   }
172 
173   protected File getFile(int type)
174 	{
175     FileDialog f = new FileDialog(this, "Select File", type);
176 
177     if ( file != null ) {
178       f.setFile(file.getName());
179       f.setDirectory(file.getParent());
180     }
181 
182     f.pack();
183     f.setVisible(true);
184 
185     return  f.getFile()==null ? null : new File(f.getDirectory(),f.getFile());
186   }
187 
188   public void AddGoal()
189 	{
190     if ( !hubOK() ) return;
191     /* if ( goalUI == null )
192          goalUI = new GoalUI(Util.getFrame(this),"Goal Editor",GoalUI.EXTENDED,
193                              this,ontology);
194 
195       if ( goalUI.isShowing() )
196          goalUI.toFront();
197       else {
198          String storeKey = AllNames.AGENT_TYPE_PREFIX + AllNames.AGENT;
199          String[] agents = model.getData(storeKey);
200          if ( agents == null ) {
201             JOptionPane.showMessageDialog(Util.getJFrame(this),
202                           "No task agents currently online");
203          }
204          else {
205             goalUI.display(agents);
206          }
207       } */
208    }
209 
210    public void BrowseGoals()
211 	 {
212      if ( !hubOK() ) return;
213 
214      JOptionPane.showMessageDialog(this, "This is Incomplete");
215 
216      /* if ( browseGoalUI == null )
217           browseGoalUI = new BrowseGoalUI(Util.getFrame(this), "Browse Goals",
218                                          goalTable, this);
219 
220       if ( browseGoalUI.isShowing() )
221          browseGoalUI.toFront();
222       else {
223          String storeKey = AllNames.AGENT_TYPE_PREFIX + AllNames.AGENT;
224          String[] agents = model.getData(storeKey);
225          if ( agents == null ) {
226             JOptionPane.showMessageDialog(Util.getJFrame(this),
227                           "No task agents currently online");
228          }
229          else {
230             browseGoalUI.display();
231          }
232       } */
233    }
234 
235    public void StreamGoals(boolean mode)
236 	 {
237       /* if ( !hubOK() ) return;
238 
239       String storeKey = AllNames.AGENT_TYPE_PREFIX + AllNames.AGENT;
240       String[] agents = model.getAgents(); // getData(storeKey);
241 
242       if ( sa_goal == null )
243          sa_goal = new SelectAgent(Util.getFrame(this),"Select Agents",
244                                    "Add Agent:", agents);
245       else {
246          String[] chosen = sa_goal.getData();
247          sa_goal.setListData(agents);
248          sa_goal.setData(chosen);
249       }
250 
251       String[] data  = sa_goal.selectedAgents();
252       model.addReplyKey(sa_goal.getListData());
253 			//model.add(storeKey,sa_goal.getListData());
254 
255       if ( data != null && data.length > 0 )
256         stream_all(mode,keys[GOAL_KEY],"stream-all-goals","log_goal",data); */
257    }
258 
259 
260    public void RequestGoals(String agent) {
261       //stream_all(true,keys[GOAL_KEY],"stream-all-goals","log_goal",agent);
262    }
263 
264    public void SuspendGoals(String agent, String[] ids) {
265       //String content = Misc.concat("suspend goals",ids);
266       //achieve("achieve-suspend-goals",content,agent);
267    }
268 
269    public void ResumeGoals(String agent, String[] ids) {
270       //String content = Misc.concat("resume goals",ids);
271       //achieve("achieve-resume-goals",content,agent);
272    }
273 
274    public void CancelGoals(String agent, String[] ids) {
275       //String content = Misc.concat("cancel goals",ids);
276       //achieve("achieve-cancel-goals",content,agent);
277    }
278 
279    public void add(Goal g) {
280       //achieve("achieve-do-goals", "do goal " + g, g.getDesiredBy());
281    }
282 
283    public void add(Goal g, String ref) {
284       //achieve("achieve-do-goals", "do enact-goal " + ref + " " + g, g.getDesiredBy());
285    }
286 
287    private void KillAgents()
288 	 {
289      System.out.println(myName + " sending kill command");
290      String[] agents = model.getAgents(); // getData(AllNames.AGENTS);
291      if (agents == null) return;
292 
293      for( int i = 0; i < agents.length; i++ )
294        if (!agents[i].equals(myName))
295        {
296 			   System.out.println("Killing ..." + agents[i]);
297          request("kill_yourself", agents[i], "");
298 				 //achieve(context.newId(), "kill_yourself", agents[i]);
299        }
300      request("kill_yourself", myName, "");
301 	 }
302 
303    public void BrowseAgents() { }
304    public void TuneAgent() { }
305 
306    public void BrowseFacts() {}
307    public void AddFact() {}
308    public void StreamFacts(boolean mode) {}
309 
310    public void BrowseTasks() {}
311    public void AddTask() {}
312    public void StreamTasks(boolean mode) {}
313 
314    public void BrowseRelations() {}
315    public void AddRelation() {}
316    public void StreamRelations(boolean mode) {}
317 
318    public void BrowseStrategies() {}
319    public void AddStrategy() {}
320    public void StreamStrategies(boolean mode) {}
321 
322    protected void removeRequestKeys() {
323       //removeKey(keys[ADDRESS_KEY],"log_address");
324       //removeKey(keys[GOAL_KEY],"log_goal");
325    }
326 
327    protected void registerAgent(String name, String type) {
328       /* if ( type.equals(AllNames.AGENT) ) {
329          goalTable.add(name);
330          if ( browseGoalUI != null && browseGoalUI.isShowing() )
331             browseGoalUI.refresh();
332       } */
333    }
334 
335    protected boolean incomingMessage(Performative msg)
336 	 {
337      /* String in_reply_to = msg.getInReplyTo();
338      MContextItem mci = adhoc.lookUp(in_reply_to);
339 
340      String context = mci.getContext();
341      String content = msg.getContent();
342 
343      if ( context.equals("achieve-do-goals")      ||
344           context.equals("achieve-suspend-goals") ||
345           context.equals("achieve-resume-goals")  ||
346           context.equals("achieve-cancel-goals")  )
347 		 {
348         msgContext.del(in_reply_to);
349         adhoc.del(in_reply_to);
350         return true;
351      }
352      else if ( context.equals("stream-all-goals") )
353 		 {
354         Vector v = GoalSummary.getGoalSummaryList(content);
355         for( int i = 0; i < v.size(); i++ )
356         {
357         	String s = ((GoalSummary)v.elementAt(i)).toString();
358           goalTable.add(s);
359 				}
360         if ( !v.isEmpty() && browseGoalUI != null && browseGoalUI.isShowing() )
361            browseGoalUI.refresh();
362         return true;
363      } */
364      return false;
365    }
366 
367 
368    public void showHelp()
369    {
370      HelpWindow helpWin;
371      Point dispos = getLocation();
372      helpWin = new HelpWindow(this, dispos, "visualiser", "Control Tool");
373      helpWin.setSource(toolbar.helpBtn);
374      helpWin.setSize(new Dimension(getWidth(), 440));
375      helpWin.setLocation(dispos.x, dispos.y+getHeight());
376      helpWin.validate();
377    }
378 
379     /***
380         a main method so the control tool can work by itself 
381         */
382    public static void main( String[] arg )
383 	 {
384      AgentContext ac = new ZeusAgentContext("Test", "visualiser");
385      ControlTool z = new ControlTool(ac, new VisualiserModel(ac));
386    }
387 }