Переглянути джерело

gvDraw now includes ports

rparedis 3 роки тому
батько
коміт
c6d7330580
2 змінених файлів з 107 додано та 11 видалено
  1. 79 0
      doc/_figures/EvenNumberGV.svg
  2. 28 11
      src/CBD/converters/CBDDraw.py

+ 79 - 0
doc/_figures/EvenNumberGV.svg

@@ -0,0 +1,79 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
+ "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
+<!-- Generated by graphviz version 2.47.3 (20210619.1520)
+ -->
+<!-- Title: model Pages: 1 -->
+<svg width="430pt" height="106pt"
+ viewBox="0.00 0.00 430.16 106.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
+<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 102)">
+<title>model</title>
+<polygon fill="white" stroke="transparent" points="-4,4 -4,-102 426.16,-102 426.16,4 -4,4"/>
+<text text-anchor="start" x="110.08" y="-79" font-family="Times New Roman,serif" font-weight="bold" font-size="20.00">gen (EvenNumberGen)</text>
+<!-- node_1748730290816 -->
+<g id="node1" class="node">
+<title>node_1748730290816</title>
+<polygon fill="none" stroke="black" points="68,-68 0,-68 0,0 68,0 68,-68"/>
+<polyline fill="none" stroke="black" points="12,-68 0,-56 "/>
+<polyline fill="none" stroke="black" points="0,-12 12,0 "/>
+<polyline fill="none" stroke="black" points="56,0 68,-12 "/>
+<polyline fill="none" stroke="black" points="68,-56 56,-68 "/>
+<text text-anchor="middle" x="34" y="-37.8" font-family="Times New Roman,serif" font-size="14.00">Counter</text>
+<text text-anchor="middle" x="34" y="-22.8" font-family="Times New Roman,serif" font-size="14.00">(counter)</text>
+</g>
+<!-- inter_1748730290816_OutCount -->
+<g id="node2" class="node">
+<title>inter_1748730290816_OutCount</title>
+<ellipse fill="black" stroke="black" cx="140.36" cy="-34" rx="0.36" ry="0.36"/>
+</g>
+<!-- node_1748730290816&#45;&gt;inter_1748730290816_OutCount -->
+<g id="edge1" class="edge">
+<title>node_1748730290816&#45;&gt;inter_1748730290816_OutCount</title>
+<path fill="none" stroke="black" d="M78.24,-34C78.24,-34 139.89,-34 139.89,-34"/>
+<polygon fill="none" stroke="black" points="68.24,-30.5 78.24,-34 68.24,-37.5 68.24,-30.5"/>
+<text text-anchor="middle" x="96.24" y="-37.8" font-family="Times New Roman,serif" font-size="14.00">OutCount</text>
+</g>
+<!-- node_1748741777488 -->
+<g id="node3" class="node">
+<title>node_1748741777488</title>
+<polygon fill="none" stroke="black" points="276.72,-66 212.72,-66 212.72,-2 276.72,-2 276.72,-66"/>
+<polyline fill="none" stroke="black" points="224.72,-66 212.72,-54 "/>
+<polyline fill="none" stroke="black" points="212.72,-14 224.72,-2 "/>
+<polyline fill="none" stroke="black" points="264.72,-2 276.72,-14 "/>
+<polyline fill="none" stroke="black" points="276.72,-54 264.72,-66 "/>
+<text text-anchor="middle" x="244.72" y="-37.8" font-family="Times New Roman,serif" font-size="14.00">Double</text>
+<text text-anchor="middle" x="244.72" y="-22.8" font-family="Times New Roman,serif" font-size="14.00">(double)</text>
+</g>
+<!-- inter_1748730290816_OutCount&#45;&gt;node_1748741777488 -->
+<g id="edge2" class="edge">
+<title>inter_1748730290816_OutCount&#45;&gt;node_1748741777488</title>
+<path fill="none" stroke="black" d="M141.03,-34C141.03,-34 202.64,-34 202.64,-34"/>
+<polygon fill="black" stroke="black" points="202.64,-37.5 212.64,-34 202.64,-30.5 202.64,-37.5"/>
+<text text-anchor="middle" x="184.14" y="-37.8" font-family="Times New Roman,serif" font-size="14.00">InNumber</text>
+</g>
+<!-- inter_1748741777488_OutDouble -->
+<g id="node4" class="node">
+<title>inter_1748741777488_OutDouble</title>
+<ellipse fill="black" stroke="black" cx="349.08" cy="-34" rx="0.36" ry="0.36"/>
+</g>
+<!-- node_1748741777488&#45;&gt;inter_1748741777488_OutDouble -->
+<g id="edge3" class="edge">
+<title>node_1748741777488&#45;&gt;inter_1748741777488_OutDouble</title>
+<path fill="none" stroke="black" d="M286.88,-34C286.88,-34 348.61,-34 348.61,-34"/>
+<polygon fill="none" stroke="black" points="276.88,-30.5 286.88,-34 276.88,-37.5 276.88,-30.5"/>
+<text text-anchor="middle" x="307.88" y="-37.8" font-family="Times New Roman,serif" font-size="14.00">OutDouble</text>
+</g>
+<!-- node_1748566902624_OUT1 -->
+<g id="node5" class="node">
+<title>node_1748566902624_OUT1</title>
+<ellipse fill="black" stroke="black" cx="421.8" cy="-34" rx="0.36" ry="0.36"/>
+</g>
+<!-- inter_1748741777488_OutDouble&#45;&gt;node_1748566902624_OUT1 -->
+<g id="edge4" class="edge">
+<title>inter_1748741777488_OutDouble&#45;&gt;node_1748566902624_OUT1</title>
+<path fill="none" stroke="black" d="M349.54,-34C349.54,-34 411.38,-34 411.38,-34"/>
+<polygon fill="none" stroke="black" points="411.38,-37.5 421.38,-34 411.38,-30.5 411.38,-37.5"/>
+<text text-anchor="middle" x="402.88" y="-37.8" font-family="Times New Roman,serif" font-size="14.00">OUT1</text>
+</g>
+</g>
+</svg>

+ 28 - 11
src/CBD/converters/CBDDraw.py

@@ -1,26 +1,30 @@
 """
-Useful drawing function to easily gvDraw a CBD model in Graphviz.
+Useful drawing function to easily draw a CBD model in Graphviz.
 """
 from CBD.Core import CBD
 from CBD.lib.std import *
 
 
-def gvDraw(cbd, filename, colors=None):
+def gvDraw(cbd, filename, rankdir="LR", colors=None):
 	"""
-	Outputs a :class:`CBD` as a graphviz script to filename.
+	Outputs a :class:`CBD` as a `GraphViz <https://graphviz.org/>`_ script to filename.
 
-	Warning:
-		This function may be removed in the future as DrawIO becomes more important.
+	For instance, drawing the CBD given in the :doc:`examples/EvenNumberGen` example, the following figure
+	can be obtained:
+
+	.. figure:: _figures/EvenNumberGV.svg
 
 	Note:
 		The resulting Graphviz file might look "clunky" and messy when rendering with
-		the standard dot engine. The :code:`neato`, :code:`twopi` and :code:`circo`
+		the standard :code:`dot` engine. The :code:`neato`, :code:`twopi` and :code:`circo`
 		engines might provide a cleaner and more readable result.
 
 	Args:
-		cbd (CBD):      The :class:`CBD` to gvDraw.
-		filename (str): The name of the dot-file.
-		colors (dict):  An optional dictionary of :code:`blockname -> color`.
+		cbd (CBD):          The :class:`CBD` to draw.
+		filename (str):     The name of the dot-file.
+		rankdir (str):      The GraphViz rankdir of the plot. Must be either :code:`TB`
+							or :code:`LR`.
+		colors (dict):      An optional dictionary of :code:`blockname -> color`.
 	"""
 	# f = sys.stdout
 	f = open(filename, "w")
@@ -33,7 +37,8 @@ digraph model {{
  label=<<B>{n} ({t})</B>>;
  labelloc=\"t\";
  fontsize=20;
-""".format(n=cbd.getPath(), t=cbd.getBlockType()))
+ rankdir=\"{rd}\";
+""".format(n=cbd.getPath(), t=cbd.getBlockType(), rd=rankdir))
 
 	if colors is None:
 		colors = {}
@@ -72,6 +77,12 @@ digraph model {{
 	def nodeName(block):
 		return "node_%d" % id(block)
 
+	for port in cbd.getInputPorts():
+		s = "%s_%s" % (nodeName(cbd), port.name)
+		write(" {s} [shape=point, width=0.01, height=0.01];\n".format(s=s))
+		i = "inter_%d_%s" % (id(port.block), port.name)
+		write(" {i} [shape=point, width=0.01, height=0.01];\n".format(i=i))
+		write(" {b} -> {i} [taillabel=\"{inp}\", arrowhead=\"none\", arrowtail=\"inv\", dir=both];\n".format(i=i, b=s, inp=port.name))
 	for block in cbd.getBlocks():
 		writeBlock(block)
 		for in_port in block.getInputPorts():
@@ -83,8 +94,14 @@ digraph model {{
 			i = "inter_%d_%s" % (id(block), op)
 			# if i not in conn: continue
 			write(" {i} [shape=point, width=0.01, height=0.01];\n".format(i=i))
-			write(" {a} -> {i} [taillabel=\"{out}\", arrowtail=\"invempty\", arrowhead=\"none\", dir=both];\n"\
+			write(" {a} -> {i} [taillabel=\"{out}\", arrowtail=\"oinv\", arrowhead=\"none\", dir=both];\n"\
 			      .format(i=i, a=nodeName(block), out=op))
+	for port in cbd.getOutputPorts():
+		other = port.getIncoming().source
+		i = "inter_%d_%s" % (id(other.block), other.name)
+		t = "%s_%s" % (nodeName(cbd), port.name)
+		write(" {b} [shape=point, width=0.01, height=0.01];\n".format(b=t))
+		write(" {i} -> {b} [headlabel=\"{inp}\", arrowhead=\"onormal\", arrowtail=\"none\", dir=both];\n".format(i=i, b=t, inp=port.name))
 
 	write("\n}")
 	f.close()