Awali
Another Weighted Automata library
draw_exp.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2023 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_DRAW_EXP_HH
18 # define AWALI_ALGOS_DRAW_EXP_HH
19 
20 #include <awali/sttc/ctx/fwd.hh>
21 #include <awali/sttc/ctx/traits.hh>
24 #include <awali/sttc/misc/raise.hh>
28 
29 namespace awali { namespace sttc {
30 
31  namespace rat
32  {
35  template <typename RatExpSet>
37  : public RatExpSet::context_t::const_visitor
38  {
39  public:
43  using context_t = typename RatExpSet::context_t;
47  using super_type = typename context_t::const_visitor;
49 
50  constexpr static const char* me() { return "draw_exp"; }
51 
52  drawexp_visitor(const RatExpSet& rs)
53  : ctx_(rs.context())
54  { }
55 
57  operator()(const typename RatExpSet::ratexp_t& v)
58  {
59  v->accept(*this);
60  res_->set_initial(initial_);
61  return std::move(res_);
62  }
63 
65  {
66  initial_ = res_->add_state();
67  res_->set_state_name(initial_,"[z]");
68  }
69 
71  {
72  initial_ = res_->add_state();
73  res_->set_state_name(initial_,"[e]");
74  }
75 
77  {
78  std::ostringstream oss;
79  ls_.print(e.value(), oss);
80  initial_ = res_->add_state();
81  res_->set_state_name(initial_,oss.str());
82  }
83 
85  {
86  state_t initial = res_->add_state();
87  std::string l("");
88  for (auto c: e)
89  {
90  c->accept(*this);
91  l+=".";
92  res_->new_transition(initial, initial_, l);
93  }
94  initial_ = initial;
95  res_->set_state_name(initial_,"[+]");
96  }
97 
103 
105  {
106  state_t initial = res_->add_state();
107  std::string l("");
108  for (auto c: e)
109  {
110  c->accept(*this);
111  l+=".";
112  res_->new_transition(initial, initial_, l);
113  }
114  initial_ = initial;
115  res_->set_state_name(initial_,"[.]");
116  }
117 
119  {
120  e.sub()->accept(*this);
121  state_t initial = res_->add_state();
122  res_->new_transition(initial, initial_, " ");
123  initial_ = initial;
124  res_->set_state_name(initial_,"[*]");
125  }
126 
128  {
129  e.sub()->accept(*this);
130  state_t initial = res_->add_state();
131  res_->new_transition(initial, initial_, " ");
132  initial_ = initial;
133  res_->set_state_name(initial_,"[?]");
134  }
135 
137  {
138  e.sub()->accept(*this);
139  state_t initial = res_->add_state();
140  res_->new_transition(initial, initial_, " ");
141  initial_ = initial;
142  res_->set_state_name(initial_,"[^+]");
143  }
144 
146  {
147  e.sub()->accept(*this);
148  std::ostringstream oss;
149  oss<<"<";
150  ws_.print(e.weight(), oss)<< ">";
151  res_->print_state(initial_, oss);
152  res_->set_state_name(initial_,oss.str());
153  }
154 
156  {
157  e.sub()->accept(*this);
158  std::ostringstream oss;
159  res_->print_state(initial_, oss) << "<";
160  ws_.print(e.weight(), oss)<< ">";
161  res_->set_state_name(initial_,oss.str());
162  }
163 
164  private:
165  automaton_t make_automaton () {
166  typename aut_labelset_t::genset_t alph{'.',' '};
167  aut_labelset_t ls(alph);
168  aut_context_t ct(ls, b());
169  return make_shared_ptr<automaton_t>(ct);
170  }
171 
172  automaton_t res_ = make_automaton() ;
173  const context_t& ctx_;
174  const weightset_t& ws_ = *ctx_.weightset();
175  const labelset_t& ls_ = *ctx_.labelset();
176  const label_t epsilon_ = res_->labelset()->special();
177  state_t initial_ = automaton_t::element_type::null_state();
178  };
179 
180  } // rat::
181 
182  /* @brief build the graph of the expression
183  *
184  * This function builds an automaton over the alphabet {.}
185  * where labels are words.
186  * Every node of the expression becomes a state of the automaton
187  * the root is the initial state; there is no final state.
188  * This automaton is actually a tree.
189  * The label of every state depends on the nature of the corresponding node.
190  *
191  * @tparam RatExpSet the type of the ratexpset of the expression
192  * @param rs the ratexpset of the expression
193  * @param exp the rational expression
194  * @return an automaton representing the tree of the rational expression
195  */
196  template <typename RatExpSet>
197  mutable_automaton<context<ctx::law_char, b>>
198  draw_exp(const RatExpSet& rs, const typename RatExpSet::ratexp_t& exp)
199  {
201  return drawexp(exp);
202  }
203  }
204 }//end of ns awali::stc
205 
206 #endif // !AWALI_ALGOS_DRAW_EXP_HH
The Boolean semring.
Definition: b.hh:38
The semiring of complex numbers.
Definition: c.hh:44
carries the algebraic settings of automata
Definition: context.hh:40
Definition: ratexp.hh:280
Definition: ratexp.hh:262
Definition: draw_exp.hh:38
drawexp_visitor(const RatExpSet &rs)
Definition: draw_exp.hh:52
ctx::law_char aut_labelset_t
Definition: draw_exp.hh:40
AWALI_RAT_VISIT(one,)
Definition: draw_exp.hh:70
weight_t_of< context_t > weight_t
Definition: draw_exp.hh:46
typename RatExpSet::context_t context_t
Definition: draw_exp.hh:43
AWALI_RAT_VISIT(sum, e)
Definition: draw_exp.hh:84
context< aut_labelset_t, b > aut_context_t
Definition: draw_exp.hh:41
typename context_t::const_visitor super_type
Definition: draw_exp.hh:47
AWALI_RAT_VISIT(star, e)
Definition: draw_exp.hh:118
AWALI_RAT_VISIT(lweight, e)
Definition: draw_exp.hh:145
sttc::mutable_automaton< aut_context_t > automaton_t
Definition: draw_exp.hh:42
constexpr static const char * me()
Definition: draw_exp.hh:50
automaton_t operator()(const typename RatExpSet::ratexp_t &v)
Definition: draw_exp.hh:57
AWALI_RAT_VISIT(rweight, e)
Definition: draw_exp.hh:155
AWALI_RAT_VISIT(zero,)
Definition: draw_exp.hh:64
label_t_of< automaton_t > label_t
Definition: draw_exp.hh:48
AWALI_RAT_VISIT(maybe, e)
Definition: draw_exp.hh:127
AWALI_RAT_VISIT(atom, e)
Definition: draw_exp.hh:76
weightset_t_of< context_t > weightset_t
Definition: draw_exp.hh:44
AWALI_RAT_VISIT(plus, e)
Definition: draw_exp.hh:136
labelset_t_of< context_t > labelset_t
Definition: draw_exp.hh:45
Definition: ratexp.hh:176
An inner node with multiple children.
Definition: ratexp.hh:115
An inner node implementing a weight.
Definition: ratexp.hh:208
Implementation of labels are words.
Definition: wordset.hh:35
GenSet genset_t
Definition: wordset.hh:37
wordset< sttc::set_alphabet< sttc::char_letters > > law_char
Definition: law_char.hh:28
typename internal::label_t_of_impl< internal::base_t< ValueSet > >::type label_t_of
Helper to retrieve the type of the labels of a value set.
Definition: traits.hh:71
typename internal::weight_t_of_impl< internal::base_t< ValueSet > >::type weight_t_of
Helper to retrieve the type of the weights of a value set.
Definition: traits.hh:81
typename internal::labelset_t_of_impl< internal::base_t< ValueSet > >::type labelset_t_of
Helper to retrieve the type of the labelset of a value set.
Definition: traits.hh:76
mutable_automaton< context< ctx::law_char, b > > draw_exp(const RatExpSet &rs, const typename RatExpSet::ratexp_t &exp)
Definition: draw_exp.hh:198
std::shared_ptr< internal::mutable_automaton_impl< Context > > mutable_automaton
Definition: mutable_automaton.hh:45
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
unsigned state_t
Definition: types.hh:21
#define AWALI_RAT_UNSUPPORTED(Type)
Definition: visitor.hh:75