View Javadoc

1   /*
2    * Copyright (c) 2005, The K-Wf Grid Consortium
3    * Fraunhofer Institute for Computer Architecture and Software Technology
4    * See http://www.kwfgrid.eu and http://www.first.fraunhofer.de for more details.
5    */
6   package net.kwfgrid.gwui.graphview;
7   
8   import de.fzi.wim.guibase.graphview.graph.*;
9   
10  import net.kwfgrid.gworkflowdl.structure.Workflow;
11  import net.kwfgrid.gworkflowdl.structure.Place;
12  import net.kwfgrid.gworkflowdl.structure.Transition;
13  import net.kwfgrid.gwui.util.HashMapOfMaps;
14  
15  import java.util.Collection;
16  import java.util.Iterator;
17  import java.util.HashMap;
18  import java.util.Map;
19  import java.util.LinkedList;
20  import java.util.EventListener;
21  import java.awt.Rectangle;
22  
23  /***
24     A graph representing a workflow.
25   */
26  public class WorkflowGraph extends DefaultGraph implements WorkflowGraphElement {
27      protected Rectangle _bb;
28      protected Workflow _workflow;
29      /*** Place ID -> PlaceNode */
30      protected HashMap _placenodes;
31      /*** Transition ID -> TransitionNode */
32      protected HashMap _transitionnodes;
33      /*** Transition ID |-> Place ID -> ReadEdge */
34      protected HashMapOfMaps _readedges;
35      /*** Transition ID -> Place ID -> InEdge */
36      protected HashMapOfMaps _inedges;
37      /*** Transition ID -> Place ID -> OutEdge */
38      protected HashMapOfMaps _outedges;
39  
40      public WorkflowGraph(Workflow wf) {
41  	super();
42  	_workflow = wf;
43  	_placenodes = new HashMap();
44  	_transitionnodes = new HashMap();
45      _readedges = new HashMapOfMaps();
46  	_inedges = new HashMapOfMaps();
47  	_outedges = new HashMapOfMaps();
48      }
49  
50      public Object getStructureObject() {
51  	return _workflow;
52      }
53  
54      public void setBoundingBox(Rectangle bb) {
55  	_bb = bb;
56      }
57  
58      public Rectangle getBoundingBox() {
59  	return _bb;
60      }
61  
62      /***
63         Get the workflow of this graph.
64       */
65      public Workflow getWorkflow() {
66  	return _workflow;
67      }
68      /***
69         Get an read edge of the workflow pointing from the specified place to the specified transition.
70         The IDs of the place and transition will be used to lookup the ReadEdge.
71       */
72      public ReadEdge getReadEdge(Place from, Transition to) {
73  	return (ReadEdge)_readedges.get(to.getID(), from.getID());
74      }
75      /***
76         Get an in edge of the workflow pointing from the specified place to the specified transition.
77         The IDs of the place and transition will be used to lookup the InEdge.
78       */
79      public InEdge getInEdge(Place from, Transition to) {
80  	return (InEdge)_inedges.get(to.getID(), from.getID());
81      }
82      
83      /***
84         Get an out edge of the workflow pointing from the specified transition to the specified place.
85         The IDs of the place and transition will be used to lookup the OutEdge.
86       */
87      public OutEdge getOutEdge(Transition from, Place to) {
88  	return (OutEdge)_outedges.get(from.getID(), to.getID());
89      }
90  
91      /***
92         Get a place node of the workflow.
93         The ID of the place will be used to lookup the node.
94       */
95      public PlaceNode getPlaceNode(Place p) {
96  	return (PlaceNode)_placenodes.get(p.getID());
97      }
98  
99      /***
100        Get a transition node of the workflow.
101        The ID of the transition will be used to lookup the node.
102      */
103     public TransitionNode getTransitionNode(Transition t) {
104 	return (TransitionNode)_transitionnodes.get(t.getID());
105     }
106 
107     public synchronized void addElements(Collection nodes, Collection edges) {
108 	if (nodes!=null) {
109 	    Iterator i = nodes.iterator(); while (i.hasNext()) {
110 		Object node = i.next();
111 		if (node instanceof TransitionNode) {
112 		    TransitionNode tn = (TransitionNode)node;
113 		    _transitionnodes.put(tn.getTransition().getID(), tn);
114 		} else if (node instanceof PlaceNode) {
115 		    PlaceNode pn = (PlaceNode)node;
116 		    _placenodes.put(pn.getPlace().getID(), pn);
117 		}
118 	    }
119 	} 
120 	if (edges!=null) {
121 	    Iterator i = edges.iterator(); while (i.hasNext()) {
122 		Object edge = i.next();
123         if (edge instanceof ReadEdge) {
124             ReadEdge re = (ReadEdge)edge;
125             _readedges.put(re.getTransition().getTransition().getID(),
126                  re.getPlace().getPlace().getID(),
127                  re);
128         }
129         else if (edge instanceof InEdge) {
130 		    InEdge ie = (InEdge)edge;
131 		    _inedges.put(ie.getTransition().getTransition().getID(), 
132 				 ie.getPlace().getPlace().getID(),
133 				 ie);
134 		} else if (edge instanceof OutEdge) {
135 		    OutEdge oe = (OutEdge)edge;
136 		    _outedges.put(oe.getTransition().getTransition().getID(), 
137 				  oe.getPlace().getPlace().getID(),
138 				  oe);
139 		}
140 	    }
141 	}
142 	super.addElements(nodes, edges);
143     }
144 
145     public synchronized void deleteElements(Collection nodes, Collection edges) {
146 	if (nodes!=null) {
147 	    Iterator i = nodes.iterator(); while (i.hasNext()) {
148 		Object node = i.next();
149 		if (node instanceof TransitionNode) {
150 		    TransitionNode tn = (TransitionNode)node;
151 		    _transitionnodes.remove(tn.getTransition().getID());
152 		} else if (node instanceof PlaceNode) {
153 		    PlaceNode pn = (PlaceNode)node;
154 		    _placenodes.remove(pn.getPlace().getID());
155 		}
156 	    }
157 	} 
158 	if (edges!=null) {
159 	    Iterator i = edges.iterator(); while (i.hasNext()) {
160 		Object edge = i.next();
161         if (edge instanceof ReadEdge) {
162             ReadEdge re = (ReadEdge)edge;
163             _readedges.remove(re.getTransition().getTransition().getID(),
164                     re.getPlace().getPlace().getID());
165         } else if (edge instanceof InEdge) {
166 		    InEdge ie = (InEdge)edge;
167 		    _inedges.remove(ie.getTransition().getTransition().getID(), 
168 				    ie.getPlace().getPlace().getID());
169 		} else if (edge instanceof OutEdge) {
170 		    OutEdge oe = (OutEdge)edge;
171 		    _outedges.remove(oe.getTransition().getTransition().getID(), 
172 				     oe.getPlace().getPlace().getID());
173 		}
174 	    }
175 	}
176 	super.deleteElements(nodes, edges);
177     }
178 
179     public void clear() {
180 	_placenodes.clear();
181 	_transitionnodes.clear();
182     _readedges.clear();
183     _inedges.clear();
184 	_outedges.clear();
185 	super.clear();
186     }
187 }