dol: initial dol commit
[jump.git] / dol / src / dol / visitor / dot / PNDotVisitor.java
1 /* $Id: PNDotVisitor.java 1 2010-02-24 13:03:05Z haidw $ */
2 package dol.visitor.dot;
3
4 import java.util.Iterator;
5
6 import dol.datamodel.mapping.ComputationBinding;
7 import dol.datamodel.mapping.Mapping;
8 import dol.datamodel.pn.Channel;
9 import dol.datamodel.pn.Port;
10 import dol.datamodel.pn.Process;
11 import dol.datamodel.pn.ProcessNetwork;
12 import dol.util.CodePrintStream;
13 import dol.visitor.PNVisitor;
14
15 /**
16  * This class is a class for a visitor that is used to generate
17  * ".dot" output in order to visualize a PN using the DOTTY tool.
18  */
19 public class PNDotVisitor extends PNVisitor {
20
21     Mapping _mapping = null;
22     
23     /**
24      * Constructor.
25      *
26      * @param printStream print stream to which the contents is written
27      */
28     public PNDotVisitor(CodePrintStream printStream) {
29         _printStream = printStream;
30     }
31
32     /**
33      * Print a .dot file in the correct format for DOTTY.
34      *
35      * @param x process network that needs to be rendered
36      */
37     public void visitComponent(ProcessNetwork x) {
38         _printStream.printPrefixln("digraph pn {");
39         _printStream.println();
40         _printStream.prefixInc();
41         _printStream.printPrefixln("ratio = auto;");
42         _printStream.printPrefixln("rankdir = LR;");
43         _printStream.printPrefixln("ranksep = 0.3;");
44         _printStream.printPrefixln("nodesep = 0.2;");
45         _printStream.printPrefixln("center = true;");
46         _printStream.printPrefixln("");
47         _printStream.printPrefixln("node [ fontsize=12, height=0.4, "
48                 + "width=0.4, style=filled, color=\"0.65 0.20 1.00\" ];");
49         _printStream.printPrefixln("edge [ fontsize=10, arrowhead=normal, "
50                 + "arrowsize=0.8, style=\"setlinewidth(2)\" ];");
51         _printStream.println();
52         
53         //visit all processes
54         Iterator<Process> pIter;
55         Process p;
56         pIter = x.getProcessList().iterator();
57         while (pIter.hasNext())
58         {
59             p = pIter.next();
60             p.accept(this);
61         }
62         _printStream.println();
63         
64         //visit all channels
65         Iterator<Channel> cIter;
66         Channel c;
67         cIter = x.getChannelList().iterator();
68         while( cIter.hasNext() ) {
69             c = cIter.next();
70             c.accept(this);
71         }
72
73         _printStream.prefixDec();
74         _printStream.println();
75         _printStream.printPrefixln("}");
76     }
77     
78     /**
79      * Print a line for the process in the correct format for DOTTY.
80      *
81      * @param x process that needs to be rendered
82      */
83     public void visitComponent(Process x) {
84         //other colors: beige, lightgoldenrod, orange, tan, khaki3,
85         //aliceblue, lightskyblue, lightseagreen, mintcream,
86         //burlywood3, lightblue1, linen, papayawhip, azure1,2,3
87         String color = "lightskyblue";
88
89         _printStream.printPrefix();
90         _printStream.print("\"" + x.getName() + "\" [ label=\""
91                                    + x.getName() + "\", color=" +
92                                    color);
93         if (!x.hasInPorts() || !x.hasOutPorts())
94             _printStream.print(", shape=diamond");
95         else
96             _printStream.print(", shape=ellipse");
97         _printStream.print(" ];");
98         _printStream.println();
99     }
100
101     /**
102      * Print a line for the channel in the correct format for DOTTY.
103      *
104      * @param x channel that needs to be rendered
105      */
106     public void visitComponent(Channel x) {
107         String color = "lightblue3";
108
109         //change the color of the channel to indicate whether connection
110         //is on-tile or off-tile 
111         if (_mapping != null) {
112             String process1 = _mapping.getPN().getProcess(x.getOrigin().
113                     getName()).getName();
114             String process2 = _mapping.getPN().getProcess(x.getTarget().
115                     getName()).getName();
116             String processor1 = "";
117             String processor2 = "";
118             
119             for (ComputationBinding binding :
120                 _mapping.getCompBindList()) {
121                 if (binding.getProcess().getName().
122                         equals(process1)) {
123                     processor1 = binding.getProcessor().getName();
124                 } else if (binding.getProcess().getName().
125                         equals(process2)) {
126                     processor2 = binding.getProcessor().getName();
127                 }
128             }
129             if (processor1.equals(processor2)) {
130             } else if (processor1.length() >= 6 && processor2.length() >=6
131                     && processor1.substring(0, 6).equals(
132                     processor2.substring(0, 6))) {
133                 color = "orange";
134             } else {
135                 color = "red";
136             }
137         }
138         
139
140         Iterator<Port> i;
141         Port port, portNext;
142         i = x.getPortList().iterator();
143
144         port = (Port) i.next();
145
146         portNext = (Port) i.next();
147         _printStream.printPrefix();
148
149         _printStream.print("\"" + port.getPeerResource().getName()
150                 + "\" -> " + "\"" + portNext.getPeerResource().getName()
151                 + "\" [" );
152         _printStream.print(" label=\"" + x.getName() + "\""
153                 + ", color=" + color + " ];");
154         _printStream.println();
155     }
156
157     /**
158      * 
159      */
160     public void setMapping(Mapping mapping) {
161         _mapping = mapping;
162     }
163 }