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