author | Sverre Rabbelier <srabbelier@gmail.com> |
Sat, 29 Nov 2008 19:45:19 +0000 | |
changeset 614 | 53a3e46fc512 |
parent 594 | 06c2228e39cb |
permissions | -rw-r--r-- |
594
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
1 |
#!/usr/bin/env python |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
2 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
3 |
# Copyright (c) 2007-2008 Pedro Matiello <pmatiello@gmail.com> |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
4 |
# |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
5 |
# Permission is hereby granted, free of charge, to any person |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
6 |
# obtaining a copy of this software and associated documentation |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
7 |
# files (the "Software"), to deal in the Software without |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
8 |
# restriction, including without limitation the rights to use, |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
9 |
# copy, modify, merge, publish, distribute, sublicense, and/or sell |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
10 |
# copies of the Software, and to permit persons to whom the |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
11 |
# Software is furnished to do so, subject to the following |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
12 |
# conditions: |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
13 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
14 |
# The above copyright notice and this permission notice shall be |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
15 |
# included in all copies or substantial portions of the Software. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
16 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
17 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
18 |
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
19 |
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
20 |
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
21 |
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
22 |
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
23 |
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
24 |
# OTHER DEALINGS IN THE SOFTWARE. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
25 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
26 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
27 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
28 |
This small application will build and draw a graph for a given finite definite automaton described |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
29 |
as a labelled transition system. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
30 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
31 |
This is a very naive, probably useless, possibly incorrect, barely tested implementation. No |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
32 |
validation is ever performed. Take care or it will burn your house and kill your cat. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
33 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
34 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
35 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
36 |
# Module metadata |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
37 |
__authors__ = "Pedro Matiello" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
38 |
__license__ = "MIT" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
39 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
40 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
41 |
# Imports |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
42 |
import sys |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
43 |
sys.path.append('..') |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
44 |
import graph |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
45 |
sys.path.append('/usr/lib/graphviz/python/') |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
46 |
import gv |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
47 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
48 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
49 |
def load_automaton(filename): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
50 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
51 |
Read a automaton described as a labelled transition system and build the equivalent graph. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
52 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
53 |
@type filename: string |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
54 |
@param filename: Name of the file containing the LTS-described automaton. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
55 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
56 |
@rtype: graph |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
57 |
@return: Automaton's graph. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
58 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
59 |
gr = graph.digraph() |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
60 |
infile = file(filename,'r') |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
61 |
line = infile.readline() |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
62 |
final = [] |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
63 |
while (line): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
64 |
line = line.replace("\n",'').split(' ') |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
65 |
datatype = line[0] |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
66 |
data = line[1:] |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
67 |
if (datatype == 'Q'): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
68 |
# States |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
69 |
for each in data: |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
70 |
gr.add_node(each) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
71 |
if (datatype == 'A'): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
72 |
# Alphabet |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
73 |
pass |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
74 |
if (datatype == 'F'): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
75 |
# Final states |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
76 |
final = final + data |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
77 |
if (datatype == 's'): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
78 |
# Initial state |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
79 |
gr.add_node('.',attrs=[('shape','point')]) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
80 |
gr.add_edge('.',data[0]) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
81 |
if (datatype == 't'): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
82 |
# Transitions |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
83 |
if (gr.has_edge(data[1], data[2])): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
84 |
gr.set_edge_label(data[1], data[2], \ |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
85 |
gr.get_edge_label(data[1], data[2]) + ', ' + data[0]) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
86 |
else: |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
87 |
gr.add_edge(data[1], data[2], label=data[0]) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
88 |
line = infile.readline() |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
89 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
90 |
for node in gr: |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
91 |
if (node in final and node != '.'): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
92 |
gr.add_node_attribute(node, ('shape','doublecircle')) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
93 |
elif (node != '.'): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
94 |
gr.add_node_attribute(node, ('shape','circle')) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
95 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
96 |
return gr, final |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
97 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
98 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
99 |
# Main |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
100 |
try: |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
101 |
filename = sys.argv[1] |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
102 |
gr, final = load_automaton(sys.argv[1]) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
103 |
dot = gr.write(fmt='dot') |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
104 |
except IndexError: |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
105 |
print "Syntax: %s filename" % sys.argv[0] |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
106 |
sys.exit(1) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
107 |
except IOError: |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
108 |
print "Can't open file %s" % filename |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
109 |
sys.exit(2) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
110 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
111 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
112 |
# Print graph as PNG image |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
113 |
gvv = gv.readstring(dot) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
114 |
gv.layout(gvv,'circo') |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
115 |
gv.render(gvv,'png',filename + '.png') |