Vcsn  2.2
Be Rational
is-valid.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <type_traits>
4 
5 #include <vcsn/algos/copy.hh>
8 #include <vcsn/algos/strip.hh>
9 #include <vcsn/core/kind.hh>
10 #include <vcsn/misc/star-status.hh>
11 #include <vcsn/misc/direction.hh>
12 
14 
15 namespace vcsn
16 {
17 
18  /*----------------.
19  | is_valid(aut). |
20  `----------------*/
21 
22  namespace detail
23  {
24 
27  template <Automaton Aut>
28  fresh_automaton_t_of<Aut>
29  absval(const Aut& aut)
30  {
31  auto res = copy(aut);
32  // Apply absolute value to the weight of each transition.
33  const auto& ws = *aut->weightset();
34  for (auto t: transitions(res))
35  res->set_weight(t, ws.abs(res->weight_of(t)));
36  return res;
37  }
38 
41  template <Automaton Aut>
42  bool is_properable(Aut&& aut)
43  {
44  try
45  {
46  detail::epsilon_remover<Aut> remover(aut, true);
47  remover();
48  return true;
49  }
50  catch (const std::runtime_error&)
51  {
52  return false;
53  }
54  }
55 
56 
57  template <Automaton Aut, bool has_one = context_t_of<Aut>::has_one()>
58  class is_valider
59  {
60  public:
61  using automaton_t = std::remove_cv_t<Aut>;
63 
82  static bool is_valid(const automaton_t& aut)
83  {
84  return is_valid_<weightset_t::star_status()>(aut);
85  }
86 
87  private:
88  template <star_status_t Status>
89  static
90  std::enable_if_t<Status == star_status_t::TOPS, bool>
91  is_valid_(const automaton_t& aut)
92  {
93  return (is_proper(aut)
94  || is_eps_acyclic(aut)
95  || is_properable(copy(aut)));
96  }
97 
98  template <star_status_t Status>
99  static
100  std::enable_if_t<Status == star_status_t::ABSVAL, bool>
101  is_valid_(const automaton_t& aut)
102  {
103  return (is_proper(aut)
104  || is_eps_acyclic(aut)
105  || is_properable(absval(aut)));
106  }
107 
108  template <star_status_t Status>
109  static
110  std::enable_if_t<Status == star_status_t::STARRABLE, bool>
112  {
113  return true;
114  }
115 
116  template <star_status_t Status>
117  static
118  std::enable_if_t<Status == star_status_t::NON_STARRABLE, bool>
119  is_valid_(const automaton_t& aut)
120  {
121  return is_proper(aut) || is_eps_acyclic(aut);
122  }
123  };
124 
125  template <Automaton Aut>
126  class is_valider<Aut, false>
127  {
128  using automaton_t = std::remove_cv_t<Aut>;
129  public:
130  static constexpr bool is_valid(const automaton_t&)
131  {
132  return true;
133  }
134  };
135 
136  }
137 
138 
139  template <Automaton Aut>
140  inline
141  bool is_valid(const Aut& aut)
142  {
144  }
145 
146  namespace dyn
147  {
148  namespace detail
149  {
151  template <Automaton Aut>
152  bool is_valid(const automaton& aut)
153  {
154  const auto& a = aut->as<Aut>();
155  return is_valid(strip(a));
156  }
157  }
158  }
159 } // namespace vcsn
bool is_proper(const Aut &aut)
Test whether an automaton is proper.
Definition: is-proper.hh:48
This class contains the core of the proper algorithm.
fresh_automaton_t_of< Aut > absval(const Aut &aut)
Copy of aut, with absolute values.
Definition: is-valid.hh:29
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:59
static std::enable_if_t< Status==star_status_t::STARRABLE, bool > is_valid_(const automaton_t &)
Definition: is-valid.hh:111
bool is_valid(const automaton &aut)
Bridge.
Definition: is-valid.hh:152
Definition: a-star.hh:8
weightset_t_of< automaton_t > weightset_t
Definition: is-valid.hh:62
static constexpr bool is_valid(const automaton_t &)
Definition: is-valid.hh:130
automaton strip(const automaton &aut)
Bridge.
Definition: strip.hh:46
ATTRIBUTE_CONST bool is_eps_acyclic(const Aut &aut)
std::remove_cv_t< Aut > automaton_t
Definition: is-valid.hh:61
std::shared_ptr< detail::automaton_base > automaton
Definition: automaton.hh:69
auto transitions(const Aut &aut) -> decltype(all_transitions(aut, is_special_t< Aut >
All the transition indexes between visible states.
Definition: automaton.hh:218
bool is_properable(Aut &&aut)
Whether proper_here(aut) succeeds.
Definition: is-valid.hh:42
bool is_valid(const Aut &aut)
Definition: is-valid.hh:141
std::remove_cv_t< Aut > automaton_t
Definition: is-valid.hh:128
static std::enable_if_t< Status==star_status_t::NON_STARRABLE, bool > is_valid_(const automaton_t &aut)
Definition: is-valid.hh:119
static std::enable_if_t< Status==star_status_t::ABSVAL, bool > is_valid_(const automaton_t &aut)
Definition: is-valid.hh:101
static std::enable_if_t< Status==star_status_t::TOPS, bool > is_valid_(const automaton_t &aut)
Definition: is-valid.hh:91
static bool is_valid(const automaton_t &aut)
Whether an automaton is valid.
Definition: is-valid.hh:82
auto copy(const AutIn &input, KeepState keep_state, KeepTrans keep_trans) -> decltype(keep_state(input->null_state()), keep_trans(input->null_transition()), make_fresh_automaton< AutIn, AutOut >(input))
A copy of input keeping only its states that are accepted by keep_state, and transitions accepted by ...
Definition: copy.hh:308