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  * AbilityModel.java
26  *
27  * The underlying model for the Ability Table 
28  *****************************************************************************/
29  
30  package zeus.generator.agent;
31  
32  import java.util.*;
33  import javax.swing.table.*;
34  import javax.swing.event.*;
35  
36  import zeus.util.*;
37  import zeus.concepts.*;
38  import zeus.generator.*;
39  import zeus.generator.util.*;
40  
41  public class AbilityModel extends AbstractTableModel
42                         implements ChangeListener {
43  
44    static final int TYPE    = 0;
45    static final int TIME    = 1;
46    static final int COST    = 2;
47    static final int ABILITY = 3;
48  
49    static int count  = 0;
50  
51    protected String[]        columnNames = { "Ability Type", "Time", "Cost" };  
52    protected int             selectedRow = -1;
53    protected AttributeModel  attributeModel;
54    protected OntologyDb      ontologyDb;  
55    protected Vector          data = new Vector(100);
56    protected Acquaintance    acquaintance = null;
57  
58  	
59    public AbilityModel(OntologyDb ontologyDb,
60                        AttributeModel attributeModel) {
61  
62       this.ontologyDb = ontologyDb;
63       this.attributeModel = attributeModel;
64       ontologyDb.addChangeListener(this);
65    }
66  
67    public void reset(Acquaintance input) {
68       if ( acquaintance != null )
69          acquaintance.setAbilities(data);
70  
71       int size = data.size();
72  
73       this.acquaintance = input;
74       data.removeAllElements();
75       selectRow(-1);
76  
77       if ( size != 0 )
78          fireTableRowsDeleted(0,size-1);
79  
80       if ( input != null ) {
81          AbilitySpec[] in = input.getAbilities();
82          for(int i = 0; i < in.length; i++ ) 
83             data.addElement(in[i]);
84          fireTableRowsInserted(0,in.length-1);
85       }
86    }
87    
88    public void removeRows(int[] rows) {
89       for(int i = 0; i < rows.length; i++ ) {
90          data.removeElementAt(rows[i]-i);
91          fireTableRowsDeleted(rows[i]-i,rows[i]-i);
92       }
93       selectRow(-1);
94       fireChanged();
95    }
96  
97    public void selectRow(int row) {
98       selectedRow = row;
99       if ( attributeModel != null ) {
100         if ( selectedRow >= 0 ) {
101            AbilitySpec a = (AbilitySpec)data.elementAt(selectedRow);
102            attributeModel.reset(a.getFact());
103         }
104         else
105            attributeModel.reset(null);
106      }
107   }
108 
109   public void addNewRows(String[] names)  {
110      if ( names == null || names.length == 0 ) return;
111      Fact f;
112      AbilitySpec[] input = new AbilitySpec[names.length];
113      for(int i = 0; i < names.length; i++ ) {
114         f = ontologyDb.getFact(Fact.VARIABLE,names[i]);
115         input[i] = new AbilitySpec(f,0,0);
116      }
117      addRows(input);
118   }
119 
120   public void addRows(AbilitySpec[] input) {
121      if ( input == null     ) return;
122      if ( input.length == 0 ) return;
123 
124      AbilitySpec a1;
125      String id;
126      int size = data.size();
127      for(int i = 0; i < input.length; i++ ) {
128         a1 = new AbilitySpec(input[i]);
129         id = a1.getFact().ID();
130         while( contains(id) )
131            id += (count++);
132         a1.getFact().setId(id);
133         data.addElement(a1);
134      }
135      selectRow(-1);
136      fireTableRowsInserted(size-1,size+input.length-1);
137      fireChanged();
138   }
139   protected boolean contains(String id) {
140     // check that model does not already contain task
141     AbilitySpec a;
142     for(int i = 0; i < data.size(); i++ ) {
143        a = (AbilitySpec)data.elementAt(i);
144        if ( id.equals(a.getFact().ID()) )
145           return true;
146     }
147     return false;
148   }
149 
150   
151   // ----------------------------------------------------------------------
152 
153   public int     getColumnCount()                 { return columnNames.length; }
154   public int     getRowCount()                    { return data.size(); }
155   public boolean isCellEditable(int row, int col) { return col != TYPE; }
156   public String  getColumnName(int col)           { return columnNames[col]; }
157 
158   public Object getValueAt(int row, int column)  {
159      AbilitySpec a = (AbilitySpec)data.elementAt(row);
160      switch(column) {
161         case TYPE:
162              return a.getType();
163         case COST:
164              return new Double(a.getCost());
165         case TIME:
166              return new Integer(a.getTime());
167         case ABILITY:
168              return a;
169      }
170      return null;
171   }
172 
173   public void setValueAt(Object aValue, int row, int column)  {
174     // prevents the table being accidently loaded with a null value
175     // current table implementation needs this - possibly because of a bug 
176     if (aValue.toString().equals(""))
177        return;
178 
179      AbilitySpec a = (AbilitySpec)data.elementAt(row);
180      switch(column) {
181         case TYPE:
182              Core.ERROR(null,1,this);
183              break;
184         case TIME:
185              int time = Integer.parseInt((String)aValue);
186              if ( time == a.getTime() ) return;
187              a.setTime(time);
188              fireTableCellUpdated(row,column);
189              fireChanged();
190              break;
191         case COST:
192              double cost = (Double.valueOf((String)aValue)).doubleValue();
193              if ( Math.abs(cost - a.getCost()) < 1.0e-12 ) return;
194              a.setCost(cost);
195              fireTableCellUpdated(row,column);
196              fireChanged();
197              break;
198      }
199   }
200   
201   public void stateChanged(ChangeEvent e) {
202      // Underlying ontology has changed!!
203      // NEED to verify all facts!!
204   }
205 
206   protected EventListenerList  changeListeners = new EventListenerList();
207   public void addChangeListener(ChangeListener x) {
208      changeListeners.add(ChangeListener.class, x);
209   }
210   public void removeChangeListener(ChangeListener x) {
211      changeListeners.remove(ChangeListener.class, x);
212   }
213   
214   protected void fireChanged() {
215      ChangeEvent c = new ChangeEvent(this);
216      Object[] listeners = changeListeners.getListenerList();
217      for(int i= listeners.length-2; i >= 0; i -=2) {
218         if (listeners[i] == ChangeListener.class) {
219            ChangeListener cl = (ChangeListener)listeners[i+1];
220            cl.stateChanged(c);
221         }
222      }
223   }
224 }