Awali
Another Weighted Automata library
context.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_CTX_CONTEXT_HH
18 # define AWALI_CTX_CONTEXT_HH
19 
20 # include <cassert>
21 # include <memory>
22 # include <string>
23 
24 #include <awali/common/json/node.cc>
25 #include <awali/common/version.hh>
26 #include <awali/sttc/core/kind.hh>
28 #include <awali/sttc/ctx/fwd.hh>
30 
31 namespace awali { namespace sttc {
32 
38  template <typename LabelSet, typename WeightSet>
39  class context
40  {
41  public:
42  using labelset_t = LabelSet;
43  using weightset_t = WeightSet;
44  using labelset_ptr = std::shared_ptr<const labelset_t>;
45  using weightset_ptr = std::shared_ptr<const weightset_t>;
46 
47  using kind_t = typename labelset_t::kind_t;
48  enum
49  {
56  };
58  using label_t = typename labelset_t::value_t;
60  using weight_t = typename weightset_t::value_t;
63  using ratexp_t = std::shared_ptr<const node_t>;
66 
67  context(const context& that)
68  : context(that.ls_, that.ws_)
69  {}
70 
73  context(const labelset_ptr& ls, const weightset_ptr& ws)
74  : ls_{ls}
75  , ws_{ws}
76  {}
77 
81  context(const labelset_t& ls, const weightset_t& ws = {})
82  : context(std::make_shared<const labelset_t>(ls),
83  std::make_shared<const weightset_t>(ws))
84  {}
85 
89  //
90  // Use SFINAE to avoid requiring labelset_t to define letter_t.
91  // labels_are_tuples does not define it for instance.
92  //
93  // It would be simpler to just use "= {}", but the C++ standard
94  // does not support it (and this is properly considered a
95  // defect: see http://cplusplus.github.io/LWG/lwg-active.html#2193).
96  //
97  // Gcc accepts though.
98  // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56922
99  // But clang rejects it.
100  // http://llvm.org/bugs/show_bug.cgi?id=15724
101  template <typename LabelSet2 = labelset_t>
102  context(const std::initializer_list<typename LabelSet2::letter_t>& gs,
103  const weightset_t& ws = {})
104  : context{labelset_t{gs}, ws}
105  {}
106 
108  template <typename LabelSet2 = labelset_t>
110  : context{typename std::enable_if<is_lao, labelset_t>::type{},
111  weightset_t{}}
112  {}
113 
114  /*
115  context& operator=(context&& that)
116  {
117  if (this != &that)
118  {
119  std::swap(ls_, that.ls_);
120  std::swap(ws_, that.ws_);
121  }
122  return *this;
123  }
124  */
125 
128  static std::string sname()
129  {
130  return (labelset_t::sname()
131  + "_" + weightset_t::sname());
132  }
133 
134  std::string vname(bool full = true) const
135  {
136  return (labelset()->vname(full)
137  + "_" + weightset()->vname(full));
138  }
139 
141  static context make(std::istream& is)
142  {
143  auto ls = labelset_t::make(is);
144  eat(is, '_');
145  auto ws = weightset_t::make(is);
146  return {ls, ws};
147  }
148 
149  const labelset_ptr& labelset() const
150  {
151  return ls_;
152  }
153 
154  const weightset_ptr& weightset() const
155  {
156  return ws_;
157  }
158 
159  std::ostream&
160  print_set(std::ostream& o, const std::string& format = "text") const
161  {
162  labelset()->print_set(o, format);
163  if (format == "latex")
164  o << "\\rightarrow";
165  else
166  o << '_';
167  return weightset()->print_set(o, format);
168  }
169 
170 
171 
172  template<unsigned version = version::fsm_json>
173  json::node_t*
174  to_json() const {
175  json::object_t* obj = new json::object_t();
176  obj->push_back("labels", ls_->template to_json<version>());
177  obj->push_back("weights", ws_->template to_json<version>());
178  return obj;
179  }
180 
181  static constexpr bool
183  {
184  return labelset_t::has_one();
185  }
186 
187  private:
188  labelset_ptr ls_;
189  weightset_ptr ws_;
190  };
191 
192  /*-----------------.
193  | join_t, meet_t. |
194  `-----------------*/
205  template <typename... ValueSets>
206  using join_t = decltype(join(std::declval<ValueSets>()...));
207 
215  template <typename... ValueSets>
216  using meet_t = decltype(meet(std::declval<ValueSets>()...));
217 
218 
219  /*-------------------------.
220  | Variadic join and meet. |
221  `-------------------------*/
222 
225  template <typename ValueSet>
226  auto
227  join(const ValueSet& vs)
228  -> ValueSet
229  {
230  return vs;
231  }
232 
233  template <typename ValueSet1, typename ValueSet2,
234  typename ValueSet3, typename... VSs>
235  auto
236  join(const ValueSet1& vs1, const ValueSet2& vs2, const ValueSet3& vs3,
237  const VSs&... vs)
238  -> decltype(join(join(vs1, vs2), vs3, vs...))
239  {
240  return join(join(vs1, vs2), vs3, vs...);
241  }
242 
245  template <typename ValueSet>
246  auto
247  meet(const ValueSet& vs)
248  -> ValueSet
249  {
250  return vs;
251  }
252 
253  template <typename ValueSet1, typename ValueSet2, typename ValueSet3,
254  typename... VSs>
255  auto
256  meet(const ValueSet1& vs1, const ValueSet2& vs2, const ValueSet3& vs3,
257  const VSs&... vs)
258  -> decltype(meet(meet(vs1, vs2), vs3, vs...))
259  {
260  return meet(meet(vs1, vs2), vs3, vs...);
261  }
262 
263 
264  /*-------------------------.
265  | join(context, context). |
266  `-------------------------*/
267 
269  template <typename LhsLabelSet, typename LhsWeightSet,
270  typename RhsLabelSet, typename RhsWeightSet>
271  auto
276  {
277  auto ls = join(*a.labelset(), *b.labelset());
278  auto ws = join(*a.weightset(), *b.weightset());
279  return {ls, ws};
280  }
281 
282 
283  /*-------------------------.
284  | meet(context, context). |
285  `-------------------------*/
286 
288  template <typename LhsLabelSet, typename LhsWeightSet,
289  typename RhsLabelSet, typename RhsWeightSet>
290  auto
295  {
296  auto ls = meet(*a.labelset(), *b.labelset());
297  auto ws = join(*a.weightset(), *b.weightset());
298  return {ls, ws};
299  }
300 
301 
302 }}//end of ns awali::stc
303 
304 #endif // !AWALI_CTX_CONTEXT_HH
Definition: node.hh:191
Definition: node.hh:365
object_t * push_back(std::string key, node_t *node)
The Boolean semring.
Definition: b.hh:38
carries the algebraic settings of automata
Definition: context.hh:40
std::string vname(bool full=true) const
Definition: context.hh:134
LabelSet labelset_t
Definition: context.hh:42
const weightset_ptr & weightset() const
Definition: context.hh:154
typename labelset_t::kind_t kind_t
Definition: context.hh:47
static std::string sname()
The name of this context, built from its parameters.
Definition: context.hh:128
@ is_law
Definition: context.hh:55
@ is_lao
Definition: context.hh:52
@ is_lat
Definition: context.hh:54
@ is_lal
Definition: context.hh:50
@ is_lar
Definition: context.hh:53
@ is_lan
Definition: context.hh:51
context(const labelset_ptr &ls, const weightset_ptr &ws)
Definition: context.hh:73
json::node_t * to_json() const
Definition: context.hh:174
static constexpr bool has_one()
Definition: context.hh:182
typename labelset_t::value_t label_t
Type of transition labels, and type of RatExp atoms.
Definition: context.hh:58
std::shared_ptr< const weightset_t > weightset_ptr
Definition: context.hh:45
context()
Build a context whose labelset constructor takes no argument.
Definition: context.hh:109
static context make(std::istream &is)
Build from the description in is.
Definition: context.hh:141
std::shared_ptr< const labelset_t > labelset_ptr
Definition: context.hh:44
context(const std::initializer_list< typename LabelSet2::letter_t > &gs, const weightset_t &ws={})
Build a context.
Definition: context.hh:102
WeightSet weightset_t
Definition: context.hh:43
typename weightset_t::value_t weight_t
Type of weights.
Definition: context.hh:60
std::shared_ptr< const node_t > ratexp_t
Definition: context.hh:63
const labelset_ptr & labelset() const
Definition: context.hh:149
context(const context &that)
Definition: context.hh:67
context(const labelset_t &ls, const weightset_t &ws={})
Build a context.
Definition: context.hh:81
std::ostream & print_set(std::ostream &o, const std::string &format="text") const
Definition: context.hh:160
Definition: visitor.hh:30
The abstract parameterized, root for all rational expression types.
Definition: ratexp.hh:74
static constexpr TOP< void > value
Definition: priority.hh:93
void eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.hh:62
decltype(join(std::declval< ValueSets >()...)) join_t
Computation of the join of some value sets.
Definition: context.hh:206
auto join(const ratexpset< Ctx1 > &a, const ratexpset< Ctx2 > &b) -> ratexpset< join_t< Ctx1, Ctx2 >>
The union of two ratexpsets.
Definition: ratexpset.hh:448
auto meet(const ratexpset< Ctx1 > &a, const ratexpset< Ctx2 > &b) -> ratexpset< meet_t< Ctx1, Ctx2 >>
The meet of two ratexpsets.
Definition: ratexpset.hh:437
auto format(const ValueSet &vs, const typename ValueSet::value_t &v, Args &&... args) -> std::string
Format v via vs.print.
Definition: stream.hh:109
decltype(meet(std::declval< ValueSets >()...)) meet_t
Computation of the meet of some value sets.
Definition: context.hh:216
static const std::string full
Completely version of Awali as a std::string.
Definition: version.hh:40
Main namespace of Awali.
Definition: ato.hh:22