Awali
Another Weighted Automata library
is_valid.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_IS_VALID_HH
18 # define AWALI_ALGOS_IS_VALID_HH
19 
20 # include <stdexcept>
21 # include <type_traits>
22 # include <unordered_map>
23 # include <utility>
24 # include <vector>
25 
27 #include <awali/sttc/algos/copy.hh>
31 #include <awali/sttc/core/kind.hh>
32 #include <awali/common/enums.hh>
33 
34 namespace awali {
35  namespace sttc {
36 
37 
38  /*,----------------.
39  | is_valid(aut). |
40  `----------------'*/
41 
42  namespace internal {
43 
46  template <typename Aut>
47  typename Aut::element_type::automaton_nocv_t
48  absval(const Aut& aut)
49  {
50  auto res = copy(aut);
51  // Apply absolute value to the weight of each transition.
52  const auto& ws = *aut->weightset();
53  for (auto t: res->transitions())
54  res->set_weight(t, ws.abs(res->weight_of(t)));
55  return res;
56  }
57 
60  template <typename Aut>
61  bool is_properable(Aut&& aut)
62  {
63  return in_situ_remover(aut, false);
64  }
65 
66 
67  template <typename Aut, bool has_one = context_t_of<Aut>::has_one()>
68  class is_valider {
69  public:
70  using automaton_t = typename std::remove_cv<Aut>::type;
72 
73  static bool is_valid(const automaton_t& aut) {
74  return is_valid_<weightset_t::star_status()>(aut);
75  }
76 
77  private:
78  template <star_status_t Status>
79  static
80  typename std::enable_if<Status == star_status_t::TOPS,
81  bool>::type
82  is_valid_(const automaton_t& aut) {
83  return (is_proper(aut)
84  || is_eps_acyclic(aut)
85  || is_properable(copy(aut)));
86  }
87 
88  template <star_status_t Status>
89  static
90  typename std::enable_if<Status == star_status_t::ABSVAL,
91  bool>::type
92  is_valid_(const automaton_t& aut) {
93  return (is_proper(aut)
94  || is_eps_acyclic(aut)
95  || is_properable(absval(aut)));
96  }
97 
98  template <star_status_t Status>
99  static
100  typename std::enable_if<Status == star_status_t::STARRABLE,
101  bool>::type
102  is_valid_(const automaton_t&) {
103  return true;
104  }
105 
106  template <star_status_t Status>
107  static
108  typename std::enable_if<Status == star_status_t::NON_STARRABLE,
109  bool>::type
110  is_valid_(const automaton_t& aut) {
111  return is_proper(aut) || is_eps_acyclic(aut);
112  }
113  };
114 
115  template <typename Aut>
116  class is_valider<Aut, false> {
117  using automaton_t = typename std::remove_cv<Aut>::type;
118  public:
119  static constexpr bool is_valid(const automaton_t&)
120  {
121  return true;
122  }
123  };
124 
125  }
126 
127 
160  template <typename Aut>
161  inline
162  bool is_valid(const Aut& aut)
163  {
165  }
166 
167  /*,-------------------.
168  | is_valid(ratexp). |
169  `-------------------'*/
170 
171  template <typename RatExpSet>
172  bool
173  is_valid(const RatExpSet& rs, const typename RatExpSet::ratexp_t& e)
174  {
176  try
177  {
178  constant_term(e);
179  return true;
180  }
181  catch (const std::runtime_error&)
182  {
183  return false;
184  }
185  }
186 
187  }
188 }//end of ns awali::stc
189 
190 #endif // !AWALI_ALGOS_IS_VALID_HH
static constexpr bool is_valid(const automaton_t &)
Definition: is_valid.hh:119
Definition: is_valid.hh:68
static bool is_valid(const automaton_t &aut)
Definition: is_valid.hh:73
weightset_t_of< automaton_t > weightset_t
Definition: is_valid.hh:71
typename std::remove_cv< Aut >::type automaton_t
Definition: is_valid.hh:70
Definition: constant_term.hh:40
@ NON_STARRABLE
Definition: enums.hh:166
@ TOPS
Definition: enums.hh:167
@ ABSVAL
Definition: enums.hh:168
@ STARRABLE
The star of every element exists.
Definition: enums.hh:165
bool is_properable(Aut &&aut)
Whether proper_here(aut) succeeds.
Definition: is_valid.hh:61
Aut::element_type::automaton_nocv_t absval(const Aut &aut)
Copy of aut, with absolute values.
Definition: is_valid.hh:48
bool is_proper(const Aut &aut) ATTRIBUTE_CONST
Test whether an automaton is proper.
Definition: is_proper.hh:94
AutOut copy(const AutIn &input, Pred keep_state, bool keep_history=true, bool transpose=false)
A copy of input keeping only its states that are accepted by keep_state.
Definition: copy.hh:189
bool in_situ_remover(Aut &aut, bool prune=true)
Blindly eliminate epsilon transitions without checking for the validity of the automaton.
Definition: proper.hh:418
weight_t_of< RatExpSet > constant_term(const RatExpSet &rs, const typename RatExpSet::ratexp_t &e)
Definition: constant_term.hh:155
ATTRIBUTE_CONST bool is_eps_acyclic(const Aut &aut)
Definition: is_eps_acyclic.hh:123
bool is_valid(const Aut &aut)
Tests if aut is valid.
Definition: is_valid.hh:162
typename internal::weightset_t_of_impl< internal::base_t< ValueSet > >::type weightset_t_of
Helper to retrieve the type of the weightset of a value set.
Definition: traits.hh:86
Main namespace of Awali.
Definition: ato.hh:22