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-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_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  std::ostringstream oss;
131  oss<<"<";
132  ws_.print(e.weight(), oss)<< ">";
133  res_->print_state(initial_, oss);
134  res_->set_state_name(initial_,oss.str());
135  }
136 
138  {
139  e.sub()->accept(*this);
140  std::ostringstream oss;
141  res_->print_state(initial_, oss) << "<";
142  ws_.print(e.weight(), oss)<< ">";
143  res_->set_state_name(initial_,oss.str());
144  }
145 
146  private:
147  automaton_t make_automaton () {
148  typename aut_labelset_t::genset_t alph{'.',' '};
149  aut_labelset_t ls(alph);
150  aut_context_t ct(ls, b());
151  return make_shared_ptr<automaton_t>(ct);
152  }
153 
154  automaton_t res_ = make_automaton() ;
155  const context_t& ctx_;
156  const weightset_t& ws_ = *ctx_.weightset();
157  const labelset_t& ls_ = *ctx_.labelset();
158  const label_t epsilon_ = res_->labelset()->special();
159  state_t initial_ = automaton_t::element_type::null_state();
160  };
161 
162  } // rat::
163 
164  /* @brief build the graph of the expression
165  *
166  * This function builds an automaton over the alphabet {.}
167  * where labels are words.
168  * Every node of the expression becomes a state of the automaton
169  * the root is the initial state; there is no final state.
170  * This automaton is actually a tree.
171  * The label of every state depends on the nature of the corresponding node.
172  *
173  * @tparam RatExpSet the type of the ratexpset of the expression
174  * @param rs the ratexpset of the expression
175  * @param exp the rational expression
176  * @return an automaton representing the tree of the rational expression
177  */
178  template <typename RatExpSet>
179  mutable_automaton<context<ctx::law_char, b>>
180  draw_exp(const RatExpSet& rs, const typename RatExpSet::ratexp_t& exp)
181  {
183  return drawexp(exp);
184  }
185  }
186 }//end of ns awali::stc
187 
188 #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:127
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:137
AWALI_RAT_VISIT(zero,)
Definition: draw_exp.hh:64
label_t_of< automaton_t > label_t
Definition: draw_exp.hh:48
AWALI_RAT_VISIT(atom, e)
Definition: draw_exp.hh:76
weightset_t_of< context_t > weightset_t
Definition: draw_exp.hh:44
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:180
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:73