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.table.*;
27  import java.util.*;
28  import zeus.concepts.*;
29  import zeus.concepts.fn.*;
30  import zeus.actors.*;
31  import zeus.gui.editors.*;
32  import zeus.util.Misc;
33  
34  public class FactTableModel  extends AbstractTableModel implements
35                                                              ValidatingModel{
36        static final int ATTRIBUTE = 0;
37        static final int VALUE = 1;
38  
39        protected String[]  columnNames = { "Attribute", "Value" };
40        private Fact   fact ;
41        OntologyDb ontologyDb;
42  
43  //------------------------------------------------------------------------------
44        public FactTableModel(OntologyDb ontology){
45            ontologyDb = ontology;
46            this.fact = null;
47  
48        }
49  //------------------------------------------------------------------------------
50         public int getRowCount() {
51            if (fact == null)
52             return 0;
53            else
54             return fact.listAttributes().length;
55         }
56  //------------------------------------------------------------------------------
57         public int getColumnCount(){
58             return columnNames.length;
59         }
60  //------------------------------------------------------------------------------
61         public boolean isCellEditable(int row, int col) {
62            return  (col == VALUE);
63         }
64  
65  
66  //--------------------------------------------------------------------------
67    public void setValueAt(Object aValue, int row, int column)   {
68  
69       String fValue = (fact.listValues())[row].toString();
70       String fAttr =  (fact.listAttributes())[row];
71  
72  
73       String value = (aValue == null) ? null : ((String)aValue).trim();
74       if ( value.equals("") ) value = null;
75  
76       if ( value == null ) {
77          if ( fValue == null )
78             return;
79          else {
80             fValue = value;
81             fact.setValue(fAttr,fact.newVar());
82          }
83       }
84       else {
85          // always use this keyword for ?this reference!
86          value = Misc.substitute(value,fact.getId(),Fact.THIS);
87          if (fValue != null && fValue.equals(value) )
88             return;
89          else {
90            fValue = value;
91             ValueFunction fn = ZeusParser.Expression(fValue);
92             if ( fn != null )
93                fact.setValue(fAttr,fn);
94          }
95       }
96       fireTableCellUpdated(row,column);
97    }
98  
99  
100 
101 //--------------------------------------------------------------------------
102       public boolean isValidEntry(int row, int column) {
103           switch(column) {
104             case VALUE:
105              if ( (fact.listValues())[row] == null )
106                 return true;
107              else if ( ((fact.listValues())[row]).equals("") )
108                 return true;
109              else
110                 return ZeusParser.Expression((fact.listValues())[row].toString()) != null;
111            case ATTRIBUTE:
112              return true;
113           }
114           return false; // sh never get here
115       }
116 
117 //------------------------------------------------------------------------------
118        public Object getValueAt(int row, int col) {
119 
120              if (col == ATTRIBUTE)
121               return (fact.listAttributes())[row];
122              else if (col == VALUE )
123               return (fact.listValues())[row].toString();
124              else
125               return  new String("Error in AttributesTableModel at getValueAt");
126        }
127 //------------------------------------------------------------------------------
128        public String getColumnName(int col) {
129             return  columnNames[col];
130        }
131 //------------------------------------------------------------------------------
132        public void setFact(String factName){
133               if ( factName == null ) return;
134               fact = ontologyDb.getFact(Fact.VARIABLE,factName);
135               System.out.println(fact);
136               fireTableDataChanged();
137        }
138 //------------------------------------------------------------------------------
139        public Fact getFact(){
140            return fact;
141        }
142 //------------------------------------------------------------------------------
143 
144 }