{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import logging\n", "\n", "logging.basicConfig(format='[%(levelname)s@%(name)s] %(message)s', level=logging.DEBUG)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[DEBUG@graphviz.backend] run ['dot', '-Tsvg']\n" ] }, { "data": { "image/svg+xml": [ "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "%3\r\n", "\r\n", "\r\n", "A\r\n", "\r\n", "King Arthur\r\n", "\r\n", "\r\n", "B\r\n", "\r\n", "Sir Bedevere the Wise\r\n", "\r\n", "\r\n", "A->B\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "L\r\n", "\r\n", "Sir Lancelot the Brave\r\n", "\r\n", "\r\n", "A->L\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "B->L\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from graphviz import Digraph\n", "\n", "dot = Digraph(comment='The Round Table')\n", "\n", "dot.node('A', 'King Arthur')\n", "dot.node('B', 'Sir Bedevere the Wise')\n", "dot.node('L', 'Sir Lancelot the Brave')\n", "\n", "dot.edges(['AB', 'AL'])\n", "dot.edge('B', 'L', constraint='false')\n", "\n", "dot" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[DEBUG@graphviz.backend] run ['dot', '-Tsvg']\n" ] }, { "data": { "image/svg+xml": [ "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "the holy hand grenade\r\n", "\r\n", "\r\n", "1\r\n", "\r\n", "1\r\n", "\r\n", "\r\n", "2\r\n", "\r\n", "2\r\n", "\r\n", "\r\n", "1->2\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "3\r\n", "\r\n", "3\r\n", "\r\n", "\r\n", "2->3\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "lob\r\n", "\r\n", "lob\r\n", "\r\n", "\r\n", "3->lob\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from graphviz import Source\n", "\n", "src = Source('digraph \"the holy hand grenade\" { rankdir=LR; 1 -> 2 -> 3 -> lob }')\n", "src" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[DEBUG@graphviz.backend] run ['dot', '-Tsvg']\n" ] }, { "data": { "image/svg+xml": [ "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "finite_state_machine\r\n", "\r\n", "\r\n", "LR_0\r\n", "\r\n", "\r\n", "LR_0\r\n", "\r\n", "\r\n", "LR_2\r\n", "\r\n", "LR_2\r\n", "\r\n", "\r\n", "LR_0->LR_2\r\n", "\r\n", "\r\n", "SS(B)\r\n", "\r\n", "\r\n", "LR_1\r\n", "\r\n", "LR_1\r\n", "\r\n", "\r\n", "LR_0->LR_1\r\n", "\r\n", "\r\n", "SS(S)\r\n", "\r\n", "\r\n", "LR_3\r\n", "\r\n", "\r\n", "LR_3\r\n", "\r\n", "\r\n", "LR_4\r\n", "\r\n", "\r\n", "LR_4\r\n", "\r\n", "\r\n", "LR_8\r\n", "\r\n", "\r\n", "LR_8\r\n", "\r\n", "\r\n", "LR_6\r\n", "\r\n", "LR_6\r\n", "\r\n", "\r\n", "LR_8->LR_6\r\n", "\r\n", "\r\n", "S(b)\r\n", "\r\n", "\r\n", "LR_5\r\n", "\r\n", "LR_5\r\n", "\r\n", "\r\n", "LR_8->LR_5\r\n", "\r\n", "\r\n", "S(a)\r\n", "\r\n", "\r\n", "LR_2->LR_4\r\n", "\r\n", "\r\n", "S(A)\r\n", "\r\n", "\r\n", "LR_2->LR_6\r\n", "\r\n", "\r\n", "SS(b)\r\n", "\r\n", "\r\n", "LR_2->LR_5\r\n", "\r\n", "\r\n", "SS(a)\r\n", "\r\n", "\r\n", "LR_1->LR_3\r\n", "\r\n", "\r\n", "S($end)\r\n", "\r\n", "\r\n", "LR_6->LR_6\r\n", "\r\n", "\r\n", "S(b)\r\n", "\r\n", "\r\n", "LR_6->LR_5\r\n", "\r\n", "\r\n", "S(a)\r\n", "\r\n", "\r\n", "LR_5->LR_5\r\n", "\r\n", "\r\n", "S(a)\r\n", "\r\n", "\r\n", "LR_7\r\n", "\r\n", "LR_7\r\n", "\r\n", "\r\n", "LR_5->LR_7\r\n", "\r\n", "\r\n", "S(b)\r\n", "\r\n", "\r\n", "LR_7->LR_8\r\n", "\r\n", "\r\n", "S(b)\r\n", "\r\n", "\r\n", "LR_7->LR_5\r\n", "\r\n", "\r\n", "S(a)\r\n", "\r\n", "\r\n", "\r\n" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# http://www.graphviz.org/content/fsm\n", "\n", "from graphviz import Digraph\n", "\n", "f = Digraph('finite_state_machine', filename='fsm.gv')\n", "f.attr(rankdir='LR', size='8,5')\n", "\n", "f.attr('node', shape='doublecircle')\n", "f.node('LR_0')\n", "f.node('LR_3')\n", "f.node('LR_4')\n", "f.node('LR_8')\n", "\n", "f.attr('node', shape='circle')\n", "f.edge('LR_0', 'LR_2', label='SS(B)')\n", "f.edge('LR_0', 'LR_1', label='SS(S)')\n", "f.edge('LR_1', 'LR_3', label='S($end)')\n", "f.edge('LR_2', 'LR_6', label='SS(b)')\n", "f.edge('LR_2', 'LR_5', label='SS(a)')\n", "f.edge('LR_2', 'LR_4', label='S(A)')\n", "f.edge('LR_5', 'LR_7', label='S(b)')\n", "f.edge('LR_5', 'LR_5', label='S(a)')\n", "f.edge('LR_6', 'LR_6', label='S(b)')\n", "f.edge('LR_6', 'LR_5', label='S(a)')\n", "f.edge('LR_7', 'LR_8', label='S(b)')\n", "f.edge('LR_7', 'LR_5', label='S(a)')\n", "f.edge('LR_8', 'LR_6', label='S(b)')\n", "f.edge('LR_8', 'LR_5', label='S(a)')\n", "\n", "f" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[DEBUG@graphviz.backend] run ['dot', '-Tsvg']\n" ] }, { "data": { "image/svg+xml": [ "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "G\r\n", "\r\n", "cluster0\r\n", "\r\n", "\r\n", "cluster1\r\n", "\r\n", "\r\n", "\r\n", "a\r\n", "\r\n", "a\r\n", "\r\n", "\r\n", "b\r\n", "\r\n", "b\r\n", "\r\n", "\r\n", "a->b\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "c\r\n", "\r\n", "c\r\n", "\r\n", "\r\n", "a->c\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "d\r\n", "\r\n", "d\r\n", "\r\n", "\r\n", "b->d\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "f\r\n", "\r\n", "f\r\n", "\r\n", "\r\n", "b->f\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "c->d\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "e\r\n", "\r\n", "e\r\n", "\r\n", "\r\n", "c->e\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "g\r\n", "\r\n", "g\r\n", "\r\n", "\r\n", "c->g\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "d->e\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "h\r\n", "\r\n", "h\r\n", "\r\n", "\r\n", "d->h\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "e->g\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "e->f\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# http://www.graphviz.org/pdf/dotguide.pdf Figure 20\n", "\n", "from graphviz import Digraph\n", "\n", "g = Digraph('G', filename='cluster_edge.gv')\n", "g.attr(compound='true')\n", "\n", "with g.subgraph(name='cluster0') as c:\n", " c.edges(['ab', 'ac', 'bd', 'cd'])\n", "\n", "with g.subgraph(name='cluster1') as c:\n", " c.edges(['eg', 'ef'])\n", "\n", "g.edge('b', 'f', lhead='cluster1')\n", "g.edge('d', 'e')\n", "g.edge('c', 'g', ltail='cluster0', lhead='cluster1')\n", "g.edge('c', 'e', ltail='cluster0')\n", "g.edge('d', 'h')\n", "\n", "g" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.4" } }, "nbformat": 4, "nbformat_minor": 1 }