dol: initial dol commit
[jump.git] / dol / src / dol / visitor / dot / PNDotVisitor.java
diff --git a/dol/src/dol/visitor/dot/PNDotVisitor.java b/dol/src/dol/visitor/dot/PNDotVisitor.java
new file mode 100644 (file)
index 0000000..3a0205a
--- /dev/null
@@ -0,0 +1,163 @@
+/* $Id: PNDotVisitor.java 1 2010-02-24 13:03:05Z haidw $ */
+package dol.visitor.dot;
+
+import java.util.Iterator;
+
+import dol.datamodel.mapping.ComputationBinding;
+import dol.datamodel.mapping.Mapping;
+import dol.datamodel.pn.Channel;
+import dol.datamodel.pn.Port;
+import dol.datamodel.pn.Process;
+import dol.datamodel.pn.ProcessNetwork;
+import dol.util.CodePrintStream;
+import dol.visitor.PNVisitor;
+
+/**
+ * This class is a class for a visitor that is used to generate
+ * ".dot" output in order to visualize a PN using the DOTTY tool.
+ */
+public class PNDotVisitor extends PNVisitor {
+
+    Mapping _mapping = null;
+    
+    /**
+     * Constructor.
+     *
+     * @param printStream print stream to which the contents is written
+     */
+    public PNDotVisitor(CodePrintStream printStream) {
+        _printStream = printStream;
+    }
+
+    /**
+     * Print a .dot file in the correct format for DOTTY.
+     *
+     * @param x process network that needs to be rendered
+     */
+    public void visitComponent(ProcessNetwork x) {
+        _printStream.printPrefixln("digraph pn {");
+        _printStream.println();
+        _printStream.prefixInc();
+        _printStream.printPrefixln("ratio = auto;");
+        _printStream.printPrefixln("rankdir = LR;");
+        _printStream.printPrefixln("ranksep = 0.3;");
+        _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.printPrefixln("edge [ fontsize=10, arrowhead=normal, "
+                + "arrowsize=0.8, style=\"setlinewidth(2)\" ];");
+        _printStream.println();
+        
+        //visit all processes
+        Iterator<Process> pIter;
+        Process p;
+        pIter = x.getProcessList().iterator();
+        while (pIter.hasNext())
+        {
+            p = pIter.next();
+            p.accept(this);
+        }
+        _printStream.println();
+        
+        //visit all channels
+        Iterator<Channel> cIter;
+        Channel c;
+        cIter = x.getChannelList().iterator();
+        while( cIter.hasNext() ) {
+            c = cIter.next();
+            c.accept(this);
+        }
+
+        _printStream.prefixDec();
+        _printStream.println();
+        _printStream.printPrefixln("}");
+    }
+    
+    /**
+     * Print a line for the process in the correct format for DOTTY.
+     *
+     * @param x process that needs to be rendered
+     */
+    public void visitComponent(Process x) {
+        //other colors: beige, lightgoldenrod, orange, tan, khaki3,
+        //aliceblue, lightskyblue, lightseagreen, mintcream,
+        //burlywood3, lightblue1, linen, papayawhip, azure1,2,3
+        String color = "lightskyblue";
+
+        _printStream.printPrefix();
+        _printStream.print("\"" + x.getName() + "\" [ label=\""
+                                   + x.getName() + "\", color=" +
+                                   color);
+        if (!x.hasInPorts() || !x.hasOutPorts())
+            _printStream.print(", shape=diamond");
+        else
+            _printStream.print(", shape=ellipse");
+        _printStream.print(" ];");
+        _printStream.println();
+    }
+
+    /**
+     * Print a line for the channel in the correct format for DOTTY.
+     *
+     * @param x channel that needs to be rendered
+     */
+    public void visitComponent(Channel x) {
+        String color = "lightblue3";
+
+        //change the color of the channel to indicate whether connection
+        //is on-tile or off-tile 
+        if (_mapping != null) {
+            String process1 = _mapping.getPN().getProcess(x.getOrigin().
+                    getName()).getName();
+            String process2 = _mapping.getPN().getProcess(x.getTarget().
+                    getName()).getName();
+            String processor1 = "";
+            String processor2 = "";
+            
+            for (ComputationBinding binding :
+                _mapping.getCompBindList()) {
+                if (binding.getProcess().getName().
+                        equals(process1)) {
+                    processor1 = binding.getProcessor().getName();
+                } else if (binding.getProcess().getName().
+                        equals(process2)) {
+                    processor2 = binding.getProcessor().getName();
+                }
+            }
+            if (processor1.equals(processor2)) {
+            } else if (processor1.length() >= 6 && processor2.length() >=6
+                    && processor1.substring(0, 6).equals(
+                    processor2.substring(0, 6))) {
+                color = "orange";
+            } else {
+                color = "red";
+            }
+        }
+        
+
+        Iterator<Port> i;
+        Port port, portNext;
+        i = x.getPortList().iterator();
+
+        port = (Port) i.next();
+
+        portNext = (Port) i.next();
+        _printStream.printPrefix();
+
+        _printStream.print("\"" + port.getPeerResource().getName()
+                + "\" -> " + "\"" + portNext.getPeerResource().getName()
+                + "\" [" );
+        _printStream.print(" label=\"" + x.getName() + "\""
+                + ", color=" + color + " ];");
+        _printStream.println();
+    }
+
+    /**
+     * 
+     */
+    public void setMapping(Mapping mapping) {
+        _mapping = mapping;
+    }
+}