Awali
Another Weighted Automata library
witness.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2022 Sylvain Lombardy, Victor Marsault, Jacques Sakarovitch
3 //
4 // Awali is a free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef AWALI_ALGOS_WITNESS_HH
18 # define AWALI_ALGOS_WITNESS_HH
19 
20 #include <awali/sttc/ctx/traits.hh>
24 #include <awali/sttc/misc/raise.hh>
25 
26 namespace awali { namespace sttc {
27 
61  template <typename Context>
62  mutable_automaton<Context>
63  witness(const Context& ctx, unsigned n)
64  {
65  using context_t = Context;
66  using automaton_t = mutable_automaton<context_t>;
67  require(2 <= n, "u: n must be at least 3");
68  const auto& gens = ctx.labelset()->genset();
69  std::vector<typename context_t::labelset_t::letter_t> letters
70  {std::begin(gens), std::end(gens)};
71  require(3 <= letters.size(), "u: the alphabet needs at least 3 letters");
72  automaton_t res = make_shared_ptr<automaton_t>(ctx);
73 
74  // The states.
75  std::vector<state_t> states;
76  for (unsigned i = 0; i < n; ++i)
77  states.push_back(res->add_state());
78  res->set_initial(states[0]);
79  res->set_final(states[n-1]);
80 
81  // The 'a' transitions.
82  auto a = letters[0];
83  for (unsigned i = 0; i < n; ++i)
84  res->new_transition(states[i], states[(i+1) % n], a);
85 
86  // The 'b' transitions.
87  auto b = letters[1];
88  res->new_transition(states[0], states[1], b);
89  res->new_transition(states[1], states[0], b);
90  for (unsigned i = 2; i < n; ++i)
91  res->new_transition(states[i], states[i], b);
92 
93  // The 'c' transitions.
94  auto c = letters[2];
95  for (unsigned i = 0; i < n - 1; ++i)
96  res->new_transition(states[i], states[i], c);
97  res->new_transition(states[n - 1], states[0], c);
98 
99  return res;
100  }
101 
102 }}//end of ns awali::stc
103 
104 #endif // !AWALI_ALGOS_U_HH
The Boolean semring.
Definition: b.hh:38
The semiring of complex numbers.
Definition: c.hh:44
The semiring of Natural numbers.
Definition: n.hh:34
std::vector< state_t > states(abstract_automaton_t const *aut, bool all)
mutable_automaton< Context > witness(const Context &ctx, unsigned n)
Returns an automaton with n states.
Definition: witness.hh:63
std::shared_ptr< internal::mutable_automaton_impl< Context > > mutable_automaton
Definition: mutable_automaton.hh:45
void require(bool b, Args &&... args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:55
Main namespace of Awali.
Definition: ato.hh:22