Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Rick Smetsers
z3gi
Commits
140c4d65
Commit
140c4d65
authored
Jul 01, 2017
by
Paul Fiterau Brostean
Browse files
model export to dot
parent
158e9d26
Changes
1
Hide whitespace changes
Inline
Side-by-side
z3gi/model/parsing.py
0 → 100644
View file @
140c4d65
import
sys
from
model
import
*
from
model.fa
import
IOTransition
from
model.ra
import
RATransition
,
IORATransition
__all__
=
[
"to_dot"
,
]
sep
=
"_"
def
w_state
(
automaton
:
Automaton
,
state
):
if
isinstance
(
automaton
,
Acceptor
):
return
sep
.
join
(
state
,
str
(
automaton
.
is_accepting
(
state
)))
return
state
def
w_trans
(
automaton
:
Automaton
,
trans
:
Transition
):
input_elements
=
[
str
(
trans
.
start_label
)]
if
isinstance
(
trans
,
RATransition
):
input_elements
.
extend
([
str
(
trans
.
guard
),
str
(
trans
.
assignment
)])
input_label
=
sep
.
join
(
input_elements
)
output_elements
=
[]
if
isinstance
(
automaton
,
Acceptor
):
output_elements
.
append
(
str
(
automaton
.
is_accepting
(
trans
.
end_state
)))
elif
isinstance
(
trans
,
IOTransition
):
output_elements
.
append
(
trans
.
output
)
elif
isinstance
(
trans
,
IORATransition
):
output_elements
.
extend
(
map
(
lambda
x
:
str
(
x
),
[
trans
.
output_label
,
trans
.
output_mapping
,
trans
.
output_assignment
]))
output_label
=
sep
.
join
(
output_elements
)
return
'{0} -> {1} [label="{2}/{3}"]'
.
format
(
w_state
(
automaton
,
trans
.
start_state
),
w_state
(
automaton
,
trans
.
end_state
),
input_label
,
output_label
)
def
to_dot
(
automaton
:
Automaton
,
file_name
:
str
):
"""For an automaton produces a .dot representation"""
f
=
open
(
file_name
,
'w'
)
if
file_name
is
not
None
else
sys
.
stdout
print
(
'digraph g {
\n
'
,
file
=
f
)
for
state
in
automaton
.
states
():
print
(
'
\t
%s;'
%
w_state
(
automaton
,
state
),
file
=
f
)
for
state
in
automaton
.
states
():
for
trans
in
automaton
.
transitions
(
state
):
print
(
"
\t
%s;"
%
w_trans
(
automaton
,
trans
),
file
=
f
)
print
(
'}'
,
file
=
f
)
if
file_name
is
not
None
:
f
.
close
()
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment