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.generator.code;
25  
26  import java.util.*;
27  import javax.swing.event.*;
28  import zeus.util.*;
29  import zeus.concepts.*;
30  import zeus.generator.event.*;
31  import zeus.generator.*;
32  
33  /******************************************************************************
34  * GenerationPlan.java
35  *
36  * Underlying Model of the Zeus Agent Generator
37  * Change Log
38  *-----------
39  * Simon made some variables public 
40  *****************************************************************************/
41  public class GenerationPlan
42               implements AgentListener, TaskListener, ChangeListener {
43  
44    public static final String WINDOWS = "Windows";
45    public static final String UNIX    = "Unix";
46    public static final String ZSH = "zsh";
47    public static final String NONE = "no shell"; 
48  
49    public static final String SAVE_NEEDED    = "Modified";
50    public static final String NO_SAVE_NEEDED = "Saved";
51  
52    protected EventListenerList changeListeners = new EventListenerList();
53  
54    protected Hashtable agentTable = new Hashtable();
55    protected Hashtable taskTable = new Hashtable();
56    protected Hashtable nameserverTable = new Hashtable();
57    protected Hashtable visualiserTable = new Hashtable();
58    protected Hashtable facilitatorTable = new Hashtable();
59    protected Hashtable dbProxyTable = new Hashtable();
60  
61    protected String directory = System.getProperty("user.dir");
62    protected String platform = UNIX;
63    protected String shell = NONE; 
64  
65    protected GeneratorModel genmodel;
66    protected OntologyDb     ontology;
67  
68    public GenerationPlan(GeneratorModel genmodel, OntologyDb ontology) {
69       this.genmodel = genmodel;
70       this.ontology = ontology;
71       genmodel.addAgentListener(this);
72       genmodel.addTaskListener(this);
73       ontology.addChangeListener(this);
74       reset();
75    }
76  
77  // why are these synchronized? 
78    public synchronized void setPlatform(String input) {
79       if ( !input.equals(UNIX) && !input.equals(WINDOWS) ) {
80          System.err.println("Unknown platform");
81          return;
82       }
83       if ( platform != null && platform.equals(input) ) return;
84       platform = input;
85       recomputeOntologyFilePath();
86       recomputeSavedState();
87       fireChanged();
88    }
89  
90     /*** 
91      set the shell selected by the user
92      */
93    public void setShell (String input) { 
94      if (input.equals (ZSH))
95          shell = input; 
96          else 
97          shell = NONE; 
98    }
99    
100   
101   /*** 
102     should we generate scripts for a shell, or not? Which one? 
103     */
104   public String getShell() { 
105     return shell;
106   }
107 
108 
109   public synchronized String getPlatform() {
110      return platform;
111   }
112 
113 
114   public synchronized void setDirectory(String input) {
115      if ( directory != null && directory.equals(input) ) return;
116      directory = input;
117      recomputeOntologyFilePath();
118      recomputeSavedState();
119      fireChanged();
120   }
121 
122 
123   public synchronized String getDirectory() {
124      return directory;
125   }
126   
127 
128   protected void recomputeSavedState() {
129      Enumeration enum;
130      AgentInfo agentInfo;
131      TaskInfo taskInfo;
132      enum = agentTable.elements();
133      while( enum.hasMoreElements() ) {
134         agentInfo = (AgentInfo)enum.nextElement();
135         agentInfo.generate = true;
136         agentInfo.status = SAVE_NEEDED;
137      }
138      enum = taskTable.elements();
139      while( enum.hasMoreElements() ) {
140         taskInfo = (TaskInfo)enum.nextElement();
141         taskInfo.generate = true;
142         taskInfo.status = SAVE_NEEDED;
143      }
144   }
145 
146 
147   protected void recomputeOntologyFilePath() {
148      Enumeration enum;
149      AgentInfo agentInfo;
150      FacilitatorInfo facilitatorInfo;
151      VisualiserInfo visualiserInfo;
152      String ontology_file = getOntologyFilename();
153      enum = agentTable.elements();
154      while( enum.hasMoreElements() ) {
155         agentInfo = (AgentInfo)enum.nextElement();
156         agentInfo.ontology_file = ontology_file;
157      }
158      enum = facilitatorTable.elements();
159      while( enum.hasMoreElements() ) {
160         facilitatorInfo = (FacilitatorInfo)enum.nextElement();
161         facilitatorInfo.ontology_file = ontology_file;
162      }
163      enum = visualiserTable.elements();
164      while( enum.hasMoreElements() ) {
165         visualiserInfo = (VisualiserInfo)enum.nextElement();
166         visualiserInfo.ontology_file = ontology_file;
167      }
168   }
169 
170 
171   public synchronized AgentInfo[] getAgents() {
172      AgentInfo[] out = new AgentInfo[agentTable.size()];
173      Enumeration enum = agentTable.elements();
174      for(int i = 0; i < out.length; i++ )
175         out[i] = (AgentInfo)enum.nextElement();
176      return out;
177   }
178 
179 
180   public synchronized void setAgent(AgentInfo info) {
181      info.icon_file = genmodel.getAgentIcon(info.id);
182      agentTable.put(info.id,info);
183      fireChanged();
184   }
185 
186 
187   public synchronized void setAgentIcon(AgentInfo info) {
188      genmodel.setAgentIcon(info.id,info.icon_file);
189   }
190 
191 
192   public synchronized TaskInfo[] getTasks() {
193      TaskInfo[] out = new TaskInfo[taskTable.size()];
194      Enumeration enum = taskTable.elements();
195      for(int i = 0; i < out.length; i++ )
196         out[i] = (TaskInfo)enum.nextElement();
197      return out;
198   }
199 
200 
201   public synchronized void setTask(TaskInfo info) {
202      taskTable.put(info.id,info);
203      fireChanged();
204   }
205 
206 
207   public synchronized TaskInfo[] getSelectedTasks() {
208      // return only tasks the are marked for generation;
209      Enumeration enum = taskTable.elements();
210      Vector results = new Vector();
211      TaskInfo info;
212      while( enum.hasMoreElements() ) {
213         info = (TaskInfo)enum.nextElement();
214 	if ( info.generate ) results.addElement(info);
215      }
216      TaskInfo[] out = new TaskInfo[results.size()];
217      for(int i = 0; i < out.length; i++ )
218         out[i] = (TaskInfo)results.elementAt(i);
219      return out;
220   }
221   
222   
223   public synchronized AgentInfo[] getSelectedAgents() {
224      // return only tasks the are marked for generation;
225      Enumeration enum = agentTable.elements();
226      Vector results = new Vector();
227      AgentInfo info;
228      while( enum.hasMoreElements() ) {
229         info = (AgentInfo)enum.nextElement();
230 	if ( info.generate ) results.addElement(info);
231      }
232      AgentInfo[] out = new AgentInfo[results.size()];
233      for(int i = 0; i < out.length; i++ )
234         out[i] = (AgentInfo)results.elementAt(i);
235      return out;
236   }
237 
238 
239   public synchronized NameserverInfo[] getNameservers() {
240      NameserverInfo[] out = new NameserverInfo[nameserverTable.size()];
241      Enumeration enum = nameserverTable.elements();
242      for(int i = 0; i < out.length; i++ )
243         out[i] = (NameserverInfo)enum.nextElement();
244      return out;
245   }
246 
247 
248   public synchronized void setNameserver(NameserverInfo info) {
249      nameserverTable.put(info.id,info);
250      fireChanged();
251   }
252 
253 
254   public synchronized void removeNameserver(String id) {
255      nameserverTable.remove(id);
256      fireChanged();
257   }
258   
259 
260   public synchronized void createNameserver() {
261      NameserverInfo info = new NameserverInfo();
262      nameserverTable.put(info.id,info);
263      fireChanged();
264   }
265   
266 
267   public synchronized FacilitatorInfo[] getFacilitators() {
268      FacilitatorInfo[] out = new FacilitatorInfo[facilitatorTable.size()];
269      Enumeration enum = facilitatorTable.elements();
270      for(int i = 0; i < out.length; i++ )
271         out[i] = (FacilitatorInfo)enum.nextElement();
272      return out;
273   }
274   
275 
276   public synchronized void setFacilitator(FacilitatorInfo info) {
277      facilitatorTable.put(info.id,info);
278      fireChanged();
279   }
280 
281 
282   public synchronized void removeFacilitator(String id) {
283      facilitatorTable.remove(id);
284      fireChanged();
285   }
286 
287 
288   public synchronized void createFacilitator() {
289      FacilitatorInfo info = new FacilitatorInfo(getOntologyFilename());
290      facilitatorTable.put(info.id,info);
291      fireChanged();
292   }
293 
294 
295   public synchronized VisualiserInfo[] getVisualisers() {
296      VisualiserInfo[] out = new VisualiserInfo[visualiserTable.size()];
297      Enumeration enum = visualiserTable.elements();
298      for(int i = 0; i < out.length; i++ )
299         out[i] = (VisualiserInfo)enum.nextElement();
300      return out;
301   }
302 
303 
304   public synchronized void setVisualiser(VisualiserInfo info) {
305      visualiserTable.put(info.id,info);
306      fireChanged();
307   }
308 
309 
310   public synchronized void removeVisualiser(String id) {
311      visualiserTable.remove(id);
312      fireChanged();
313   }
314 
315 
316   public synchronized void createVisualiser() {
317      VisualiserInfo info = new VisualiserInfo(getOntologyFilename());
318      visualiserTable.put(info.id,info);
319      fireChanged();
320   }
321 
322 
323   public synchronized DbProxyInfo[] getDbProxys() {
324      DbProxyInfo[] out = new DbProxyInfo[dbProxyTable.size()];
325      Enumeration enum = dbProxyTable.elements();
326      for(int i = 0; i < out.length; i++ )
327         out[i] = (DbProxyInfo)enum.nextElement();
328      return out;
329   }
330 
331 
332   public synchronized void setDbProxy(DbProxyInfo info) {
333      dbProxyTable.put(info.id,info);
334      fireChanged();
335   }
336 
337 
338   public synchronized void removeDbProxy(String id) {
339      dbProxyTable.remove(id);
340      fireChanged();
341   }
342 
343 
344   public synchronized void createDbProxy() {
345      DbProxyInfo info = new DbProxyInfo();
346      dbProxyTable.put(info.id,info);
347      fireChanged();
348   }
349 
350 
351   public synchronized String[][] summarizeNameservers() {
352      String[][] out = new String[nameserverTable.size()][4];
353      Enumeration enum = nameserverTable.elements();
354      for(int i = 0; i < out.length; i++ )
355         out[i] = ((GenerationInfo)enum.nextElement()).summarize();
356      return out;
357   }
358   
359   
360   public synchronized String[][] summarizeVisualisers() {
361      String[][] out = new String[visualiserTable.size()][4];
362      Enumeration enum = visualiserTable.elements();
363      for(int i = 0; i < out.length; i++ )
364         out[i] = ((GenerationInfo)enum.nextElement()).summarize();
365      return out;
366   }
367   
368   
369   public synchronized String[][] summarizeFacilitators() {
370      String[][] out = new String[facilitatorTable.size()][4];
371      Enumeration enum = facilitatorTable.elements();
372      for(int i = 0; i < out.length; i++ )
373         out[i] = ((GenerationInfo)enum.nextElement()).summarize();
374      return out;
375   }
376   
377   
378   public synchronized String[][] summarizeDbProxys() {
379      String[][] out = new String[dbProxyTable.size()][4];
380      Enumeration enum = dbProxyTable.elements();
381      for(int i = 0; i < out.length; i++ )
382         out[i] = ((GenerationInfo)enum.nextElement()).summarize();
383      return out;
384   }
385   
386   
387   public synchronized String[][] summarizeSelectedTasks() {
388      // show only tasks the are marked for generation;
389      Enumeration enum = taskTable.elements();
390      Vector results = new Vector();
391      TaskInfo info;
392      while( enum.hasMoreElements() ) {
393         info = (TaskInfo)enum.nextElement();
394 	if ( info.generate ) results.addElement(info);
395      }
396      String[][] out = new String[results.size()][4];
397      for(int i = 0; i < out.length; i++ )
398         out[i] = ((TaskInfo)results.elementAt(i)).summarize();
399      return out;
400   }
401   
402   
403   public synchronized String[][] summarizeSelectedAgents() {
404      // show only tasks the are marked for generation;
405      Enumeration enum = agentTable.elements();
406      Vector results = new Vector();
407      AgentInfo info;
408      while( enum.hasMoreElements() ) {
409         info = (AgentInfo)enum.nextElement();
410 	if ( info.generate ) results.addElement(info);
411      }
412      String[][] out = new String[results.size()][4];
413      for(int i = 0; i < out.length; i++ )
414         out[i] = ((AgentInfo)results.elementAt(i)).summarize();
415      return out;
416   }
417   
418   
419   public synchronized String[][] summarizeTasks() {
420      String[][] out = new String[taskTable.size()][4];
421      Enumeration enum = taskTable.elements();
422      for(int i = 0; i < out.length; i++ )
423         out[i] = ((GenerationInfo)enum.nextElement()).summarize();
424      return out;
425   }
426   
427   
428   public synchronized String[][] summarizeAgents() {
429      String[][] out = new String[agentTable.size()][4];
430      Enumeration enum = agentTable.elements();
431      for(int i = 0; i < out.length; i++ )
432         out[i] = ((GenerationInfo)enum.nextElement()).summarize();
433      return out;
434   }
435   
436 
437   public synchronized void removeEntry(String type, String id) {
438      if ( type.equals(GenerationInfo.NAMESERVER) ) {
439         removeNameserver(id);
440         return;
441      }
442      else if ( type.equals(GenerationInfo.VISUALISER) ) {
443         removeVisualiser(id);
444         return;
445      }
446      else if ( type.equals(GenerationInfo.FACILITATOR) ) {
447         removeFacilitator(id);
448         return;
449      }
450      else if ( type.equals(GenerationInfo.DBPROXY) ) {
451         removeDbProxy(id);
452         return;
453      }
454      else if ( type.equals(GenerationInfo.AGENT) ) {
455         AgentInfo agentInfo = (AgentInfo)agentTable.get(id);
456         agentInfo.generate = false;
457         fireChanged();
458      }
459      else if ( type.equals(GenerationInfo.TASK) ) {
460         TaskInfo taskInfo = (TaskInfo)taskTable.get(id);
461         taskInfo.generate = false;
462         fireChanged();
463      }
464   }
465 
466 
467   public synchronized String getOntologyFilename() {
468      String file = ontology.getFilename();
469      file = Misc.relativePath(directory,file);
470      if ( platform.equals(WINDOWS) )
471         file = Writer.updateFilename(file,Writer.WINDOWS);
472      else if ( platform.equals(UNIX) )
473         file = Writer.updateFilename(file,Writer.UNIX);
474      return file;
475   }
476   
477   
478   public synchronized void purge() {
479      agentTable.clear();
480      taskTable.clear();
481      nameserverTable.clear();
482      visualiserTable.clear();
483      facilitatorTable.clear();
484      dbProxyTable.clear();
485      fireChanged();
486   }
487 
488 
489   public synchronized void reset() {
490      purge();
491      // create defaults: 1-nameserver, 1-facilitator & 1-visualiser
492      createNameserver();
493      createFacilitator();
494      createVisualiser();
495 
496      String file = getOntologyFilename();
497      AgentInfo agentInfo;
498      AgentDescription[] agents = genmodel.getAgents();
499      for(int i = 0; i < agents.length; i++ ) {
500         agentInfo = new AgentInfo(agents[i].getName(),
501            genmodel.getAgentName(agents[i].getName()),file);
502         agentInfo.icon_file = genmodel.getAgentIcon(agents[i].getName());
503         agentTable.put(agentInfo.id,agentInfo);
504      }
505 
506      TaskInfo taskInfo;
507      AbstractTask[] tasks = genmodel.getTasks();
508      for(int i = 0; i < tasks.length; i++ ) {
509         switch( tasks[i].getType() ) {
510            case AbstractTask.PRIMITIVE:
511            case AbstractTask.BEHAVIOUR:
512                 taskInfo = new TaskInfo(tasks[i].getName(),
513                 genmodel.getTaskName(tasks[i].getName()));
514                 taskTable.put(taskInfo.id,taskInfo);
515                 break;
516         }
517      }
518 
519      fireChanged();
520   }
521   
522 
523   public void stateChanged(ChangeEvent e) {
524      if ( e.getSource() == ontology ) {
525         recomputeOntologyFilePath();
526         fireChanged();
527      }
528   }
529 
530 // mabey there needs to be something about agent_external here?
531   public void agentStateChanged(AgentChangeEvent e) {
532      AgentDescription agent = e.getAgent();
533      int mode = e.getEventType();
534      AgentInfo agentInfo;
535      switch(mode) {
536         case AgentChangeEvent.ADD:
537              agentInfo = new AgentInfo(agent.getName(),
538                 genmodel.getAgentName(agent.getName()),
539 		getOntologyFilename());
540              agentInfo.icon_file = genmodel.getAgentIcon(agent.getName());
541              agentTable.put(agentInfo.id,agentInfo);
542              break;
543 
544         case AgentChangeEvent.MODIFY:
545              agentInfo = (AgentInfo)agentTable.get(agent.getName());
546              agentInfo.name = genmodel.getAgentName(agent.getName());
547              agentInfo.icon_file = genmodel.getAgentIcon(agent.getName());
548              agentInfo.status = SAVE_NEEDED;
549              agentInfo.generate = true;
550              break;
551 
552         case AgentChangeEvent.DELETE:
553              agentTable.remove(agent.getName());
554              break;
555      }
556      fireChanged();
557   }
558 
559 
560   public void taskStateChanged(TaskChangeEvent e) {
561      AbstractTask task = e.getTask();
562      if ( !task.isPrimitive() && !task.isBehaviour() ) return;
563 
564      int mode = e.getEventType();
565      TaskInfo taskInfo;
566      switch(mode) {
567         case TaskChangeEvent.ADD:
568              taskInfo = new TaskInfo(task.getName(),
569                 genmodel.getTaskName(task.getName()));
570              taskTable.put(taskInfo.id,taskInfo);
571              break;
572 
573         case TaskChangeEvent.MODIFY:
574              taskInfo = (TaskInfo)taskTable.get(task.getName());
575              taskInfo.name = genmodel.getTaskName(task.getName());
576              taskInfo.status = SAVE_NEEDED;
577              taskInfo.generate = true;
578          
579              AgentDescription[] agent = genmodel.getAgents();
580              AgentInfo agentInfo;
581              for(int i = 0; i < agent.length; i++ ) {
582                 if ( agent[i].containsTask(task.getName()) ) {
583                    agentInfo = (AgentInfo)agentTable.get(agent[i].getName());
584                    agentInfo.generate = true;
585                    agentInfo.status = SAVE_NEEDED;
586                 }
587              }
588              break;
589 
590         case TaskChangeEvent.DELETE:
591              taskTable.remove(task.getName());
592              break;
593      }
594      fireChanged();
595   }
596 
597 
598   public void addChangeListener(ChangeListener x) {
599     changeListeners.add(ChangeListener.class, x);
600   }
601   
602   
603   public void removeChangeListener(ChangeListener x) {
604     changeListeners.remove(ChangeListener.class, x);
605   }
606   
607   
608   protected void fireChanged() {
609     ChangeEvent c = new ChangeEvent(this);
610     Object[] listeners = changeListeners.getListenerList();
611     for(int i= listeners.length-2; i >= 0; i -=2) {
612        if (listeners[i] == ChangeListener.class) {
613           ChangeListener cl = (ChangeListener)listeners[i+1];
614           cl.stateChanged(c);
615        }
616     }
617   }
618   
619   
620 }