Commit 16204e8e authored by Alexandre Duret-Lutz's avatar Alexandre Duret-Lutz
Browse files

python: better bindings for testing automata

* src/taalgos/dotty.cc, src/taalgos/dotty.hh: Add an interface
similar to that of tgba/dotty.hh, even if we have to ignore
most options.
* src/taalgos/tgba2ta.cc, src/taalgos/tgba2ta.hh: Add an option
to display the intermediate automaton with explicit stuttering
transitions, for the purpose of making demonstrations.
* src/tgba/tgbagraph.hh: Tweak the file so that SWIG can
read it.
* wrap/python/spot.py: Add wrappers for testing automata.
* wrap/python/spot_impl.i: Fix support for
atomic_prop_collect_as_bdd, and include a few more files.
* wrap/python/tests/testingaut.ipynb: New file.
* wrap/python/tests/Makefile.am: Add it.
parent 7bb183b9
...@@ -23,6 +23,8 @@ ...@@ -23,6 +23,8 @@
#include "reachiter.hh" #include "reachiter.hh"
#include "misc/escape.hh" #include "misc/escape.hh"
#include "misc/bareword.hh" #include "misc/bareword.hh"
#include <cstdlib>
#include <cstring>
namespace spot namespace spot
{ {
...@@ -30,10 +32,83 @@ namespace spot ...@@ -30,10 +32,83 @@ namespace spot
{ {
class dotty_bfs : public ta_reachable_iterator_breadth_first class dotty_bfs : public ta_reachable_iterator_breadth_first
{ {
void
parse_opts(const char* options)
{
const char* orig = options;
while (char c = *options++)
switch (c)
{
case '.':
{
// Copy the value in a string, so future calls to
// parse_opts do not fail if the environment has
// changed. (This matters particularly in an ipython
// notebook, where it is tempting to redefine
// SPOT_DOTDEFAULT.)
static std::string def = []()
{
auto s = getenv("SPOT_DOTDEFAULT");
return s ? s : "";
}();
// Prevent infinite recursions...
if (orig == def.c_str())
throw std::runtime_error
(std::string("SPOT_DOTDEFAULT should not contain '.'"));
if (!def.empty())
parse_opts(def.c_str());
break;
}
case 'A':
opt_hide_sets_ = true;
break;
case 'c':
opt_circles_ = true;
break;
case 'h':
opt_horizontal_ = true;
break;
case 'f':
if (*options != '(')
throw std::runtime_error
(std::string("invalid font specification for dotty()"));
{
auto* end = strchr(++options, ')');
if (!end)
throw std::runtime_error
(std::string("invalid font specification for dotty()"));
opt_font_ = std::string(options, end - options);
options = end + 1;
}
break;
case 'v':
opt_horizontal_ = false;
break;
case 'a':
case 'b':
case 'n':
case 'N':
case 'r':
case 'R':
case 's':
case 't':
// All these options are implemented by dotty() on TGBA,
// but are not implemented here. We simply ignore them,
// because raising an exception if they are in
// SPOT_DEFAULT would be annoying.
break;
default:
throw std::runtime_error
(std::string("unknown option for dotty(): ") + c);
}
}
public: public:
dotty_bfs(std::ostream& os, const const_ta_ptr& a) : dotty_bfs(std::ostream& os, const const_ta_ptr& a,
const char* opt) :
ta_reachable_iterator_breadth_first(a), os_(os) ta_reachable_iterator_breadth_first(a), os_(os)
{ {
parse_opts(opt ? opt : ".");
} }
void void
...@@ -41,6 +116,16 @@ namespace spot ...@@ -41,6 +116,16 @@ namespace spot
{ {
os_ << "digraph G {\n"; os_ << "digraph G {\n";
if (opt_horizontal_)
os_ << " rankdir=LR\n";
if (opt_circles_)
os_ << " node [shape=\"circle\"]\n";
if (!opt_font_.empty())
os_ << " fontname=\"" << opt_font_
<< "\"\n node [fontname=\"" << opt_font_
<< "\"]\n edge [fontname=\"" << opt_font_
<< "\"]\n";
// Always copy the environment variable into a static string, // Always copy the environment variable into a static string,
// so that we (1) look it up once, but (2) won't crash if the // so that we (1) look it up once, but (2) won't crash if the
// environment is changed. // environment is changed.
...@@ -120,8 +205,12 @@ namespace spot ...@@ -120,8 +205,12 @@ namespace spot
if (label.empty()) if (label.empty())
label = "{}"; label = "{}";
if (!opt_hide_sets_)
{
label += "\n"; label += "\n";
label += t_automata_->acc().format(si->current_acceptance_conditions()); label += t_automata_->acc().
format(si->current_acceptance_conditions());
}
os_ << " " << in << " -> " << out << " [label=\""; os_ << " " << in << " -> " << out << " [label=\"";
escape_str(os_, label); escape_str(os_, label);
...@@ -131,14 +220,19 @@ namespace spot ...@@ -131,14 +220,19 @@ namespace spot
private: private:
std::ostream& os_; std::ostream& os_;
spot::state* artificial_initial_state_; spot::state* artificial_initial_state_;
bool opt_horizontal_ = true;
bool opt_circles_ = false;
bool opt_hide_sets_ = false;
std::string opt_font_;
}; };
} }
std::ostream& std::ostream&
dotty_reachable(std::ostream& os, const const_ta_ptr& a) dotty_reachable(std::ostream& os, const const_ta_ptr& a, const char* opt)
{ {
dotty_bfs d(os, a); dotty_bfs d(os, a, opt);
d.run(); d.run();
return os; return os;
} }
......
// -*- coding: utf-8 -*- // -*- coding: utf-8 -*-
// Copyright (C) 2010, 2013, 2014 Laboratoire de Recherche et Développement // Copyright (C) 2010, 2013, 2014, 2015 Laboratoire de Recherche et
// de l'Epita (LRDE). // Développement de l'Epita (LRDE).
// //
// This file is part of Spot, a model checking library. // This file is part of Spot, a model checking library.
// //
...@@ -25,5 +25,6 @@ ...@@ -25,5 +25,6 @@
namespace spot namespace spot
{ {
SPOT_API std::ostream& SPOT_API std::ostream&
dotty_reachable(std::ostream& os, const const_ta_ptr& a); dotty_reachable(std::ostream& os, const const_ta_ptr& a,
const char* opt = nullptr);
} }
...@@ -410,7 +410,8 @@ namespace spot ...@@ -410,7 +410,8 @@ namespace spot
build_ta(const ta_explicit_ptr& ta, bdd atomic_propositions_set_, build_ta(const ta_explicit_ptr& ta, bdd atomic_propositions_set_,
bool degeneralized, bool degeneralized,
bool single_pass_emptiness_check, bool single_pass_emptiness_check,
bool artificial_livelock_state_mode) bool artificial_livelock_state_mode,
bool no_livelock)
{ {
std::stack<state_ta_explicit*> todo; std::stack<state_ta_explicit*> todo;
...@@ -517,6 +518,9 @@ namespace spot ...@@ -517,6 +518,9 @@ namespace spot
delete tgba_succ_it; delete tgba_succ_it;
} }
if (no_livelock)
return ta;
state_ta_explicit* artificial_livelock_acc_state = 0; state_ta_explicit* artificial_livelock_acc_state = 0;
trace << "*** build_ta: artificial_livelock_acc_state_mode = ***" trace << "*** build_ta: artificial_livelock_acc_state_mode = ***"
...@@ -542,7 +546,9 @@ namespace spot ...@@ -542,7 +546,9 @@ namespace spot
ta_explicit_ptr ta_explicit_ptr
tgba_to_ta(const const_tgba_ptr& tgba_, bdd atomic_propositions_set_, tgba_to_ta(const const_tgba_ptr& tgba_, bdd atomic_propositions_set_,
bool degeneralized, bool artificial_initial_state_mode, bool degeneralized, bool artificial_initial_state_mode,
bool single_pass_emptiness_check, bool artificial_livelock_state_mode) bool single_pass_emptiness_check,
bool artificial_livelock_state_mode,
bool no_livelock)
{ {
ta_explicit_ptr ta; ta_explicit_ptr ta;
...@@ -563,7 +569,8 @@ namespace spot ...@@ -563,7 +569,8 @@ namespace spot
// build ta automaton // build ta automaton
build_ta(ta, atomic_propositions_set_, degeneralized, build_ta(ta, atomic_propositions_set_, degeneralized,
single_pass_emptiness_check, artificial_livelock_state_mode); single_pass_emptiness_check, artificial_livelock_state_mode,
no_livelock);
// (degeneralized=true) => TA // (degeneralized=true) => TA
if (degeneralized) if (degeneralized)
...@@ -607,7 +614,7 @@ namespace spot ...@@ -607,7 +614,7 @@ namespace spot
// build a Generalized TA automaton involving a single_pass_emptiness_check // build a Generalized TA automaton involving a single_pass_emptiness_check
// (without an artificial livelock state): // (without an artificial livelock state):
auto ta = tgta->get_ta(); auto ta = tgta->get_ta();
build_ta(ta, atomic_propositions_set_, false, true, false); build_ta(ta, atomic_propositions_set_, false, true, false, false);
trace << "***tgba_to_tgbta: POST build_ta***" << std::endl; trace << "***tgba_to_tgbta: POST build_ta***" << std::endl;
......
// -*- coding: utf-8 -*- // -*- coding: utf-8 -*-
// Copyright (C) 2010, 2012, 2013, 2014 Laboratoire de Recherche et // Copyright (C) 2010, 2012, 2013, 2014, 2015 Laboratoire de Recherche et
// Développement de l'Epita (LRDE). // Développement de l'Epita (LRDE).
// //
// This file is part of Spot, a model checking library. // This file is part of Spot, a model checking library.
...@@ -75,6 +75,9 @@ namespace spot ...@@ -75,6 +75,9 @@ namespace spot
/// Buchi-accepting state, then s has no successors. A STA product requires /// Buchi-accepting state, then s has no successors. A STA product requires
/// only one-pass emptiness check algorithm (see spot::ta_check::check) /// only one-pass emptiness check algorithm (see spot::ta_check::check)
/// ///
/// \param no_livelock when set, this disable the replacement of
/// stuttering components by livelock states. Use this flag to
/// demonstrate an intermediate step of the construction.
/// ///
/// \return A spot::ta_explicit that recognizes the same language as the /// \return A spot::ta_explicit that recognizes the same language as the
/// TGBA \a tgba_to_convert. /// TGBA \a tgba_to_convert.
...@@ -83,7 +86,8 @@ namespace spot ...@@ -83,7 +86,8 @@ namespace spot
bool degeneralized = true, bool degeneralized = true,
bool artificial_initial_state_mode = true, bool artificial_initial_state_mode = true,
bool single_pass_emptiness_check = false, bool single_pass_emptiness_check = false,
bool artificial_livelock_state_mode = false); bool artificial_livelock_state_mode = false,
bool no_livelock = false);
/// \ingroup tgba_ta /// \ingroup tgba_ta
/// \brief Build a spot::tgta_explicit* (TGTA) from an LTL formula. /// \brief Build a spot::tgta_explicit* (TGTA) from an LTL formula.
......
...@@ -381,6 +381,7 @@ namespace spot ...@@ -381,6 +381,7 @@ namespace spot
return g_.new_transition(src, dst, cond); return g_.new_transition(src, dst, cond);
} }
#ifndef SWIG
auto out(unsigned src) const auto out(unsigned src) const
SPOT_RETURN(g_.out(src)); SPOT_RETURN(g_.out(src));
auto out(unsigned src) auto out(unsigned src)
...@@ -403,6 +404,7 @@ namespace spot ...@@ -403,6 +404,7 @@ namespace spot
auto is_dead_transition(const graph_t::trans_storage_t& t) const auto is_dead_transition(const graph_t::trans_storage_t& t) const
SPOT_RETURN(g_.is_dead_transition(t)); SPOT_RETURN(g_.is_dead_transition(t));
#endif
virtual bdd compute_support_conditions(const state* s) const virtual bdd compute_support_conditions(const state* s) const
{ {
......
...@@ -46,6 +46,7 @@ def _render_automaton_as_svg(a, opt=None): ...@@ -46,6 +46,7 @@ def _render_automaton_as_svg(a, opt=None):
return _ostream_to_svg(ostr) return _ostream_to_svg(ostr)
tgba._repr_svg_ = _render_automaton_as_svg tgba._repr_svg_ = _render_automaton_as_svg
ta._repr_svg_ = _render_automaton_as_svg
def _render_formula_as_svg(a): def _render_formula_as_svg(a):
# Load the SVG function only if we need it. This way the bindings # Load the SVG function only if we need it. This way the bindings
...@@ -56,13 +57,14 @@ def _render_formula_as_svg(a): ...@@ -56,13 +57,14 @@ def _render_formula_as_svg(a):
dotty(ostr, a) dotty(ostr, a)
return SVG(_ostream_to_svg(ostr)) return SVG(_ostream_to_svg(ostr))
def _render_tgba_as_svg(a, opt=None): def _return_automaton_as_svg(a, opt=None):
# Load the SVG function only if we need it. This way the bindings # Load the SVG function only if we need it. This way the bindings
# can still be used outside of IPython if IPython is not # can still be used outside of IPython if IPython is not
# installed. # installed.
from IPython.display import SVG from IPython.display import SVG
return SVG(_render_automaton_as_svg(a, opt)) return SVG(_render_automaton_as_svg(a, opt))
tgba.show = _render_tgba_as_svg tgba.show = _return_automaton_as_svg
ta.show = _return_automaton_as_svg
def _formula_str_ctor(self, str): def _formula_str_ctor(self, str):
self.this = parse_formula(str) self.this = parse_formula(str)
......
...@@ -135,6 +135,7 @@ namespace std { ...@@ -135,6 +135,7 @@ namespace std {
#include "tgbaalgos/isdet.hh" #include "tgbaalgos/isdet.hh"
#include "tgbaalgos/simulation.hh" #include "tgbaalgos/simulation.hh"
#include "tgbaalgos/postproc.hh" #include "tgbaalgos/postproc.hh"
#include "tgbaalgos/product.hh"
#include "tgbaalgos/stutter.hh" #include "tgbaalgos/stutter.hh"
#include "tgbaalgos/translate.hh" #include "tgbaalgos/translate.hh"
#include "tgbaalgos/hoa.hh" #include "tgbaalgos/hoa.hh"
...@@ -225,7 +226,14 @@ using namespace spot; ...@@ -225,7 +226,14 @@ using namespace spot;
%include "ltlparse/public.hh" %include "ltlparse/public.hh"
/* these must come before apcollect.hh */
%include "tgba/bdddict.hh" %include "tgba/bdddict.hh"
%include "tgba/bddprint.hh"
%include "tgba/fwd.hh"
%feature("flatnested") spot::acc_cond::mark_t;
%feature("flatnested") spot::acc_cond::acc_code;
%include "tgba/acc.hh"
%include "tgba/tgba.hh"
%include "ltlvisit/apcollect.hh" %include "ltlvisit/apcollect.hh"
%include "ltlvisit/dotty.hh" %include "ltlvisit/dotty.hh"
...@@ -243,21 +251,9 @@ using namespace spot; ...@@ -243,21 +251,9 @@ using namespace spot;
// Help SWIG with namespace lookups. // Help SWIG with namespace lookups.
#define ltl spot::ltl #define ltl spot::ltl
%include "tgba/bddprint.hh"
%include "tgba/fwd.hh"
%feature("flatnested") spot::acc_cond::mark_t;
%include "tgba/acc.hh"
%include "tgba/tgba.hh"
%include "tgba/taatgba.hh" %include "tgba/taatgba.hh"
%include "tgba/tgbaproduct.hh" %include "tgba/tgbaproduct.hh"
%include "tgba/tgbagraph.hh"
// We won't parse tgba_digraph, so just pretend it is a subclass of tgba.
%nodefaultctor spot::tgba_digraph;
namespace spot {
class tgba_digraph: public tgba
{
};
}
// Should come after the definition of tgba_digraph // Should come after the definition of tgba_digraph
...@@ -280,6 +276,7 @@ namespace spot { ...@@ -280,6 +276,7 @@ namespace spot {
%include "tgbaalgos/isdet.hh" %include "tgbaalgos/isdet.hh"
%include "tgbaalgos/simulation.hh" %include "tgbaalgos/simulation.hh"
%include "tgbaalgos/postproc.hh" %include "tgbaalgos/postproc.hh"
%include "tgbaalgos/product.hh"
%include "tgbaalgos/stutter.hh" %include "tgbaalgos/stutter.hh"
%include "tgbaalgos/translate.hh" %include "tgbaalgos/translate.hh"
%include "tgbaalgos/hoa.hh" %include "tgbaalgos/hoa.hh"
......
...@@ -47,4 +47,5 @@ TESTS = \ ...@@ -47,4 +47,5 @@ TESTS = \
piperead.ipynb \ piperead.ipynb \
randaut.ipynb \ randaut.ipynb \
randltl.ipynb \ randltl.ipynb \
setxor.py setxor.py \
testingaut.ipynb
This diff is collapsed.
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