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

Make it possible to output UTF-8 for dotty().

* src/tgba/tgbaexplicit.hh: Rerganize a bit to
allow different functions to be used to format
states.  Add an enabled_utf8() method to
tgba_explicit_formula.
* src/tgbaalgos/dotty.hh, src/tgbaalgos/dotty.cc:
Simplify the interface by not depending on
dotty_decorator explicitely.
* src/tgba/bddprint.hh (enable_utf8): New function.
* src/tgba/bddprint.cc (enable_utf8): Implement it
and use the global utf8 flag in other functions.
* src/tgbatest/ltl2tgba.cc: Add an -8 option for
UTF-8 outpout.
* wrap/python/spot.i: Adjust for tgbexplicit.hh
changes.
parent f082700f
// Copyright (C) 2009 Laboratoire de Recherche et Dveloppement // Copyright (C) 2009, 2012 Laboratoire de Recherche et Dveloppement
// de l'Epita (LRDE). // de l'Epita (LRDE).
// Copyright (C) 2003, 2004 Laboratoire d'Informatique de Paris 6 (LIP6), // Copyright (C) 2003, 2004 Laboratoire d'Informatique de Paris 6 (LIP6),
// dpartement Systmes Rpartis Coopratifs (SRC), Universit Pierre // dpartement Systmes Rpartis Coopratifs (SRC), Universit Pierre
...@@ -36,6 +36,19 @@ namespace spot ...@@ -36,6 +36,19 @@ namespace spot
/// Global flag to enable Acc[x] output (instead of `x'). /// Global flag to enable Acc[x] output (instead of `x').
static bool want_acc; static bool want_acc;
/// Global flag to enable UTF-8 output.
static bool utf8;
static
std::ostream& print_ltl(const ltl::formula* f, std::ostream& o)
{
if (utf8)
ltl::to_utf8_string(f, o);
else
ltl::to_string(f, o);
return o;
}
/// Stream handler used by Buddy to display BDD variables. /// Stream handler used by Buddy to display BDD variables.
static void static void
print_handler(std::ostream& o, int var) print_handler(std::ostream& o, int var)
...@@ -52,12 +65,12 @@ namespace spot ...@@ -52,12 +65,12 @@ namespace spot
if (want_acc) if (want_acc)
{ {
o << "Acc["; o << "Acc[";
to_string(isi->second, o) << "]"; print_ltl(isi->second, o) << "]";
} }
else else
{ {
o << "\""; o << "\"";
to_string(isi->second, o) << "\""; print_ltl(isi->second, o) << "\"";
} }
} }
else else
...@@ -65,14 +78,16 @@ namespace spot ...@@ -65,14 +78,16 @@ namespace spot
isi = dict->now_formula_map.find(var); isi = dict->now_formula_map.find(var);
if (isi != dict->now_formula_map.end()) if (isi != dict->now_formula_map.end())
{ {
o << "Now["; to_string(isi->second, o) << "]"; o << "Now[";
print_ltl(isi->second, o) << "]";
} }
else else
{ {
isi = dict->now_formula_map.find(var - 1); isi = dict->now_formula_map.find(var - 1);
if (isi != dict->now_formula_map.end()) if (isi != dict->now_formula_map.end())
{ {
o << "Next["; to_string(isi->second, o) << "]"; o << "Next[";
print_ltl(isi->second, o) << "]";
} }
else else
{ {
...@@ -202,7 +217,7 @@ namespace spot ...@@ -202,7 +217,7 @@ namespace spot
bdd_print_formula(std::ostream& os, const bdd_dict* d, bdd b) bdd_print_formula(std::ostream& os, const bdd_dict* d, bdd b)
{ {
const ltl::formula* f = bdd_to_formula(b, d); const ltl::formula* f = bdd_to_formula(b, d);
to_string(f, os); print_ltl(f, os);
f->destroy(); f->destroy();
return os; return os;
} }
...@@ -237,4 +252,9 @@ namespace spot ...@@ -237,4 +252,9 @@ namespace spot
return os; return os;
} }
void
enable_utf8()
{
utf8 = true;
}
} }
// Copyright (C) 2003, 2004 Laboratoire d'Informatique de Paris 6 (LIP6), // Copyright (C) 2003, 2004, 2012 Laboratoire d'Informatique de Paris 6 (LIP6),
// département Systèmes Répartis Coopératifs (SRC), Université Pierre // département Systèmes Répartis Coopératifs (SRC), Université Pierre
// et Marie Curie. // et Marie Curie.
// //
...@@ -113,6 +113,8 @@ namespace spot ...@@ -113,6 +113,8 @@ namespace spot
std::ostream& bdd_print_table(std::ostream& os, std::ostream& bdd_print_table(std::ostream& os,
const bdd_dict* dict, bdd b); const bdd_dict* dict, bdd b);
/// \brief Enable UTF-8 output for bdd printers.
void enable_utf8();
} }
#endif // SPOT_TGBA_BDDPRINT_HH #endif // SPOT_TGBA_BDDPRINT_HH
...@@ -80,7 +80,7 @@ namespace spot ...@@ -80,7 +80,7 @@ namespace spot
{ {
} }
typedef Label Label_t; typedef Label label_t;
typedef label_hash label_hash_t; typedef label_hash label_hash_t;
struct transition struct transition
...@@ -154,12 +154,6 @@ namespace spot ...@@ -154,12 +154,6 @@ namespace spot
} }
static const int default_val; static const int default_val;
static std::string to_string(int l)
{
std::stringstream ss;
ss << l;
return ss.str();
}
}; };
/// States labeled by a string /// States labeled by a string
...@@ -183,10 +177,6 @@ namespace spot ...@@ -183,10 +177,6 @@ namespace spot
} }
static const std::string default_val; static const std::string default_val;
static std::string to_string(const std::string& l)
{
return l;
}
}; };
/// States labeled by a formula /// States labeled by a formula
...@@ -210,10 +200,6 @@ namespace spot ...@@ -210,10 +200,6 @@ namespace spot
} }
static const ltl::formula* default_val; static const ltl::formula* default_val;
static std::string to_string(const ltl::formula* l)
{
return ltl::to_string(l);
}
}; };
/// Successor iterators used by spot::tgba_explicit. /// Successor iterators used by spot::tgba_explicit.
...@@ -280,19 +266,22 @@ namespace spot ...@@ -280,19 +266,22 @@ namespace spot
bdd all_acceptance_conditions_; bdd all_acceptance_conditions_;
}; };
/// Graph implementation for automata representation /// Graph implementation for explicit automgon
/// \ingroup tgba_representation /// \ingroup tgba_representation
template<typename State, typename Type> template<typename State, typename Type>
class explicit_graph: public Type class explicit_graph: public Type
{ {
public:
typedef typename State::label_t label_t;
typedef typename State::label_hash_t label_hash_t;
typedef typename State::transitions_t transitions_t;
typedef typename State::transition transition;
typedef State state;
protected: protected:
typedef Sgi::hash_map<typename State::Label_t, State, typedef Sgi::hash_map<label_t, State, label_hash_t> ls_map;
typename State::label_hash_t> ls_map; typedef Sgi::hash_map<const State*, label_t, ptr_hash<State> > sl_map;
typedef Sgi::hash_map<const State*, typename State::Label_t,
ptr_hash<State> > sl_map;
public: public:
typedef typename State::transition transition;
explicit_graph(bdd_dict* dict) explicit_graph(bdd_dict* dict)
: ls_(), : ls_(),
...@@ -319,15 +308,14 @@ namespace spot ...@@ -319,15 +308,14 @@ namespace spot
t.condition = bddtrue; t.condition = bddtrue;
t.acceptance_conditions = bddfalse; t.acceptance_conditions = bddfalse;
typename State::transitions_t::iterator i = typename transitions_t::iterator i =
source->successors.insert(source->successors.end(), t); source->successors.insert(source->successors.end(), t);
return &*i; return &*i;
} }
transition* transition*
create_transition(const typename State::Label_t& source, create_transition(const label_t& source, const label_t& dest)
const typename State::Label_t& dest)
{ {
// It's important that the source be created before the // It's important that the source be created before the
// destination, so the first source encountered becomes the // destination, so the first source encountered becomes the
...@@ -361,19 +349,19 @@ namespace spot ...@@ -361,19 +349,19 @@ namespace spot
} }
//old tgba explicit labelled interface //old tgba explicit labelled interface
bool has_state(const typename State::Label_t& name) bool has_state(const label_t& name)
{ {
return ls_.find(name) != ls_.end(); return ls_.find(name) != ls_.end();
} }
const typename State::Label_t& get_label(const State* s) const const label_t& get_label(const State* s) const
{ {
typename sl_map::const_iterator i = sl_.find(s); typename sl_map::const_iterator i = sl_.find(s);
assert(i != sl_.end()); assert(i != sl_.end());
return i->second; return i->second;
} }
const typename State::Label_t& get_label(const spot::state* s) const const label_t& get_label(const spot::state* s) const
{ {
const State* se = down_cast<const State*>(s); const State* se = down_cast<const State*>(s);
assert(se); assert(se);
...@@ -381,8 +369,7 @@ namespace spot ...@@ -381,8 +369,7 @@ namespace spot
} }
transition* transition*
create_trainsition(const typename State::Label_t& source, create_trainsition(const label_t& source, const label_t& dest)
const typename State::Label_t& dest)
{ {
// It's important that the source be created before the // It's important that the source be created before the
// destination, so the first source encountered becomes the // destination, so the first source encountered becomes the
...@@ -398,7 +385,7 @@ namespace spot ...@@ -398,7 +385,7 @@ namespace spot
typename ls_map::iterator i; typename ls_map::iterator i;
for (i = ls_.begin(); i != ls_.end(); ++i) for (i = ls_.begin(); i != ls_.end(); ++i)
{ {
typename State::transitions_t::iterator i2; typename transitions_t::iterator i2;
for (i2 = i->second.successors.begin(); for (i2 = i->second.successors.begin();
i2 != i->second.successors.end(); ++i2) i2 != i->second.successors.end(); ++i2)
i2->acceptance_conditions = all - i2->acceptance_conditions; i2->acceptance_conditions = all - i2->acceptance_conditions;
...@@ -411,21 +398,20 @@ namespace spot ...@@ -411,21 +398,20 @@ namespace spot
typename ls_map::iterator i; typename ls_map::iterator i;
for (i = ls_.begin(); i != ls_.end(); ++i) for (i = ls_.begin(); i != ls_.end(); ++i)
{ {
typename State::transitions_t::iterator t1; typename transitions_t::iterator t1;
for (t1 = i->second.successors.begin(); for (t1 = i->second.successors.begin();
t1 != i->second.successors.end(); ++t1) t1 != i->second.successors.end(); ++t1)
{ {
bdd acc = t1->acceptance_conditions; bdd acc = t1->acceptance_conditions;
const state_explicit<typename State::Label_t, const state_explicit<label_t, label_hash_t>* dest = t1->dest;
typename State::label_hash_t>* dest = t1->dest;
// Find another transition with the same destination and // Find another transition with the same destination and
// acceptance conditions. // acceptance conditions.
typename State::transitions_t::iterator t2 = t1; typename transitions_t::iterator t2 = t1;
++t2; ++t2;
while (t2 != i->second.successors.end()) while (t2 != i->second.successors.end())
{ {
typename State::transitions_t::iterator t2copy = t2++; typename transitions_t::iterator t2copy = t2++;
if (t2copy->acceptance_conditions == acc && t2copy->dest == dest) if (t2copy->acceptance_conditions == acc && t2copy->dest == dest)
{ {
t1->condition |= t2copy->condition; t1->condition |= t2copy->condition;
...@@ -438,7 +424,7 @@ namespace spot ...@@ -438,7 +424,7 @@ namespace spot
/// Return the state_explicit for \a name, creating the state if /// Return the state_explicit for \a name, creating the state if
/// it does not exist. /// it does not exist.
State* add_state(const typename State::Label_t& name) State* add_state(const label_t& name)
{ {
typename ls_map::iterator i = ls_.find(name); typename ls_map::iterator i = ls_.find(name);
if (i == ls_.end()) if (i == ls_.end())
...@@ -458,7 +444,7 @@ namespace spot ...@@ -458,7 +444,7 @@ namespace spot
} }
State* State*
set_init_state(const typename State::Label_t& state) set_init_state(const label_t& state)
{ {
State* s = add_state(state); State* s = add_state(state);
init_ = s; init_ = s;
...@@ -472,14 +458,14 @@ namespace spot ...@@ -472,14 +458,14 @@ namespace spot
while (i != ls_.end()) while (i != ls_.end())
{ {
typename State::Label_t s = i->first; label_t s = i->first;
// Do not erase the same state twice(Because of possible aliases). // Do not erase the same state twice(Because of possible aliases).
if (sl_.erase(&(i->second))) if (sl_.erase(&(i->second)))
i->second.destroy(); i->second.destroy();
++i; ++i;
destroy_key<typename State::Label_t> dest; destroy_key<label_t> dest;
dest.destroy(s); dest.destroy(s);
} }
...@@ -515,21 +501,33 @@ namespace spot ...@@ -515,21 +501,33 @@ namespace spot
->all_acceptance_conditions()); ->all_acceptance_conditions());
} }
//no need?
typedef std::string (*to_string_func_t)(const label_t& t);
void set_to_string_func(to_string_func_t f)
{
to_string_func_ = f;
}
to_string_func_t get_to_string_func() const
{
return to_string_func_;
}
virtual std::string format_state(const spot::state* state) const virtual std::string format_state(const spot::state* state) const
{ {
const State* se = down_cast<const State*>(state); const State* se = down_cast<const State*>(state);
assert(se); assert(se);
typename sl_map::const_iterator i = sl_.find(se); typename sl_map::const_iterator i = sl_.find(se);
assert(i != sl_.end()); assert(i != sl_.end());
return State::to_string(i->second); assert(to_string_func_);
return to_string_func_(i->second);
} }
/// old implementation in tgba_explicit_string /// old implementation in tgba_explicit_string
/// Create an alias for a state. Any reference to \a alias_name /// Create an alias for a state. Any reference to \a alias_name
/// will act as a reference to \a real_name. /// will act as a reference to \a real_name.
void add_state_alias(const typename State::Label_t& alias, void add_state_alias(const label_t& alias, const label_t& real)
const typename State::Label_t& real)
{ {
ls_[alias] = *(add_state(real)); ls_[alias] = *(add_state(real));
} }
...@@ -614,7 +612,7 @@ namespace spot ...@@ -614,7 +612,7 @@ namespace spot
typename explicit_graph<State, tgba>::ls_map::iterator i; typename explicit_graph<State, tgba>::ls_map::iterator i;
for (i = this->ls_.begin(); i != this->ls_.end(); ++i) for (i = this->ls_.begin(); i != this->ls_.end(); ++i)
{ {
typename State::transitions_t::iterator i2; typename transitions_t::iterator i2;
for (i2 = i->second.successors.begin(); for (i2 = i->second.successors.begin();
i2 != i->second.successors.end(); ++i2) i2 != i->second.successors.end(); ++i2)
i2->acceptance_conditions &= neg; i2->acceptance_conditions &= neg;
...@@ -639,11 +637,11 @@ namespace spot ...@@ -639,11 +637,11 @@ namespace spot
{ {
const State* s = down_cast<const State*>(in); const State* s = down_cast<const State*>(in);
assert(s); assert(s);
const typename State::transitions_t& st = s->successors; const transitions_t& st = s->successors;
bdd res = bddfalse; bdd res = bddfalse;
typename State::transitions_t::const_iterator i; typename transitions_t::const_iterator i;
for (i = st.begin(); i != st.end(); ++i) for (i = st.begin(); i != st.end(); ++i)
res |= i->condition; res |= i->condition;
...@@ -654,11 +652,11 @@ namespace spot ...@@ -654,11 +652,11 @@ namespace spot
{ {
const State* s = down_cast<const State*>(in); const State* s = down_cast<const State*>(in);
assert(s); assert(s);
const typename State::transitions_t& st = s->successors; const transitions_t& st = s->successors;
bdd res = bddtrue; bdd res = bddtrue;
typename State::transitions_t::const_iterator i; typename transitions_t::const_iterator i;
for (i = st.begin(); i != st.end(); ++i) for (i = st.begin(); i != st.end(); ++i)
res &= bdd_support(i->condition); res &= bdd_support(i->condition);
...@@ -674,17 +672,14 @@ namespace spot ...@@ -674,17 +672,14 @@ namespace spot
mutable bdd all_acceptance_conditions_; mutable bdd all_acceptance_conditions_;
mutable bool all_acceptance_conditions_computed_; mutable bool all_acceptance_conditions_computed_;
bdd neg_acceptance_conditions_; bdd neg_acceptance_conditions_;
to_string_func_t to_string_func_;
}; };
template <typename State> template <typename State>
class tgba_explicit: public explicit_graph<State, tgba> class tgba_explicit: public explicit_graph<State, tgba>
{ {
public: public:
typedef typename State::transition transition; tgba_explicit(bdd_dict* dict): explicit_graph<State,tgba>(dict)
typedef State state;
tgba_explicit(bdd_dict* dict):
explicit_graph<State,tgba>(dict)
{ {
} }
...@@ -702,11 +697,7 @@ namespace spot ...@@ -702,11 +697,7 @@ namespace spot
class sba_explicit: public explicit_graph<State, sba> class sba_explicit: public explicit_graph<State, sba>
{ {
public: public:
typedef typename State::transition transition; sba_explicit(bdd_dict* dict): explicit_graph<State, sba>(dict)
typedef State state;
sba_explicit(bdd_dict* dict):
explicit_graph<State, sba>(dict)
{ {
} }
...@@ -741,15 +732,90 @@ namespace spot ...@@ -741,15 +732,90 @@ namespace spot
}; };
// It is tempting to write
//
// template<template<typename T>class graph, typename Type>
// class explicit_conf: public graph<T>
//
// to simplify the typedefs at the end of the file, however swig
// cannot parse this syntax.
/// Configuration of graph automata
/// \ingroup tgba_representation
template<class graph, typename Type>
class explicit_conf: public graph
{
public:
explicit_conf(bdd_dict* d): graph(d)
{
set_to_string_func(to_string);
};
static std::string to_string(const typename Type::label_t& l)
{
std::stringstream ss;
ss << l;
return ss.str();
}
};
template<class graph>
class explicit_conf<graph, state_explicit_string>: public graph
{
public:
explicit_conf(bdd_dict* d): graph(d)
{
set_to_string_func(to_string);
};
static std::string to_string(const std::string& l)
{
return l;
}
};
template<class graph>
class explicit_conf<graph, state_explicit_formula>: public graph
{
public:
explicit_conf(bdd_dict* d): graph(d)
{
set_to_string_func(to_string);
};
// Enable UTF8 output for the formulae that label states.
void enable_utf8()
{
set_to_string_func(to_utf8_string);
}
static