acc: introduce fin_one_extract()

* spot/twa/acc.cc, spot/twa/acc.hh (acc_cond::fin_one_extract,
acc_code::acc_cond::fin_one_extract): New methods.
* python/spot/impl.i: Add support for the return type.
* tests/python/acc_cond.ipynb: Test them.
This commit is contained in:
Alexandre Duret-Lutz 2021-09-10 18:17:17 +02:00
parent 2d1cb0ddcd
commit 7fedb3dc61
4 changed files with 246 additions and 16 deletions

View file

@ -2644,6 +2644,118 @@ namespace spot
return -1;
}
namespace
{
int has_top_fin(const acc_cond::acc_word* pos, bool top = true)
{
if (pos->sub.op == acc_cond::acc_op::Fin)
{
acc_cond::mark_t m = pos[-1].mark;
if (top || m.is_singleton())
return m.min_set() - 1;
}
else if (pos->sub.op == acc_cond::acc_op::And)
{
auto sub = pos - pos->sub.size;
do
{
--pos;
if (int f = has_top_fin(pos, false); f >= 0)
return f;
pos -= pos->sub.size;
}
while (sub < pos);
}
else if (top && pos->sub.op == acc_cond::acc_op::Or)
{
auto sub = pos - pos->sub.size;
do
{
--pos;
if (int f = has_top_fin(pos); f >= 0)
return f;
pos -= pos->sub.size;
}
while (sub < pos);
}
return -1;
}
bool uses_fin(const acc_cond::acc_word* pos, acc_cond::mark_t f)
{
auto sub = pos - pos->sub.size;
do
{
switch (pos->sub.op)
{
case acc_cond::acc_op::And:
case acc_cond::acc_op::Or:
--pos;
break;
case acc_cond::acc_op::Fin:
if (pos[-1].mark & f)
return true;
SPOT_FALLTHROUGH;
case acc_cond::acc_op::Inf:
case acc_cond::acc_op::InfNeg:
case acc_cond::acc_op::FinNeg:
pos -= 2;
break;
}
}
while (sub < pos);
return false;
}
acc_cond::acc_code extract_fin(const acc_cond::acc_word* pos,
acc_cond::mark_t f)
{
auto start = pos - pos->sub.size;
switch (pos->sub.op)
{
case acc_cond::acc_op::And:
case acc_cond::acc_op::Fin:
case acc_cond::acc_op::Inf:
return pos;
case acc_cond::acc_op::Or:
{
--pos;
auto res = acc_cond::acc_code::f();
do
{
if (uses_fin(pos, f))
{
acc_cond::acc_code tmp(pos);
tmp |= std::move(res);
std::swap(tmp, res);
}
pos -= pos->sub.size + 1;
}
while (pos > start);
return res;
}
case acc_cond::acc_op::FinNeg:
case acc_cond::acc_op::InfNeg:
SPOT_UNREACHABLE();
return {};
}
SPOT_UNREACHABLE();
return {};
}
}
std::pair<int, acc_cond::acc_code>
acc_cond::acc_code::fin_one_extract() const
{
if (is_t() || is_f())
return {-1, *this};
const acc_cond::acc_word* pos = &back();
int selected_fin = has_top_fin(pos);
if (selected_fin < 0)
selected_fin = fin_one();
return {selected_fin, extract_fin(pos, {(unsigned) selected_fin})};
}
namespace
{
bool

View file

@ -1251,12 +1251,6 @@ namespace spot
/// `Fin(0)&Fin(1)&(Inf(2)|Fin(3))`, this will return `{0,1}`.
mark_t fin_unit() const;
/// \brief Return one acceptance set i that appear as `Fin(i)`
/// in the condition.
///
/// Return -1 if no such set exist.
int fin_one() const;
/// \brief Find a `Inf(i)` that is a unit clause.
///
/// This return a mark_t `{i}` such that `Inf(i)` appears as a
@ -1270,6 +1264,31 @@ namespace spot
/// `Inf(0)&Inf(1)&(Inf(2)|Fin(3))`, this will return `{0,1}`.
mark_t inf_unit() const;
/// \brief Return one acceptance set i that appears as `Fin(i)`
/// in the condition.
///
/// Return -1 if no such set exist.
int fin_one() const;
/// \brief Return one acceptance set i that appears as `Fin(i)`
/// in the condition, and all disjuncts containing it.
///
/// If the condition is a disjunction and one of the disjunct
/// has the shape `...&Fin(i)&...`, then `i` will be prefered
/// over any arbitrary Fin.
///
/// The second element of the pair, is the same acceptance
/// condition in which all top-level disjunct not featuring
/// `Fin(i)` have been removed.
///
/// For example on
/// `Fin(1)&Inf(2)|Inf(3)&Inf(4)|Inf(5)&(Fin(1)|Fin(7))`
/// the output would be the pair
/// `(1, Fin(1)&Inf(2)|Inf(5)&(Fin(1)|Fin(7)))`.
/// On that example `Fin(1)` is prefered to `Fin(7)` because
/// it appears at the top-level.
std::pair<int, acc_code> fin_one_extract() const;
/// \brief Help closing accepting or rejecting cycle.
///
/// Assuming you have a partial cycle visiting all acceptance
@ -2157,15 +2176,6 @@ namespace spot
return code_.fin_unit();
}
/// \brief Return one acceptance set i that appear as `Fin(i)`
/// in the condition.
///
/// Return -1 if no such set exist.
int fin_one() const
{
return code_.fin_one();
}
/// \brief Find a `Inf(i)` that is a unit clause.
///
/// This return a mark_t `{i}` such that `Inf(i)` appears as a
@ -2182,6 +2192,38 @@ namespace spot
return code_.inf_unit();
}
/// \brief Return one acceptance set i that appear as `Fin(i)`
/// in the condition.
///
/// Return -1 if no such set exist.
int fin_one() const
{
return code_.fin_one();
}
/// \brief Return one acceptance set i that appears as `Fin(i)`
/// in the condition, and all disjuncts containing it.
///
/// If the condition is a disjunction and one of the disjunct
/// has the shape `...&Fin(i)&...`, then `i` will be prefered
/// over any arbitrary Fin.
///
/// The second element of the pair, is the same acceptance
/// condition in which all top-level disjunct not featuring
/// `Fin(i)` have been removed.
///
/// For example on
/// `Fin(1)&Inf(2)|Inf(3)&Inf(4)|Inf(5)&(Fin(1)|Fin(7))`
/// the output would be the pair
/// `(1, Fin(1)&Inf(2)|Inf(5)&(Fin(1)|Fin(7)))`.
/// On that example `Fin(1)` is prefered to `Fin(7)` because
/// it appears at the top-level.
std::pair<int, acc_cond> fin_one_extract() const
{
auto [f, c] = code_.fin_one_extract();
return {f, {num_sets(), std::move(c)}};
}
/// \brief Return the top-level disjuncts.
///
/// For instance, if the formula is