Awali
Another Weighted Automata library
automaton.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2021 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 
31 namespace awali {
32  namespace sttc {
33 
43  template<typename T>
44  mutable_automaton<context<ctx::lal_char,T>>
45  make_automaton(const std::set<char>& letters)
46  {
47  return make_shared_ptr<mutable_automaton<context<ctx::lal_char,T>>>
49  }
50 
55  mutable_automaton<context<ctx::lal_char,b>>
56  make_automaton(const std::set<char>& letters)
57  {
58  return make_automaton<b>(letters);
59  }
60 
70  template<typename T>
71  mutable_automaton<context<ctx::lan_char,T>>
72  make_automaton_with_epsilon(const std::set<char>& letters)
73  {
74  return make_shared_ptr<mutable_automaton<context<ctx::lan_char,T>>>
76  }
77 
82  mutable_automaton<context<ctx::lan_char,b>>
83  make_automaton_with_epsilon(const std::set<char>& letters)
84  {
85  return make_automaton_with_epsilon<b>(letters);
86  }
87 
88  template<typename T>
89  mutable_automaton<context<ctx::lat<ctx::lan_char,ctx::lan_char>,T>>
90  make_transducer(const std::set<char>& letterA, const std::set<char>& letterB)
91  {
92  return make_shared_ptr<mutable_automaton<context<ctx::lat<ctx::lan_char,ctx::lan_char>,T>>>
93  (context<ctx::lat<ctx::lan_char,ctx::lan_char>,T>(std::make_tuple(ctx::lan_char(letterA), ctx::lan_char(letterB)),T()));
94  }
95 
96  mutable_automaton<context<ctx::lat<ctx::lan_char,ctx::lan_char>,b>>
97  make_transducer(const std::set<char>& letterA, const std::set<char>& letterB)
98  {
99  return make_transducer<b>(letterA, letterB);
100  }
101 
102  template<typename T>
103  mutable_automaton<context<ctx::lal_char,T>>
104  load_automaton(std::istream& is) {
106  if(!jo->has_child("kind") || jo->at("kind")->to_string() != "Automaton")
107  throw std::runtime_error("json: Automaton");
108  awali::json::object_t* jl=jo->at({"context","labels"})->object();
109  if(jl->at("labelKind")->to_string() != "Letters"
110  || jl->at("letterType")->to_string() != "Char")
111  throw std::runtime_error("Not a letter automaton");
112  if(jl->has_child("withEpsilon"))
113  throw std::runtime_error("Epsilon transitions not supported by this function");
114  auto ctx = context<ctx::lal_char,T>(ctx::lal_char({}),T());
115  for(auto jv : *jl->at("alphabet")->array())
116  const_cast<ctx::lal_char::genset_t&>(ctx.labelset()->genset()).add_letter(jv->to_string()[0]);
117  auto aut = js_parse_aut_content(ctx, jo->at("data")->object());
118  if(jo->has_child("metadata"))
119  js_add_metadata(aut, jo->at("metadata")->object());
120  delete jo;
121  return aut;
122  }
123 
124  mutable_automaton<context<ctx::lal_char,b>>
125  load_automaton(std::istream& is) {
126  return load_automaton<b>(is);
127  }
128 
129  template<typename T>
130  ratexpset_of<context<ctx::lal_char, T>>
132  using context_t = context<ctx::lal_char, T>;
133  using ratset_t = ratexpset_of<context_t>;
134  context_t ctx(ctx::lal_char{},T());
135  return {get_rat_context(ctx), ratset_t::identities_t::trivial};
136  }
137 
138  ratexpset_of<context<ctx::lal_char, b>>
139  make_ratexpset() {
140  return make_ratexpset<b>();
141  }
142 
143  template<typename T>
144  ratexpset_of<context<ctx::lat<ctx::lan_char,ctx::lan_char>, T>>
146  using context_t = context<ctx::lat<ctx::lan_char,ctx::lan_char>, T>;
147  using ratset_t = ratexpset_of<context_t>;
149  return {get_rat_context(ctx), ratset_t::identities_t::trivial};
150  }
151 
152  ratexpset_of<context<ctx::lat<ctx::lan_char,ctx::lan_char>, b>>
154  return make_tdc_ratexpset<b>();
155  }
156 
157 
158  template<typename RATEXPSET>
159  auto
160  make_ratexp(RATEXPSET& ratset, const std::string& exp) -> typename RATEXPSET::ratexp_t
161  {
162  return parse_exp(ratset, exp, true, false);
163  }
164 
165  template<typename AUTOMATON>
166  auto
168  {
169  using ratset_t = ratexpset_of<context_t_of<AUTOMATON>>;
170  return ratset_t(get_rat_context(aut->context()),ratset_t::identities_t::trivial);
171  }
172  }
173 }
174 
175 #endif
virtual std::string to_string() const
Coerces this node_t to an std::string.
Definition: node.hh:329
virtual object_t const * object() const
Casts this node to object_t.
Definition: node.hh:201
virtual array_t const * array() const
Casts this node to array_t.
Definition: node.hh:205
Definition: node.hh:365
virtual bool has_child(std::string const &key) const override
Definition: node.hh:378
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
A ValueSet which is a Cartesian product of ValueSets.
Definition: tupleset.hh:80
node_t * parse(std::istream &)
auto make_ratexp(RATEXPSET &ratset, const std::string &exp) -> typename RATEXPSET::ratexp_t
Definition: automaton.hh:160
auto get_rat_context(const Context &ctx) -> context< typename labelset_trait< typename Context::labelset_t >::ratlabelset_t, typename Context::weightset_t >
Definition: traits.hh:238
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:425
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:72
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:45
mutable_automaton< context< ctx::lal_char, T > > load_automaton(std::istream &is)
Definition: automaton.hh:104
auto get_ratexpset(AUTOMATON &aut) -> ratexpset_of< context_t_of< AUTOMATON >>
Definition: automaton.hh:167
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:90
mutable_automaton< Context > js_parse_aut_content(const Context &context, json::object_t const *p)
Definition: js_parser.hh:104
void js_add_metadata(Aut &aut, json::object_t *p)
Definition: js_parser.hh:165
ratexpset_of< context< ctx::lat< ctx::lan_char, ctx::lan_char >, T > > make_tdc_ratexpset()
Definition: automaton.hh:145
ratexpset_of< context< ctx::lal_char, T > > make_ratexpset()
Definition: automaton.hh:131
Main namespace of Awali.
Definition: ato.hh:22
Provide a variadic mul on top of a binary mul(), and one().
Definition: weightset.hh:38