In [1]:
import spot
spot.setup()
from IPython.display import display
This notebook shows you different ways in which states or transitions can be highlighted in Spot.
It should be noted that highlighting works using some special named properties: basically, two maps that are attached to the automaton, and associated state or edge numbers to color numbers. This named properties are fragile: they will be lost if the automaton is transformed into a new automaton, and they can become meaningless of the automaton is modified in place (e.g., if the transitions or states are reordered).
Nonetheless, highlighting is OK to use right before displaying or printing the automaton. The dot
and hoa
printer both know how to represent highlighted states and transitions.
In [2]:
a = spot.translate('a U b U c')
The #
option of print_dot()
can be used to display the internal number of each transition
In [3]:
a.show('.#')
Out[3]:
Using these numbers you can selectively hightlight some transitions. The second argument is a color number (from a list of predefined colors).
In [4]:
a.highlight_edges([2, 4, 5], 1)
Out[4]:
Note that these highlight_
functions work for edges and states, and come with both singular (changing the color of single state or edge) and plural versions.
They modify the automaton in place.
In [5]:
a.highlight_edge(6, 2).highlight_states((0, 1), 0)
Out[5]:
In [6]:
print(a.to_str('HOA', '1'))
print()
print(a.to_str('HOA', '1.1'))
One use of this highlighting is to highlight a run in an automaton.
The following few command generate an automaton, then an accepting run on this automaton, and highlight that accepting run on the automaton. Note that a run knows the automaton from which it was generated, so calling highlight()
will directly decorate that automaton.
In [7]:
b = spot.translate('X (F(Ga <-> b) & GF!b)'); b
Out[7]:
In [8]:
r = b.accepting_run(); r
Out[8]:
In [9]:
r.highlight(5) # the parameter is a color number
The call of highlight(5)
on the accepting run r
modified the original automaton b
:
In [10]:
b
Out[10]:
In [11]:
left = spot.translate('a U b')
right = spot.translate('GFa')
display(left, right)
In [12]:
prod = spot.product(left, right); prod
Out[12]:
In [13]:
run = prod.accepting_run(); run
Out[13]:
In [14]:
run.highlight(5)
# Note that by default project() needs to know on which side you project, but it cannot
# guess it. The left-side is assumed unless you pass True as a second argument.
run.project(left).highlight(5)
run.project(right, True).highlight(5)
In [15]:
display(prod, left, right)
The projection also works for products generated on-the-fly, but the on-the-fly product itself cannot be highlighted (it does not store states or transitions).
In [16]:
left2 = spot.translate('!b & FG a')
right2 = spot.translate('XXXb')
prod2 = spot.otf_product(left2, right2) # Note "otf_product()"
run2 = prod2.accepting_run()
run2.project(left2).highlight(5)
run2.project(right2, True).highlight(5)
display(run2, prod2, left2, right2)
In [17]:
b = spot.translate('X (F(Ga <-> b) & GF!b)')
spot.highlight_nondet_states(b, 5)
spot.highlight_nondet_edges(b, 4)
b
Out[17]:
As explained at the top of this notebook, named properties (such as highlights) are fragile, and you should not really on them being preserved across algorithms. In-place algorithm are probably the worst, because they might modify the automaton and ignore the attached named properties.
randomize()
is one such in-place algorithm: it reorder states or transitions of the automaton. By doing so it renumber the states and edges, and that process would completely invalidate the highlights information. Fortunately randomize()
know about highlights: it will preserve highlighted states, but it will drop all highlighted edges.
In [18]:
spot.randomize(b); b
Out[18]:
For simplicity, rendering of partial automata is actually implemented by copying the original automaton and marking some states as "incomplete". This also allows the same display code to work with automata generated on-the-fly. However since there is a copy, propagating the highlighting information requires extra work. Let's make sure it has been done:
In [19]:
spot.highlight_nondet_edges(b, 4) # let's get those highlighted edges back
display(b, b.show('.<4'), b.show('.<2'))
For deterministic automata, the function spot.highlight_languages()
can be used to highlight states that recognize the same language. This can be a great help in reading automata. States with a colored border share their language, and states with a black border all have a language different from all other states.
In [20]:
aut = spot.translate('(b W Xa) & GF(c <-> Xb) | a', 'generic', 'det')
spot.highlight_languages(aut)
aut.show('.bas')
Out[20]: