dol: initial dol commit
[jump.git] / dol / src / dol / visitor / dot / ArchDotVisitor.java
1 /* $Id: ArchDotVisitor.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.architecture.ArchiConnection;
7 import dol.datamodel.architecture.Architecture;
8 import dol.datamodel.architecture.HWChannel;
9 import dol.datamodel.architecture.Memory;
10 import dol.datamodel.architecture.Processor;
11 import dol.datamodel.architecture.ReadPath;
12 import dol.datamodel.architecture.WritePath;
13 import dol.datamodel.pn.Process;
14 import dol.util.CodePrintStream;
15 import dol.visitor.ArchiVisitor;
16
17 /**
18  * Helps to generate DOTTY information for architecture resources.
19  */
20 public class ArchDotVisitor extends ArchiVisitor
21 {
22     /**
23      * Constructor.
24      *
25      * @param printStream print stream to which the contents is written
26      */
27     public ArchDotVisitor(CodePrintStream printStream) {
28         _printStream = printStream;
29         _pnVisitor = new PNDotVisitor(printStream);
30     }
31
32     /**
33      * Print a .dot file in the correct format for DOTTY.
34      *
35      * @param arch architecture that needs to be rendered
36      */
37     public void visitComponent(Architecture arch) {
38         _printStream.printPrefixln("digraph architecture {");
39         _printStream.println();
40         _printStream.prefixInc();
41         _printStream.printPrefixln("ratio = auto;");
42         _printStream.printPrefixln("rankdir = LR;");
43         _printStream.printPrefixln("ranksep = 2;");
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.println();
50
51         //visit all processors
52         Processor processor;
53         Iterator<Processor> processorIter = arch.getProcessorList().iterator();
54         while( processorIter.hasNext() ) {
55             processor = processorIter.next();
56             processor.accept(this);
57         }
58
59         //visit all hw_channels
60         HWChannel chan;
61         Iterator<HWChannel> chanIter = arch.getHWChannelList().iterator();
62         while(chanIter.hasNext()) {
63             chan = chanIter.next();
64             chan.accept(this);
65         }
66
67         //visit all memories
68         Memory mem;
69         Iterator<Memory> memIter = arch.getMemoryList().iterator();
70         while(memIter.hasNext()) {
71             mem = memIter.next();
72             mem.accept(this);
73         }
74         
75         //visit all connections
76         ArchiConnection cn;
77         Iterator<ArchiConnection> cnIter = arch.getConnectionList().iterator();
78         while(cnIter.hasNext()) {
79             cn = cnIter.next();
80             cn.accept(this);
81         }
82
83         ReadPath rPath;
84         Iterator<ReadPath> rpIter = arch.getReadPathList().iterator();
85         while(rpIter.hasNext()) {
86             rPath = rpIter.next();
87             rPath.accept(this);
88         }
89
90         WritePath wPath;
91         Iterator<WritePath> wpIter = arch.getWritePathList().iterator();
92         while(wpIter.hasNext()) {
93             wPath = wpIter.next();
94             wPath.accept(this);
95         }
96
97         _printStream.prefixDec();
98         _printStream.println();
99         _printStream.printPrefixln("}");
100     }
101
102     public void visitComponent(HWChannel  chan)
103     {
104         _printStream.printPrefixln("subgraph cluster_"
105                 + chan.getName().replaceAll("\\.", "") + " {");
106         _printStream.prefixInc();
107         _printStream.printPrefixln("label = \"" + chan.getName() + "\"");
108
109         if (!chan.getPathList().isEmpty()) {
110             Iterator<String> pIter = chan.getPathList().iterator();
111             String path;
112             while (pIter.hasNext()) {
113                 path = (String) pIter.next();
114                 _printStream.printPrefixln("\"" + path + "_" + chan.getName()
115                                            + "\" [label=\"  " +
116                                            "\" shape=circle style=solid]");
117             }
118         }
119
120         _printStream.prefixDec();
121         _printStream.printPrefixln("}");
122         _printStream.println();
123     }
124
125     public void visitComponent(Memory mem)
126     {
127        _printStream.printPrefixln("subgraph cluster_"
128                + mem.getName().replaceAll("\\.", "") + " {");
129        _printStream.prefixInc();
130        _printStream.printPrefixln("label = \"" + mem.getName() + "\"");
131
132        if (!mem.getRXBufList().isEmpty()) {
133            Iterator<String> rIter = mem.getRXBufList().iterator();
134            String rxBuf;
135            while (rIter.hasNext()) {
136                rxBuf = (String) rIter.next();
137                _printStream.printPrefixln("\"" + rxBuf.replaceAll("\\.", "")
138                        + "_RX\" [label=\"RX"
139                        + "\" shape=circle style=dotted]");
140            }
141        }
142
143        if (!mem.getTXBufList().isEmpty()) {
144            Iterator<String> rIter = mem.getTXBufList().iterator();
145            String txBuf;
146            while (rIter.hasNext()) {
147                txBuf = (String) rIter.next();
148                _printStream.printPrefixln("\"" + txBuf.replaceAll("\\.", "")
149                        + "_TX\" [label=\"TX"
150                        + "\" shape=circle style=dashed]");
151            }
152        }
153
154        if (!mem.getCHBufList().isEmpty()) {
155            Iterator<String> rIter = mem.getCHBufList().iterator();
156            String chBuf;
157            while (rIter.hasNext()) {
158                chBuf = (String) rIter.next();
159                _printStream.printPrefixln("\"" + chBuf.replaceAll("\\.", "")
160                        + "_CH\" [label=\"CH"
161                        + "\" shape=circle style=bold]");
162            }
163        }
164
165        _printStream.prefixDec();
166        _printStream.printPrefixln("}");
167        _printStream.println();
168     }
169
170
171     public void visitComponent(ArchiConnection cn)
172     {
173         _printStream.printPrefix();
174         _printStream.print("\""
175                 + cn.getOrigin().getName().replaceAll("\\.", "") + "\" -> \""
176                 + cn.getTarget().getName().replaceAll("\\.", "")
177                 + "\" [ color=" + _color + " ];");
178         _printStream.println();
179     }
180
181     public void visitComponent(ReadPath rp)
182     {
183         String rName = rp.getName().replaceAll("\\.", "");
184
185         _printStream.printPrefix();
186         _printStream.print(rName + "_RPath_CH->" );
187
188         Iterator<HWChannel> cIter = rp.getHWChannelList().iterator();
189         while (cIter.hasNext()) {
190             HWChannel channel = cIter.next();
191             _printStream.print(rName + "_RPath_"
192                     + channel.getName().replaceAll("\\.", "")
193                     + "->");
194         }
195         _printStream.println(rName + "_RX");
196     }
197
198     public void visitComponent(WritePath rp)
199     {
200         String rName = rp.getName().replaceAll("\\.", "");
201
202         _printStream.printPrefix();
203         _printStream.print(rName + "_TX->" );
204
205         Iterator<HWChannel> cIter = rp.getHWChannelList().iterator();
206         while (cIter.hasNext()) {
207             HWChannel channel = cIter.next();
208             _printStream.print(rName + "_WPath_"
209                     + channel.getName().replaceAll("\\.", "")
210                     + "->");
211         }
212         _printStream.println(rName + "_WPath_CH");
213     }
214
215     /**
216      * Clusters all processes of this processor.
217      */
218     public void visitComponent(Processor processor)
219     {
220         /*
221        _printStream.printPrefix();
222        _printStream.print("\"" + processor.getName() + "\" [ label=\""
223                           + processor.getName() + "\", color=" + _color
224                           + ", shape=box];");
225        _printStream.println();
226         */
227
228        _printStream.printPrefixln("subgraph cluster_"
229                + processor.getName().replaceAll("\\.", "") + " {");
230        _printStream.prefixInc();
231        _printStream.printPrefixln("label = \"" + processor.getName() + "\"");
232
233        // labeleling clusters with dot2.8 & graphviz2.8 seems buggy
234        //_printStream.printPrefixln("label = \"" + processor.getName()+"\";");
235        if (!processor.getProcessList().isEmpty()) {
236            Iterator<Process> pIter = processor.getProcessList().iterator();
237            while (pIter.hasNext()) {
238                pIter.next().accept(_pnVisitor);
239            }
240        }
241        _printStream.prefixDec();
242        _printStream.printPrefixln("}");
243        _printStream.println();
244     }
245     
246     /** DOT Visitor to visit process network resources */
247     protected PNDotVisitor _pnVisitor = null;
248
249     protected String _color = "dimgray";
250 }