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

python: port the tut22.org example to Python

* wrap/python/spot_impl.i: Extend acc_cond::mark_t to with a constructor
that takes a vector.
* doc/org/tut22.org: Add a Python version.
* doc/org/tut.org: Adjust the list, we don't have any C++-specific
example.
* NEWS: Mention it.
parent 9313222e
...@@ -39,6 +39,10 @@ New in spot 1.99.6a (not yet released) ...@@ -39,6 +39,10 @@ New in spot 1.99.6a (not yet released)
twa_graph::out() C++ function) is now possible in Python. See twa_graph::out() C++ function) is now possible in Python. See
https://spot.lrde.epita.fr/tut21.html for a demonstration. https://spot.lrde.epita.fr/tut21.html for a demonstration.
* Membership to acceptance sets can be specified using Python list
when calling for instance the Python version of twa_graph::new_edge().
See https://spot.lrde.epita.fr/tut22.html for a demonstration.
Documentation: Documentation:
* There is a new page explaining how to compile example programs and * There is a new page explaining how to compile example programs and
......
...@@ -26,13 +26,7 @@ three interfaces supported by Spot: shell commands, Python, or C++. ...@@ -26,13 +26,7 @@ three interfaces supported by Spot: shell commands, Python, or C++.
- [[file:tut03.org][Constructing and transforming formulas]] - [[file:tut03.org][Constructing and transforming formulas]]
- [[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]]
* Examples in C++ only
The following examples are too low-level to be implemented in shell or
Python (at least at the moment), so they are purely C++ so far.
- [[file:tut22.org][Creating an automaton in C++]]
* Examples in Python only * Examples in Python only
......
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
#+TITLE: Creating an automaton in C++ #+TITLE: Creating an automaton by adding states and transitions
#+SETUPFILE: setup.org #+SETUPFILE: setup.org
#+HTML_LINK_UP: tut.html #+HTML_LINK_UP: tut.html
This example demonstrates how to create an automaton in C++, and then print it. This example demonstrates how to create an automaton and then print it.
* C++
#+BEGIN_SRC C++ :results verbatim :exports both :wrap SRC hoa #+BEGIN_SRC C++ :results verbatim :exports both :wrap SRC hoa
#include <iostream> #include <iostream>
...@@ -12,8 +14,8 @@ This example demonstrates how to create an automaton in C++, and then print it. ...@@ -12,8 +14,8 @@ This example demonstrates how to create an automaton in C++, and then print it.
int main(void) int main(void)
{ {
// The dict is used to maintain the correspondence between the // The bdd_dict is used to maintain the correspondence between the
// atomic propositions and the BDD variables to label the edges of // atomic propositions and the BDD variables that label the edges of
// the automaton. // the automaton.
spot::bdd_dict_ptr dict = spot::make_bdd_dict(); spot::bdd_dict_ptr dict = spot::make_bdd_dict();
// This creates an empty automaton that we have yet to fill. // This creates an empty automaton that we have yet to fill.
...@@ -31,10 +33,9 @@ This example demonstrates how to create an automaton in C++, and then print it. ...@@ -31,10 +33,9 @@ This example demonstrates how to create an automaton in C++, and then print it.
// States are numbered from 0. // States are numbered from 0.
aut->new_states(3); aut->new_states(3);
// new_edge() takes 3 mandatory parameters: // new_edge() takes 3 mandatory parameters: source state,
// source state, destination state, label // destination state, and label. A last optional parameter can be
// and a last optional parameter can be used // used to specify membership to acceptance sets.
// to specify membership to acceptance sets.
aut->new_edge(0, 1, p1); aut->new_edge(0, 1, p1);
aut->new_edge(1, 1, p1 & p2, {0}); aut->new_edge(1, 1, p1 & p2, {0});
aut->new_edge(1, 2, p2, {1}); aut->new_edge(1, 2, p2, {1});
...@@ -65,3 +66,61 @@ State: 2 ...@@ -65,3 +66,61 @@ State: 2
[0 | 1] 1 {0 1} [0 | 1] 1 {0 1}
--END-- --END--
#+END_SRC #+END_SRC
* Python
#+BEGIN_SRC python :results output :exports both :wrap SRC hoa
import spot
import buddy
# The bdd_dict is used to maintain the correspondence between the
# atomic propositions and the BDD variables that label the edges of
# the automaton.
bdict = spot.make_bdd_dict();
# This creates an empty automaton that we have yet to fill.
aut = spot.make_twa_graph(bdict)
# Since a BDD is associated to every atomic proposition, the register_ap()
# function returns a BDD variable number that can be converted into a BDD using
# bdd_ithvar() from the BuDDy library.
p1 = buddy.bdd_ithvar(aut.register_ap("p1"))
p2 = buddy.bdd_ithvar(aut.register_ap("p2"))
# Set the acceptance condition of the automaton to Inf(0)&Inf(1)
aut.set_generalized_buchi(2)
# States are numbered from 0.
aut.new_states(3)
# new_edge() takes 3 mandatory parameters: source state, destination state, and
# label. A last optional parameter can be used to specify membership to
# acceptance sets. In the Python version, the list of acceptance sets the
# transition belongs to should be specified as a list.
aut.new_edge(0, 1, p1)
aut.new_edge(1, 1, p1 & p2, [0])
aut.new_edge(1, 2, p2, [1]);
aut.new_edge(2, 1, p1 | p2, [0, 1]);
# Print the resulting automaton.
print(aut.to_str('hoa'))
#+END_SRC
#+RESULTS:
#+BEGIN_SRC hoa
HOA: v1
States: 3
Start: 0
AP: 2 "p1" "p2"
acc-name: generalized-Buchi 2
Acceptance: 2 Inf(0)&Inf(1)
properties: trans-labels explicit-labels trans-acc
--BODY--
State: 0
[0] 1
State: 1
[0&1] 1 {0}
[1] 2 {1}
State: 2
[0 | 1] 1 {0 1}
--END--
#+END_SRC
...@@ -317,6 +317,7 @@ namespace swig ...@@ -317,6 +317,7 @@ namespace swig
namespace std { namespace std {
%template(liststr) list<std::string>; %template(liststr) list<std::string>;
%template(vectorformula) vector<spot::formula>; %template(vectorformula) vector<spot::formula>;
%template(vectorunsigned) vector<unsigned>;
%template(atomic_prop_set) set<spot::formula>; %template(atomic_prop_set) set<spot::formula>;
%template(relabeling_map) map<spot::formula, spot::formula>; %template(relabeling_map) map<spot::formula, spot::formula>;
} }
...@@ -330,6 +331,7 @@ namespace std { ...@@ -330,6 +331,7 @@ namespace std {
%include <spot/twa/bdddict.hh> %include <spot/twa/bdddict.hh>
%include <spot/twa/bddprint.hh> %include <spot/twa/bddprint.hh>
%include <spot/twa/fwd.hh> %include <spot/twa/fwd.hh>
%implicitconv spot::acc_cond::mark_t;
%feature("flatnested") spot::acc_cond::mark_t; %feature("flatnested") spot::acc_cond::mark_t;
%feature("flatnested") spot::acc_cond::acc_code; %feature("flatnested") spot::acc_cond::acc_code;
%apply bool* OUTPUT {bool& max, bool& odd}; %apply bool* OUTPUT {bool& max, bool& odd};
...@@ -439,6 +441,14 @@ namespace std { ...@@ -439,6 +441,14 @@ namespace std {
std::string __str__() { return spot::str_psl(*self); } std::string __str__() { return spot::str_psl(*self); }
} }
%extend spot::acc_cond::mark_t {
// http://comments.gmane.org/gmane.comp.programming.swig/14822
mark_t(const std::vector<unsigned>& f)
{
return new spot::acc_cond::mark_t(f.begin(), f.end());
}
}
%extend spot::twa { %extend spot::twa {
void set_name(std::string name) void set_name(std::string name)
{ {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment