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

python: fix support for std::vector<const_twa_graph_ptr>

* spot/twaalgos/aiger.hh, spot/twaalgos/aiger.cc: Fix prototypes, as
well as several error messages.
* python/spot/impl.i: Implement an ad-hoc conversion for
std::vector<const_twa_graph_ptr>.
* tests/python/synthesis.ipynb: Use it to simplify the example.
Adjust some comments.
parent 98ebbea1
......@@ -408,6 +408,37 @@ namespace swig
$result = SWIG_FromCharPtr($1->c_str());
}
// For some reason, Swig can convert [aut1,aut2,...] into
// std::vector<spot::twa_graph_ptr>, but not into
// std::vector<spot::const_twa_graph_ptr>. Let's fix that by using
// the Swig machinery that successfully converts to
// std::vector<spot::twa_graph_ptr>, and then converting this into
// std::vector<spot::const_twa_graph_ptr> manually.
%typemap(in) std::vector<spot::const_twa_graph_ptr>& {
void *tmp;
int res = SWIG_ConvertPtr($input, &tmp, $descriptor(std::vector<spot::twa_graph_ptr>*),
SWIG_POINTER_IMPLICIT_CONV);
if (!SWIG_IsOK(res) || !tmp)
%argument_fail(res, "std::vector<spot::const_twa_graph_ptr>", $symname, $argnum);
std::vector<spot::twa_graph_ptr>* temp;
temp = reinterpret_cast<std::vector<spot::twa_graph_ptr>*>(tmp);
$1 = new std::vector<spot::const_twa_graph_ptr>(temp->begin(),
temp->end());
if (SWIG_IsNewObj(res)) delete temp;
}
%typemap(freearg) std::vector<spot::const_twa_graph_ptr>& {
delete $1;
}
%typemap(typecheck, precedence=2000) std::vector<spot::const_twa_graph_ptr>& {
$1 = SWIG_CheckState(SWIG_ConvertPtr($input, nullptr,
$descriptor(std::vector<spot::twa_graph_ptr>*),
SWIG_POINTER_IMPLICIT_CONV));
}
%{
// This function is called whenever an exception has been caught.
// Doing all the conversion in a separate function (rather than
......@@ -462,7 +493,6 @@ static void handle_any_exception()
%implicitconv std::vector<spot::formula>;
%implicitconv std::vector<spot::twa_graph_ptr>;
%implicitconv std::vector<spot::const_twa_graph_ptr>;
%implicitconv spot::formula;
%implicitconv std::vector<bool>;
......@@ -672,7 +702,6 @@ def state_is_accepting(self, src) -> "bool":
%template(scc_info_scc_edges) spot::internal::scc_edges<spot::digraph<spot::twa_graph_state, spot::twa_graph_edge_data> const, spot::internal::keep_all>;
%template(scc_info_inner_scc_edges) spot::internal::scc_edges<spot::digraph<spot::twa_graph_state, spot::twa_graph_edge_data> const, spot::internal::keep_inner_scc>;
%template(vector_twa_graph) std::vector<spot::twa_graph_ptr>;
%template(vector_const_twa_graph) std::vector<spot::const_twa_graph_ptr>;
%include <spot/twaalgos/strength.hh>
%include <spot/twaalgos/sccfilter.hh>
%include <spot/twaalgos/stats.hh>
......
......@@ -640,7 +640,7 @@ namespace spot
unsigned aig::cube2var_(const bdd& b, const int use_split_off)
{
assert(bdd_is_cube(b) && "bdd is not a cube!");
assert(bdd_is_cube(b) && "bdd is not a cube");
static std::vector<bdd> parts_;
static std::vector<unsigned> prod_vars_;
static std::vector<unsigned> prod_parts_;
......@@ -729,7 +729,7 @@ namespace spot
if (method == 1 || method == 2)
used_m.push_back(1);
assert(used_m.size()
&& "Can not convert the given method. "
&& "Cannot convert the given method. "
"Only 0,1 and 2 are currently supported");
const auto negate = use_dual ? std::vector<bool>{false}
......@@ -766,7 +766,7 @@ namespace spot
{
cond_vars.push_back(enc_1(cpart, m));
if (num_gates() >= ngates_min)
break; // Can not be optimal
break; // Cannot be optimal
}
// Compute the and if there is still hope
unsigned this_res = -1u;
......@@ -967,7 +967,7 @@ namespace spot
it2.first.first.id(),
it2.first.second.id()); });
if (itm == occur_map.cend())
throw std::runtime_error("Empty occurence map!");
throw std::runtime_error("Empty occurence map");
return *itm;
};
......@@ -1134,7 +1134,7 @@ namespace spot
if (it != var2bdd.end())
{
assert(bdd2var.at(var2bdd.at(v).id()) == v
&& "Inconsistent bdd!\n");
&& "Inconsistent bdd.");
return it->second;
}
//get the vars of the input to the gates
......@@ -1170,7 +1170,7 @@ namespace spot
{
if (not (var2bdd.count(v)))
throw std::runtime_error("variable " + std::to_string(v)
+ " has no bdd associated!\n");
+ " has no bdd associated.");
};
std::for_each(circ.next_latches_.begin(), circ.next_latches_.end(),
check);
......@@ -1342,7 +1342,7 @@ namespace spot
assert(inputs.size() == num_inputs()
&& "Input length does not match");
assert(state_.size() == max_var_ + 2
&& "State vector does not have the correct size.\n"
&& "State vector does not have the correct size. "
"Forgot to initialize?");
// Set the inputs
for (unsigned i = 0; i < num_inputs(); ++i)
......@@ -1855,7 +1855,7 @@ namespace spot
{
if (!m)
throw std::runtime_error("mealy_machine_to_aig(): "
"m cannot be null");
"m cannot be null.");
return auts_to_aiger({{m, get_synthesis_outputs(m)}}, mode);
}
......@@ -1865,7 +1865,7 @@ namespace spot
{
if (m.success != mealy_like::realizability_code::REALIZABLE_REGULAR)
throw std::runtime_error("mealy_machine_to_aig(): "
"Can only handle regular mealy machine, TBD");
"Can only handle regular mealy machine, yet.");
return mealy_machine_to_aig(m.mealy_like, mode);
}
......@@ -1916,7 +1916,7 @@ namespace spot
{
if (m.success != mealy_like::realizability_code::REALIZABLE_REGULAR)
throw std::runtime_error("mealy_machine_to_aig(): "
"Can only handle regular mealy machine, TBD");
"Can only handle regular mealy machine, yet.");
return mealy_machine_to_aig(m.mealy_like, mode, ins, outs);
}
......@@ -1931,7 +1931,7 @@ namespace spot
if (usedbdd != s->get_dict())
throw std::runtime_error("mealy_machines_to_aig(): "
"All machines have to "
"share a bdd_dict!\n");
"share a bdd_dict.");
});
std::vector<std::pair<const_twa_graph_ptr, bdd>> new_vec;
......@@ -1945,7 +1945,7 @@ namespace spot
if (bdd_and(bdd_not(this_outputs), all_outputs) == bddfalse)
throw std::runtime_error("mealy_machines_to_aig(): "
"\"outs\" of the machines are not "
"distinct!.\n");
"distinct.");
all_outputs &= this_outputs;
new_vec.emplace_back(am, this_outputs);
}
......@@ -1962,7 +1962,7 @@ namespace spot
mealy_like::realizability_code::REALIZABLE_REGULAR; }))
throw std::runtime_error("mealy_machines_to_aig(): "
"Can only handle regular mealy machine for "
"the moment, TBD");
"the moment.");
auto new_vec = std::vector<const_twa_graph_ptr>();
new_vec.reserve(m_vec.size());
std::transform(m_vec.cbegin(), m_vec.cend(),
......@@ -1974,14 +1974,17 @@ namespace spot
// Note: This ignores the named property
aig_ptr
mealy_machines_to_aig(const std::vector<twa_graph_ptr>& m_vec,
mealy_machines_to_aig(const std::vector<const_twa_graph_ptr>& m_vec,
const char *mode,
const std::vector<std::string>& ins,
const std::vector<std::vector<std::string>>& outs)
{
if (m_vec.empty())
throw std::runtime_error("mealy_machines_to_aig(): No strategy given.");
if (m_vec.size() != outs.size())
throw std::runtime_error("mealy_machines_to_aig(): "
"Expected as many outs as strategies!\n");
"Expecting as many outs as strategies.");
std::for_each(m_vec.begin()+1, m_vec.end(),
[usedbdd = m_vec.at(0)->get_dict()](auto&& it)
......@@ -1989,7 +1992,7 @@ namespace spot
if (usedbdd != it->get_dict())
throw std::runtime_error("mealy_machines_to_aig(): "
"All strategies have to "
"share a bdd_dict!\n");
"share a bdd_dict.");
});
{
......@@ -2042,7 +2045,7 @@ namespace spot
{
// todo extend to TGBA and possibly others
const unsigned ns = strat_vec.size();
std::vector<twa_graph_ptr> m_machines;
std::vector<const_twa_graph_ptr> m_machines;
m_machines.reserve(ns);
std::vector<std::vector<std::string>> outs_used;
outs_used.reserve(ns);
......
......@@ -472,7 +472,7 @@ namespace spot
mealy_machines_to_aig(const std::vector<mealy_like>& m_vec,
const char* mode);
SPOT_API aig_ptr
mealy_machines_to_aig(const std::vector<twa_graph_ptr>& m_vec,
mealy_machines_to_aig(const std::vector<const_twa_graph_ptr>& m_vec,
const char* mode,
const std::vector<std::string>& ins,
const std::vector<std::vector<std::string>>& outs);
......@@ -483,7 +483,6 @@ namespace spot
const std::vector<std::vector<std::string>>& outs);
/// @}
/// \ingroup twa_io
/// \brief Print the aig to stream in AIGER format
SPOT_API std::ostream&
......
This diff is collapsed.
Supports Markdown
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