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.graphviz;
7   
8   import org.glassbox.dotparser.*;
9   
10  import net.kwfgrid.gworkflowdl.structure.*;
11  import net.kwfgrid.gwui.graphview.*;
12  
13  import java.awt.*;
14  import java.awt.geom.Point2D;
15  import java.io.StringReader;
16  import java.util.Iterator;
17  
18  /***
19     This class can be used to apply a layout generated with graphviz to the layout of a WorkflowGraph.
20   */
21  public class DOTLayout2WorkflowGraph {
22      private WorkflowGraph _target;
23      private String _dot;
24  
25      /***
26         Constructor. The dot string should contain layout information for every node and edge in the workflow graph.
27         @param dot The workflow in attributed dot format.
28         @param target The workflow graph to layout.
29       */
30      public DOTLayout2WorkflowGraph(String dot, WorkflowGraph target) {
31  	_target = target;
32  	_dot = dot;
33      }
34  
35      protected Point2D toPoint2D(Rectangle bb, Point p) {
36  	if (p == null) return null;
37  	return new Point2D.Double(p.x, bb.height-p.y);
38      }
39  
40      protected Point2D[] toPoint2D(Rectangle bb, Point[] p) {
41  	if (p == null) return null;
42  	Point2D[] ret = new Point2D[p.length];
43  	for (int i=0; i<p.length; i++) ret[i] = toPoint2D(bb, p[i]);
44  	return ret;
45      }
46  
47      /***
48         Execute the process. After calling this method the worklflow graph will be layouted according to the
49         layout information in the dot file.
50         @exception GraphVizException If the layout information was incorrect.
51       */
52      public void run() throws GraphVizException {
53  	try {
54  	    // extract layout information from dot
55  	    DOTParser parser = new DOTParser(new StringReader(_dot));
56  	    ASTgraph astgraph = parser.graph();
57  	    LayoutBuilder layoutbuilder = new LayoutBuilder();
58  	    ASTPreorderDirector layoutdirector = new ASTPreorderDirector(astgraph, layoutbuilder);
59  	    layoutdirector.build();
60  
61  	    // apply the information to the graph
62  	    Rectangle bb = layoutbuilder.getGraphBoundingBox();
63  	    _target.setBoundingBox(bb);	    
64  
65  	    Iterator i = _target.getNodes().iterator(); while (i.hasNext()) {
66  		de.fzi.wim.guibase.graphview.graph.Node node = (de.fzi.wim.guibase.graphview.graph.Node)i.next();
67  		node.setFixed(false);
68  		if (node instanceof PlaceNode) {
69  		    PlaceNode pn = (PlaceNode)node;
70  		    Point p = layoutbuilder.getNodePosition(pn.getPlace().getID());
71  		    if (p != null) {
72  			pn.setLocation(p.getX(), bb.height-p.getY());
73  			pn.setFixed(true);
74  		    } 
75  		} else if (node instanceof TransitionNode) {
76  		    TransitionNode pn = (TransitionNode)node;
77  		    Transition transition = pn.getTransition();
78  		    Point p = layoutbuilder.getNodePosition(transition.getID());
79  		    if (p != null) {
80  			pn.setLocation(p.getX(), bb.height-p.getY());
81  			pn.setFixed(true);
82  		    } 
83  
84  		    Iterator inedges = pn.getEdgesTo().iterator(); while (inedges.hasNext()) {
85  			ArcEdge edge = (ArcEdge)inedges.next();
86  			Point[] cp = layoutbuilder.getEdgeControlPoints(edge.getPlace().getPlace().getID(), transition.getID());			
87  			edge.setControlPoints(toPoint2D(bb, cp));
88  			Point ps = layoutbuilder.getEdgePointOfArrowAtStart(edge.getPlace().getPlace().getID(), transition.getID());
89  			edge.setPointOfArrowAtStart(toPoint2D(bb, ps));
90  			Point pe = layoutbuilder.getEdgePointOfArrowAtEnd(edge.getPlace().getPlace().getID(), transition.getID());
91  			edge.setPointOfArrowAtEnd(toPoint2D(bb, pe));
92  			Point lp = layoutbuilder.getEdgeLabelPosition(edge.getPlace().getPlace().getID(), transition.getID());
93  			edge.setLabelPosition(toPoint2D(bb, lp));
94  		    }
95  
96  		    Iterator outedges = pn.getEdgesFrom().iterator(); while (outedges.hasNext()) {
97  			ArcEdge edge = (ArcEdge)outedges.next();
98  			Point[] cp = layoutbuilder.getEdgeControlPoints(transition.getID(), edge.getPlace().getPlace().getID());			
99  			edge.setControlPoints(toPoint2D(bb, cp));
100 			Point ps = layoutbuilder.getEdgePointOfArrowAtStart(transition.getID(), edge.getPlace().getPlace().getID());
101 			edge.setPointOfArrowAtStart(toPoint2D(bb, ps));
102 			Point pe = layoutbuilder.getEdgePointOfArrowAtEnd(transition.getID(), edge.getPlace().getPlace().getID());
103 			edge.setPointOfArrowAtEnd(toPoint2D(bb, pe));
104 			Point lp = layoutbuilder.getEdgeLabelPosition(transition.getID(), edge.getPlace().getPlace().getID());
105 			edge.setLabelPosition(toPoint2D(bb, lp));
106 		    }
107 		}
108 	    }
109 	} catch (ParseException x) {
110 	    GraphVizException lx = new GraphVizException("Could not parse attributed dot.");
111 	    lx.initCause(x);
112 	    throw lx;
113 	}	
114     }
115 }
116