Awali
Another Weighted Automata library
json_visitor.hxx
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 
18 #ifndef AWALI_STTC_CORE_RAT_JSON_VISITOR_HXX
19 #define AWALI_STTC_CORE_RAT_JSON_VISITOR_HXX
20 
23 #include <awali/sttc/ctx/traits.hh>
24 
25 #include <awali/common/version.hh>
26 #include <awali/common/json/node.cc>
27 
28 namespace awali { namespace sttc {
29 
30 
31  /*---------------------.
32  | Json export(ratexp). |
33  `---------------------*/
34 
35  namespace rat
36  {
37  template <typename RatExpSet, unsigned version=version::fsm_json>
39  : public RatExpSet::const_visitor
40  {
41  public:
42  using ratexpset_t = RatExpSet;
45  using ratexp_t = typename ratexpset_t::value_t;
47  using super_type = typename ratexpset_t::const_visitor;
48  using node_t = typename super_type::node_t;
49 
50 
51  constexpr static const char* me() { return "json_visitor"; }
52 
54  : rs_(rs)
55  {}
56 
57  /* Getter and setter might be made more safe */
58  json::object_t* ptr() { return _ptr;}
59  void set_ptr(json::object_t* x) { _ptr =x;}
60 
62  {
63  v->accept(*this);
64  return ptr();
65  }
66 
71 
73  {
74  set_ptr(new json::object_t("zero",new json::null_t()));
75  }
76 
78  {
79  set_ptr(new json::object_t("one",new json::null_t()));
80  }
81 
83  {
84  set_ptr(new json::object_t("label", ls_.value_to_json(e.value())));
85  }
86 
87  template <typename T>
88  json::object_t* array_from_children(T e, std::string const& str)
89  {
90  json::array_t* arr= new json::array_t();
91  for (unsigned i = 0, n = e.size(); i < n; ++i) {
92  const auto& v = e[i];
93  v->accept(*this);
94  arr->push_back(ptr());
95  }
96  return new json::object_t(str, arr);
97  }
98 
100  {
101  set_ptr(array_from_children(e,"sum"));
102  }
103 
105  {
106  set_ptr(array_from_children(e,"prod"));
107  }
108 
110  {
111  set_ptr(array_from_children(e,"shuffle"));
112  }
113 
115  {
116  e.sub()->accept(*this);
117  set_ptr(new json::object_t("star",ptr()));
118  }
119 
121  {
122  e.sub()->accept(*this);
123  json::object_t* o = ptr();
124  o->push_front("lweight",ws_.value_to_json(e.weight()));
125  }
126 
128  {
129  e.sub()->accept(*this);
130  json::object_t* o = ptr();
131  o->push_front("rweight",ws_.value_to_json(e.weight()));
132  }
133 
134  private:
135  ratexpset_t rs_;
137  json::object_t* _ptr = nullptr;
138  weightset_t ws_ = *rs_.weightset();
139  labelset_t ls_ = *rs_.labelset();
140  };
141 
142 
143 
144 }//end of namespace awali::sttc::rat
145 }//end of namespace awali::sttc
146 }//end of namespace awali
147 
148 #endif
Definition: node.hh:431
array_t * push_back(node_t *p)
Definition: node.hh:578
Definition: node.hh:367
object_t * push_front(std::string key, node_t *node)
The semiring of Natural numbers.
Definition: n.hh:34
Definition: ratexp.hh:280
Definition: ratexp.hh:262
Definition: json_visitor.hxx:40
AWALI_RAT_VISIT(prod, e)
Definition: json_visitor.hxx:104
weightset_t_of< ratexpset_t > weightset_t
Definition: json_visitor.hxx:46
AWALI_RAT_VISIT(sum, e)
Definition: json_visitor.hxx:99
AWALI_RAT_VISIT(rweight, e)
Definition: json_visitor.hxx:127
json::object_t * operator()(const ratexp_t &v)
Definition: json_visitor.hxx:61
void set_ptr(json::object_t *x)
Definition: json_visitor.hxx:59
AWALI_RAT_VISIT(one,)
Definition: json_visitor.hxx:77
AWALI_RAT_VISIT(lweight, e)
Definition: json_visitor.hxx:120
labelset_t_of< context_t > labelset_t
Definition: json_visitor.hxx:44
json_visitor(const ratexpset_t &rs)
Definition: json_visitor.hxx:53
AWALI_RAT_VISIT(star, e)
Definition: json_visitor.hxx:114
typename super_type::node_t node_t
Definition: json_visitor.hxx:48
AWALI_RAT_VISIT(zero,)
Definition: json_visitor.hxx:72
RatExpSet ratexpset_t
Definition: json_visitor.hxx:42
typename ratexpset_t::value_t ratexp_t
Definition: json_visitor.hxx:45
json::object_t * array_from_children(T e, std::string const &str)
Definition: json_visitor.hxx:88
typename ratexpset_t::const_visitor super_type
Definition: json_visitor.hxx:47
context_t_of< ratexpset_t > context_t
Definition: json_visitor.hxx:43
AWALI_RAT_VISIT(atom, e)
Definition: json_visitor.hxx:82
AWALI_RAT_VISIT(shuffle, e)
Definition: json_visitor.hxx:109
constexpr static const char * me()
Definition: json_visitor.hxx:51
json::object_t * ptr()
Definition: json_visitor.hxx:58
Definition: ratexp.hh:176
An inner node with multiple children.
Definition: ratexp.hh:115
An inner node implementing a weight.
Definition: ratexp.hh:208
typename internal::context_t_of_impl< internal::base_t< ValueSet > >::type context_t_of
Helper to retrieve the type of the context of a value set.
Definition: traits.hh:66
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
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
#define AWALI_RAT_UNSUPPORTED(Type)
Definition: visitor.hh:73