Debian Codegraph @

automaton/converters/pydot.py

raw symbols SCIP
1 # Copyright (C) 2015 Yahoo! Inc. All Rights Reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14
15 try:
16 import pydot
17 PYDOT_AVAILABLE = True
18 except ImportError:
19 PYDOT_AVAILABLE = False
20
21
22 def convert(machine, graph_name,
23 graph_attrs=None, node_attrs_cb=None, edge_attrs_cb=None,
24 add_start_state=True, name_translations=None):
25 """Translates the state machine into a pydot graph.
26
27 :param machine: state machine to convert
28 :type machine: FiniteMachine
29 :param graph_name: name of the graph to be created
30 :type graph_name: string
31 :param graph_attrs: any initial graph attributes to set
32 (see http://www.graphviz.org/doc/info/attrs.html for
33 what these can be)
34 :type graph_attrs: dict
35 :param node_attrs_cb: a callback that takes one argument ``state``
36 and is expected to return a dict of node attributes
37 (see http://www.graphviz.org/doc/info/attrs.html for
38 what these can be)
39 :type node_attrs_cb: callback
40 :param edge_attrs_cb: a callback that takes three arguments ``start_state,
41 event, end_state`` and is expected to return a dict
42 of edge attributes (see
43 http://www.graphviz.org/doc/info/attrs.html for
44 what these can be)
45 :type edge_attrs_cb: callback
46 :param add_start_state: when enabled this creates a *private* start state
47 with the name ``__start__`` that will be a point
48 node that will have a dotted edge to the
49 ``default_start_state`` that your machine may have
50 defined (if your machine has no actively defined
51 ``default_start_state`` then this does nothing,
52 even if enabled)
53 :type add_start_state: bool
54 :param name_translations: a dict that provides alternative ``state``
55 string names for each state
56 :type name_translations: dict
57 """
58 if not PYDOT_AVAILABLE:
59 raise RuntimeError("pydot (or pydot2 or equivalent) is required"
60 " to convert a state machine into a pydot"
61 " graph")
62 if not name_translations:
63 name_translations = {}
64 graph_kwargs = {
65 'rankdir': 'LR',
66 'nodesep': '0.25',
67 'overlap': 'false',
68 'ranksep': '0.5',
69 'size': "11x8.5",
70 'splines': 'true',
71 'ordering': 'in',
72 }
73 if graph_attrs is not None:
74 graph_kwargs.update(graph_attrs)
75 graph_kwargs['graph_name'] = graph_name
76 g = pydot.Dot(**graph_kwargs)
77 node_attrs = {
78 'fontsize': '11',
79 }
80 nodes = {}
81 for (start_state, event, end_state) in machine:
82 if start_state not in nodes:
83 start_node_attrs = node_attrs.copy()
84 if node_attrs_cb is not None:
85 start_node_attrs.update(node_attrs_cb(start_state))
86 pretty_start_state = name_translations.get(start_state,
87 start_state)
88 nodes[start_state] = pydot.Node(pretty_start_state,
89 **start_node_attrs)
90 g.add_node(nodes[start_state])
91 if end_state not in nodes:
92 end_node_attrs = node_attrs.copy()
93 if node_attrs_cb is not None:
94 end_node_attrs.update(node_attrs_cb(end_state))
95 pretty_end_state = name_translations.get(end_state, end_state)
96 nodes[end_state] = pydot.Node(pretty_end_state, **end_node_attrs)
97 g.add_node(nodes[end_state])
98 edge_attrs = {}
99 if edge_attrs_cb is not None:
100 edge_attrs.update(edge_attrs_cb(start_state, event, end_state))
101 g.add_edge(pydot.Edge(nodes[start_state], nodes[end_state],
102 **edge_attrs))
103 if add_start_state and machine.default_start_state:
104 start = pydot.Node("__start__", shape="point", width="0.1",
105 xlabel='start', fontcolor='green', **node_attrs)
106 g.add_node(start)
107 g.add_edge(pydot.Edge(start, nodes[machine.default_start_state],
108 style='dotted'))
109 return g