Awali
Another Weighted Automata library
automaton.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_STTC_WAUTOMATON_HH
18 #define AWALI_STTC_WAUTOMATON_HH
19 
23 #include <awali/sttc/ctx/lat.hh>
25 #include<vector>
29 #include<awali/sttc/labelset/traits.hh>// ratexpset_of
30 #include<awali/sttc/json.hxx>//json_support
31 
32 
33 namespace awali {
34  namespace sttc {
35 
45  template<typename T>
46  mutable_automaton<context<ctx::lal_char,T>>
47  make_automaton(const std::set<char>& letters)
48  {
49  return make_shared_ptr<mutable_automaton<context<ctx::lal_char,T>>>
51  }
52 
57  mutable_automaton<context<ctx::lal_char,b>>
58  make_automaton(const std::set<char>& letters)
59  {
60  return make_automaton<b>(letters);
61  }
62 
63  template<typename T>
64  context<ctx::lal_char,T>
65  make_context(const std::set<char>& letters) {
66  return {ctx::lal_char(letters),T()};
67  }
68 
69  context<ctx::lal_char,b>
70  make_context(const std::set<char>& letters) {
71  return {ctx::lal_char(letters),b()};
72  }
73 
75  return ctx::lan_char::one();
76  }
77 
79  return ctx::lan_char::is_one(v);
80  }
81 
91  template<typename T>
92  mutable_automaton<context<ctx::lan_char,T>>
93  make_automaton_with_epsilon(const std::set<char>& letters)
94  {
95  return make_shared_ptr<mutable_automaton<context<ctx::lan_char,T>>>
97  }
98 
103  mutable_automaton<context<ctx::lan_char,b>>
104  make_automaton_with_epsilon(const std::set<char>& letters)
105  {
106  return make_automaton_with_epsilon<b>(letters);
107  }
108 
109  template<typename T>
110  mutable_automaton<context<ctx::lat<ctx::lan_char,ctx::lan_char>,T>>
111  make_transducer(const std::set<char>& letterA, const std::set<char>& letterB)
112  {
113  return make_shared_ptr<mutable_automaton<context<ctx::lat<ctx::lan_char,ctx::lan_char>,T>>>
114  (context<ctx::lat<ctx::lan_char,ctx::lan_char>,T>(std::make_tuple(ctx::lan_char(letterA), ctx::lan_char(letterB)),T()));
115  }
116 
117  mutable_automaton<context<ctx::lat<ctx::lan_char,ctx::lan_char>,b>>
118  make_transducer(const std::set<char>& letterA, const std::set<char>& letterB)
119  {
120  return make_transducer<b>(letterA, letterB);
121  }
122 
123  template<typename T>
124  mutable_automaton<context<ctx::lal_char,T>>
125  load_automaton(std::istream& is) {
126  json_ast_t p = json_ast::from(is);
127  if(!p->has_child("kind") || p->at("kind")->to_string() != "Automaton")
128  throw std::runtime_error("json: Automaton");
129  if(!p->has_child("context"))
130  throw std::runtime_error("No context in Automaton description");
131  awali::json::object_t* jk=p->at({"context"})->object();
132  awali::json::object_t* jl=jk->at({"labels"})->object();
133  if(jl->at("labelKind")->to_string() != "Letters"
134  || jl->at("letterType")->to_string() != "Char")
135  throw std::runtime_error("Not a letter automaton");
136  if(jl->has_child("allowEpsilon"))
137  throw std::runtime_error("Epsilon transitions not supported by this function");
138  auto ctx = context<ctx::lal_char,T>(ctx::lal_char({}),T());
139  for(auto jv : *jl->at("alphabet")->array())
140  const_cast<ctx::lal_char::genset_t&>(ctx.labelset()->genset()).add_letter(jv->to_string()[0]);
141  auto aut = js_parse_aut_content(ctx, p->at("data")->object());
142  if(p->has_child("metadata"))
143  js_add_metadata(aut, p->at("metadata")->object());
144  return aut;
145  }
146 
147  template<typename T>
148  mutable_automaton<context<ctx::lan_char,T>>
149  load_automaton_with_epsilon(std::istream& is) {
150  json_ast_t p = json_ast::from(is);
151  if(!p->has_child("kind") || p->at("kind")->to_string() != "Automaton")
152  throw std::runtime_error("json: Automaton");
153  if(!p->has_child("context"))
154  throw std::runtime_error("No context in Automaton description");
155  awali::json::object_t* jk=p->at({"context"})->object();
156  awali::json::object_t* jl=jk->at({"labels"})->object();
157  if(jl->at("labelKind")->to_string() != "Letters"
158  || jl->at("letterType")->to_string() != "Char")
159  throw std::runtime_error("Not a letter automaton");
160  auto ctx = context<ctx::lan_char,T>(ctx::lan_char(std::set<char>()),T());
161  for(auto jv : *jl->at("alphabet")->array())
162  const_cast<ctx::lan_char::genset_t&>(ctx.labelset()->genset()).add_letter(jv->to_string()[0]);
163  auto aut = js_parse_aut_content(ctx, p->at("data")->object());
164  if(p->has_child("metadata"))
165  js_add_metadata(aut, p->at("metadata")->object());
166  return aut;
167  }
168 
169  mutable_automaton<context<ctx::lal_char,b>>
170  load_automaton(std::istream& is) {
171  return load_automaton<b>(is);
172  }
173 
174  template<typename T>
175  ratexpset_of<context<ctx::lal_char, T>>
177  using context_t = context<ctx::lal_char, T>;
178  using ratset_t = ratexpset_of<context_t>;
179  context_t ctx(ctx::lal_char{},T());
180  return {get_rat_context(ctx), ratset_t::identities_t::trivial};
181  }
182 
183  ratexpset_of<context<ctx::lal_char, b>>
184  make_ratexpset() {
185  return make_ratexpset<b>();
186  }
187 
188  template<typename T>
189  ratexpset_of<context<ctx::lat<ctx::lan_char,ctx::lan_char>, T>>
191  using context_t = context<ctx::lat<ctx::lan_char,ctx::lan_char>, T>;
192  using ratset_t = ratexpset_of<context_t>;
194  return {get_rat_context(ctx), ratset_t::identities_t::trivial};
195  }
196 
197  ratexpset_of<context<ctx::lat<ctx::lan_char,ctx::lan_char>, b>>
199  return make_tdc_ratexpset<b>();
200  }
201 
202 
203  template<typename RATEXPSET>
204  auto
205  make_ratexp(RATEXPSET& ratset, const std::string& exp) -> typename RATEXPSET::ratexp_t
206  {
207  return parse_exp(ratset, exp, true, false);
208  }
209 
210  template<typename AUTOMATON>
211  auto
213  {
214  using ratset_t = ratexpset_of<context_t_of<AUTOMATON>>;
215  return ratset_t(get_rat_context(aut->context()),ratset_t::identities_t::trivial);
216  }
217 
218  template <typename Automaton, unsigned version = version::fsm_json>
219  std::ostream& js_print(Automaton aut,
220  std::ostream& o,
221  bool full = false,
222  json_ast_t extra_metadata = json_ast::empty()) {
223  return put(aut_to_ast(aut, extra_metadata, full), o);
224  }
225 
226  template <typename RatExpSet, unsigned version = version::fsm_json>
227  std::ostream& js_print(const RatExpSet& rs,
228  const typename RatExpSet::ratexp_t& e,
229  std::ostream& o,
230  json_ast_t extra_metadata = json_ast::empty()) {
231  return put(ratexp_to_ast(rs, e, extra_metadata), o);
232  }
233 
234  }
235 }
236 
237 #endif
virtual std::string to_string() const
Coerces this node_t to an std::string.
Definition: node.hh:331
virtual array_t const * array() const
Casts this node to array_t.
Definition: node.hh:207
Definition: node.hh:367
virtual bool has_child(std::string const &key) const override
Definition: node.hh:380
virtual node_t * at(std::string const &key) override
carries the algebraic settings of automata
Definition: context.hh:40
Implementation of labels are letters.
Definition: letterset.hh:43
GenSet genset_t
Definition: letterset.hh:45
Implementation of labels are nullables (letter or empty).
Definition: nullableset.hh:189
typename helper_t::value_t value_t
Definition: nullableset.hh:197
static constexpr ATTRIBUTE_PURE value_t one()
Definition: nullableset.hh:257
static ATTRIBUTE_PURE bool is_one(value_t l)
Definition: nullableset.hh:264
A ValueSet which is a Cartesian product of ValueSets.
Definition: tupleset.hh:80
json_ast_t empty()
Builds an empty json_ast_t.
Definition: json_ast.hh:33
json_ast_t from(std::istream &i)
Builds a json_ast_t from an input stream.
letterset< set_alphabet< char_letters > > lal_char
Definition: lal_char.hh:28
mutable_automaton< context< ctx::lan_char, T > > load_automaton_with_epsilon(std::istream &is)
Definition: automaton.hh:149
ctx::lan_char::value_t get_epsilon()
Helper to get the value of epsilon.
Definition: automaton.hh:74
auto make_ratexp(RATEXPSET &ratset, const std::string &exp) -> typename RATEXPSET::ratexp_t
Definition: automaton.hh:205
json_ast_t aut_to_ast(Automaton aut, json_ast_t extra_metadata=json_ast::empty(), bool full=false)
Definition: js_print.hh:233
RatExpSet::ratexp_t parse_exp(const RatExpSet &ratexpset, const std::string &s, bool check_complete=true, bool strict_alphabet=true)
parser of rational expressions
Definition: exp_parser.hh:438
mutable_automaton< context< ctx::lan_char, T > > make_automaton_with_epsilon(const std::set< char > &letters)
Build an awali weighted automaton labeled by letters with epsilon transitions.
Definition: automaton.hh:93
mutable_automaton< context< ctx::lal_char, T > > make_automaton(const std::set< char > &letters)
Build an awali weighted automaton labeled by letters.
Definition: automaton.hh:47
mutable_automaton< context< ctx::lal_char, T > > load_automaton(std::istream &is)
Definition: automaton.hh:125
auto get_ratexpset(AUTOMATON &aut) -> ratexpset_of< context_t_of< AUTOMATON >>
Definition: automaton.hh:212
mutable_automaton< context< ctx::lat< ctx::lan_char, ctx::lan_char >, T > > make_transducer(const std::set< char > &letterA, const std::set< char > &letterB)
Definition: automaton.hh:111
mutable_automaton< Context > js_parse_aut_content(const Context &context, json::object_t const *p)
Definition: js_parser.hh:104
json_ast_t ratexp_to_ast(const RatExpSet &rs, const typename RatExpSet::ratexp_t &e, json_ast_t extra_metadata=json_ast::empty())
Definition: js_print.hh:338
bool is_epsilon(ctx::lan_char::value_t v)
Definition: automaton.hh:78
context< ctx::lal_char, T > make_context(const std::set< char > &letters)
Definition: automaton.hh:65
std::ostream & js_print(Automaton aut, std::ostream &o, bool full=false, json_ast_t extra_metadata=json_ast::empty())
Definition: automaton.hh:219
void js_add_metadata(Aut &aut, json::object_t *p)
Definition: js_parser.hh:164
ratexpset_of< context< ctx::lat< ctx::lan_char, ctx::lan_char >, T > > make_tdc_ratexpset()
Definition: automaton.hh:190
ratexpset_of< context< ctx::lal_char, T > > make_ratexpset()
Definition: automaton.hh:176
ratexp_context_of< Context > get_rat_context(const Context &ctx)
Definition: traits.hh:295
static const std::string full
Completely version of Awali as a std::string.
Definition: version.hh:42
Main namespace of Awali.
Definition: ato.hh:22
std::shared_ptr< json::object_t > json_ast_t
Definition: json_ast.hh:27
std::ostream & put(json_ast_t tree, std::ostream &out)
Provide a variadic mul on top of a binary mul(), and one().
Definition: weightset.hh:38