spot/tests/core/twagraph.cc
Florian Renkin 96ff2225e3 Fix typos in doc, comments and messages
* bin/README, bin/common_conv.hh, bin/common_trans.cc,
    bin/ltlsynt.cc, bin/spot-x.cc, spot/gen/automata.hh,
    spot/graph/graph.hh, spot/ltsmin/ltsmin.hh,
    spot/ltsmin/spins_interface.hh, spot/ltsmin/spins_kripke.hh,
    spot/mc/bloemen.hh, spot/mc/bloemen_ec.hh, spot/mc/cndfs.hh,
    spot/mc/deadlock.hh, spot/mc/intersect.hh, spot/mc/lpar13.hh,
    spot/mc/mc_instanciator.hh, spot/misc/bareword.cc,
    spot/misc/fixpool.hh, spot/misc/formater.hh, spot/misc/minato.hh,
    spot/misc/satsolver.hh, spot/misc/timer.hh,
    spot/parseaut/public.hh, spot/priv/partitioned_relabel.cc,
    spot/priv/satcommon.hh, spot/ta/ta.hh, spot/ta/taexplicit.cc,
    spot/ta/taproduct.hh, spot/ta/tgta.hh, spot/taalgos/reachiter.hh,
    spot/taalgos/tgba2ta.hh, spot/tl/apcollect.cc,
    spot/tl/apcollect.hh, spot/tl/formula.cc, spot/tl/parse.hh,
    spot/tl/randomltl.hh, spot/tl/relabel.hh, spot/tl/simplify.cc,
    spot/twa/acc.hh, spot/twa/bddprint.hh, spot/twa/formula2bdd.cc,
    spot/twa/twa.hh, spot/twa/twagraph.cc, spot/twa/twagraph.hh,
    spot/twaalgos/aiger.cc, spot/twaalgos/aiger.hh,
    spot/twaalgos/alternation.hh,  spot/twaalgos/cleanacc.cc,
    spot/twaalgos/cobuchi.cc, spot/twaalgos/contains.cc,
    spot/twaalgos/couvreurnew.cc, spot/twaalgos/cycles.hh,
    spot/twaalgos/degen.cc, spot/twaalgos/degen.hh,
    spot/twaalgos/dot.hh, spot/twaalgos/dtbasat.cc,
    spot/twaalgos/dtwasat.cc, spot/twaalgos/dtwasat.hh,
    spot/twaalgos/dualize.cc, spot/twaalgos/emptiness.hh,
    spot/twaalgos/emptiness_stats.hh, spot/twaalgos/game.cc,
    spot/twaalgos/genem.hh, spot/twaalgos/hoa.hh,
    spot/twaalgos/langmap.hh, spot/twaalgos/ltl2tgba_fm.hh,
    spot/twaalgos/magic.cc, spot/twaalgos/magic.hh,
    spot/twaalgos/mask.hh, spot/twaalgos/mealy_machine.cc,
    spot/twaalgos/mealy_machine.hh,
    spot/twaalgos/minimize.hh, spot/twaalgos/parity.cc,
    spot/twaalgos/parity.hh, spot/twaalgos/postproc.cc,
    spot/twaalgos/product.hh, spot/twaalgos/reachiter.hh,
    spot/twaalgos/relabel.cc, spot/twaalgos/remfin.cc,
    spot/twaalgos/remfin.hh, spot/twaalgos/sccfilter.cc,
    spot/twaalgos/sccinfo.hh, spot/twaalgos/se05.cc,
    spot/twaalgos/se05.hh, spot/twaalgos/simulation.hh,
    spot/twaalgos/split.hh, spot/twaalgos/stats.hh,
    spot/twaalgos/synthesis.cc, spot/twaalgos/synthesis.hh,
    spot/twaalgos/tau03.hh, spot/twaalgos/tau03opt.hh,
    spot/twaalgos/toparity.hh, spot/twaalgos/totgba.hh,
    spot/twaalgos/translate.hh, spot/twaalgos/word.cc,
    spot/twaalgos/word.hh, spot/twaalgos/zlktree.cc,
    spot/twaalgos/zlktree.hh, spot/twacube/cube.hh,
    spot/twacube/twacube.hh, tests/core/cube.cc,
    tests/core/ltlsynt.test, tests/core/parity.cc,
    tests/core/safra.cc, tests/core/twagraph.cc: here
2024-04-16 17:01:31 +02:00

270 lines
6.9 KiB
C++

// -*- coding: utf-8 -*-
// Copyright (C) by the Spot authors, see the AUTHORS file for details.
//
// 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 "config.h"
#include <iostream>
#include <spot/twa/twagraph.hh>
#include <spot/twaalgos/dot.hh>
#include <spot/twaalgos/hoa.hh>
#include <spot/tl/defaultenv.hh>
static void f1()
{
auto d = spot::make_bdd_dict();
auto tg = make_twa_graph(d);
bdd p1 = bdd_ithvar(tg->register_ap("p1"));
bdd p2 = bdd_ithvar(tg->register_ap("p2"));
tg->acc().add_sets(2);
for (auto f: tg->ap())
std::cout << f.ap_name() << '\n';
auto s1 = tg->new_state();
auto s2 = tg->new_state();
auto s3 = tg->new_state();
tg->new_edge(s1, s1, bddfalse, {});
tg->new_edge(s1, s2, p1, {});
tg->new_edge(s1, s3, p2, tg->acc().mark(1));
tg->new_edge(s2, s3, p1 & p2, tg->acc().mark(0));
tg->new_edge(s3, s1, p1 | p2, spot::acc_cond::mark_t({0, 1}));
tg->new_edge(s3, s2, p1 >> p2, {});
tg->new_edge(s3, s3, bddtrue, spot::acc_cond::mark_t({0, 1}));
spot::print_dot(std::cout, tg);
{
auto i = tg->get_graph().out_iteraser(s3);
++i;
i.erase();
i.erase();
assert(!i);
spot::print_dot(std::cout, tg);
}
{
auto i = tg->get_graph().out_iteraser(s3);
i.erase();
assert(!i);
spot::print_dot(std::cout, tg);
}
spot::acc_cond::mark_t all({0, 1});
tg->new_edge(s3, s1, p1 | p2, all);
tg->new_edge(s3, s2, p1 >> p2, {});
tg->new_edge(s3, s1, bddtrue, all);
std::cerr << tg->num_edges() << '\n';
assert(tg->num_edges() == 7);
spot::print_dot(std::cout, tg);
tg->merge_edges();
spot::print_dot(std::cout, tg);
std::cerr << tg->num_edges() << '\n';
assert(tg->num_edges() == 5);
// Add enough states so that the state vector is reallocated.
for (unsigned i = 0; i < 100; ++i)
tg->new_state();
spot::print_dot(std::cout, tg);
}
// Test purge with named and highlighted states.
static void f2()
{
auto
d = spot::make_bdd_dict();
auto tg = make_twa_graph(d);
auto s1 = tg->new_state();
auto s2 = tg->new_state();
auto s3 = tg->new_state();
(void) s2;
tg->set_named_prop("state-names",
new std::vector<std::string>({"s1", "s2", "s3"}));
{
auto hs = new std::map<unsigned, unsigned>;
hs->emplace(s1, 5);
hs->emplace(s3, 7);
tg->set_named_prop("highlight-states", hs);
}
tg->set_init_state(s3);
spot::print_dot(std::cout, tg);
void (*action)(const std::vector<unsigned>&, void*) =
[](const std::vector<unsigned>& newst, void*)
{
for (unsigned i = 0; i != newst.size(); ++i)
{
std::cout << i << " -> ";
if (newst[i] == -1U)
std::cout << "deleted" << std::endl;
else
std::cout << newst[i] << std::endl;
}
};
tg->purge_unreachable_states(&action);
spot::print_dot(std::cout, tg);
}
// Make sure the HOA printer adjusts the highlighted edges numbers
static void f3()
{
auto d = spot::make_bdd_dict();
auto tg = make_twa_graph(d);
auto hs = new std::map<unsigned, unsigned>;
tg->set_named_prop("highlight-edges", hs);
auto s1 = tg->new_state();
auto s2 = tg->new_state();
auto s3 = tg->new_state();
tg->set_init_state(s3);
hs->emplace(tg->new_edge(s3, s2, bddtrue), 1);
hs->emplace(tg->new_edge(s2, s1, bddtrue), 2);
hs->emplace(tg->new_edge(s1, s1, bddtrue), 3);
spot::print_hoa(std::cout, tg, "1.1") << '\n';
}
// Test creation of universal edges via initializer-list
static void f4()
{
auto d = spot::make_bdd_dict();
auto tg = make_twa_graph(d);
auto s1 = tg->new_state();
auto s2 = tg->new_state();
auto s3 = tg->new_state();
tg->set_univ_init_state({s3, s1});
tg->new_univ_edge(s3, {s1, s2}, bddtrue);
tg->new_univ_edge(s2, {s1}, bddtrue);
tg->new_edge(s1, s1, bddtrue);
spot::print_hoa(std::cout, tg, "1.1") << '\n';
}
// Test merge_states()
static void f5()
{
auto d = spot::make_bdd_dict();
auto tg = make_twa_graph(d);
auto s1 = tg->new_state();
auto s2 = tg->new_state();
auto s3 = tg->new_state();
auto s4 = tg->new_state();
auto s5 = tg->new_state();
tg->set_init_state(s5);
tg->new_edge(s1, s2, bddtrue);
tg->new_edge(s2, s2, bddtrue);
tg->new_edge(s3, s2, bddtrue);
tg->new_edge(s4, s4, bddtrue);
tg->new_edge(s5, s1, bddtrue);
tg->new_edge(s5, s2, bddtrue);
tg->new_edge(s5, s3, bddtrue);
tg->new_edge(s5, s4, bddtrue);
unsigned out = tg->merge_states();
std::cerr << out << '\n';
assert(out == 3);
spot::print_hoa(std::cout, tg) << '\n';
}
// Test merge_states_of()
static void f6()
{
auto d = spot::make_bdd_dict();
auto tg = make_twa_graph(d);
auto s1 = tg->new_state();
auto s2 = tg->new_state();
auto s3 = tg->new_state();
auto s4 = tg->new_state();
auto s5 = tg->new_state();
tg->set_init_state(s5);
tg->new_edge(s1, s2, bddtrue);
tg->new_edge(s2, s2, bddtrue);
tg->new_edge(s3, s2, bddtrue);
tg->new_edge(s4, s4, bddtrue);
tg->new_edge(s5, s1, bddtrue);
tg->new_edge(s5, s2, bddtrue);
tg->new_edge(s5, s3, bddtrue);
tg->new_edge(s5, s4, bddtrue);
unsigned out = tg->merge_states_of();
assert(out == 3);
(void) out;
}
// Compare merge_states() and merge_states_of()
// when faced with a more involved problem
static void f7()
{
// The current merge_states implementation of "next"
// needs two successive calls to obtain an automaton with only 3 states
// This is especially annoying as this depends on the numbering.
// By renumbering 2->1 3->2 1->3 the current version only needs one call too
auto get_aut = []()
{
auto d = spot::make_bdd_dict();
auto aut = make_twa_graph(d);
aut->new_states(5);
aut->new_edge(0, 1, bddtrue);
aut->new_edge(0, 2, bddtrue);
aut->new_edge(1, 1, bddtrue);
aut->new_edge(1, 4, bddtrue);
aut->new_edge(2, 3, bddtrue);
aut->new_edge(2, 4, bddtrue);
aut->new_edge(3, 3, bddtrue);
aut->new_edge(3, 4, bddtrue);
return aut;
};
auto aut = get_aut();
// Basic merge_states needs 2 iterations
// Merging only one step each
assert(aut->merge_states() == 1u);
assert(aut->merge_states() == 1u);
assert(aut->num_states() == 3u);
// merge_states_of can do it in one iteration
// when used on all states
aut = get_aut();
assert(aut->merge_states_of() == 2u);
assert(aut->num_states() == 3u);
}
int main()
{
f1();
f2();
f3();
f4();
f5();
f6();
f7();
}