dol: initial dol commit
[jump.git] / dol / src / dol / visitor / dot / ArchDotVisitor.java
diff --git a/dol/src/dol/visitor/dot/ArchDotVisitor.java b/dol/src/dol/visitor/dot/ArchDotVisitor.java
new file mode 100644 (file)
index 0000000..5f71ee8
--- /dev/null
@@ -0,0 +1,250 @@
+/* $Id: ArchDotVisitor.java 1 2010-02-24 13:03:05Z haidw $ */
+package dol.visitor.dot;
+
+import java.util.Iterator;
+
+import dol.datamodel.architecture.ArchiConnection;
+import dol.datamodel.architecture.Architecture;
+import dol.datamodel.architecture.HWChannel;
+import dol.datamodel.architecture.Memory;
+import dol.datamodel.architecture.Processor;
+import dol.datamodel.architecture.ReadPath;
+import dol.datamodel.architecture.WritePath;
+import dol.datamodel.pn.Process;
+import dol.util.CodePrintStream;
+import dol.visitor.ArchiVisitor;
+
+/**
+ * Helps to generate DOTTY information for architecture resources.
+ */
+public class ArchDotVisitor extends ArchiVisitor
+{
+    /**
+     * Constructor.
+     *
+     * @param printStream print stream to which the contents is written
+     */
+    public ArchDotVisitor(CodePrintStream printStream) {
+        _printStream = printStream;
+        _pnVisitor = new PNDotVisitor(printStream);
+    }
+
+    /**
+     * Print a .dot file in the correct format for DOTTY.
+     *
+     * @param arch architecture that needs to be rendered
+     */
+    public void visitComponent(Architecture arch) {
+        _printStream.printPrefixln("digraph architecture {");
+        _printStream.println();
+        _printStream.prefixInc();
+        _printStream.printPrefixln("ratio = auto;");
+        _printStream.printPrefixln("rankdir = LR;");
+        _printStream.printPrefixln("ranksep = 2;");
+        _printStream.printPrefixln("nodesep = 0.2;");
+        _printStream.printPrefixln("center = true;");
+        _printStream.printPrefixln("");
+        _printStream.printPrefixln("node [ fontsize=12, height=0.4, "
+                + "width=0.4, style=filled, color=\"0.65 0.20 1.00\" ]");
+        _printStream.println();
+
+        //visit all processors
+        Processor processor;
+        Iterator<Processor> processorIter = arch.getProcessorList().iterator();
+        while( processorIter.hasNext() ) {
+            processor = processorIter.next();
+            processor.accept(this);
+        }
+
+        //visit all hw_channels
+        HWChannel chan;
+        Iterator<HWChannel> chanIter = arch.getHWChannelList().iterator();
+        while(chanIter.hasNext()) {
+            chan = chanIter.next();
+            chan.accept(this);
+        }
+
+        //visit all memories
+        Memory mem;
+        Iterator<Memory> memIter = arch.getMemoryList().iterator();
+        while(memIter.hasNext()) {
+            mem = memIter.next();
+            mem.accept(this);
+        }
+        
+        //visit all connections
+        ArchiConnection cn;
+        Iterator<ArchiConnection> cnIter = arch.getConnectionList().iterator();
+        while(cnIter.hasNext()) {
+            cn = cnIter.next();
+            cn.accept(this);
+        }
+
+        ReadPath rPath;
+        Iterator<ReadPath> rpIter = arch.getReadPathList().iterator();
+        while(rpIter.hasNext()) {
+            rPath = rpIter.next();
+            rPath.accept(this);
+        }
+
+        WritePath wPath;
+        Iterator<WritePath> wpIter = arch.getWritePathList().iterator();
+        while(wpIter.hasNext()) {
+            wPath = wpIter.next();
+            wPath.accept(this);
+        }
+
+        _printStream.prefixDec();
+        _printStream.println();
+        _printStream.printPrefixln("}");
+    }
+
+    public void visitComponent(HWChannel  chan)
+    {
+        _printStream.printPrefixln("subgraph cluster_"
+                + chan.getName().replaceAll("\\.", "") + " {");
+        _printStream.prefixInc();
+        _printStream.printPrefixln("label = \"" + chan.getName() + "\"");
+
+        if (!chan.getPathList().isEmpty()) {
+            Iterator<String> pIter = chan.getPathList().iterator();
+            String path;
+            while (pIter.hasNext()) {
+                path = (String) pIter.next();
+                _printStream.printPrefixln("\"" + path + "_" + chan.getName()
+                                           + "\" [label=\"  " +
+                                           "\" shape=circle style=solid]");
+            }
+        }
+
+        _printStream.prefixDec();
+        _printStream.printPrefixln("}");
+        _printStream.println();
+    }
+
+    public void visitComponent(Memory mem)
+    {
+       _printStream.printPrefixln("subgraph cluster_"
+               + mem.getName().replaceAll("\\.", "") + " {");
+       _printStream.prefixInc();
+       _printStream.printPrefixln("label = \"" + mem.getName() + "\"");
+
+       if (!mem.getRXBufList().isEmpty()) {
+           Iterator<String> rIter = mem.getRXBufList().iterator();
+           String rxBuf;
+           while (rIter.hasNext()) {
+               rxBuf = (String) rIter.next();
+               _printStream.printPrefixln("\"" + rxBuf.replaceAll("\\.", "")
+                       + "_RX\" [label=\"RX"
+                       + "\" shape=circle style=dotted]");
+           }
+       }
+
+       if (!mem.getTXBufList().isEmpty()) {
+           Iterator<String> rIter = mem.getTXBufList().iterator();
+           String txBuf;
+           while (rIter.hasNext()) {
+               txBuf = (String) rIter.next();
+               _printStream.printPrefixln("\"" + txBuf.replaceAll("\\.", "")
+                       + "_TX\" [label=\"TX"
+                       + "\" shape=circle style=dashed]");
+           }
+       }
+
+       if (!mem.getCHBufList().isEmpty()) {
+           Iterator<String> rIter = mem.getCHBufList().iterator();
+           String chBuf;
+           while (rIter.hasNext()) {
+               chBuf = (String) rIter.next();
+               _printStream.printPrefixln("\"" + chBuf.replaceAll("\\.", "")
+                       + "_CH\" [label=\"CH"
+                       + "\" shape=circle style=bold]");
+           }
+       }
+
+       _printStream.prefixDec();
+       _printStream.printPrefixln("}");
+       _printStream.println();
+    }
+
+
+    public void visitComponent(ArchiConnection cn)
+    {
+        _printStream.printPrefix();
+        _printStream.print("\""
+                + cn.getOrigin().getName().replaceAll("\\.", "") + "\" -> \""
+                + cn.getTarget().getName().replaceAll("\\.", "")
+                + "\" [ color=" + _color + " ];");
+        _printStream.println();
+    }
+
+    public void visitComponent(ReadPath rp)
+    {
+        String rName = rp.getName().replaceAll("\\.", "");
+
+        _printStream.printPrefix();
+        _printStream.print(rName + "_RPath_CH->" );
+
+        Iterator<HWChannel> cIter = rp.getHWChannelList().iterator();
+        while (cIter.hasNext()) {
+            HWChannel channel = cIter.next();
+            _printStream.print(rName + "_RPath_"
+                    + channel.getName().replaceAll("\\.", "")
+                    + "->");
+        }
+        _printStream.println(rName + "_RX");
+    }
+
+    public void visitComponent(WritePath rp)
+    {
+        String rName = rp.getName().replaceAll("\\.", "");
+
+        _printStream.printPrefix();
+        _printStream.print(rName + "_TX->" );
+
+        Iterator<HWChannel> cIter = rp.getHWChannelList().iterator();
+        while (cIter.hasNext()) {
+            HWChannel channel = cIter.next();
+            _printStream.print(rName + "_WPath_"
+                    + channel.getName().replaceAll("\\.", "")
+                    + "->");
+        }
+        _printStream.println(rName + "_WPath_CH");
+    }
+
+    /**
+     * Clusters all processes of this processor.
+     */
+    public void visitComponent(Processor processor)
+    {
+        /*
+       _printStream.printPrefix();
+       _printStream.print("\"" + processor.getName() + "\" [ label=\""
+                          + processor.getName() + "\", color=" + _color
+                          + ", shape=box];");
+       _printStream.println();
+        */
+
+       _printStream.printPrefixln("subgraph cluster_"
+               + processor.getName().replaceAll("\\.", "") + " {");
+       _printStream.prefixInc();
+       _printStream.printPrefixln("label = \"" + processor.getName() + "\"");
+
+       // labeleling clusters with dot2.8 & graphviz2.8 seems buggy
+       //_printStream.printPrefixln("label = \"" + processor.getName()+"\";");
+       if (!processor.getProcessList().isEmpty()) {
+           Iterator<Process> pIter = processor.getProcessList().iterator();
+           while (pIter.hasNext()) {
+               pIter.next().accept(_pnVisitor);
+           }
+       }
+       _printStream.prefixDec();
+       _printStream.printPrefixln("}");
+       _printStream.println();
+    }
+    
+    /** DOT Visitor to visit process network resources */
+    protected PNDotVisitor _pnVisitor = null;
+
+    protected String _color = "dimgray";
+}