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 java.util.*;
27  import java.awt.*;
28  import java.awt.event.*;
29  import javax.swing.*;
30  import javax.swing.event.*;
31  import javax.swing.table.*;
32  
33  
34  import zeus.util.*;
35  import zeus.concepts.*;
36  import zeus.actors.rtn.Engine;
37  import zeus.ontology.*;
38  import zeus.actors.*;
39  import zeus.gui.editors.*;
40  import zeus.gui.fields.*;
41  
42  
43  public class GoalDialog extends JDialog
44                          implements ItemListener,
45  			           ActionListener,
46                                     FactSelector {
47  
48     final int TOP_PANE_MIN_HEIGHT = 150;
49     final int TOP_PANE_MIN_WIDTH = 400;    //155;
50  
51     public static final String[] FORMAT_TYPE = {
52        "popup",
53        "email",
54        "phone",
55        "voice"
56     };
57  
58     public static final boolean BASIC = true;
59     public static final boolean EXTENDED = false;
60  
61     protected JLabel gLabel, fLabel, aLabel, eLabel, rLabel,
62                     sLabel, cLabel, tLabel, dLabel, pLabel,
63                     nLabel, slaLabel, cmLabel;
64     protected JTextField fTextF, gTextF, dTextF, slaTextF;
65     protected WholeNumberField cTextF, eTextF, cmTextF, sTextF, nTextF, pTextF;
66     protected JButton browseBtn, browseAgentBtn = null;
67     protected JComboBox typeChooser, goalType;
68     protected JList agentDialog = null;
69     protected OntologyDb ontology = null;
70     protected Engine engine = null;
71     protected Fact fact = null;
72     protected String[] agents = null;
73     protected JButton okButton;
74     protected JButton cancelButton;
75  
76     protected int              nFormat = FORMAT_TYPE.length;
77     protected LocalCheckBox[]  _cb     = new LocalCheckBox[nFormat];
78     protected JTextField[]     _cbTf   = new JTextField[nFormat];
79     protected boolean          type    = BASIC;
80  
81     protected Vector sla = new Vector();
82     protected Vector sla_data = new Vector();
83     protected String II_key = null, IC_key = null;
84     protected String WMP_key = null;
85     JScrollPane topPane;
86     JTable table;
87     JPanel contentPane;
88     FactTableModel gdModel;
89  
90     AgentContext context;
91  
92  
93     public GoalDialog(Frame parent, boolean type,
94                   AgentContext context) {
95  
96        super(parent," Enter Goal Dialog",false);
97        this.context = context;
98        set(context.Engine());
99        set(context.OntologyDb());
100       gdModel = new FactTableModel(ontology);
101 
102       contentPane = (JPanel) getContentPane();
103       contentPane.setLayout(new BorderLayout());
104       this.type = type;
105 
106 
107 
108       JPanel p1 = new JPanel();
109       p1.setLayout(new GridLayout(1,2,10,10));
110       okButton = new JButton("Ok");
111       okButton.setForeground(Color.black);
112       okButton.setFont(new  Font("Helvetica", Font.BOLD, 14));
113       cancelButton = new JButton("Cancel");
114       cancelButton.setForeground(Color.black);
115       cancelButton.setFont(new  Font("Helvetica", Font.BOLD, 14));
116       p1.add(okButton);
117       p1.add(cancelButton);
118 
119       GridBagLayout gb = new GridBagLayout();
120       GridBagConstraints gbc = new GridBagConstraints();
121       gbc.insets = new Insets(10,0,10,0);
122       gbc.anchor = GridBagConstraints.NORTHWEST;
123       gbc.fill = GridBagConstraints.HORIZONTAL;
124       gbc.gridwidth = GridBagConstraints.REMAINDER;
125       gbc.weightx = 1;
126 
127       JPanel btnPanel = new JPanel();
128       btnPanel.setLayout(gb);
129 
130       gbc.anchor = GridBagConstraints.CENTER;
131       gbc.fill = GridBagConstraints.NONE;
132       gbc.insets = new Insets(10,10,10,10);
133       gb.setConstraints(p1,gbc);
134       btnPanel.add(p1);
135 
136 
137 
138       JPanel outerBtn = new JPanel(new BorderLayout());
139       outerBtn.add(BorderLayout.NORTH,new JSeparator(SwingConstants.HORIZONTAL));
140       outerBtn.add(BorderLayout.CENTER,btnPanel);
141       contentPane.add(BorderLayout.SOUTH,outerBtn);
142 
143       JPanel p2 = new JPanel();
144       p2.setLayout(gb);
145 
146 
147       gbc.insets = new Insets(10,10,0,0);
148       gbc.anchor = GridBagConstraints.NORTHWEST;
149       gbc.fill = GridBagConstraints.NONE;
150 
151       gLabel = new JLabel("Goal:");
152       gbc.gridwidth = 1;
153       gb.setConstraints(gLabel,gbc);
154       p2.add(gLabel);
155 
156       gTextF = new JTextField(15);
157       gTextF.setEditable(false);
158       gbc.gridwidth = GridBagConstraints.REMAINDER;
159       gbc.weightx = 1;
160       gb.setConstraints(gTextF,gbc);
161       p2.add(gTextF);
162 
163       gbc.weightx = 0;
164 
165 /*
166       tLabel = new JLabel("Type:");
167       gbc.gridwidth = 1;
168       gb.setConstraints(tLabel,gbc);
169       p2.add(tLabel);
170 
171       String[] choiceTypes = {"discrete"} // ,"continuous"};
172       typeChooser = new JComboBox(choiceTypes);
173       gbc.gridwidth = GridBagConstraints.REMAINDER;
174       gb.setConstraints(typeChooser,gbc);
175       p2.add(typeChooser);
176 */
177 
178       fLabel = new JLabel("Fact:");
179       gbc.gridwidth = 1;
180       gb.setConstraints(fLabel,gbc);
181       p2.add(fLabel);
182 
183       String[] goalTypes = {"achieve","sell","buy"}; //, "enact"};
184       goalType = new JComboBox(goalTypes);
185       gb.setConstraints(goalType,gbc);
186       p2.add(goalType);
187 
188       fTextF = new JTextField(15);
189       gbc.gridwidth = 1;
190       gb.setConstraints(fTextF,gbc);
191       p2.add(fTextF);
192       fTextF.setEditable(false);
193 
194       browseBtn = new JButton("Browse...");
195       gbc.anchor = GridBagConstraints.WEST;
196       gbc.gridwidth = GridBagConstraints.REMAINDER;
197       gb.setConstraints(browseBtn,gbc);
198       p2.add(browseBtn);
199 
200       aLabel = new JLabel("Attributes:");
201       gbc.anchor = GridBagConstraints.NORTHWEST;
202       gbc.gridwidth = 1;
203       gb.setConstraints(aLabel,gbc);
204       p2.add(aLabel);
205 
206 
207 //----------------- table
208 
209      TableColumnModel tm = new DefaultTableColumnModel();
210      TableColumn column;
211      column = new TableColumn(FactTableModel.ATTRIBUTE,12);
212      column.setHeaderValue(gdModel.getColumnName(FactTableModel.ATTRIBUTE));
213      tm.addColumn(column);
214      ExpressionCellEditor editor = new ExpressionCellEditor(gdModel);
215      column = new TableColumn(FactTableModel.VALUE,24,
216         new ValidatingCellRenderer(gdModel,FactTableModel.VALUE),editor);
217      column.setHeaderValue(gdModel.getColumnName(FactTableModel.VALUE));
218      tm.addColumn(column);
219 
220      table = new JTable(gdModel,tm);
221      table.getTableHeader().setReorderingAllowed(false);
222      table.setColumnSelectionAllowed(false);
223 
224 
225       topPane = new JScrollPane(table);
226       table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
227       topPane.setPreferredSize(new Dimension(TOP_PANE_MIN_WIDTH,
228                                                                  TOP_PANE_MIN_HEIGHT));
229 
230 
231       gbc.insets = new Insets(10,10,10,10);
232       gbc.gridwidth = GridBagConstraints.REMAINDER;
233       gb.setConstraints(topPane,gbc);
234       p2.add(topPane);
235 
236 /*
237       sLabel = new JLabel("Start Time:");
238       gbc.insets = new Insets(0,10,0,0);
239       gbc.gridwidth = 1;
240       gb.setConstraints(sLabel,gbc);
241       p2.add(sLabel);
242 
243       sTextF = new WholeNumberField();
244       gbc.gridwidth = GridBagConstraints.REMAINDER;
245       gb.setConstraints(sTextF,gbc);
246       p2.add(sTextF);
247 */
248 
249       gbc.insets = new Insets(10,10,0,0);
250       eLabel = new JLabel("End Time:");
251       gbc.gridwidth = 1;
252       gb.setConstraints(eLabel,gbc);
253       p2.add(eLabel);
254 
255       eTextF = new WholeNumberField();
256       eTextF.setPreferredSize(new Dimension(100,20));
257       eTextF.setMinimumSize(new Dimension(100,20));
258       gbc.gridwidth = GridBagConstraints.REMAINDER;
259       gb.setConstraints(eTextF,gbc);
260       p2.add(eTextF);
261 
262       gbc.insets = new Insets(10,10,0,0);
263       cmLabel = new JLabel("Confirm Time:");
264       gbc.gridwidth = 1;
265       gb.setConstraints(cmLabel,gbc);
266       p2.add(cmLabel);
267 
268       cmTextF = new WholeNumberField();
269       cmTextF.setPreferredSize(new Dimension(100,20));
270       cmTextF.setMinimumSize(new Dimension(100,20));
271       gbc.gridwidth = GridBagConstraints.REMAINDER;
272       gb.setConstraints(cmTextF,gbc);
273       p2.add(cmTextF);
274 
275       cLabel = new JLabel("Cost:");
276       gbc.gridwidth = 1;
277       gb.setConstraints(cLabel,gbc);
278       p2.add(cLabel);
279 
280       cTextF = new WholeNumberField();
281       cTextF.setPreferredSize(new Dimension(100,20));
282       cTextF.setMinimumSize(new Dimension(100,20));
283       gbc.gridwidth = GridBagConstraints.REMAINDER;
284       gb.setConstraints(cTextF,gbc);
285       p2.add(cTextF);
286 /*
287       pLabel = new JLabel("Priority:");
288       gbc.gridwidth = 1;
289       gb.setConstraints(pLabel,gbc);
290       p2.add(pLabel);
291 
292       pTextF = new WholeNumberField(Goal.MIN_PRIORITY,Goal.MAX_PRIORITY);
293       gbc.gridwidth = GridBagConstraints.REMAINDER;
294       gb.setConstraints(pTextF,gbc);
295       p2.add(pTextF);
296 
297       nLabel = new JLabel("Invocations:");
298       gbc.gridwidth = 1;
299       gb.setConstraints(nLabel,gbc);
300       p2.add(nLabel);
301 
302       nTextF = new WholeNumberField();
303       gbc.gridwidth = GridBagConstraints.REMAINDER;
304       gb.setConstraints(nTextF,gbc);
305       p2.add(nTextF);
306 
307       if ( type == BASIC ) {
308          dLabel = new JLabel("Desired By:");
309          gbc.gridwidth = 1;
310          gb.setConstraints(dLabel,gbc);
311          p2.add(dLabel);
312 
313          dTextF = new JTextField(15);
314          dTextF.setEditable(false);
315          gbc.gridwidth = GridBagConstraints.REMAINDER;
316          gbc.weighty = 1;
317          gb.setConstraints(dTextF,gbc);
318          p2.add(dTextF);
319       }
320       else {
321          dLabel = new JLabel("Agent:");
322          gbc.gridwidth = 1;
323          gb.setConstraints(dLabel,gbc);
324          p2.add(dLabel);
325 
326          dTextF = new JTextField(15);
327          gb.setConstraints(dTextF,gbc);
328          p2.add(dTextF);
329 
330          browseAgentBtn = new JButton("Browse...");
331          gbc.anchor = GridBagConstraints.WEST;
332          gbc.gridwidth = GridBagConstraints.REMAINDER;
333          gbc.weighty = 1;
334          gb.setConstraints(browseAgentBtn,gbc);
335          p2.add(browseAgentBtn);
336       }
337 
338       slaLabel = new JLabel("SLA Ref:");
339       gbc.anchor = GridBagConstraints.NORTHWEST;
340       gbc.gridwidth = 1;
341       gb.setConstraints(slaLabel,gbc);
342       p2.add(slaLabel);
343 
344       slaTextF = new JTextField(15);
345       slaTextF.setEditable(false);
346       gbc.gridwidth = GridBagConstraints.REMAINDER;
347       gbc.weighty = 1;
348       gb.setConstraints(slaTextF,gbc);
349       p2.add(slaTextF);
350 
351       rLabel = new JLabel("Result Format:");
352       gbc.gridwidth = 1;
353       gb.setConstraints(rLabel,gbc);
354       p2.add(rLabel);
355 
356       JPanel p3 = new JPanel();
357       gbc.gridwidth = GridBagConstraints.REMAINDER;
358       gbc.weighty = 1;
359       gb.setConstraints(p3,gbc);
360       p2.add(p3);
361 
362       p3.setLayout(gb);
363       gbc.insets = new Insets(0,0,2,0);
364       gbc.anchor = GridBagConstraints.WEST;
365       gbc.weightx = gbc.weighty = 1;
366 
367       for( int i = 0; i < nFormat; i++ ) {
368          _cb[i] = new LocalCheckBox(FORMAT_TYPE[i]);
369          _cbTf[i] = new JTextField(10);
370 
371          gbc.gridwidth = 1;
372          gbc.fill = GridBagConstraints.NONE;
373          gb.setConstraints(_cb[i],gbc);
374          p3.add(_cb[i]);
375 
376          gbc.fill = GridBagConstraints.HORIZONTAL;
377          gbc.gridwidth = GridBagConstraints.REMAINDER;
378          gb.setConstraints(_cbTf[i],gbc);
379          p3.add(_cbTf[i]);
380       }
381 */
382       contentPane.add(BorderLayout.CENTER,p2);
383 
384       // set initial state
385 //      typeChooser.addItemListener(this);
386       goalType.addItemListener(this);
387       browseBtn.addActionListener(this);
388       okButton.addActionListener(this);
389       cancelButton.addActionListener(this);
390       this.addWindowListener(
391          new WindowAdapter() {
392             public void windowClosing(WindowEvent evt) { setVisible(false); }
393          }
394       );
395 /*
396       for( int i = 0; i < nFormat; i++ )
397          _cb[i].addItemListener(this);
398 
399       if ( type == EXTENDED )
400          browseAgentBtn.addActionListener(this);
401 */
402       pack();
403       setVisible(true);
404       setResizable(false);
405    }
406 
407    public void set(OntologyDb ontology) {
408       Assert.notNull(ontology);
409       this.ontology = ontology;
410    }
411    public void set(Engine engine) {
412       Assert.notNull(engine);
413       this.engine = engine;
414    }
415    public void set(String[] agents) {
416       Assert.notFalse(type == EXTENDED);
417       this.agents = agents;
418    }
419 //------------------------------------------------------------------------------
420     public void factSelected(String[] name){
421        if ( name == null ) return;
422         gdModel.setFact(name[0]);
423         fact = gdModel.getFact();
424         fTextF.setText(name[0]);
425     }
426 //------------------------------------------------------------------------------
427    public void itemStateChanged(ItemEvent evt) {
428       Object src = evt.getSource();
429 /*
430       if ( src == typeChooser ) {
431          boolean state = typeChooser.getSelectedItem().equals("discrete");
432          sLabel.setEnabled(!state);
433          sTextF.setEnabled(!state);
434          sTextF.setEditable(!state);
435          nLabel.setEnabled(!state);
436          nTextF.setEnabled(!state);
437          nTextF.setEditable(!state);
438 
439          if ( !state ) { // continuous goal
440             slaLabel.setEnabled(false);
441             slaTextF.setEnabled(false);
442             slaTextF.setEditable(false);
443             if ( goalType.getSelectedItem().equals("enact") )
444                goalType.setSelectedIndex(0);
445          }
446          else { // discrete goal
447             boolean state1 = goalType.getSelectedItem().equals("enact");
448             slaLabel.setEnabled(state1);
449             slaTextF.setEnabled(state1);
450             slaTextF.setEditable(state1);
451          }
452       }
453       else if ( src == goalType ) {
454          boolean state1 = goalType.getSelectedItem().equals("enact");
455          slaLabel.setEnabled(state1);
456          slaTextF.setEnabled(state1);
457          slaTextF.setEditable(state1);
458       }
459       else {
460          for( int i = 0; i < nFormat; i++ )
461             if ( src == _cb[i] ) {
462                _cbTf[i].setEnabled(_cb[i].getState());
463                break;
464             }
465       }
466 */
467    }
468 //------------------------------------------------------------------------------
469    public void actionPerformed(ActionEvent evt) {
470       Object src = evt.getSource();
471       if ( src == browseBtn && ontology != null)
472          browseBtnFn();
473 //      else if ( browseAgentBtn != null && src == browseAgentBtn )
474 //         browseAgentBtnFn();
475       if ( src == okButton ) {
476          if ( okBtnFn() ) this.setVisible(false);
477       }
478       else if ( src == cancelButton )
479          this.setVisible(false);
480    }
481 //------------------------------------------------------------------------------
482    protected void browseBtnFn() {
483        AddFactDialog afd = new AddFactDialog(this,gdModel);
484        afd.display(this);
485        fact = gdModel.getFact();
486        topPane.validate();
487    }
488 //------------------------------------------------------------------------------
489    protected void browseAgentBtnFn() {
490       if ( agents != null ) {
491          if ( agentDialog == null )
492 
493          agentDialog = new JList(agents);
494          String text = dTextF.getText();
495          /*
496          if ( text != null && !text.equals("") )
497             agentDialog.setSelection(text);
498          */
499          text = (String) agentDialog.getSelectedValue();
500          if ( text != null && !text.equals("") )
501             dTextF.setText(text);
502       }
503       return;
504    }
505 
506 //------------------------------------------------------------------------------
507    public boolean okBtnFn() {
508       String factName = fTextF.getText();
509       String action = (String) goalType.getSelectedItem();
510 
511       if (factName == null || factName.equals("") ) {
512          JOptionPane.showMessageDialog(null,"Fact Type is undefined","Error",
513                                             JOptionPane.ERROR_MESSAGE);
514          return false;
515       }
516       if ( fact == null ) fact =  ontology.getFact(Fact.VARIABLE,factName);
517 
518 
519       Long confirmTime, endTime, startTime, cost;
520       Long priority, invocations;
521 
522 /*
523       boolean type = ((String)typeChooser.getSelectedItem()).equals("discrete")
524                      ? Goal.DISCRETE : Goal.CONTINUOUS;
525 */
526       boolean type = Goal.DISCRETE;
527 
528       endTime = eTextF.getValue();
529       confirmTime = cmTextF.getValue();
530       cost = cTextF.getValue();
531 /*
532       priority = pTextF.getValue(1);
533       if ( type == Goal.CONTINUOUS ) {
534          invocations = nTextF.getValue(1);
535          startTime = sTextF.getValue(-1);
536       }
537 */
538 
539       if ( confirmTime == null || endTime == null || cost == null )
540 //           startTime == null || priority == null || invocations == null )
541          return false;
542 
543       String goalId = gTextF.getText();
544       String desiredBy = context.whoami();
545 
546 /*
547       String text;
548       int count = 0;
549       String[] format = null;
550       for( int i = 0; i < nFormat; i++ ) {
551          if ( _cb[i].getState() ) {
552             if ( (text = _cbTf[i].getText()) == null || text.equals("") ) {
553                String msg =  "Destination expected in " +
554                              FORMAT_TYPE[i] + " text field";
555                JOptionPane.showMessageDialog(null,msg,"Error",
556                                             JOptionPane.ERROR_MESSAGE);
557                return false;
558             }
559             else
560                count++;
561          }
562       }
563       if ( count != 0 ) {
564          format = new String[count*2];
565          int j = 0;
566          for( int i = 0; i < nFormat; i++ ) {
567             if ( _cb[i].getState() ) {
568                format[j++] = FORMAT_TYPE[i];
569                format[j++] = _cbTf[i].getText();
570             }
571          }
572       }
573 */
574 
575 
576 //      Goal g = (type == Goal.DISCRETE)
577 //         : new Goal(goalId,fact,startTime,endTime,cost,priority,
578 //                    invocations,desiredBy,null,new Time((double)confirmTime));
579 //      g.setTargetMedia(format);
580 
581         Goal g = new Goal(goalId,fact,endTime.intValue(),cost.intValue(),
582 	   Goal.MIN_PRIORITY,desiredBy,null,new Time(confirmTime.doubleValue()));
583 
584       if ( engine == null ) return true;
585       if ( type == Goal.DISCRETE ) {
586          if ( action.equals("achieve") )
587             engine.achieve(g);
588          else if ( action.equals("sell") )
589             engine.sell(g);
590          else if ( action.equals("buy") )
591             engine.buy(g);
592 /*
593          else if ( gtype.equals("enact") && !slaTextF.getText().equals("") )
594             engine.add(g,slaTextF.getText());
595 */
596       }
597       else {
598 /*
599          engine.add(g);
600          String ftype = g.getFact().getType();
601          ftype = ftype + " {" + g.getId() + "}";
602          sla.addElement(ftype);
603          sla_data.addElement(g.getFact());
604 */
605       }
606       return true;
607    }
608 //------------------------------------------------------------------------------
609    public void display(String[] agents) {
610       set(agents);
611       display();
612    }
613 //------------------------------------------------------------------------------
614    public void display() {
615          // select discrete
616 /*
617          typeChooser.setSelectedIndex(0);
618          sLabel.setEnabled(false);
619          sTextF.setEnabled(false);
620          sTextF.setEditable(false);
621          nLabel.setEnabled(false);
622          nTextF.setEnabled(false);
623          nTextF.setEditable(false);
624          slaLabel.setEnabled(false);
625          slaTextF.setEnabled(false);
626          slaTextF.setEditable(false);
627 */
628 
629          //select("achieve");
630          goalType.setSelectedIndex(0);
631 
632          gTextF.setText( context.newId("goal") );
633 
634 /*
635          sTextF.setText("");
636 */
637          eTextF.setText("");
638          cmTextF.setText("");
639 /*
640          if ( type == BASIC )
641             dTextF.setText( context.whoami() );
642          else
643             dTextF.setText("");
644          pTextF.setText( Integer.toString(Goal.DEFAULT_PRIORITY) );
645          nTextF.setText("");
646 */
647          cTextF.setText( Integer.toString(0) );
648          fTextF.setText("");
649 /*
650 	 slaTextF.setText("");
651 
652 
653          for( int i = 0; i < nFormat; i++ ) {
654             _cb[i].setState(false);
655             _cbTf[i].setText("");
656             _cbTf[i].setEnabled(_cb[i].getState());
657          }
658 */
659    }
660 //------------------------------------------------------------------------------
661    class LocalCheckBox extends JCheckBox implements ItemListener {
662              boolean state = false;
663               public LocalCheckBox(String text){
664                 super(text);
665                 addItemListener(this);
666               }
667 
668               public void itemStateChanged(ItemEvent e) {
669 
670                   if (e.getStateChange() == ItemEvent.SELECTED)
671                      state = true;
672                   else
673                     state = false;
674               }
675               //------
676               boolean getState(){
677                 return state;
678               }
679              void setState(boolean b){
680                 state = b;
681                 setSelected(b);
682              }
683           }
684 
685 
686 }