Commit da464d81 authored by Alexandre Duret-Lutz's avatar Alexandre Duret-Lutz

org: document explicit vs. on-the-fly

* doc/org/tut50.org: New file.
* doc/org/tut.org: Add it.
* NEWS: Mention it.
* doc/Makefile.am: Add tut50.org, and download plantuml.jar when needed.
* doc/org/.dir-locals.el.in, doc/org/init.el.in: Activate plantum.
* HACKING: Mention the Java dependency.
parent d7d6b409
...@@ -37,6 +37,7 @@ since the generated files they produce are distributed.) ...@@ -37,6 +37,7 @@ since the generated files they produce are distributed.)
ImageMagick ImageMagick
Python >= 3.3, IPython >= 2.3 Python >= 3.3, IPython >= 2.3
GraphViz GraphViz
Java >= 1.7 (needed to run PlantUML while generating the doc)
The following additional tools are used if they are present, or The following additional tools are used if they are present, or
only for certain operations (like releases): only for certain operations (like releases):
......
...@@ -171,6 +171,8 @@ New in spot 2.0.3a (not yet released) ...@@ -171,6 +171,8 @@ New in spot 2.0.3a (not yet released)
* A new example page shows how to test the equivalence of * A new example page shows how to test the equivalence of
two LTL/PSL formulas. https://spot.lrde.epita.fr/tut04.html two LTL/PSL formulas. https://spot.lrde.epita.fr/tut04.html
* A new page discusses explicit vs. on-the-fly interfaces for
exploring automata in C++. https://spot.lrde.epita.fr/tut50.html
* The concepts.html page now lists all named properties * The concepts.html page now lists all named properties
used by automata. used by automata.
......
...@@ -58,9 +58,12 @@ dist_pkgdata_DATA = $(srcdir)/spot.tag ...@@ -58,9 +58,12 @@ dist_pkgdata_DATA = $(srcdir)/spot.tag
org: org:
cd $(top_builddir) && $(MAKE) doc/org/init.el cd $(top_builddir) && $(MAKE) doc/org/init.el
rm -rf $(srcdir)/userdoc rm -rf $(srcdir)/userdoc
$(MAKE) org-man $(MAKE) org-man org/plantuml.jar
$(EMACS) --batch -Q -l org/init.el $(EMACS) --batch -Q -l org/init.el
org/plantuml.jar:
wget http://downloads.sourceforge.net/project/plantuml/plantuml.jar -O org/plantuml.jar
org-man: org-man:
mkdir -p $(srcdir)/userdoc/man mkdir -p $(srcdir)/userdoc/man
$(top_srcdir)/tools/man2html.pl $(top_srcdir)/bin/man $(srcdir)/userdoc/man $(top_srcdir)/tools/man2html.pl $(top_srcdir)/bin/man $(srcdir)/userdoc/man
...@@ -103,6 +106,7 @@ ORG_FILES = \ ...@@ -103,6 +106,7 @@ ORG_FILES = \
org/tut21.org \ org/tut21.org \
org/tut22.org \ org/tut22.org \
org/tut30.org \ org/tut30.org \
org/tut50.org \
org/upgrade2.org \ org/upgrade2.org \
org/satmin.org \ org/satmin.org \
org/satmin.tex \ org/satmin.tex \
...@@ -137,3 +141,5 @@ dist-hook: optipng ...@@ -137,3 +141,5 @@ dist-hook: optipng
.PHONY: optipng .PHONY: optipng
optipng: optipng:
-(optipng -v >/dev/null) && optipng -o5 $(distdir)/userdoc/*.png -(optipng -v >/dev/null) && optipng -o5 $(distdir)/userdoc/*.png
DISTCLEANFILES = org/plantuml.jar
...@@ -26,8 +26,10 @@ ...@@ -26,8 +26,10 @@
(org-babel-do-load-languages 'org-babel-load-languages (org-babel-do-load-languages 'org-babel-load-languages
'((sh . t) '((sh . t)
(python . t) (python . t)
(plantuml . t)
(dot . t) (dot . t)
(C . t))))) (C . t)))))
(org-plantuml-jar-path . "./plantuml.jar")
(org-confirm-babel-evaluate . nil) (org-confirm-babel-evaluate . nil)
(org-babel-python-command . "@PYTHON@") (org-babel-python-command . "@PYTHON@")
(org-babel-C++-compiler . "./g++wrap") (org-babel-C++-compiler . "./g++wrap")
......
...@@ -23,9 +23,10 @@ ...@@ -23,9 +23,10 @@
'((sh . t) '((sh . t)
(dot . t) (dot . t)
(python . t) (python . t)
(plantuml . t)
(C . t))) (C . t)))
(setq org-confirm-babel-evaluate nil) (setq org-confirm-babel-evaluate nil)
(setq org-plantuml-jar-path "./plantuml.jar")
(setq org-babel-python-command "@PYTHON@") (setq org-babel-python-command "@PYTHON@")
(setq org-babel-C++-compiler "./g++wrap") (setq org-babel-C++-compiler "./g++wrap")
(setq shell-file-name "@SHELL@") (setq shell-file-name "@SHELL@")
......
...@@ -33,6 +33,10 @@ three interfaces supported by Spot: shell commands, Python, or C++. ...@@ -33,6 +33,10 @@ three interfaces supported by Spot: shell commands, Python, or C++.
- [[file:tut21.org][Custom print of an automaton]] - [[file:tut21.org][Custom print of an automaton]]
- [[file:tut22.org][Creating an automaton by adding states and transitions]] - [[file:tut22.org][Creating an automaton by adding states and transitions]]
* Examples in C++ only
- [[file:tut50.org][Explicit vs. on-the-fly: two interfaces for exploring automata]]
* Examples in Python only * Examples in Python only
In directory =python/tests=, the [[file:install.org][Spot tarball]] contains a small In directory =python/tests=, the [[file:install.org][Spot tarball]] contains a small
......
# -*- coding: utf-8 -*-
#+TITLE: Explicit vs. on-the-fly: two interfaces for exploring automata
#+DESCRIPTION: Explanation of the explicit and on-the-fly automata interfaces in Spot
#+SETUPFILE: setup.org
#+HTML_LINK_UP: tut.html
When exploring automata (i.e., following its transition structure),
there are two different interfaces that can be used:
1. the *on-the-fly* =twa= interface, and
2. the *explicit* =twa_graph= interface.
To demonstrate the difference between the two interfaces, we will
write an small depth-first search that prints all states accessible
from the initial state of an automaton.
* The explicit interface
:PROPERTIES:
:CUSTOM_ID: explicit-interface
:END:
The explicit interface can only be used on =twa_graph= objects. In
this interface, states and edges are referred to by numbers that are
indices into state and edge vectors. This interface is lightweight,
and is the preferred interface for writing most automata algorithms in
Spot.
** How this interface works
The picture below gives a partial view of the classes involved:
#+BEGIN_SRC plantuml :file uml-explicit-classes.png
package stl {
class "Forward Iterator" <<Concept>>
hide "Forward Iterator" members
hide "Forward Iterator" circle
}
package spot {
package internal {
class "state_out<digraph<twa_graph_state, twa_graph_edge_data>>" {
unsigned t_
__
edge_iterator<digraph<twa_graph_state, twa_graph_edge_data>> begin()
edge_iterator<digraph<twa_graph_state, twa_graph_edge_data>> end()
}
class "edge_iterator<digraph<twa_graph_state, twa_graph_edge_data>>" {
unsigned t_
__
bool operator==(edge_iterator)
bool operator!=(edge_iterator)
edge_storage<twa_graph_edge_data>& operator*(edge_iterator)
edge_storage<twa_graph_edge_data>* operator->(edge_iterator)
edge_iterator operator++()
.. other methods hidden ..
}
"Forward Iterator" <|.. "edge_iterator<digraph<twa_graph_state, twa_graph_edge_data>>": model of
class "edge_storage<twa_graph_edge_data>" {
+unsigned src
+unsigned dst
+unsigned next_succ
}
class "distate_storage<twa_graph_state>" {
+unsigned succ
+unsigned succ_tail
}
}
class twa_graph {
+typedef digraph<twa_graph_state, twa_graph_edge_data> graph_t
__
+twa_graph(const bdd_dict_ptr&)
+graph_t& get_graph()
.. exploration ..
+unsigned get_init_state_number()
+internal::state_out<graph_t> out(unsigned src)
.. other methods hidden ..
}
twa <|-- twa_graph
abstract class twa
hide twa members
class "digraph<twa_graph_state, twa_graph_edge_data>" {
+internal::edge_storage<twa_graph_edge_data> edge_storage(unsigned s)
+internal::distate_storage<twa_graph_state> state_storage(unsigned s)
+internal::state_out<graph_t> out(unsigned src)
.. other details omitted ..
}
twa_graph *--> "1" "digraph<twa_graph_state, twa_graph_edge_data>"
class twa_graph_edge_data {
+bdd cond
+acc_cond::mark_t acc
}
"digraph<twa_graph_state, twa_graph_edge_data>" *--> "*" "edge_storage<twa_graph_edge_data>"
"digraph<twa_graph_state, twa_graph_edge_data>" *--> "*" "distate_storage<twa_graph_state>"
twa_graph_edge_data <|-- "edge_storage<twa_graph_edge_data>"
twa_graph_state <|-- "distate_storage<twa_graph_state>"
hide twa_graph_state members
class acc_cond
hide acc_cond members
twa *--> acc_cond
"state_out<digraph<twa_graph_state, twa_graph_edge_data>>" o--> "digraph<twa_graph_state, twa_graph_edge_data>"
"digraph<twa_graph_state, twa_graph_edge_data>" ...> "state_out<digraph<twa_graph_state, twa_graph_edge_data>>" : "create"
"state_out<digraph<twa_graph_state, twa_graph_edge_data>>" ...> "edge_iterator<digraph<twa_graph_state, twa_graph_edge_data>>" : "create"
"edge_iterator<digraph<twa_graph_state, twa_graph_edge_data>>" o--> "digraph<twa_graph_state, twa_graph_edge_data>"
}
#+END_SRC
#+RESULTS:
[[file:uml-explicit-classes.png]]
An ω-automaton can be defined as a labeled directed graph, plus an
initial state and an acceptance condition. The =twa_graph= of Spot
stores exactly these three components: the transition structure is
stored as an instance of =digraph= (directed graph), the initial state
is just a number, and the acceptance condition is an instance of
=acc_cond= which is actually inherited from the =twa= parent. You can
ignore the =twa= inheritance for now, we will discuss it when we talk
about [[#on-the-fly-interface][the on-the-fly interface in the next section]].
In this section we are discussing the "explicit interface", which is a
way of exploring the stored graph directly.
The =digraph= template class in Spot is parameterized by classes
representing additional data to store on state, and on edges. In the
case of a =twa_graph=, these extra data are implemented as
=twa_graph_state= (but we won't be concerned about this type) and
=twa_graph_edge_data=. The class =twa_graph_edge_data= has two
fields: =cond= is a BDD representing the label of the edge, and
=acc= represents the acceptance sets to which the edge belong.
The =digraph= stores a vector of states, and a vector of edges, but
both states and edges need to be equipped with field necessary to
represent the graph structure. In particular, a state holds two edges
numbers representing the first (=succ=) and last (=succ_tail=) edges
exiting the state (that "last edge" is only useful to append new
transitions, it is not used for iteration), and each edge has three
additional fields: =src= (the source state), =dst= (the destination
state), and =next_succ= (the index of the next edge leaving =src=, in
the edge vector). By way of template inheritance, these
=digraph=-fields are combined with the =twa_graph= specific fields, so
that all edges can be represented by an instance of
=std::vector<internal::edge_storage<twa_graph_edges_data>>=: each
element of this vector acts as a structure with 5 fields; likewise for
the state vector.
Calling =get_init_state_number()= will return a state number which is
just an index into the state vector of the underlying graph.
From a state number =s=, it is possible to iterate over all successors
by doing a =for= loop on =out(s)=, as in:
#+BEGIN_SRC C++ :results verbatim :exports both
#include <iostream>
#include <spot/twa/twagraph.hh>
#include <spot/tl/parse.hh>
#include <spot/twaalgos/translate.hh>
void example(spot::const_twa_graph_ptr aut)
{
unsigned s = aut->get_init_state_number();
for (auto& e: aut->out(s))
std::cout << e.src << "->" << e.dst << '\n';
}
int main()
{
// Create a small example automaton
spot::parsed_formula pf = spot::parse_infix_psl("FGa | FGb");
if (pf.format_errors(std::cerr))
return 1;
example(spot::translator().run(pf.f));
}
#+END_SRC
#+RESULTS:
: 2->0
: 2->1
: 2->2
In the above lines, =aut->out(s)= delegates to
=aut->get_graphs().out(s)= and returns a =state_out<graph_t>=
instance, which is a small temporary object masquerading as an STL
container with =begin()= and =end()= methods. The ranged-for loop
syntax of C++ works exactly as if we had typed
#+BEGIN_SRC C++
// You could write this, but why not let the compiler do it for you?
// In any case, do not spell out the types of tmp and i, as those
// should be considered internal details.
void example(spot::const_twa_graph_ptr aut)
{
unsigned s = aut->get_init_state_number();
auto tmp = aut->get_graph().out(s);
for (auto i = tmp.begin(), end = tmp.end(); i != end; ++i)
std::cout << i->src << "->" << i->dst << '\n';
}
#+END_SRC
In the above =example()= function, the iterators =i= and =end= are
instance of the =internal::edge_iterator<spot::twa_graph::graph_t>=
class, which redefines enough operators to act like an STL Foward
Iterator over all outgoing edges of =s=. Note that the =tmp= and =i=
objects hold a pointer to the graph, but it does not really matters
because the compiler will optimize this away.
In fact after operators are inlined and useless temporary variables
removed, the above loop compiles to something equivalent to this:
#+BEGIN_SRC C++
// You could also write this lower-level version, and that sometimes
// helps (e.g., if you want to pause the loop and then resume it, as
// we will do later).
void example(spot::const_twa_graph_ptr aut)
{
unsigned s = aut->get_init_state_number();
auto& g = aut->get_graph();
unsigned b = g.state_storage(s).succ; // first edge of state s
while (b)
{
auto& e = g.edge_storage(b);
std::cout << e.src << "->" << e.dst << '\n';
b = e.next_succ;
}
}
#+END_SRC
Note that ~next_succ==0~ marks the last edge in a successor group;
this is why edge numbers start at 1.
Despite the various levels of abstractions, these three loops compile
to exactly the same machine code.
** Recursive DFS
Let us write a DFS using this interface. A recursive version is easy:
we call =dfs_rec()= from the initial state, that function updates a
vector of visited states in order to not visit them twice, and recurse
on all successors of the given state.
#+BEGIN_SRC C++ :results verbatim :exports both
#include <iostream>
#include <spot/twa/twagraph.hh>
#include <spot/tl/parse.hh>
#include <spot/twaalgos/translate.hh>
void dfs_rec(spot::const_twa_graph_ptr aut, unsigned state, std::vector<bool>& seen)
{
seen[state] = true;
for (auto& e: aut->out(state))
{
std::cout << e.src << "->" << e.dst << '\n';
if (!seen[e.dst])
dfs_rec(aut, e.dst, seen);
}
}
void dfs(spot::const_twa_graph_ptr aut)
{
std::vector<bool> seen(aut->num_states());
dfs_rec(aut, aut->get_init_state_number(), seen);
}
int main()
{
// Create a small example automaton
spot::parsed_formula pf = spot::parse_infix_psl("FGa | FGb");
if (pf.format_errors(std::cerr))
return 1;
dfs(spot::translator().run(pf.f));
}
#+END_SRC
#+RESULTS:
: 2->0
: 0->0
: 2->1
: 1->1
: 2->2
** Iterative DFS (two versions)
Recursive graph algorithms are usually avoided, especially if large
graphs should be processed.
By maintaining a stack of states to process, we can visit all
accessible transitions in a "DFS-ish" way, but without producing
exactly the same output as above.
#+BEGIN_SRC C++ :results verbatim :exports both
#include <iostream>
#include <stack>
#include <spot/twa/twagraph.hh>
#include <spot/tl/parse.hh>
#include <spot/twaalgos/translate.hh>
void almost_dfs(spot::const_twa_graph_ptr aut)
{
std::vector<bool> seen(aut->num_states());
std::stack<unsigned> todo;
auto push_state = [&](unsigned state)
{
todo.push(state);
seen[state] = true;
};
push_state(aut->get_init_state_number());
while (!todo.empty())
{
unsigned s = todo.top();
todo.pop();
for (auto& e: aut->out(s))
{
std::cout << e.src << "->" << e.dst << '\n';
if (!seen[e.dst])
push_state(e.dst);
}
}
}
int main()
{
// Create a small example automaton
spot::parsed_formula pf = spot::parse_infix_psl("FGa | FGb");
if (pf.format_errors(std::cerr))
return 1;
almost_dfs(spot::translator().run(pf.f));
}
#+END_SRC
#+RESULTS:
: 2->0
: 2->1
: 2->2
: 1->1
: 0->0
So this still prints all accessible edges, but not in the same order
as our recursive DFS. This is because this version prints all the
outgoing edges of one state before processing the successors.
For many algorithms, this different ordering makes no difference, and
this order should even be preferred: groups of transitions leaving
the same state are usually stored consecutively in memory, so they
are better processed in chain, rather than trying to follow exactly
the order we would get from a recursive DFS, which will jump at
random places in the edge vector.
In fact writing the iterative equivalent of the recursive =dfs()= is
a bit challenging if we do not want to be wasteful. Clearly, we
can no longer use the ranged-for loop, because we need to process
one edge, save the current iterator on a stack to process the
successor, and finally advance the iterator once we pop back to it.
Given the above data structure, it is tempting to use a
=std::stack<spot::internal::edge_iterator<spot::twa_graph::graph_t>>=,
but that is a bad idea. Remember that those =internal::edge_iterator=
are meant to be short-lived temporary objects, and they all store a
pointer to graph. We do not want to store multiple copies of this
pointer on our stack. Besides, *you* do not want to ever write
=spot::internal= in your code.
So a better implementation (better than the
=std::stack<spot::internal::edge_iterator<...>>= suggestion) would be
to maintain a stack of edge numbers. Indeed, each edge
stores the number of the next edge leaving the same source
state, so this is enough to remember where we are.
#+BEGIN_SRC C++ :results verbatim :exports both
#include <iostream>
#include <stack>
#include <spot/twa/twagraph.hh>
#include <spot/tl/parse.hh>
#include <spot/twaalgos/translate.hh>
void dfs(spot::const_twa_graph_ptr aut)
{
std::vector<bool> seen(aut->num_states());
std::stack<unsigned> todo; // Now storing edges numbers
auto& gr = aut->get_graph();
auto push_state = [&](unsigned state)
{
todo.push(gr.state_storage(state).succ);
seen[state] = true;
};
push_state(aut->get_init_state_number());
while (!todo.empty())
{
unsigned edge = todo.top();
todo.pop();
if (edge == 0U) // No more outgoing edge
continue;
auto& e = gr.edge_storage(edge);
todo.push(e.next_succ); // Prepare next sibling edge.
if (!seen[e.dst])
push_state(e.dst);
std::cout << e.src << "->" << e.dst << '\n';
}
}
int main()
{
// Create a small example automaton
spot::parsed_formula pf = spot::parse_infix_psl("FGa | FGb");
if (pf.format_errors(std::cerr))
return 1;
dfs(spot::translator().run(pf.f));
}
#+END_SRC
#+RESULTS:
: 2->0
: 0->0
: 2->1
: 1->1
: 2->2
This version is functionally equivalent to the recursive one, but its
implementation requires more knowledge of the graph data structure
than both the recursive and the =almost_dfs()= version.
* The on-the-fly =twa= interface
:PROPERTIES:
:CUSTOM_ID: on-the-fly-interface
:END:
The =twa= class defines an abstract interface suitable for on-the-fly
exploration of an automaton. Subclasses of =twa= need not represent
the entire automaton in memory; if they prefer, they can compute it as
it is explored.
Naturally =twa_graph=, even if they store the automaton graph
explicitly, are subclasses of =twa=, so they also implement the
on-the-fly interface, even if they do not have to compute anything.
** How this interface works
The following class diagram has two classes in common with the
previous one: =twa= and =twa_graph=, but this time the focus is on the
abstract interface defined in =twa=, not in the explicit interface
defined in =twa_graph=.
#+BEGIN_SRC plantuml :file uml-otf-classes.png
package spot {
package internal {
class succ_iterable {
+internal::succ_iterator begin()
+internal::succ_iterator end()
}
class succ_iterator {
succ_iterator(twa_succ_iterator*)
bool operator==(succ_iterator) const
bool operator!=(succ_iterator) const
const twa_succ_iterator* operator*() const
void operator++()
}
}
class acc_cond
hide acc_cond members
together {
abstract class twa {
#twa_succ_iterator* iter_cache_
#bdd_dict_ptr dict_
__
#twa(const bdd_dict_ptr&)
.. exploration ..
+{abstract}state* get_init_state()
+{abstract}twa_succ_iterator* succ_iter(state*)
+internal::succ_iterable succ(const state*)
+void release_iter(twa_succ_iterator*)
.. state manipulation ..
+{abstract} std::string format_state(const state*)
+state* project_state(const state*, const const_twa_ptr&)
.. other methods not shown..
}
abstract class twa_succ_iterator {
.. iteration ..
{abstract}+bool first()
{abstract}+bool next()
{abstract}+bool done()
.. inspection ..
{abstract}+const state* dst()
{abstract}+bdd cond()
{abstract}+acc_cond::mark_t acc()
}
abstract class state {
+{abstract}int compare(const state*) const
+{abstract}size_t hash() const
+{abstract}state* clone() const
+void destroy() const
#~state()
}
}
class twa_graph
twa <|-- twa_graph
twa *--> acc_cond
class twa_graph_state
hide twa_graph members
hide twa_graph_state members
hide twa_graph_succ_iterator members
twa_succ_iterator <|-- twa_graph_succ_iterator
succ_iterable o--> twa
twa_succ_iterator <--* succ_iterable
twa_succ_iterator <--o succ_iterator
twa ...> succ_iterable : "create"
succ_iterable ...> succ_iterator : "create"