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.acquaintances;
25  
26  import javax.swing.table.*;
27  import javax.swing.*;
28  import java.util.*;
29  import zeus.concepts.AbilitySpec;
30  import zeus.concepts.Fact;
31  import zeus.util.OrderedHashtable;
32  import zeus.actors.*;
33  import zeus.actors.event.*;
34  import zeus.concepts.*;
35  import zeus.ontology.*;
36  
37  
38  
39  public class AbilitiesTableModel  extends AbstractTableModel 
40                                    implements AbilityMonitor{
41  
42        private static final int FACT     = 0;
43        private static final int COST     = 1;
44        private static final int DURATION = 2;
45  
46        private String[] header = {"Fact","Cost","Duration"};
47        private Vector   data ;
48        private String agent;
49  
50        private OrderedHashtable allAbilities;
51        OrganisationDb organisationDb;
52        OntologyDb ontologyDb;
53  //------------------------------------------------------------------------------
54        public AbilitiesTableModel(AgentContext context){
55            allAbilities = new OrderedHashtable();
56            organisationDb = context.OrganisationDb();
57            ontologyDb = context.OntologyDb();
58            data = null;
59            agent = null;
60            organisationDb.addAbilityMonitor(this,
61               AbilityEvent.ADD_MASK | AbilityEvent.DELETE_MASK |
62               AbilityEvent.MODIFY_MASK,true );
63        }
64  
65  //------------------------------------------------------------------------------
66         public int getRowCount() {
67  
68               return (data == null )? 0:data.size();
69         }
70  //------------------------------------------------------------------------------
71         public int getColumnCount(){
72             return header.length;
73         }
74  //------------------------------------------------------------------------------
75         public boolean isCellEditable(int row, int col) {
76            return false;
77         }
78  //------------------------------------------------------------------------------
79         public Object getValueAt(int row, int col) {
80  
81             AbilitySpec des = (AbilitySpec) data.elementAt(row);
82              switch(col) {
83                  case FACT:
84                       return  des.getType();
85                  case COST:
86                       return new Double(des.getCost() ) ;
87                  case DURATION:
88                       return new Integer(des.getTime() );
89              }
90              return null;
91         }
92  //------------------------------------------------------------------------------
93         public String getColumnName(int col) { return  header[col]; }
94  //------------------------------------------------------------------------------
95         boolean validateInput(String str){
96             try {
97                Integer.parseInt(str.trim());
98             }
99             catch (NumberFormatException e) {
100               JOptionPane.showMessageDialog(null,"Invalid Input","Enter values",
101                                             JOptionPane.ERROR_MESSAGE);
102               return false;
103            }
104            return true;
105        }
106 //------------------------------------------------------------------------------
107 
108         public Fact  getAttributesof(int row){
109           return ((AbilitySpec)data.elementAt(row)).getFact();
110        }
111 //------------------------------------------------------------------------------
112        void makeFact(String name){
113            Fact f = ontologyDb.getFact(Fact.VARIABLE,name);
114            AbilitySpec a = new AbilitySpec(f,0,0);
115            organisationDb.add(agent,a);
116            System.out.println("make fact:agent  "+ agent);
117        }
118 //------------------------------------------------------------------------------
119        public void addAbility(String agent, AbilitySpec ability) {
120           if (allAbilities.containsKey(agent)) {
121             Vector abilities = (Vector) allAbilities.get(agent);
122             if (abilities.contains(ability)) return;
123              abilities.addElement(ability);
124           }
125           else {
126             Vector abilities = new Vector();
127             abilities.addElement(ability);
128             allAbilities.put(agent,abilities);
129           }
130           fireTableDataChanged();
131        }
132 
133 //------------------------------------------------------------------------------
134        public void removeAbility(String agent, AbilitySpec ability) {
135           //System.out.println("removing ability: " + ability);
136           if (allAbilities.containsKey(agent)) {
137             Vector abilities = (Vector) allAbilities.get(agent);
138 
139             /*
140             System.out.println("all:  " + abilities);
141             abilities.removeElement(ability);
142             */
143             for (int i = 0; i < abilities.size(); i++) {
144                AbilitySpec anAbility = (AbilitySpec) abilities.elementAt(i);
145                if ( anAbility.equals(ability) ) {
146                 // System.out.println("removed");
147                  abilities.removeElement(anAbility);
148                  // no abilities, so remove
149                  if (abilities.isEmpty())
150                   allAbilities.remove(agent);
151                  break;
152                }
153             }
154 
155           }
156           fireTableDataChanged();
157        }
158 //------------------------------------------------------------------------------
159        public void modifyAbility(String agent, AbilitySpec ability) {
160           if (allAbilities.containsKey(agent)) {
161             Vector abilities = (Vector) allAbilities.get(agent);
162             for (int i = 0; i < abilities.size(); i++) {
163                AbilitySpec anAbility = (AbilitySpec) abilities.elementAt(i);
164                if ( anAbility.getType().equals(ability.getType()) ) {
165                  anAbility = ability;
166                  break;
167                }
168             }
169 
170           }
171           fireTableDataChanged();
172        }
173 
174 //------------------------------------------------------------------------------
175        public AbilitySpec getAbility(String agent, String afactType) {
176 
177           if (allAbilities.containsKey(agent)) {
178             Vector abilities = (Vector) allAbilities.get(agent);
179              for (int i = 0; i < abilities.size(); i++) {
180                AbilitySpec anAbility = (AbilitySpec) abilities.elementAt(i);
181                if ( anAbility.getType().equals(afactType) )
182                  return anAbility;
183              }
184           }
185           return null;
186        }
187 //------------------------------------------------------------------------------
188        public AbilitySpec getAbility(String afactType) {
189 
190             Vector abilities = (Vector) allAbilities.get(getAgent());
191              for (int i = 0; i < abilities.size(); i++) {
192                AbilitySpec anAbility = (AbilitySpec) abilities.elementAt(i);
193                if ( anAbility.getType().equals(afactType) )
194                  return anAbility;
195              }
196              return null;
197        }
198 
199 
200 //------------------------------------------------------------------------------
201        public void setToNull(){
202           data = null;
203           fireTableDataChanged();
204        }
205 //------------------------------------------------------------------------------
206        public void setAbilitiesof(String agent){
207             data = (Vector) allAbilities.get(agent);
208             this.agent = agent;
209             fireTableDataChanged();
210        }
211 //------------------------------------------------------------------------------
212        public String getAgent(){
213           return agent;
214        }
215 //------------------------------------------------------------------------------
216        void deleteFact(int row){
217          String factType = (String) getValueAt(row,0);
218          organisationDb.del(agent,getAbility(agent,factType));
219        }
220 //------------------------------------------------------------------------------
221        public boolean hasAbilities(String agent){
222             if (allAbilities.containsKey(agent) == false) {
223               data = null;
224               this.agent = agent;
225               fireTableDataChanged();
226               return false;
227             }
228             else
229              return true;
230        }
231 
232 //------------------------------------------------------------------------------
233       public void abilityAddedEvent(AbilityEvent event) {
234         addAbility(event.getAgent(), event.getAbility());
235       }
236 //------------------------------------------------------------------------------
237      public void abilityModifiedEvent(AbilityEvent event) {
238        System.out.println("Called event modified");
239         modifyAbility(event.getAgent(), event.getAbility());
240      }
241 //------------------------------------------------------------------------------
242      public void abilityDeletedEvent(AbilityEvent event) {
243        removeAbility(event.getAgent(), event.getAbility());
244      }
245 //------------------------------------------------------------------------------
246      public void abilityAccessedEvent(AbilityEvent event) {}
247 
248 //------------------------------------------------------------------------------
249        public void removeZeusEventMonitors(){
250          organisationDb.removeAbilityMonitor(this,
251                                            AbilityEvent.ADD_MASK | AbilityEvent.DELETE_MASK
252                                            | AbilityEvent.MODIFY_MASK);
253         
254        }
255 
256 
257 }