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.

# Manual highlighting¶

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]:

The plural version can take a list or tuple of state numbers (as above) or of Booleans (as below). In the latter case the indices of the True values give the states to highlight.

In [6]:
a.highlight_states([False, True, True], 5)

Out[6]:

# Saving to HOA 1.1¶

When saving to HOA format, the highlighting is only output if version 1.1 of the format is selected, because the headers spot.highlight.edges and spot.highlight.states contain dots, which are disallowed in version 1. Compare these two outputs:

In [7]:
print(a.to_str('HOA', '1'))
print()
print(a.to_str('HOA', '1.1'))

HOA: v1
States: 3
Start: 2
AP: 3 "a" "b" "c"
acc-name: Buchi
Acceptance: 1 Inf(0)
properties: trans-labels explicit-labels state-acc deterministic
properties: stutter-invariant terminal
--BODY--
State: 0 {0}
[t] 0
State: 1
[2] 0
[1&!2] 1
State: 2
[2] 0
[!0&1&!2] 1
[0&!2] 2
--END--

HOA: v1.1
States: 3
Start: 2
AP: 3 "a" "b" "c"
acc-name: Buchi
Acceptance: 1 Inf(0)
properties: trans-labels explicit-labels state-acc !complete
properties: deterministic stutter-invariant terminal
spot.highlight.states: 0 0 1 5 2 5
spot.highlight.edges: 2 1 4 1 5 1 6 2
--BODY--
State: 0 {0}
[t] 0
State: 1
[2] 0
[1&!2] 1
State: 2
[2] 0
[!0&1&!2] 1
[0&!2] 2
--END--


# Highlighting a run¶

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 [8]:
b = spot.translate('X (F(Ga <-> b) & GF!b)'); b

Out[8]:
In [9]:
r = b.accepting_run(); print(r)

Prefix:
4
|  1
0
|  !a & !b
Cycle:
1
|  !b	{0}


In [10]:
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 [11]:
b

Out[11]:

# Highlighting an accepting run in an automaton with arbitrary acceptance¶

This is not as easy as finding accepting runs in (generalized) Büchi automata, so let's have a few example for testing.

In [12]:
def show_accrun(string):
aut = spot.automaton(string)
run = aut.accepting_run()
run.highlight(5)
display(aut)

show_accrun("""
HOA: v1 States: 10 Start: 0 AP: 2 "a" "b" Acceptance: 5 (Inf(1) | (Fin(0)
& Inf(4)) | Fin(2)) & Fin(3) properties: trans-labels explicit-labels
trans-acc --BODY-- State: 0 [0&1] 9 {3} [!0&!1] 0 {3} [0&!1] 5 {0 1} State:
1 [0&!1] 9 {4} [0&1] 8 {3} State: 2 [!0&!1] 8 {0} [!0&1] 6 {2 4} [0&1]
2 {3} [!0&1] 7 State: 3 [0&!1] 2 {0 4} [!0&!1] 3 {1 3} [!0&1] 4 {0} State: 4
[0&!1] 5 {2} [0&1] 0 [!0&1] 1 {0} State: 5 [!0&!1] 0 {3} [!0&!1] 6 {3} State: 6
[0&1] 3 {2} [!0&1] 1 [0&1] 2 {0 1 3 4} State: 7 [0&1] 1 [!0&1] 7 {0 2}
State: 8 [!0&1] 7 [!0&!1] 9 {0} State: 9 [0&1] 8 {3} [0&!1] 5 [0&!1]
1 --END--
""")
show_accrun("""
HOA: v1 States: 10 Start: 0 AP: 2 "a" "b" Acceptance: 6 Fin(5) &
((Fin(1) & (Inf(3) | Inf(4))) | Fin(0) | Fin(2)) properties: trans-labels
explicit-labels trans-acc --BODY-- State: 0 [0&1] 8 {0} [0&!1] 6 {2}
State: 1 [!0&1] 9 {0 4 5} State: 2 [!0&1] 1 State: 3 [0&!1] 3 {2}
[0&1] 4 {3 5} State: 4 [0&1] 7 {5} [0&!1] 9 {2} [!0&1] 0 {0 2} State:
5 [!0&1] 1 [!0&1] 3 {2 3} State: 6 [0&!1] 8 {1 2 5} [!0&1] 7 {3} State:
7 [0&1] 2 {0} [!0&1] 5 State: 8 [0&!1] 3 {4 5} State: 9 [!0&1] 3 {1 2}
[0&1] 1 {4} [0&!1] 5 {2} --END--""")
show_accrun("""
HOA: v1 States: 4 properties: implicit-labels trans-labels no-univ-branch
deterministic complete tool: "ltl2dstar" "0.5.4" name: "i G F a G F b"
comment: "Union{Safra[NBA=2],Safra[NBA=2]}" acc-name: Rabin 2 Acceptance:
4 (Fin(0)&Inf(1))|(Fin(2)&Inf(3)) Start: 0 AP: 2 "a" "b" --BODY-- State:
0 {0} 1 0 3 2 State: 1 {1} 1 0 3 2 State: 2 {0 3} 1 0 3 2 State: 3 {1 3}
1 0 3 2 --END--
""")


# Highlighting from a product¶

Pretty often, accepting runs are found in a product but we want to display them on one of the original automata. This can be done by projecting the runs on those automata before displaying them.

In [13]:
left = spot.translate('a U b')
right = spot.translate('GFa')
display(left, right)

In [14]:
prod = spot.product(left, right); prod

Out[14]:
In [15]:
run = prod.accepting_run(); print(run)

Prefix:
1,0
|  !a & b
Cycle:
0,0
|  a	{0}


In [16]:
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 [17]:
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 [18]:
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)
print(run2)
display(prod2, left2, right2)

Prefix:
0 * 3
|  a & !b
1 * 2
|  a	{0}
1 * 1
|  a	{0}
1 * 0
|  a & b	{0}
Cycle:
1 * 4
|  a	{0,1}



# Highlighting nondeterminism¶

Sometimes its is hard to locate non-deterministic states inside a large automaton. Here are two functions that can help for that.

In [19]:
b = spot.translate('X (F(Ga <-> b) & GF!b)')
spot.highlight_nondet_states(b, 5)
spot.highlight_nondet_edges(b, 4)
b

Out[19]:

# Disappearing highlights¶

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 [20]:
spot.randomize(b); b

Out[20]:

# Highlighting with partial output¶

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 [21]:
spot.highlight_nondet_edges(b, 4)  # let's get those highlighted edges back
display(b, b.show('.<4'), b.show('.<2'))


# Highlighting languages¶

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 [22]:
aut = spot.translate('(b W Xa) & GF(c <-> Xb) | a', 'generic', 'det')
spot.highlight_languages(aut)
aut.show('.bas')

Out[22]: