Vcsn  2.3
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  auto remover = detail::epsilon_remover<Aut>{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()>
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_valid_impl<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  template <Automaton Aut>
139  bool is_valid(const Aut& aut)
140  {
142  }
143 
144  namespace dyn
145  {
146  namespace detail
147  {
149  template <Automaton Aut>
150  bool is_valid(const automaton& aut)
151  {
152  const auto& a = aut->as<Aut>();
153  return is_valid(strip(a));
154  }
155  }
156  }
157 } // namespace vcsn
std::remove_cv_t< Aut > automaton_t
Definition: is-valid.hh:128
automaton strip(const automaton &aut)
Bridge.
Definition: strip.hh:46
static std::enable_if_t< Status==star_status_t::TOPS, bool > is_valid_(const automaton_t &aut)
Definition: is-valid.hh:91
auto transitions(const Aut &aut) -> decltype(all_transitions(aut, is_special_t< Aut >
All the transition indexes between visible states.
Definition: automaton.hh:226
This class contains the core of the proper algorithm.
static std::enable_if_t< Status==star_status_t::ABSVAL, bool > is_valid_(const automaton_t &aut)
Definition: is-valid.hh:101
return res
Definition: multiply.hh:398
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:322
static std::enable_if_t< Status==star_status_t::NON_STARRABLE, bool > is_valid_(const automaton_t &aut)
Definition: is-valid.hh:119
Definition: a-star.hh:8
ATTRIBUTE_CONST std::enable_if_t< context_t_of< Aut >::has_one(), bool > is_eps_acyclic(const Aut &aut)
Detect epsilon-circuits.
Definition: is-acyclic.hh:116
static std::enable_if_t< Status==star_status_t::STARRABLE, bool > is_valid_(const automaton_t &)
Definition: is-valid.hh:111
std::remove_cv_t< Aut > automaton_t
Definition: is-valid.hh:61
A dyn automaton.
Definition: automaton.hh:17
weightset_t_of< automaton_t > weightset_t
Definition: is-valid.hh:62
bool is_valid(const Aut &aut)
Definition: is-valid.hh:139
static constexpr bool is_valid(const automaton_t &)
Definition: is-valid.hh:130
bool is_proper(const Aut &aut)
Test whether an automaton is proper.
Definition: is-proper.hh:47
static bool is_valid(const automaton_t &aut)
Whether an automaton is valid.
Definition: is-valid.hh:82
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:67
auto & as()
Extract wrapped typed automaton.
Definition: automaton.hh:37
bool is_properable(Aut &&aut)
Whether proper_here(aut) succeeds.
Definition: is-valid.hh:42
fresh_automaton_t_of< Aut > absval(const Aut &aut)
Copy of aut, with absolute values.
Definition: is-valid.hh:29
bool is_valid(const automaton &aut)
Bridge.
Definition: is-valid.hh:150