1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 /******************************************************************************
25 * StrategyModel.java
26 *
27 * The underlying model for the Strategy 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 StrategyModel extends AbstractTableModel
42 implements ChangeListener {
43
44 static final int MODE = 0;
45 static final int TYPE = 1;
46 static final int AGENTS = 2;
47 static final int RELATIONS = 3;
48 static final int STRATEGY = 4;
49 static final int PARAMETERS = 5;
50 static final int FACT = 6;
51
52 static final int USE = StrategyInfo.USE;
53 static final int NO_USE = StrategyInfo.NO_USE;
54 static final String[] ALL = new String[0];
55
56 static int count = 0;
57 static Vector INITIATOR_STRATEGY_LIST = null;
58 static Vector RESPONDENT_STRATEGY_LIST = null;
59
60 static {
61 String sep = SystemProps.getProperty("file.separator");
62 String user1 = SystemProps.getProperty("user.strategy.initiator");
63 String user2 = SystemProps.getProperty("user.strategy.respondent");
64 String system1 = SystemProps.getProperty("system.strategy.initiator");
65 String system2 = SystemProps.getProperty("system.strategy.respondent");
66
67 StringTokenizer s1 = null;
68 HSet List = new HSet();
69
70 if ( system1 != null ) {
71 s1 = new StringTokenizer(system1,sep);
72 while( s1.hasMoreTokens() )
73 List.add(s1.nextToken());
74 }
75 if ( user1 != null ) {
76 s1 = new StringTokenizer(user1,sep);
77 while( s1.hasMoreTokens() )
78 List.add(s1.nextToken());
79 }
80 if ( StrategyInfo.DEFAULT_INITIATOR_STRATEGY != null )
81 List.add(StrategyInfo.DEFAULT_INITIATOR_STRATEGY);
82
83 INITIATOR_STRATEGY_LIST = List.toVector();
84
85 List.clear();
86 if ( system2 != null ) {
87 s1 = new StringTokenizer(system2,sep);
88 while( s1.hasMoreTokens() )
89 List.add(s1.nextToken());
90 }
91 if ( user2 != null ) {
92 s1 = new StringTokenizer(user2,sep);
93 while( s1.hasMoreTokens() )
94 List.add(s1.nextToken());
95 }
96 if ( StrategyInfo.DEFAULT_RESPONDENT_STRATEGY != null )
97 List.add(StrategyInfo.DEFAULT_RESPONDENT_STRATEGY);
98
99 RESPONDENT_STRATEGY_LIST = List.toVector();
100 };
101
102
103 protected EventListenerList changeListeners = new EventListenerList();
104 protected String[] columnNames = {
105 "Mode", "Fact Type", "Agents", "Relations", "Strategy", "Parameters",
106 };
107 protected Vector data = new Vector();
108 protected int selectedRow = -1;
109 protected AttributeModel attributeModel;
110 protected OntologyDb ontologyDb;
111 protected ProtocolInfo protocol;
112 protected GeneratorModel genmodel;
113
114 public StrategyModel(GeneratorModel genmodel, OntologyDb ontologyDb,
115 AttributeModel attributeModel) {
116
117 this.genmodel = genmodel;
118 this.ontologyDb = ontologyDb;
119 this.attributeModel = attributeModel;
120 ontologyDb.addChangeListener(this);
121 genmodel.addChangeListener(this);
122 }
123
124 public void reset(ProtocolInfo input) {
125 selectRow(-1);
126
127 if ( protocol != null )
128 protocol.setConstraints(data);
129 int size = data.size();
130 data.removeAllElements();
131 if ( size != 0 )
132 fireTableRowsDeleted(0,size-1);
133
134 this.protocol = input;
135 if ( protocol != null ) {
136 StrategyInfo[] info = protocol.getConstraints();
137 for(int i = 0; i < info.length; i++ )
138 data.addElement(info[i]);
139 fireTableRowsInserted(0,info.length-1);
140 }
141 fireTableStructureChanged();
142 }
143
144 public void removeRows(int[] rows) {
145 for(int i = 0; i < rows.length; i++ ) {
146 data.removeElementAt(rows[i]-i);
147 fireTableRowsDeleted(rows[i]-i,rows[i]-i);
148 }
149 selectRow(-1);
150 fireChanged();
151 }
152
153 public void selectRow(int row) {
154 if ( selectedRow == row ) return;
155 selectedRow = row;
156 if ( attributeModel != null ) {
157 if ( selectedRow >= 0 ) {
158 StrategyInfo info = (StrategyInfo)data.elementAt(selectedRow);
159 attributeModel.reset((Fact)info.getFact());
160 }
161 else
162 attributeModel.reset(null);
163 }
164 }
165
166 public void addNewRows(String[] names) {
167 if ( names == null || names.length == 0 ) return;
168 Fact[] input = new Fact[names.length];
169 for(int i = 0; i < names.length; i++ )
170 input[i] = ontologyDb.getFact(Fact.VARIABLE,names[i]);
171 addRows(input);
172 }
173
174 public void addRows(Fact[] input) {
175 if ( input == null ) return;
176 if ( input.length == 0 ) return;
177
178 Fact f1;
179 StrategyInfo info;
180 String strategy = null;
181 if ( protocol.getType().equals(ProtocolInfo.RESPONDENT) )
182 strategy = StrategyInfo.DEFAULT_RESPONDENT_STRATEGY;
183 else
184 strategy = StrategyInfo.DEFAULT_INITIATOR_STRATEGY;
185
186 int size = data.size();
187 for(int i = 0; i < input.length; i++ ) {
188 f1 = new Fact(input[i]);
189
190 String id = f1.ID();
191 while( contains(id) )
192 id += (count++);
193 f1.setId(id);
194 data.addElement(new StrategyInfo(f1,strategy));
195 }
196 selectRow(-1);
197 fireTableRowsInserted(size-1,size+input.length-1);
198 fireChanged();
199 }
200
201 protected boolean contains(String id) {
202
203 StrategyInfo info;
204 for(int i = 0; i < data.size(); i++ ) {
205 info = (StrategyInfo)data.elementAt(i);
206 if ( id.equals(info.getFact().ID()) )
207 return true;
208 }
209 return false;
210 }
211
212
213
214 public int getColumnCount() { return columnNames.length; }
215 public int getRowCount() { return data.size(); }
216 public String getColumnName(int col) { return columnNames[col]; }
217
218 public boolean isCellEditable(int row, int col) {
219 StrategyInfo info = (StrategyInfo)data.elementAt(row);
220 return col != TYPE && (col != STRATEGY || info.getType() == USE) &&
221 (col != PARAMETERS || info.getType() == USE);
222 }
223
224 public Object getValueAt (int row, int column) {
225 StrategyInfo info = (StrategyInfo)data.elementAt(row);
226 switch(column) {
227 case MODE:
228 return new Boolean(info.getType() == USE);
229
230 case TYPE:
231 return info.getFact().getType();
232
233 case PARAMETERS:
234 return (info.getType() == USE) ? info.getParameters() : null;
235
236 case AGENTS:
237 String[] agentIds = info.getAgents();
238 String[] agents = new String[agentIds.length];
239 for(int i = 0; i < agentIds.length; i++ )
240 agents[i] = genmodel.getAgentName(agentIds[i]);
241 return agents;
242
243 case RELATIONS:
244 return info.getRelations();
245
246 case STRATEGY:
247 return (info.getType() == USE) ? info.getStrategy() : null;
248
249 case FACT:
250 return info.getFact();
251 }
252 return null;
253 }
254
255 public void setValueAt(Object aValue, int row, int column) {
256 String[] list;
257
258 StrategyInfo info = (StrategyInfo)data.elementAt(row);
259 switch(column) {
260 case MODE:
261 Boolean b = (Boolean)aValue;
262 int mode = b.equals(Boolean.TRUE) ? USE : NO_USE;
263 if ( mode != info.getType() ) {
264 info.setType(mode);
265 if ( mode == USE ) {
266 if ( protocol.getType().equals(ProtocolInfo.RESPONDENT) )
267 info.setStrategy(StrategyInfo.DEFAULT_RESPONDENT_STRATEGY);
268 else
269 info.setStrategy(StrategyInfo.DEFAULT_INITIATOR_STRATEGY);
270 info.clearParameters();
271 }
272 fireTableCellUpdated(row,STRATEGY);
273 fireTableCellUpdated(row,PARAMETERS);
274 fireTableCellUpdated(row,column);
275 }
276 break;
277
278 case TYPE:
279 Core.ERROR(null,1,this);
280 break;
281
282 case AGENTS:
283 list = (String[])aValue;
284 String[] agentIds = new String[list.length];
285 for(int i = 0; i < agentIds.length; i++ ) {
286 agentIds[i] = genmodel.reverseAgentNameLookup(list[i]);
287 if ( agentIds[i] == null ) {
288 agentIds[i] = genmodel.createNewAgentId();
289 genmodel.createNewAgent(agentIds[i]);
290 genmodel.renameAgent(agentIds[i],list[i]);
291 }
292 }
293 info.setAgents(agentIds);
294 fireTableCellUpdated(row,column);
295 fireChanged();
296 break;
297
298 case RELATIONS:
299 list = (String[])aValue;
300 info.setRelations(list);
301 fireTableCellUpdated(row,column);
302 fireChanged();
303 break;
304
305 case STRATEGY:
306 Core.ERROR(info.getType() == USE,2,this);
307 if ( info.getStrategy().equals(aValue) )
308 return;
309 info.setStrategy((String)aValue);
310 fireTableCellUpdated(row,column);
311 fireChanged();
312 break;
313
314 case PARAMETERS:
315 Core.ERROR(info.getType() == USE,3,this);
316 if ( info.getParameters().equals(aValue) )
317 return;
318 info.setParameters((Hashtable)aValue);
319 fireTableCellUpdated(row,column);
320 fireChanged();
321 break;
322 }
323 }
324
325 public void stateChanged(ChangeEvent e) {
326 Object src = e.getSource();
327 if ( src == ontologyDb ) {
328
329 }
330 else if ( src == genmodel ) {
331
332 fireTableDataChanged();
333 }
334 }
335
336 public void addChangeListener(ChangeListener x) {
337 changeListeners.add(ChangeListener.class, x);
338 }
339 public void removeChangeListener(ChangeListener x) {
340 changeListeners.remove(ChangeListener.class, x);
341 }
342
343 protected void fireChanged() {
344 ChangeEvent c = new ChangeEvent(this);
345 Object[] listeners = changeListeners.getListenerList();
346 for(int i= listeners.length-2; i >= 0; i -=2) {
347 if (listeners[i] == ChangeListener.class) {
348 ChangeListener cl = (ChangeListener)listeners[i+1];
349 cl.stateChanged(c);
350 }
351 }
352 }
353 }