spot/doc/org/tut24.org
Alexandre Duret-Lutz 7dfeda8e77 org: simplify babel blocks using #+PROPERTY: header-args
This feature is in Org 9, which is already required.

* doc/org/autcross.org, doc/org/autfilt.org, doc/org/compile.org,
doc/org/concepts.org, doc/org/csv.org, doc/org/dstar2tgba.org,
doc/org/genaut.org, doc/org/genltl.org, doc/org/hierarchy.org,
doc/org/hoa.org, doc/org/ioltl.org, doc/org/ltl2tgba.org,
doc/org/ltl2tgta.org, doc/org/ltlcross.org, doc/org/ltldo.org,
doc/org/ltlfilt.org, doc/org/ltlgrind.org, doc/org/ltlsynt.org,
doc/org/oaut.org, doc/org/randaut.org, doc/org/randltl.org,
doc/org/satmin.org, doc/org/setup.org, doc/org/tools.org,
doc/org/tut01.org, doc/org/tut02.org, doc/org/tut03.org,
doc/org/tut04.org, doc/org/tut10.org, doc/org/tut11.org,
doc/org/tut12.org, doc/org/tut20.org, doc/org/tut21.org,
doc/org/tut22.org, doc/org/tut23.org, doc/org/tut24.org,
doc/org/tut30.org, doc/org/tut31.org, doc/org/tut50.org,
doc/org/upgrade2.org: Simplify SRC block setups for sh, python and
C++.  Also fix a few typos and examples along the way.
2019-04-17 17:30:56 +02:00

241 lines
6.6 KiB
Org Mode

# -*- coding: utf-8 -*-
#+TITLE: Iterating over alternating automata
#+DESCRIPTION: Code example for iterating of alternating ω-automata in Spot
#+INCLUDE: setup.org
#+HTML_LINK_UP: tut.html
#+PROPERTY: header-args:python :results output :exports both
#+PROPERTY: header-args:C+++ :results verbatim :noweb strip-export
Alternating automata can be explored in a very similar way as
non-alternating automata. Most of the code from our [[file:tut21.org][custom automaton
printer]] will still work; the only problem is with universal edges.
We will use the following example automaton as input (it is just a
slight variation over an [[file:tut23.org][alternating automaton created previously]] to
demonstrate a universal initial state).
#+NAME: tut24in
#+BEGIN_SRC hoa
HOA: v1
States: 3
Start: 0&1
AP: 1 "a"
acc-name: co-Buchi
Acceptance: 1 Fin(0)
--BODY--
State: 0
[0] 0
[!0] 0&1
State: 1
[!0] 1 {0}
[0] 2
State: 2
[t] 2
--END--
#+END_SRC
#+NAME: tut24dot
#+BEGIN_SRC sh :results verbatim :exports none :noweb strip-export
cat >tut24.hoa <<EOF
<<tut24in>>
EOF
autfilt --dot tut24.hoa
#+END_SRC
#+BEGIN_SRC dot :file tut24in.svg :var txt=tut24dot :exports results
$txt
#+END_SRC
#+RESULTS:
[[file:tut24in.svg]]
* C++
Let us assume that this automaton has been loaded in variable =aut=,
and that we run the following code, similar to what we did in the
[[file:tut21.org][custom automaton printer]].
#+NAME: nonalt-body
#+BEGIN_SRC C++ :export code
std::cout << "Initial state: " << aut->get_init_state_number() << '\n';
const spot::bdd_dict_ptr& dict = aut->get_dict();
unsigned n = aut->num_states();
for (unsigned s = 0; s < n; ++s)
{
std::cout << "State " << s << ":\n";
for (auto& t: aut->out(s))
{
std::cout << " edge(" << t.src << " -> " << t.dst << ")\n label = ";
spot::bdd_print_formula(std::cout, dict, t.cond);
std::cout << "\n acc sets = " << t.acc << '\n';
}
}
#+END_SRC
#+NAME: nonalt-main
#+BEGIN_SRC C++ :exports none
#include <string>
#include <iostream>
#include <spot/parseaut/public.hh>
#include <spot/twaalgos/hoa.hh>
#include <spot/twa/bddprint.hh>
void custom_print(spot::twa_graph_ptr& aut);
int main()
{
spot::parsed_aut_ptr pa = parse_aut("tut24.hoa", spot::make_bdd_dict());
if (pa->format_errors(std::cerr))
return 1;
// This cannot occur when reading a never claim, but
// it could while reading a HOA file.
if (pa->aborted)
{
std::cerr << "--ABORT-- read\n";
return 1;
}
custom_print(pa->aut);
}
#+END_SRC
#+NAME: nonalt-one
#+BEGIN_SRC C++ :exports none
<<nonalt-main>>
void custom_print(spot::twa_graph_ptr& aut)
{
<<nonalt-body>>
}
#+END_SRC
# temporary fix for an issue in Org 9.2, see
# http://lists.gnu.org/archive/html/emacs-orgmode/2019-01/msg00226.html
#+BEGIN_SRC text :noweb yes
<<nonalt-one()>>
#+END_SRC
This output seems correct only for non-universal edges. The reason is
that Spot always store all edges as a tuple (src,dst,label,acc sets),
but universal edges are indicated by setting the most significant bit
of the destination (or of the initial state).
The "universality" of an edge can be tested using the
=twa_graph::is_univ_dest()= method: it takes a destination state as
input, as in =aut->is_univ_dest(t.dst)= or
=aut->is_univ_dest(aut->get_init_state_number())=. For convenience
this method can also be called on a edge, as in =aut->is_univ_dest(t)=.
The set of destination states of a universal edge can be iterated over
via the =twa_graph::univ_dests()= method. This takes either a
destination state (=twa_graph::univ_dests(t.dst)=) or more simply an
edge (=twa_graph::univ_dests(t)=). The =univ_dests()= method will
also work on non-universal edges, but in this case it will simply
iterate on the given state.
Therefor in order to print the universal destinations of any universal
edge in an alternating automaton, we can use =univ_dests()=
unconditionally. In this example, we simply call =is_univ_dest()= to
decide whether to enclose the destinations in braces.
#+NAME: nonalt-body2
#+BEGIN_SRC C++ :export code
unsigned init = aut->get_init_state_number();
std::cout << "Initial state:";
if (aut->is_univ_dest(init))
std::cout << " {";
for (unsigned i: aut->univ_dests(init))
std::cout << ' ' << i;
if (aut->is_univ_dest(init))
std::cout << " }";
std::cout << '\n';
const spot::bdd_dict_ptr& dict = aut->get_dict();
unsigned n = aut->num_states();
for (unsigned s = 0; s < n; ++s)
{
std::cout << "State " << s << ":\n";
for (auto& t: aut->out(s))
{
std::cout << " edge(" << t.src << " ->";
if (aut->is_univ_dest(t))
std::cout << " {";
for (unsigned dst: aut->univ_dests(t))
std::cout << ' ' << dst;
if (aut->is_univ_dest(t))
std::cout << " }";
std::cout << ")\n label = ";
spot::bdd_print_formula(std::cout, dict, t.cond);
std::cout << "\n acc sets = " << t.acc << '\n';
}
}
#+END_SRC
#+NAME: nonalt-two
#+BEGIN_SRC C++ :exports none
<<nonalt-main>>
void custom_print(spot::twa_graph_ptr& aut)
{
<<nonalt-body2>>
}
#+END_SRC
# temporary fix for an issue in Org 9.2, see
# http://lists.gnu.org/archive/html/emacs-orgmode/2019-01/msg00226.html
#+BEGIN_SRC text :noweb yes
<<nonalt-two()>>
#+END_SRC
* Python
Here is the Python version of this code:
#+BEGIN_SRC python
import spot
aut = spot.automaton("tut24.hoa")
bdict = aut.get_dict()
init = aut.get_init_state_number()
ui = aut.is_univ_dest(init)
print("Initial states: {}{}{}".format("{ " if ui else "",
" ".join(map(str, aut.univ_dests(init))),
" }" if ui else ""))
for s in range(0, aut.num_states()):
print("State {}:".format(s))
for t in aut.out(s):
ud = aut.is_univ_dest(t)
print(" edge({} -> {}{}{})".format(t.src,
"{ " if ud else "",
" ".join(map(str, aut.univ_dests(t))),
" }" if ud else ""))
print(" label =", spot.bdd_format_formula(bdict, t.cond))
print(" acc sets =", t.acc)
#+END_SRC
#+RESULTS:
#+begin_example
Initial states: { 0 1 }
State 0:
edge(0 -> 0)
label = a
acc sets = {}
edge(0 -> { 0 1 })
label = !a
acc sets = {}
State 1:
edge(1 -> 1)
label = !a
acc sets = {0}
edge(1 -> 2)
label = a
acc sets = {}
State 2:
edge(2 -> 2)
label = 1
acc sets = {}
#+end_example
#+BEGIN_SRC sh :results silent :exports results
rm -f tut24.hoa
#+END_SRC