Commit 27982fb8 authored by Laurent XU's avatar Laurent XU

parity: add spot::change_parity()

This function changes the parity acceptance of an automaton.

* spot/twaalgos/parity.cc, spot/twaalgos/parity.hh: Here
* python/spot/impl.i: Add spot/twaalgos/parity.hh
* spot/twaalgos/Makefile.am: Add spot/twaalgos/parity.{cc,hh}
* tests/core/parity.cc, tests/core/parity.test: Add
spot::change_parity() tests
* tests/python/parity.ipynb: Add documentation about
spot::change_parity()
* tests/Makefile.am: Add tests/core/parity.{cc,hh} and
tests/python/parity.ipynb
* doc/org/tut.org: Add the html page of tests/python/parity.ipynb
parent b7ef7c55
...@@ -66,6 +66,8 @@ real notebooks instead. ...@@ -66,6 +66,8 @@ real notebooks instead.
- [[https://spot.lrde.epita.fr/ipynb/accparse.html][=accparse.ipynb=]] exercises the acceptance condition parser - [[https://spot.lrde.epita.fr/ipynb/accparse.html][=accparse.ipynb=]] exercises the acceptance condition parser
- [[https://spot.lrde.epita.fr/ipynb/acc_cond.html][=acc_cond.ipynb=]] documents the interface for manipulating acceptance - [[https://spot.lrde.epita.fr/ipynb/acc_cond.html][=acc_cond.ipynb=]] documents the interface for manipulating acceptance
conditions conditions
- [[https://spot.lrde.epita.fr/ipynb/parity.html][=parity.ipynb=]] documents algorithms for manipulating parity automata
in Python
- [[https://spot.lrde.epita.fr/ipynb/product.html][=product.ipynb=]] shows how to re-implement the product of two automata - [[https://spot.lrde.epita.fr/ipynb/product.html][=product.ipynb=]] shows how to re-implement the product of two automata
in Python in Python
- [[https://spot.lrde.epita.fr/ipynb/randltl.html][=randltl.ipynb=]] demonstrates a Python-version of [[file:randltl.org][=randltl=]] - [[https://spot.lrde.epita.fr/ipynb/randltl.html][=randltl.ipynb=]] demonstrates a Python-version of [[file:randltl.org][=randltl=]]
......
...@@ -151,6 +151,7 @@ ...@@ -151,6 +151,7 @@
#include <spot/twaalgos/simulation.hh> #include <spot/twaalgos/simulation.hh>
#include <spot/twaalgos/split.hh> #include <spot/twaalgos/split.hh>
#include <spot/twaalgos/sum.hh> #include <spot/twaalgos/sum.hh>
#include <spot/twaalgos/parity.hh>
#include <spot/twaalgos/postproc.hh> #include <spot/twaalgos/postproc.hh>
#include <spot/twaalgos/product.hh> #include <spot/twaalgos/product.hh>
#include <spot/twaalgos/stutter.hh> #include <spot/twaalgos/stutter.hh>
...@@ -582,6 +583,7 @@ def state_is_accepting(self, src) -> "bool": ...@@ -582,6 +583,7 @@ def state_is_accepting(self, src) -> "bool":
%include <spot/twaalgos/isunamb.hh> %include <spot/twaalgos/isunamb.hh>
%include <spot/twaalgos/isweakscc.hh> %include <spot/twaalgos/isweakscc.hh>
%include <spot/twaalgos/simulation.hh> %include <spot/twaalgos/simulation.hh>
%include <spot/twaalgos/parity.hh>
%include <spot/twaalgos/postproc.hh> %include <spot/twaalgos/postproc.hh>
%include <spot/twaalgos/product.hh> %include <spot/twaalgos/product.hh>
%include <spot/twaalgos/split.hh> %include <spot/twaalgos/split.hh>
......
...@@ -62,6 +62,7 @@ twaalgos_HEADERS = \ ...@@ -62,6 +62,7 @@ twaalgos_HEADERS = \
minimize.hh \ minimize.hh \
couvreurnew.hh \ couvreurnew.hh \
neverclaim.hh \ neverclaim.hh \
parity.hh \
postproc.hh \ postproc.hh \
powerset.hh \ powerset.hh \
product.hh \ product.hh \
...@@ -125,6 +126,7 @@ libtwaalgos_la_SOURCES = \ ...@@ -125,6 +126,7 @@ libtwaalgos_la_SOURCES = \
couvreurnew.cc \ couvreurnew.cc \
ndfs_result.hxx \ ndfs_result.hxx \
neverclaim.cc \ neverclaim.cc \
parity.cc \
postproc.cc \ postproc.cc \
powerset.cc \ powerset.cc \
product.cc \ product.cc \
......
// -*- coding: utf-8 -*-
// Copyright (C) 2016 Laboratoire de Recherche et Développement
// de l'Epita (LRDE).
//
// This file is part of Spot, a model checking library.
//
// Spot is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// Spot is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
// License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <spot/twaalgos/parity.hh>
#include <spot/twa/twagraph.hh>
#include <spot/twaalgos/product.hh>
#include <vector>
#include <utility>
#include <functional>
#include <queue>
#include <spot/twaalgos/hoa.hh>
namespace spot
{
namespace
{
unsigned change_set(unsigned x, unsigned num_sets,
bool change_kind, bool change_style)
{
// If the parity acceptance kind is changed,
// then the index of the sets are reversed
if (change_kind)
x = num_sets - x - 1;
// If the parity style is changed, then all the existing acceptance
// sets are shifted
x += change_style;
return x;
}
void
change_acc(twa_graph_ptr& aut, unsigned num_sets, bool change_kind,
bool change_style, bool output_max, bool input_max)
{
for (auto& e: aut->edge_vector())
if (e.acc)
{
unsigned msb = (input_max ? e.acc.max_set() : e.acc.min_set()) - 1;
e.acc = acc_cond::mark_t{change_set(msb, num_sets, change_kind,
change_style)};
}
else if (output_max && change_style)
{
// If the parity is changed, a new set is introduced.
// This new set is used to mark all the transitions of the input
// that don't belong to any acceptance sets.
e.acc.set(0);
}
}
}
twa_graph_ptr
change_parity(const const_twa_graph_ptr& aut,
parity_kind kind, parity_style style)
{
return change_parity_here(make_twa_graph(aut, twa::prop_set::all()),
kind, style);
}
twa_graph_ptr
change_parity_here(twa_graph_ptr aut, parity_kind kind, parity_style style)
{
bool current_max;
bool current_odd;
if (!aut->acc().is_parity(current_max, current_odd, true))
throw new std::invalid_argument("change_parity: input must have a parity "
"acceptance.");
auto old_num_sets = aut->num_sets();
bool output_max = true;
switch (kind)
{
case parity_kind_max:
output_max = true;
break;
case parity_kind_min:
output_max = false;
break;
case parity_kind_same:
output_max = current_max;
break;
case parity_kind_any:
// If we need to change the style we may change the kind not to
// introduce new accset.
output_max = (((style == parity_style_odd && !current_odd)
|| (style == parity_style_even && current_odd))
&& old_num_sets % 2 == 0) != current_max;
break;
}
bool change_kind = current_max != output_max;
bool toggle_style = change_kind && (old_num_sets % 2 == 0);
bool output_odd = true;
switch (style)
{
case parity_style_odd:
output_odd = true;
break;
case parity_style_even:
output_odd = false;
break;
case parity_style_same:
output_odd = current_odd;
break;
case parity_style_any:
output_odd = current_odd != toggle_style;
// If we need to change the kind we may change the style not to
// introduce new accset.
break;
}
current_odd = current_odd != toggle_style;
bool change_style = false;
auto num_sets = old_num_sets;
// If the parity neeeds to be changed, then a new acceptance set is created.
// The old acceptance sets are shifted
if (output_odd != current_odd)
{
change_style = true;
++num_sets;
}
if (change_kind || change_style)
{
auto new_acc = acc_cond::acc_code::parity(output_max,
output_odd, num_sets);
aut->set_acceptance(num_sets, new_acc);
}
change_acc(aut, old_num_sets, change_kind,
change_style, output_max, current_max);
return aut;
}
}
// -*- coding: utf-8 -*-
// Copyright (C) 2016 Laboratoire de Recherche et Développement
// de l'Epita (LRDE).
//
// This file is part of Spot, a model checking library.
//
// Spot is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// Spot is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
// License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#pragma once
#include <spot/misc/common.hh>
#include <spot/twa/fwd.hh>
namespace spot
{
/// \addtogroup parity_algorithms Parity algorithms
/// \ingroup twa_algorithms
/// @{
/// \brief Parity kind type
enum parity_kind
{
/// The new acceptance will be a parity max
parity_kind_max,
/// The new acceptance will be a parity min
parity_kind_min,
/// The new acceptance will keep the kind
parity_kind_same,
/// The new acceptance may change the kind
parity_kind_any
};
/// \brief Parity style type
enum parity_style
{
/// The new acceptance will be a parity odd
parity_style_odd,
/// The new acceptance will be a parity even
parity_style_even,
/// The new acceptance will keep the style
parity_style_same,
/// The new acceptance may change the style
parity_style_any
};
/// \brief Change the parity acceptance of an automaton
///
/// The parity acceptance condition of an automaton is characterized by
/// - The kind of the acceptance (min or max).
/// - The parity style, i.e., parity of the sets seen infinitely often
/// (odd or even).
/// - The number of acceptance sets.
///
/// The output will be an equivalent automaton with the new parity acceptance.
/// The number of acceptance sets may be increased by one. Every transition
/// will belong to at most one acceptance set. The automaton must have a
/// parity acceptance, otherwise an invalid_argument exception is thrown.
///
/// The parity kind is defined only if the number of acceptance sets
/// is strictly greater than 1. The parity_style is defined only if the number
/// of acceptance sets is non-zero. Some values of kind and style may result
/// in equivalent outputs if the number of acceptance sets of the input
/// automaton is not great enough.
///
/// \param aut the input automaton
///
/// \param kind the parity kind of the output automaton
///
/// \param style the parity style of the output automaton
///
/// \return the automaton with the new acceptance
/// @{
SPOT_API twa_graph_ptr
change_parity(const const_twa_graph_ptr& aut,
parity_kind kind, parity_style style);
SPOT_API twa_graph_ptr
change_parity_here(twa_graph_ptr aut, parity_kind kind, parity_style style);
/// @}
/// @}
}
...@@ -82,6 +82,7 @@ check_PROGRAMS = \ ...@@ -82,6 +82,7 @@ check_PROGRAMS = \
core/nequals \ core/nequals \
core/nenoform \ core/nenoform \
core/ngraph \ core/ngraph \
core/parity \
core/randtgba \ core/randtgba \
core/readsat \ core/readsat \
core/reduc \ core/reduc \
...@@ -128,6 +129,7 @@ core_nenoform_SOURCES = core/equalsf.cc ...@@ -128,6 +129,7 @@ core_nenoform_SOURCES = core/equalsf.cc
core_nenoform_CPPFLAGS = $(AM_CPPFLAGS) -DNENOFORM core_nenoform_CPPFLAGS = $(AM_CPPFLAGS) -DNENOFORM
core_nequals_SOURCES = core/equalsf.cc core_nequals_SOURCES = core/equalsf.cc
core_nequals_CPPFLAGS = $(AM_CPPFLAGS) -DNEGATE core_nequals_CPPFLAGS = $(AM_CPPFLAGS) -DNEGATE
core_parity_SOURCES = core/parity.cc
core_reduc_SOURCES = core/reduc.cc core_reduc_SOURCES = core/reduc.cc
core_reduccmp_SOURCES = core/equalsf.cc core_reduccmp_SOURCES = core/equalsf.cc
core_reduccmp_CPPFLAGS = $(AM_CPPFLAGS) -DREDUC core_reduccmp_CPPFLAGS = $(AM_CPPFLAGS) -DREDUC
...@@ -301,7 +303,8 @@ TESTS_twa = \ ...@@ -301,7 +303,8 @@ TESTS_twa = \
core/cycles.test \ core/cycles.test \
core/acc_word.test \ core/acc_word.test \
core/dca.test \ core/dca.test \
core/dnfstreett.test core/dnfstreett.test \
core/parity.test
############################## PYTHON ############################## ############################## PYTHON ##############################
...@@ -324,6 +327,7 @@ TESTS_ipython = \ ...@@ -324,6 +327,7 @@ TESTS_ipython = \
python/ltsmin-dve.ipynb \ python/ltsmin-dve.ipynb \
python/ltsmin-pml.ipynb \ python/ltsmin-pml.ipynb \
python/piperead.ipynb \ python/piperead.ipynb \
python/parity.ipynb \
python/product.ipynb \ python/product.ipynb \
python/randaut.ipynb \ python/randaut.ipynb \
python/randltl.ipynb \ python/randltl.ipynb \
......
This diff is collapsed.
#!/bin/sh
# -*- coding: utf-8 -*-
# Copyright (C) 2016 Laboratoire de Recherche et Développement
# de l'Epita (LRDE).
#
# This file is part of Spot, a model checking library.
#
# Spot is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Spot is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
. ./defs
set -e
run 0 ../parity
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