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  * ReportModel.java
26  *
27  *****************************************************************************/
28  
29  package zeus.visualiser.report;
30  
31  import java.io.*;
32  import java.util.*;
33  import java.awt.*;
34  import javax.swing.*;
35  import javax.swing.event.*;
36  
37  import zeus.util.*;
38  import zeus.concepts.*;
39  import zeus.actors.PlanRecord;
40  import zeus.gui.*;
41  import zeus.gui.graph.*;
42  
43  public class ReportModel extends AbstractGraphModel {
44    protected boolean           isNodeEditable    = true;
45    protected boolean           auto_delete       = false;
46    protected boolean           show_joint_graphs = false;
47    protected Hashtable         nodeTable         = new Hashtable();
48    protected Hashtable         agentTable        = new Hashtable();
49    protected Hashtable         taskTable         = new Hashtable();
50    protected DefaultListModel  taskListModel     = new DefaultListModel();
51    protected DefaultListModel  agentListModel    = new DefaultListModel();
52    protected EventListenerList listeners         = new EventListenerList();
53    protected String            isShowingAgent    = null;
54    protected String            isShowingTask     = null;
55  
56    public ReportModel() {
57       reset();
58    }
59  
60    public void reset() {
61       agentTable.clear();
62       taskTable.clear();
63       nodeTable.clear();
64       isShowingAgent = null;
65       isShowingTask = null;
66       fireChanged();
67       fireGraphStructureChanged();
68    }
69  
70    DefaultListModel getAgentListModel() { return agentListModel; }
71    DefaultListModel getTaskListModel()  { return taskListModel; }
72  
73    String getCurrentAgent() { return isShowingAgent; }
74    String getCurrentTask()  { return isShowingTask; }
75  
76    public Enumeration nodes() { return nodeTable.elements(); }
77  
78    public void setValue(GraphNode node, Object user_object) {
79       Core.ERROR(null,1,this);
80    }
81  
82    public boolean isNodeEditable(GraphNode node) {
83       return isNodeEditable;
84    }
85  
86    public boolean isLinkVisible(GraphNode from, GraphNode to) {
87       ReportRec child = (ReportRec)from.getUserObject();
88       ReportRec parent = (ReportRec)to.getUserObject();
89       return child.hasParent(parent.getName());
90    }
91  
92    public void setShowJointGraphs(boolean state) {
93       show_joint_graphs = state;
94       // resetGraphs();
95    }
96    public void    setAutoDelete(boolean state) { auto_delete = state; }
97    public boolean getAutoDelete()              { return auto_delete; }
98    public boolean getShowJointGraphs()         { return show_joint_graphs; }
99  
100   public synchronized GraphNode getNode(String name)  {
101      return (GraphNode)nodeTable.get(name);
102   }
103 
104   public synchronized void addAgent(String agent) {
105      if ( agentTable.containsKey(agent) ) return;
106      agentTable.put(agent,new HSet());
107      agentListModel.addElement(agent);
108      fireChanged();
109   }
110 
111   public synchronized void removeAgent(String agent) {
112      HSet taskList = (HSet)agentTable.remove(agent);
113      agentListModel.removeElement(agent);
114      removeTasks(taskList);
115      fireChanged();
116   }
117 
118   public void addAgents(Vector input) {
119      for(int i = 0; i < input.size(); i++ )
120         addAgent((String)input.elementAt(i));
121   }
122 
123   public synchronized void addAgents(String[] input) {
124      for(int i = 0; i < input.length; i++ )
125         addAgent(input[i]);
126   }
127 
128   public synchronized void removeAgents(Vector input) {
129      for(int i = 0; i < input.size(); i++ )
130         removeAgent((String)input.elementAt(i));
131   }
132 
133   public synchronized void removeAgents(String[] input) {
134      for(int i = 0; i < input.length; i++ )
135         removeAgent(input[i]);
136   }
137 
138   public synchronized String[] getAgents() {
139      String[] output = new String[agentTable.size()];
140      Enumeration enum = agentTable.keys();
141      for(int i = 0; enum.hasMoreElements(); i++ )
142         output[i] = (String)enum.nextElement();
143      return output;
144   }
145 
146   public synchronized String[] getTasks(String agent) {
147      HSet db = (HSet)agentTable.get(agent);
148      String[] output = new String[db.size()];
149      Enumeration enum = db.elements();
150      for(int i = 0; enum.hasMoreElements(); i++ )
151         output[i] = (String)enum.nextElement();
152      return output;
153   }
154 
155   public synchronized void removeTasks(String agent, String[] tasks) {
156      HSet taskList = (HSet)agentTable.get(agent);
157      for(int i = 0; i < tasks.length; i++ )
158         taskList.remove(tasks[i]);
159      removeTasks(tasks);
160      fireChanged();
161   }
162 
163   public synchronized void removeTask(String agent, String task) {
164      if ( agent == null ) agent = isShowingAgent;
165      HSet taskList = (HSet)agentTable.get(agent);
166      taskList.remove(task);
167      removeTask(task);
168      fireChanged();
169   }
170 
171   public synchronized void removeTasks(String[] tasks) {
172      for(int i = 0; i < tasks.length; i++ ) {
173         taskTable.remove(tasks[i]);
174         taskListModel.removeElement(tasks[i]);
175         if ( nodeTable.containsKey(tasks[i]) ) {
176            nodeTable.clear();
177            fireGraphStructureChanged();
178         }
179      }
180   }
181 
182   public synchronized void removeTasks(HSet tasks) {
183      Enumeration enum = tasks.elements();
184      String task;
185      while( enum.hasMoreElements() ) {
186         task = (String)enum.nextElement();
187         taskTable.remove(task);
188         taskListModel.removeElement(task);
189         if ( nodeTable.containsKey(task) ) {
190            nodeTable.clear();
191            fireGraphStructureChanged();
192         }
193      }
194   }
195 
196   public synchronized void removeTask(String task) {
197      taskTable.remove(task);
198      taskListModel.removeElement(task);
199      if ( nodeTable.containsKey(task) ) {
200         nodeTable.clear();
201         fireGraphStructureChanged();
202      }
203   }
204 
205   public synchronized void addReport(ReportRec rec) {
206      Core.DEBUG(3,"ReportModel adding report " + rec);
207 
208      String agent  = rec.getAgent();
209      String rootId = rec.getRootId();
210 
211      if ( rec.isRoot() ) {
212         HSet taskList = (HSet)agentTable.get(agent);
213         if ( taskList == null ) {
214            taskList = new HSet();
215            agentTable.put(agent,taskList);
216         }
217         taskList.add(rec.getName());
218      }
219 
220      Hashtable db = (Hashtable)taskTable.get(rootId);
221      if ( db == null ) {
222         db = new Hashtable();
223         taskTable.put(rootId,db);
224      }
225      db.put(rec.getName(),rec);
226 
227      // check if graph containing rec is visible
228      // if so - update graph node
229      if ( nodeTable.containsKey(rootId) ) {
230         GraphNode node = (GraphNode)nodeTable.get(rec.getName());
231         if ( node == null ) {
232            node = new GraphNode(rec);
233            nodeTable.put(rec.getName(),node);
234            doReportAdded(db,node,rec,true);
235            fireGraphNodeAdded(node);
236            fireGraphStructureChanged();
237         }
238         else {
239            node.setUserObject(rec);
240            doReportAdded(db,node,rec,true);
241            fireGraphNodeStateChanged(node);
242         }
243      }
244 
245      if ( rec.isRoot() ) {
246         if ( isShowingAgent == null )
247 	   showAgent(agent);
248 
249 	if ( agent.equals(isShowingAgent) ) {
250            if ( !taskListModel.contains(rec.getName()) )
251               taskListModel.addElement(rec.getName());
252            if ( isShowingTask == null )
253               showTask(rec.getName());
254            else if ( auto_delete && !isShowingTask.equals(rec.getName()) ) {
255               GraphNode prev_root = (GraphNode)nodeTable.get(isShowingTask);
256               ReportRec prev_rec = (ReportRec)prev_root.getUserObject();
257               switch( prev_rec.getState() ) {
258                  case PlanRecord.COMPLETED:
259                  case PlanRecord.FAILED:
260                  case PlanRecord.AGREEMENT:
261                       removeTask(agent,isShowingTask);
262                       showTask(rec.getName());
263                       break;
264                  default:
265                       break;
266               }
267            }
268         }
269         fireChanged();
270      }
271   }
272 
273   public synchronized void showAgent(String agent) {
274      Core.DEBUG(3,"Show agent: " + agent);
275      String[] tasks =  getTasks(agent);
276      taskListModel.removeAllElements();
277      nodeTable.clear();
278      fireGraphStructureChanged();
279      for(int i = 0; i < tasks.length; i++ )
280         taskListModel.addElement(tasks[i]);
281      isShowingAgent = agent;
282   }
283 
284   public synchronized void showTask(String task) {
285      nodeTable.clear();
286      Hashtable db = (Hashtable)taskTable.get(task);
287      Enumeration enum = db.elements();
288      GraphNode node;
289      ReportRec rec;
290      while( enum.hasMoreElements() ) {
291         rec = (ReportRec)enum.nextElement();
292         node = (GraphNode)nodeTable.get(rec.getName());
293         if ( node == null ) {
294            node = new GraphNode(rec);
295            nodeTable.put(rec.getName(),node);
296         }
297         doReportAdded(db,node,rec,false);
298      }
299      isShowingTask = task;
300      fireGraphStructureChanged();
301   }
302 
303   protected void doReportAdded(Hashtable db, GraphNode node1,
304                                ReportRec rec1, boolean notify) {
305      ReportRec rec2;
306      GraphNode node2;
307      String[] parents = rec1.getParents();
308      for(int i = 0; i < parents.length; i++ ) {
309         rec2 = (ReportRec)db.get(parents[i]);
310         if ( rec2 != null ) {
311            node2 = (GraphNode)nodeTable.get(rec2.getName());
312            if ( node2 == null ) {
313               node2 = new GraphNode(rec2);
314               nodeTable.put(rec2.getName(),node2);
315            }
316            node1.addParent(node2);
317            node2.addChild(node1);
318            if ( notify ) fireGraphNodeStateChanged(node2);
319         }
320         else
321            ; // Core.ERROR(rec2,2,this);
322      }
323 
324      String[] children = rec1.getChildren();
325      for(int i = 0; i < children.length; i++ ) {
326         rec2 = (ReportRec)db.get(children[i]);
327         if ( rec2 != null ) {
328            node2 = (GraphNode)nodeTable.get(rec2.getName());
329            if ( node2 == null ) {
330               node2 = new GraphNode(rec2);
331               nodeTable.put(rec2.getName(),node2);
332            }
333            node1.addChild(node2);
334            node2.addParent(node1);
335            if ( notify ) fireGraphNodeStateChanged(node2);
336         }
337         else
338            ; // Core.ERROR(rec2,3,this);
339      }
340 
341      String[] siblings = rec1.getSiblings();
342      for(int i = 0; i < siblings.length; i++ ) {
343         rec2 = (ReportRec)db.get(siblings[i]);
344         if ( rec2 != null ) {
345            node2 = (GraphNode)nodeTable.get(rec2.getName());
346            if ( node2 == null ) {
347               node2 = new GraphNode(rec2);
348               nodeTable.put(rec2.getName(),node2);
349            }
350            node1.addSibling(node2);
351            node2.addSibling(node1);
352            if ( notify ) fireGraphNodeStateChanged(node2);
353         }
354         else
355            ; // Core.ERROR(rec2,4,this);
356      }
357   }
358 
359   public void addChangeListener(ChangeListener x) {
360      listeners.add(ChangeListener.class, x);
361   }
362   public void removeChangeListener(ChangeListener x) {
363      listeners.remove(ChangeListener.class, x);
364   }
365 
366   protected void fireChanged() {
367      ChangeEvent c = new ChangeEvent(this);
368      Object[] list = listeners.getListenerList();
369      for(int i= list.length-2; i >= 0; i -=2) {
370         if (list[i] == ChangeListener.class) {
371            ChangeListener cl = (ChangeListener)list[i+1];
372            cl.stateChanged(c);
373         }
374      }
375   }
376 }