d9bae6d6e516e52b902e9c2143a6954d652b656c
3 generates aut for jtorx from dot file learned with learnlib
7 note: dot file uses I for input instead of ? and O for output instead of !
9 # Author: Harco Kuppens
12 import sys
, re
, pprint
# modules from standard lib (python 2.6 and later)
16 def get_lts_from_dotfile(dot_file
):
17 """ Get labeled transition system from graphviz dot file
20 - describes a digraph with labels
21 - encodes the start state with the color='red' attribute
22 note: this corresponds with the highlighted state in learnlib API
24 Returns: [start_state,transions]
26 - start_state: start state label
27 - transitions: list of transitions
38 # line in dot: __start0 -> s0;
40 if line
.find('->') != -1:
41 if line
.find('__start') != -1:
42 start_state
=line
[line
.find('->')+2:].strip(" ;\t\n")
47 # line in dot: s5 -> s5 [label="ARTREG 20013226 / 531"];
50 if line
.find('__start') != -1:
52 if line
.find('->') != -1:
53 transitions
.append(line
)
55 # throw away transitions with the keywords : quiescence or inconsistency or undefined
56 #transitions = [ t for t in transitions if ( 'quiescence' not in t ) and ( 'inconsistency' not in t ) and ( 'undefined' not in t )]
59 regexpr_transition
=re
.compile(r
'\s*(\w*)\s*-\>\s*(\w*)\s*\[label=\"(.*)\"\]')
60 regexpr_tag
=re
.compile(r
'<[^>]+>')
61 for transition
in transitions
:
62 match
=regexpr_transition
.match(transition
)
65 label
=regexpr_tag
.sub('',match
[2])
74 states
.add(t
['source'])
75 states
.add(t
['target'])
78 return [start_state
,states
,trans_out
]
81 def parse_labels_of_mealy_lts(transitions
):
82 """Parse labels of labeled transition system
87 [inputstr
,outputstr
]=label
.split('/')
89 'source' : t
['source'],
90 'target' : t
['target'],
96 def split_io_transitions_in_separate_input_and_output_transition(io_transitions
,nr_states
):
97 """Split transitions with both an input and output event into two transitions
99 Makes two sequential transitions with a dummy state in between:
100 - dummy state <midstate> is labeled :
103 <source> -> <midstate> for <input>
104 - second transition :
105 <midstate> -> <target> for <output>
109 for t
in io_transitions
:
110 midstate
= 'm' + str(id)
112 'source': t
['source'],
114 'label' : "?" + t
['input'].strip(),
118 'target': t
['target'],
119 'label' : "!" + t
['output'].strip(),
125 states
.add(t
['source'])
126 states
.add(t
['target'])
128 return [states
,trans_out
]
131 def transitions2aut(transitions
,first_state
,nr_of_states
):
132 nr_of_transitions
=len(transitions
)
133 strings
=[ "des(" + first_state
[1:] + "," + str(nr_of_transitions
) + "," + str(nr_of_states
) +")"]
134 for t
in transitions
:
135 #aut_edge ::= "(" start_state "," label "," end_state ")"
136 strings
.append("("+t
['source'][1:] + "," + '"' + t
['label'] + '"' + "," + t
['target'][1:] + ")" )
138 return "\n".join(strings
)
141 def dot2aut(dot_filename_in
):
143 from mealy machine in a .dot file written by DotUtil.write of learnlib
144 we create an .aut file containing an lts where input and output each
145 have its own labeled transition. An input transition has a
146 label starting with '?' and an output transition has a label
151 if dot_filename_in
[-4:].lower() != '.dot':
152 print "Problem: file '"+ dot_filename_in
+ "' is not a dot file!!"
157 [start_state
,states
,transitions
]=get_lts_from_dotfile(dot_filename_in
)
161 io_transitions
=parse_labels_of_mealy_lts(transitions
) # each transition has input and output
162 [states
,transitions
]=split_io_transitions_in_separate_input_and_output_transition(io_transitions
,len(states
)) # each transition only has label again
164 #pprint.pprint(start_state)
165 #pprint.pprint(states)
166 #pprint.pprint(transitions)
168 result
=transitions2aut(transitions
,start_state
,len(states
))
170 aut_filename
=dot_filename_in
[:-4] + ".aut"
172 f
=open(aut_filename
,'w')
176 print "written file : " + aut_filename
179 if __name__
== "__main__":
180 dot2aut(*sys
.argv
[1:])