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 net.kwfgrid.gworkflowdl.structure.*;
9   
10  import java.util.Map;
11  import java.util.HashMap;
12  import java.util.List;
13  import java.util.LinkedList;
14  
15  /***
16     Builder for the visual graph of a workflow petri net.
17     Builds instances of <code>net.kwfgrid.gwui.graphview.WorkflowGraph</code> 
18     containing <code>TransitionNodes</code>, <code>PlaceNodes</code> and <code>ArcEdges</code>.<br><br>
19     <b>NOTE:</b>The builder does not initialize the properties of the nodes and edges. This has to be done
20     by the client using the <code>PropertiesFactory</code>.
21   */
22  public class GraphBuilder {
23      protected Workflow _workflow;
24      protected WorkflowGraph _graph;
25  
26      public GraphBuilder(Workflow wf) {
27  	_workflow = wf;
28  	_graph = null;
29      }
30  
31      /***
32         Build the graph for the workflow.
33       */
34      public void build() {	
35  	if (_graph==null) {
36  	    Place[] places = _workflow.getPlaces();
37  	    Transition[] transitions = _workflow.getTransitions();
38  	    Map placenodes = new HashMap();
39  	    List transitionnodes = new LinkedList();	    
40  	    List edges = new LinkedList();
41  
42  	    if (places!=null) {
43  		for (int i=0; i<places.length; i++) {
44  		    PlaceNode node = new PlaceNode(places[i]);
45  		    placenodes.put(places[i].getID(), node);
46  		}
47  	    }
48  
49  	    if (transitions!=null) {
50  		for (int i=0; i<transitions.length; i++) {
51  		    Transition transition = transitions[i];
52  		    TransitionNode node = new TransitionNode(transition);
53  		    transitionnodes.add(node);
54              Edge[] readedges = transition.getReadEdges();
55              if (readedges!=null) {
56              for (int j=0; j<readedges.length; j++) {
57                  Edge arc = readedges[j];
58                  PlaceNode from = (PlaceNode)placenodes.get(arc.getPlaceID());
59                  ReadEdge edge = new ReadEdge(from, node, arc);
60                  edges.add(edge);
61              }
62              }
63  		    Edge[] inedges = transition.getInEdges();
64  		    if (inedges!=null) {
65  			for (int j=0; j<inedges.length; j++) {
66  			    Edge arc = inedges[j];
67  			    PlaceNode from = (PlaceNode)placenodes.get(arc.getPlaceID());
68  			    InEdge edge = new InEdge(from, node, arc);
69  			    edges.add(edge);
70  			}
71  		    }
72  		    Edge[] outedges = transition.getOutEdges();
73  		    if (outedges!=null) {
74  			for (int j=0; j<outedges.length; j++) {
75  			    Edge arc = outedges[j];
76  			    PlaceNode to = (PlaceNode)placenodes.get(arc.getPlaceID());
77  			    OutEdge edge = new OutEdge(node, to, arc);
78  			    edges.add(edge);
79  			}
80  		    }
81  		}
82  	    }
83  
84  	    _graph = new WorkflowGraph(_workflow);
85  	    _graph.addElements(placenodes.values(), null);
86  	    _graph.addElements(transitionnodes, null);
87  	    _graph.addElements(null, edges);
88  	}	
89      }
90  
91      /***
92         Get the builded graph. 
93         This method will return <code>null</code> if called before <code>build()</code>.
94       */
95      public WorkflowGraph getGraph() {
96  	return _graph;
97      }
98  }