Awali
Another Weighted Automata library
context.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_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  /* The fact that this operator is default is deprecated. */
116  context& operator=(const context& other) = default;
117  /*
118  context& operator=(context&& that)
119  {
120  if (this != &that)
121  {
122  std::swap(ls_, that.ls_);
123  std::swap(ws_, that.ws_);
124  }
125  return *this;
126  }
127  */
128 
131  static std::string sname()
132  {
133  return (labelset_t::sname()
134  + "_" + weightset_t::sname());
135  }
136 
137  std::string vname(bool full = true) const
138  {
139  return (labelset()->vname(full)
140  + "_" + weightset()->vname(full));
141  }
142 
144  static context make(std::istream& is)
145  {
146  auto ls = labelset_t::make(is);
147  eat(is, '_');
148  auto ws = weightset_t::make(is);
149  return {ls, ws};
150  }
151 
152  const labelset_ptr& labelset() const
153  {
154  return ls_;
155  }
156 
157  const weightset_ptr& weightset() const
158  {
159  return ws_;
160  }
161 
162  std::ostream&
163  print_set(std::ostream& o, const std::string& format = "text") const
164  {
165  labelset()->print_set(o, format);
166  if (format == "latex")
167  o << "\\rightarrow";
168  else
169  o << '_';
170  return weightset()->print_set(o, format);
171  }
172 
173 
174 
175  template<unsigned version = version::fsm_json>
176  json::node_t*
177  to_json() const {
178  version::check_fsmjson<version>();
179  json::object_t* obj = new json::object_t();
180  obj->push_back("labels", ls_->template to_json<version>());
181  obj->push_back("weights", ws_->template to_json<version>());
182  return obj;
183  }
184 
185  static constexpr bool
187  {
188  return labelset_t::has_one();
189  }
190 
191  private:
192  labelset_ptr ls_;
193  weightset_ptr ws_;
194  };
195 
196  /*-----------------.
197  | join_t, meet_t. |
198  `-----------------*/
209  template <typename... ValueSets>
210  using join_t = decltype(join(std::declval<ValueSets>()...));
211 
219  template <typename... ValueSets>
220  using meet_t = decltype(meet(std::declval<ValueSets>()...));
221 
222 
223  /*-------------------------.
224  | Variadic join and meet. |
225  `-------------------------*/
226 
229  template <typename ValueSet>
230  auto
231  join(const ValueSet& vs)
232  -> ValueSet
233  {
234  return vs;
235  }
236 
237  template <typename ValueSet1, typename ValueSet2,
238  typename ValueSet3, typename... VSs>
239  auto
240  join(const ValueSet1& vs1, const ValueSet2& vs2, const ValueSet3& vs3,
241  const VSs&... vs)
242  -> decltype(join(join(vs1, vs2), vs3, vs...))
243  {
244  return join(join(vs1, vs2), vs3, vs...);
245  }
246 
249  template <typename ValueSet>
250  auto
251  meet(const ValueSet& vs)
252  -> ValueSet
253  {
254  return vs;
255  }
256 
257  template <typename ValueSet1, typename ValueSet2, typename ValueSet3,
258  typename... VSs>
259  auto
260  meet(const ValueSet1& vs1, const ValueSet2& vs2, const ValueSet3& vs3,
261  const VSs&... vs)
262  -> decltype(meet(meet(vs1, vs2), vs3, vs...))
263  {
264  return meet(meet(vs1, vs2), vs3, vs...);
265  }
266 
267 
268  /*-------------------------.
269  | join(context, context). |
270  `-------------------------*/
271 
273  template <typename LhsLabelSet, typename LhsWeightSet,
274  typename RhsLabelSet, typename RhsWeightSet>
275  auto
280  {
281  auto ls = join(*a.labelset(), *b.labelset());
282  auto ws = join(*a.weightset(), *b.weightset());
283  return {ls, ws};
284  }
285 
286 
287  /*-------------------------.
288  | meet(context, context). |
289  `-------------------------*/
290 
292  template <typename LhsLabelSet, typename LhsWeightSet,
293  typename RhsLabelSet, typename RhsWeightSet>
294  auto
299  {
300  auto ls = meet(*a.labelset(), *b.labelset());
301  auto ws = join(*a.weightset(), *b.weightset());
302  return {ls, ws};
303  }
304 
305 
306 }}//end of ns awali::stc
307 
308 #endif // !AWALI_CTX_CONTEXT_HH
Definition: node.hh:193
Definition: node.hh:367
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:137
LabelSet labelset_t
Definition: context.hh:42
const weightset_ptr & weightset() const
Definition: context.hh:157
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:131
@ 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:177
static constexpr bool has_one()
Definition: context.hh:186
context & operator=(const context &other)=default
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:144
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:152
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:163
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:210
auto join(const ratexpset< Ctx1 > &a, const ratexpset< Ctx2 > &b) -> ratexpset< join_t< Ctx1, Ctx2 >>
The union of two ratexpsets.
Definition: ratexpset.hh:445
auto meet(const ratexpset< Ctx1 > &a, const ratexpset< Ctx2 > &b) -> ratexpset< meet_t< Ctx1, Ctx2 >>
The meet of two ratexpsets.
Definition: ratexpset.hh:434
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:220
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