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  /******************************************************************************
25  *
26  *
27  * A pop-up dialog that prompts selection of a fact
28  *****************************************************************************/
29  package zeus.generator.task.rulebase;
30  
31  import javax.swing.*;
32  import javax.swing.tree.*;
33  import javax.swing.border.*;
34  
35  import java.awt.*;
36  import java.awt.event.*;
37  import java.io.*;
38  
39  import zeus.ontology.facts.*;
40  import zeus.ontology.*;
41  import zeus.util.*;
42  import zeus.concepts.*;
43  import zeus.concepts.fn.*;
44  
45  
46  public class FactPanel extends JPanel implements ActionListener {
47    static final String FACT_MARKER = "<-";
48  
49    protected  FactTreeUI treeView;
50    protected  JButton    factIDBtn, factBtn;
51    RuleUI     parent;
52    OntologyDb db;
53  
54    public FactPanel(RuleUI parent, OntologyDb db) {
55      this.parent = parent;
56      this.db = db;
57      setBorder(new EmptyBorder(10,10,10,10));
58      setBackground(Color.lightGray);
59      setLayout(new BorderLayout());
60  
61      treeView = new FactTreeUI(db );
62      JScrollPane treePane = new JScrollPane(treeView);
63      treePane.setPreferredSize(new Dimension(400,150));
64      add(treePane,BorderLayout.CENTER);
65  
66      JPanel controlpane = new JPanel();
67      controlpane.setLayout(new GridLayout(1,2,10,10));
68      add(controlpane,BorderLayout.SOUTH);
69  
70      factBtn = new JButton("Insert Fact");
71      factBtn.addActionListener(this);
72      factIDBtn = new JButton("Insert with ID");
73      factIDBtn.addActionListener(this);
74      controlpane.add(factBtn);
75      controlpane.add(factIDBtn);
76    }
77  //---------------------------------------------------------------------------
78    public void actionPerformed(ActionEvent evt) {
79      String aValue = null;
80      Fact f = null;
81  
82      String name = treeView.getSelectedNodeName();
83      if ( name == null ) return;
84  
85      f = db.getFact(Fact.VARIABLE,name);
86  
87      if ( evt.getSource() == factBtn && f != null) {
88         aValue = getAttributeValues(f);
89      }
90      else if ( evt.getSource() == factIDBtn && f != null ) {
91         aValue = f.getId() + " " + FACT_MARKER + " " + getAttributeValues(f);
92      }
93      if ( aValue != null ) {
94         parent.appendTextTo(aValue);
95  //       System.out.println(aValue);
96     }
97  
98    }
99  //---------------------------------------------------------------------------
100    public String getAttributeValues(Fact f){
101       String[] a = f.listAttributes();
102       ValueFunction[] v = f.listValues();
103       String attValues = "(" + f.getType();
104 
105       for(int i = 0; i < a.length; i++ )
106           attValues += " (" + a[i] + " " + v[i] + ")";
107 
108       return attValues + ")";
109    }
110 //---------------------------------------------------------------------------
111 }