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.agentviewer;
25  
26  import javax.swing.*;
27  import javax.swing.border.*;
28  import java.awt.*;
29  import java.awt.event.*;
30  
31  
32  import zeus.util.*;
33  import zeus.concepts.*;
34  import zeus.ontology.OntologyEditor;
35  import zeus.gui.help.*;
36  import zeus.agents.ZeusAgentUI;
37  import zeus.agents.ZeusAgent;
38  import zeus.actors.*;
39  import zeus.agentviewer.mail.*;
40  import zeus.agentviewer.msghandler.*;
41  import zeus.agentviewer.resources.*;
42  import zeus.agentviewer.acquaintances.*;
43  import zeus.agentviewer.task.*;
44  import zeus.agentviewer.plansch.*;
45  import zeus.agentviewer.engine.*;
46  import zeus.agentviewer.protocol.*;
47  import zeus.agentviewer.rete.*;
48  
49  /*** 
50      this is the main class for the viewer that lets you inspect the internals of Zeus 
51      Agents. 
52      
53      Change log
54      ----------
55      12/04/01 - altered to allow toolbars to detach and set standard l&f to metal 
56                 shifted tool bar to top/north of frame 
57      */
58  public class AgentViewer extends JFrame 
59                           implements ActionListener,
60                                      ComponentListener,
61                                      ZeusAgentUI
62                                      {
63  
64     JPanel contentPane;
65     public JPanel topPanel;
66     InternalFramesPanel bottomPanel;
67     String fsep = System.getProperty("file.separator");
68     String IMAGEPATH = SystemProps.getProperty("gif.dir") + "agentviewer" + fsep;
69     final int ICONh  = 40;
70     final int ICONw  = 40;
71     final int HEADERw = 200;
72     final int HEADERh = 43;
73     final int WIDTH = 600;
74     final int HEIGHT = 400;
75     int startWidth = 600;
76     int startHeight = 300; 
77  
78  
79      private JMenuBar           menuBar;
80      private JMenu              fileMenu, helpMenu, goalMenu;
81      private JMenu              viewMenu, dbMenu;
82      private JMenuItem          goal,options,exit;
83      private JMenuItem          about, aboutZeus;
84      private JMenuItem          tile, cascade;
85      private JMenuItem          newTask,newResource,newAcquaintance;
86      private JMenuItem          nativeLF, metalLF;
87      private JToolBar           toolBar;
88  
89      /*** 
90  	changed these to protected to allow subclassing of the viewer for 
91  	specialist agent architectures 
92      */
93      protected AgentButton mailInBtn, mailOutBtn, msgHandlerBtn,
94                          reteEngineBtn, coordEngineBtn, acqDbBtn,
95                          planSchBtn,taskPlanDbBtn,
96                          resDbBtn, ontDbBtn,protocolBtn;
97  
98      AgentContext context = null;
99      Thread agentViewerThread;
100     MailInTableModel mailInBuffer;
101     MailOutTableModel mailOutBuffer;
102     MsgHandlerTableModel msgHandlerBuffer;
103     ResourceTableModel resourceBuffer;
104     RelationsTableModel relationsBuffer;
105     AbilitiesTableModel abilitiesBuffer;
106     AttributesTableModel attributesBuffer;
107 // Task
108     TaskTableModel taskBuffer;
109     TaskConditionsTableModel preCondBuffer;
110     ConditionsAttributeTableModel preAttrBuffer;
111     TaskConditionsTableModel effectBuffer;
112     ConditionsAttributeTableModel effectsAttrBuffer;
113     ConstraintsModel constraintsBuffer;
114     OrderingModel orderingBuffer;
115 
116 // Planner
117     PlanSchModel planSchBuffer;
118     EngineTableModel engineBuffer;
119     ReteEngineDataModel reteEngineBuffer;
120     ProtocolModel protocolBuffer;
121     StrategyModel strategyBuffer;
122     AttributesModel pAttributesBuffer;
123 
124 //-------------------------------------------------------------------------
125      public AgentViewer()
126       {
127           super("Agent Viewer");
128           //getNativeUI();
129           getMetalUI(); 
130           ImageIcon icon = new ImageIcon(IMAGEPATH + ViewerNames.VIEWER_IMG);
131           setIconImage(icon.getImage());
132           createMenus();
133           setJMenuBar(menuBar);
134           setContentPane();
135 
136           addWindowListener(
137            new WindowAdapter() {
138               public void windowClosing(WindowEvent evt) { 
139                 exitBtnFn(); 
140                 System.exit(0);  }
141              }
142           );
143 
144           addComponentListener(this);
145           setSize (new Dimension(startWidth, startHeight));
146           setVisible(true);
147       //    pack();
148 
149       }
150 
151 
152      void setMailInBox() {
153        mailInBuffer = new MailInTableModel(context);
154      }
155 
156 
157      void setMailOutBox() {
158        mailOutBuffer = new MailOutTableModel(context);
159      }
160 
161 
162      void setMsgHandlerBox() {
163        msgHandlerBuffer = new MsgHandlerTableModel(context);
164      }
165 
166 
167     void setResourceBuffer() {
168       resourceBuffer = new ResourceTableModel(context);
169     }
170 
171 
172     void setAcquaintanceBuffers() {
173       relationsBuffer = new RelationsTableModel(context);
174       abilitiesBuffer = new AbilitiesTableModel(context);
175       attributesBuffer = new AttributesTableModel(abilitiesBuffer);
176     }
177 
178 
179     void setTaskBuffers() {
180       taskBuffer = new TaskTableModel(context);
181       preCondBuffer = new TaskConditionsTableModel() ;
182       preAttrBuffer = new ConditionsAttributeTableModel();
183       effectBuffer = new TaskConditionsTableModel() ;
184       effectsAttrBuffer = new ConditionsAttributeTableModel();
185       constraintsBuffer = new ConstraintsModel();
186       orderingBuffer = new OrderingModel() ;
187 
188     }
189 
190 
191     void setPlanSchBuffer() {
192        planSchBuffer = new PlanSchModel(context);
193     }
194 
195 
196     void setEngineBuffer() {
197        engineBuffer = new EngineTableModel(context);
198     }
199 
200 
201     void setReteEngineBuffer() {
202        reteEngineBuffer = new ReteEngineDataModel(context);
203     }
204 
205 
206     void setProtocolBuffers() {
207        protocolBuffer = new ProtocolModel(context);
208        strategyBuffer = new StrategyModel();
209        pAttributesBuffer = new AttributesModel();
210     }
211 
212 
213     public void showMsg(String message) {
214     }
215 
216 
217     public void set(AgentContext context) 
218     {
219         Assert.notNull(context);
220 
221         this.context = context;
222         this.setVisible(true);
223         this.setTitle(context.whoami());
224 
225         setMailInBox();
226         setMailOutBox();
227         setMsgHandlerBox();
228         setResourceBuffer();
229         setAcquaintanceBuffers();
230         setEngineBuffer();
231         setProtocolBuffers();
232 
233         if (context.Planner() == null)
234         {
235           planSchBtn.setEnabled(false);
236           taskPlanDbBtn.setEnabled(false);
237         }
238         else
239         {
240           setTaskBuffers();
241           setPlanSchBuffer();
242         }
243 
244         if (context.ReteEngine() == null)
245           reteEngineBtn.setEnabled(false);
246         else
247           setReteEngineBuffer();
248     }
249 
250 
251     private void getNativeUI() {
252        String pathsep = System.getProperty("path.separator");
253        try {
254            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
255            SwingUtilities.updateComponentTreeUI(this);
256        }
257        catch (Exception e) {
258         System.out.println("Error getting UI"); 
259        }
260     }
261     
262     
263     private void getMetalUI() { 
264            try {
265                System.out.println("Metal look & feel chosen");
266                UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
267                SwingUtilities.updateComponentTreeUI(this);
268             }
269             catch (Exception exc) {
270                System.out.println("Error getting UI");
271             }
272     }
273 
274 
275     public void showHelp() {
276       HelpWindow helpWin;
277       Point dispos = getLocation();
278       helpWin = new HelpWindow(this, dispos, "visualiser", "Agent Viewer");
279       helpWin.setSize(new Dimension(600, getHeight()));
280       helpWin.setLocation(dispos.x+24, dispos.y+24);
281       helpWin.validate();
282       helpWin.setSource(about);
283     }
284 
285 
286     public void About() {
287       HelpWindow helpWin;
288       Point dispos = getLocation();
289       helpWin = new HelpWindow(this, dispos, "visualiser", "About");
290       helpWin.setSize(new Dimension(440, 440));
291       helpWin.setLocation(dispos.x+24, dispos.y+24);
292       helpWin.setTitle("About ZEUS ...");
293       helpWin.validate();
294       helpWin.setSource(aboutZeus);
295     }
296 
297 
298      public void componentResized(ComponentEvent e) {
299 
300         if (bottomPanel.tileOn())
301           bottomPanel.tile();
302         else
303           bottomPanel.cascade();
304 
305      }
306 
307 
308       public void componentMoved(ComponentEvent e) {
309        // System.out.println("component moved" + e.toString()); 
310         
311         }
312 
313 
314       public void componentShown(ComponentEvent e) { }
315 
316 
317       public void componentHidden(ComponentEvent e) { }
318 
319 
320       private void setContentPane() {
321           bottomPanel = new InternalFramesPanel(this);
322 
323           toolBar = new JToolBar(SwingConstants.HORIZONTAL);
324           JPanel leftPanel = new JPanel(new GridLayout(1,1));
325           leftPanel.add(toolBar);
326 	  //  leftPanel.setBorder(BorderFactory.createEtchedBorder(Color.red,Color.black));
327 	  
328           addButtonsToToolBar();
329 
330           contentPane = (JPanel) getContentPane();
331           contentPane.setLayout(new BorderLayout());
332           contentPane.add(BorderLayout.NORTH,toolBar);
333           contentPane.add(BorderLayout.CENTER,bottomPanel);
334           
335       }
336 
337 
338       private void createMenus() {
339           menuBar = new JMenuBar();
340 
341           fileMenu = new JMenu("File");
342           options = new JMenuItem("Control Options");
343           options.addActionListener(this);
344           fileMenu.add(options);
345 
346           exit = new JMenuItem("Exit");
347           exit.addActionListener(this);
348           fileMenu.add(exit);
349           menuBar.add(fileMenu);
350 
351           goalMenu = new JMenu("Goal");
352           goal = new JMenuItem("New Goal");
353           goalMenu.add(goal);
354           goal.addActionListener(this);
355           menuBar.add(goalMenu);
356 
357 
358           dbMenu = new JMenu("Databases");
359           newAcquaintance = new JMenuItem("Add Acquaintance");
360           dbMenu.add(newAcquaintance);
361           newAcquaintance.addActionListener(this);
362 
363           newResource = new JMenuItem("Add Resource");
364           dbMenu.add(newResource);
365           newResource.addActionListener(this);
366 
367           newTask = new JMenuItem("Add Task");
368           dbMenu.add(newTask);
369           newTask.addActionListener(this);
370           menuBar.add(dbMenu);
371 
372           viewMenu = new JMenu("View");
373           tile = new JMenuItem("Tile");
374           tile.addActionListener(this);
375           viewMenu.add(tile);
376           cascade = new JMenuItem("Cascade");
377           cascade.addActionListener(this);
378           viewMenu.add(cascade);
379           viewMenu.addSeparator();
380           nativeLF = new JMenuItem("Native Look & Feel");
381           nativeLF.addActionListener(this);
382           viewMenu.add(nativeLF);
383           metalLF = new JMenuItem("Metal Look & Feel");
384           metalLF.addActionListener(this);
385           viewMenu.add(metalLF);
386           menuBar.add(viewMenu);
387 
388           menuBar.add(Box.createHorizontalGlue());
389 
390           helpMenu = new JMenu("Help");
391           about = new JMenuItem("Using the Agent Viewer");
392           about.addActionListener(this);
393           helpMenu.add(about);
394           aboutZeus = new JMenuItem("About Zeus");
395           aboutZeus.addActionListener(this);
396           helpMenu.add(aboutZeus);
397           menuBar.add(helpMenu);
398       }
399 
400 
401       public void actionPerformed(ActionEvent evt)
402       {
403         Object source = evt.getSource();
404 
405         // --- look and feel
406         if ( source == nativeLF ) {
407           getNativeUI();
408           return;
409         }
410         else if ( source == metalLF ) {
411             try {
412                System.out.println("Metal look & feel chosen");
413                UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
414                SwingUtilities.updateComponentTreeUI(this);
415             }
416             catch (Exception exc) {
417                System.out.println("Error getting UI");
418             }
419            return;
420          }
421          else if (source == about)
422            showHelp();
423          else if (source == aboutZeus)
424            About();
425 
426          // others
427          if (context != null) {
428             if ( source == exit ) exitBtnFn();
429             else if ( source == tile      )
430          bottomPanel.tile();
431             else if ( source == cascade   )
432          bottomPanel.cascade();
433             else if ( source == options   )
434                new ControlOptionsDialog(this,context);
435             else if ( source == goal   ) {
436              GoalDialog gd = new GoalDialog(this,GoalDialog.BASIC,context);
437              gd.display();
438             }
439             else if ( source == newAcquaintance) {
440                     System.out.println("not yet implemented");
441             }
442             else if ( source == newResource) {
443                    System.out.println("not yet implemented");
444                    // new AddFactDialog (
445 
446             }
447             else if ( source == newTask) {
448                     System.out.println("not yet implemented"); 
449             }
450             else if (source == mailInBtn) {
451               new MailInTableUI(bottomPanel,mailInBuffer);
452             }
453             else if (source == mailOutBtn) {
454                new MailOutTableUI(bottomPanel,mailOutBuffer);
455             }
456             else if (source == msgHandlerBtn) {
457                 new MsgHandlerTableUI(bottomPanel,msgHandlerBuffer);
458             }
459             else if (source == coordEngineBtn) {
460                 new EngineUI(bottomPanel,engineBuffer);
461             }
462             else if (source == reteEngineBtn) {
463                 new ReteEngineUI(bottomPanel,reteEngineBuffer);
464             }
465             else if (source == acqDbBtn) {
466                 new AcquaintanceUI(bottomPanel,relationsBuffer, abilitiesBuffer,
467                                    attributesBuffer);
468             }
469             else if (source == planSchBtn) {
470                 new PlanSchTableUI(bottomPanel,planSchBuffer);
471             }
472             else if (source == taskPlanDbBtn) {
473                 new TaskTableUI(bottomPanel,taskBuffer,preCondBuffer,
474                                 preAttrBuffer,effectBuffer,effectsAttrBuffer,
475                                 constraintsBuffer,orderingBuffer);
476             }
477             else if (source == resDbBtn) {
478                 new ResourceTableUI(bottomPanel,resourceBuffer);
479             }
480             else if (source == ontDbBtn) {
481                 OntologyEditor editor = new OntologyEditor(context.OntologyDb(),true);
482                // JInternalFrame editorPanel = editor.createInternalFrame(); 
483              //   bottomPanel.addInternalFrame(editorPanel);
484               //  editorPanel.setVisible(true); 
485                 
486                 }
487             else if (source == protocolBtn) {
488                 new ProtocolUI(bottomPanel,protocolBuffer,strategyBuffer,
489                                pAttributesBuffer);
490             }
491             else
492             {
493               System.out.println("Unrecognised event source");
494             }
495          }
496          else {
497            JOptionPane.showMessageDialog(this,"No associated agent","Error Message",
498                                          JOptionPane.OK_OPTION);
499          }
500       }
501 
502 
503       private void exitBtnFn() {
504          if (context != null) {
505             mailInBuffer.removeZeusEventMonitors();
506             mailOutBuffer.removeZeusEventMonitors();
507             msgHandlerBuffer.removeZeusEventMonitors();
508             abilitiesBuffer.removeZeusEventMonitors();
509             relationsBuffer.removeZeusEventMonitors();
510             resourceBuffer.removeZeusEventMonitors();
511             engineBuffer.removeZeusEventMonitors();
512             protocolBuffer.removeZeusEventMonitors();
513             if (context.Planner() != null)
514             {
515               taskBuffer.removeZeusEventMonitors();
516               planSchBuffer.removeZeusEventMonitors();
517             }
518          }
519          MailBox mbox = context.getMailBox(); 
520          mbox.shutdown(); 
521          try { 
522             Thread.sleep(25); 
523          } catch(Exception e) { 
524             ;
525          }
526          System.exit(0);
527       }
528 
529 
530 
531      Icon getIcon(String imgFile, int w, int h) {
532        String  imgStr = new String(IMAGEPATH + imgFile);
533        Image aImg = Toolkit.getDefaultToolkit().getImage(imgStr);
534        aImg = aImg.getScaledInstance(w,h,Image.SCALE_SMOOTH);
535        Icon aIcon = new ImageIcon(aImg);
536        return aIcon;
537      }
538 
539 
540       private TitledBorder makeBorder(String title) {
541           TitledBorder border = (BorderFactory.createTitledBorder(title));
542           border.setTitlePosition(TitledBorder.TOP);
543 	  border.setTitleJustification(TitledBorder.RIGHT);
544 	  border.setTitleFont(new Font("Helvetica", Font.BOLD, 14));
545 	  border.setTitleColor(Color.black);
546 
547           return border;
548      }
549 
550 
551     private void addButtonsToToolBar()  
552     {
553      // JLabel header = new JLabel(getIcon(ViewerNames.HEADER_IMG, HEADERw, HEADERh));
554       toolBar.setFloatable(false); 
555       // toolBar.add(header);
556       toolBar.setAutoscrolls(true); 
557 
558       JToolBar topBar = new JToolBar(SwingConstants.HORIZONTAL);
559       //   topBar.setBorder(makeBorder("Processors"));
560       topBar.setFloatable(true);
561       toolBar.add(topBar);
562 
563       mailInBtn = new AgentButton(ViewerNames.MAIL_IN,
564                                   getIcon(ViewerNames.MAILIN_IMG,ICONw,ICONh));
565       mailInBtn.addActionListener(this);
566       topBar.add(mailInBtn);
567 
568       mailOutBtn = new AgentButton(ViewerNames.MAIL_OUT,
569                                    getIcon(ViewerNames.MAILOUT_IMG,ICONw,ICONh));
570       mailOutBtn.addActionListener(this);
571       topBar.add(mailOutBtn);
572 
573       msgHandlerBtn = new AgentButton(ViewerNames.MESSAGE_HANDLER,
574                                       getIcon(ViewerNames.MSGHANDLER_IMG,ICONw,ICONh)) ;
575       msgHandlerBtn.addActionListener(this);
576       topBar.add(msgHandlerBtn);
577 
578       reteEngineBtn = new AgentButton(ViewerNames.RETE_ENGINE,
579                                        getIcon(ViewerNames.RETE_ENG_IMG,ICONw,ICONh));
580       reteEngineBtn.addActionListener(this);
581       topBar.add(reteEngineBtn);
582 
583       coordEngineBtn = new AgentButton(ViewerNames.COORDINATION_ENGINE,
584                                        getIcon(ViewerNames.COORDENG_IMG,ICONw,ICONh));
585       coordEngineBtn.addActionListener(this);
586       topBar.add(coordEngineBtn);
587 
588       planSchBtn = new AgentButton(ViewerNames.PLANNER_SCHEDULER,
589                                    getIcon(ViewerNames.PLANSCH_IMG,ICONw,ICONh));
590       planSchBtn.addActionListener(this);
591       topBar.add(planSchBtn);
592 
593       JToolBar bottomBar = new JToolBar(SwingConstants.HORIZONTAL);
594       // bottomBar.setBorder(makeBorder("Databases"));
595       toolBar.add(bottomBar);
596 
597       acqDbBtn = new AgentButton(ViewerNames.ACQUAINTANCE_DATABASE,
598                                  getIcon(ViewerNames.ACQDB_IMG,ICONw,ICONh));
599       acqDbBtn.addActionListener(this);
600       bottomBar.add(acqDbBtn);
601 
602       ontDbBtn =  new AgentButton(ViewerNames.ONTOLOGY_DATABASE,
603                                   getIcon(ViewerNames.ONTDB_IMG,ICONw,ICONh));
604       ontDbBtn.addActionListener(this);
605       bottomBar.add(ontDbBtn);
606 
607 
608       protocolBtn = new AgentButton(ViewerNames.PROTOCOL,
609                                        getIcon(ViewerNames.PROTOCOL_IMG,ICONw,ICONh));
610       protocolBtn.addActionListener(this);
611       bottomBar.add(protocolBtn);
612 
613 
614       resDbBtn = new AgentButton(ViewerNames.RESOURCE_DATABASE,
615                                  getIcon(ViewerNames.RESDB_IMG,ICONw,ICONh));
616       resDbBtn.addActionListener(this);
617       bottomBar.add(resDbBtn);
618 
619       taskPlanDbBtn = new AgentButton(ViewerNames.TASK_PLAN_DATABASE,
620                                       getIcon(ViewerNames.TASKPLAN_IMG,ICONw,ICONh));
621       taskPlanDbBtn.addActionListener(this);
622       bottomBar.add(taskPlanDbBtn);
623 
624     }
625 //-------------------------------------------------------------------------
626 }