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 net.kwfgrid.gwui.graphview.*;
9   
10  import de.fzi.wim.guibase.graphview.view.*;
11  
12  import java.awt.*;
13  
14  /***
15     Converter that converts a WorkflowGraph into graphviz' dot language.
16     The implementation is rather simple at the moment as we are only interested
17     in the layout information.
18   */
19  public class WorkflowGraph2DOTConverter implements WorkflowGraphConverter {
20      protected StringBuffer _dot;
21      protected StringBuffer _sinks;
22      protected StringBuffer _sources;
23      protected boolean _finished;
24      protected JGraphPane _pane;
25      protected Rectangle _bounds;
26      
27      /*** The screen resolution. */
28      public static final double DPI = 72;
29  
30      protected WorkflowGraph2DOTConverter() {
31  	_bounds = new Rectangle();
32  	_dot = new StringBuffer();
33  	_dot.append("digraph G {\n");
34  	_dot.append("  graph [rankdir=LR,nodesep=.5,ranksep=\".5 equally\"];\n");  // ranksep=.1 equally
35  	_sinks = new StringBuffer();
36  	_sources = new StringBuffer();
37  	_finished = false;
38      }
39  
40      /***
41         Constructor.
42         @param pane The JGraphPane drawing the graph to determine the nodes' sizes.
43      */
44      public WorkflowGraph2DOTConverter(JGraphPane pane) {
45  	this();
46  	_pane = pane;
47      }
48  
49      protected void getNodeSize(PlaceNode node, Rectangle bounds) {
50  	_pane.getNodeScreenBounds(node, bounds);
51      }
52  
53      protected void getNodeSize(TransitionNode node, Rectangle bounds) {
54  	_pane.getNodeScreenBounds(node, bounds);
55      }
56  
57      public void convertNode(PlaceNode p) {
58  	getNodeSize(p, _bounds);
59  	double w = _bounds.width / DPI;
60  	double h = _bounds.height / DPI;
61  	if (p.getEdgesTo().size() == 0 || "begin".equals(p.getPlace().getID())) {
62  	    _sources.append("  \""+p.getPlace().getID()+"\" [label=\"\", shape=ellipse,height="+h+",width="+w+",fixedsize=true];\n");
63  	} else if (p.getEdgesFrom().size() == 0) {
64  	    _sinks.append("  \""+p.getPlace().getID()+"\" [label=\"\", shape=ellipse,height="+h+",width="+w+",fixedsize=true];\n");
65  	} else {
66  	    _dot.append("  \""+p.getPlace().getID()+"\" [label=\"\", shape=ellipse,height="+h+",width="+w+",fixedsize=true];\n");
67  	}
68      }
69  
70      public void convertNode(TransitionNode t) {
71  	getNodeSize(t, _bounds);
72  	double w = _bounds.width / DPI;
73  	double h = _bounds.height / DPI;
74  	String[] text = t.getText();
75  	String label = "";
76  	if (text!=null) {
77  	    for (int i=0; i<text.length; i++) {
78  		label += text[i];
79  		if (i<text.length-1) label += "//n";
80  	    }
81  	}
82  	_dot.append("  \""+t.getTransition().getID()+"\" [label=\""+label+"\", fontname=\"Arial\", fontsize=\"10\", shape=box,height="+h+",width="+w+",fixedsize=true];\n");
83      }
84  
85      public void convertEdge(ReadEdge e) {
86  	_dot.append("  \""+e.getPlace().getPlace().getID()+"\" -> \""+e.getTransition().getTransition().getID()+"\"");
87  	String label = e.getLabel();
88  	if (!("".equals(label) || label == null)) {
89  	    _dot.append(" [label=\"").append(label).append("\", fontname=\"Arial\", fontsize=\"8\"]");
90  	}
91  	_dot.append(";\n");
92      }
93  
94      public void convertEdge(InEdge e) {
95  	_dot.append("  \""+e.getPlace().getPlace().getID()+"\" -> \""+e.getTransition().getTransition().getID()+"\"");
96  	String label = e.getLabel();
97  	if (!("".equals(label) || label == null)) {
98  	    _dot.append(" [label=\"").append(label).append("\", fontname=\"Arial\", fontsize=\"8\"]");
99  	}
100 	_dot.append(";\n");
101     }
102 
103     public void convertEdge(OutEdge e) {
104 	_dot.append("  \""+e.getTransition().getTransition().getID()+"\" -> \""+e.getPlace().getPlace().getID()+"\"");
105 	String label = e.getLabel();
106 	if (!("".equals(label) || label == null)) {
107 	    _dot.append(" [label=\"").append(label).append("\", fontname=\"Arial\", fontsize=\"8\"]");
108 	}
109 	_dot.append(";\n");
110     }
111 
112     public void convertGraph(WorkflowGraph graph) {
113 	//
114     }
115 
116     /***
117        Get the String representing the workflow in graphviz's dot language.
118     */
119     public synchronized String getDOT() {	
120 	if (!_finished) {
121 	    if (_sinks.length() > 0) {
122 		_dot.append("  { rank = sink; \n").append(_sinks.toString()).append("  }\n");
123 	    } 
124 	    if (_sources.length() > 0) {
125 		_dot.append("  { rank = source; \n").append(_sources.toString()).append("  }\n");
126 	    }
127 	    _dot.append("}\n");
128 	    _finished = true;
129 	}
130 	return _dot.toString();
131     }
132 }
133