Awali
Another Weighted Automata library
js_parser.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2023 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_ALGOS_JS_PARSER_HH
18 # define AWALI_ALGOS_JS_PARSER_HH
19 
20 #include <awali/sttc/misc/raise.hh>
21 #include <awali/common/json/node.cc>
24 //#include <awali/sttc/core/rat/ratexpset.hh>
28 # include <stdexcept>
29 # include <stack>
30 # include <iostream>
31 # include <sstream>
32 
33 namespace awali { namespace sttc {
34 
35 
36  namespace internal {
37 
38  template<typename RatExpSet>
39  struct js_exp_parser {
40  using ratexpset_t = RatExpSet;
43  using ratexp_t = typename ratexpset_t::value_t;
45 
47  : rs_(rs)
48  {}
49 
51  require(p->kind==json::OBJECT,"js parse ratexp:","object expected");
52  json::object_t const* jo=dynamic_cast<json::object_t const*>(p);
53  ratexp_t e;
54  if(jo->has_child("sum")) {
55  auto& v=jo->at("sum")->array()->values;
56  e=parseNode(v[0]);
57  for(unsigned i=1; i<v.size(); ++i)
58  e=rs_.add(e,parseNode(v[i]));
59  }
60  else if(jo->has_child("prod")) {
61  auto& v=jo->at("prod")->array()->values;
62  e=parseNode(v[0]);
63  for(unsigned i=1; i<v.size(); ++i)
64  e=rs_.mul(e,parseNode(v[i]));
65  }
66  else if(jo->has_child("star"))
67  e= rs_.star(parseNode(jo->at("star")));
68  else if(jo->has_child("maybe"))
69  e= rs_.maybe(parseNode(jo->at("maybe")));
70  else if(jo->has_child("plus"))
71  e= rs_.plus(parseNode(jo->at("plus")));
72  else if(jo->has_child("label"))
73  e= rs_.atom(ls_.value_from_json(jo->at("label")));
74  else if(jo->has_child("one"))
75  e = rs_.one();
76  else if(jo->has_child("zero"))
77  e = rs_.zero();
78  else
79  raise("Json parser: ratexp:","none expected field found");
80 
81  if(jo->has_child("lweight"))
82  e=rs_.lmul(ws_.value_from_json(jo->at("lweight")), e);
83  if(jo->has_child("rweight"))
84  e=rs_.rmul(e, ws_.value_from_json(jo->at("lweight")));
85  return e;
86  }
87 
88  private:
89  const ratexpset_t& rs_;
90  weightset_t ws_ = *rs_.weightset();
91  labelset_t ls_ = *rs_.labelset();
92 
93  };
94  }
95 
96  template <typename RatExpSet>
97  inline
98  typename RatExpSet::ratexp_t
99  js_parse_exp_content(const RatExpSet& rs,
100  json::node_t const* p)
101  {
103  return parser.parseNode(p);
104  }
105 
106  template <typename Context>
107  mutable_automaton<Context>
108  js_parse_aut_content(const Context& context, json::object_t const* p)
109  {
110  // p is a pointer on the value of the "Content" field
111  // using aut_t = mutable_automaton<Context>;
112  auto ws = context.weightset();
113  auto ls = context.labelset();
115  std::unordered_map<unsigned,state_t> states;
116  unsigned s;
117  require(p->has_child("states"),"json automaton:","no field states");
118  for(json::node_t * jv : *p->at("states")->array()) {
119  json::object_t* jstate = dynamic_cast<json::object_t*>(jv);
120  require(jstate->has_child("id"),"json automaton:","no state id");
121  s=jstate->at("id")->to_int();
122  states[s]=aut->add_state();
123  if(jstate->has_child("name"))
124  aut->set_state_name(states[s],jstate->at("name")->to_string());
125  if(jstate->has_child("history")) {
126  auto history=aut->history();
127  if(history->get_nature() == history_kind_t::NO_HISTORY) {
128  history=std::make_shared<string_history>();
129  aut->set_history(history);
130  }
131  auto& hs = dynamic_cast<string_history&>(*history);
132  hs.add_state(states[s], jstate->at("history")->to_string());
133  }
134  if(jstate->has_child("initial"))
135  aut->set_initial(states[s] , ws->value_from_json(jstate->at("initial")));
136  if(jstate->has_child("final"))
137  aut->set_final(states[s] , ws->value_from_json(jstate->at("final")));
138  }
139  if(p->has_child("transitions"))
140  for(json::node_t * jv : *p->at("transitions")->array()) {
141  json::object_t* jtr = dynamic_cast<json::object_t*>(jv);
142  require(jtr->has_child("source"),"js automaton:","no transition source");
143  require(jtr->has_child("destination"),"js automaton:","no transition destination");
144  s=jtr->at("source")->to_int();
145  unsigned t=jtr->at("destination")->to_int();
146  if(jtr->has_child("label")) {
147  auto l = ls->value_from_json(jtr->at("label"));
148  if(jtr->has_child("weight")) {
149  auto w = ws->value_from_json(jtr->at("weight"));
150  aut->new_transition(states[s], states[t],l,w);
151  }
152  else
153  aut->new_transition(states[s], states[t],l);
154  }
155  else
156  if(jtr->has_child("weight")) {
157  auto w = ws->value_from_json(jtr->at("weight"));
158  new_epsilon_trans(aut, states[s], states[t],w);
159  }
160  else
161  new_epsilon_trans(aut, states[s], states[t]);
162  }
163  return aut;
164  }
165 
166  template <typename Aut>
167  void
169  if(p->has_child("name"))
170  aut->set_name(p->at("name")->to_string());
171  if(p->has_child("caption"))
172  aut->set_desc(p->at("caption")->to_string());
173  }
174 
175 }}//end of ns awali::stc
176 
177 #endif // !AWALI_ALGOS_JS_PARSER_HH
std::vector< node_t * > const & values
Definition: node.hh:436
Definition: node.hh:193
virtual std::string to_string() const
Coerces this node_t to an std::string.
Definition: node.hh:331
virtual int to_int() const
Coerces this node_t to int.
Definition: node.hh:321
node_kind_t const kind
Definition: node.hh:196
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
const weightset_ptr & weightset() const
Definition: context.hh:157
const labelset_ptr & labelset() const
Definition: context.hh:152
Definition: string_history.hh:29
void add_state(state_t s, const std::string &str)
Definition: string_history.hh:89
@ NO_HISTORY
The state has no history.
std::vector< state_t > states(abstract_automaton_t const *aut, bool all)
@ OBJECT
Definition: node.hh:93
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
RatExpSet::ratexp_t js_parse_exp_content(const RatExpSet &rs, json::node_t const *p)
Definition: js_parser.hh:99
mutable_automaton< Context > js_parse_aut_content(const Context &context, json::object_t const *p)
Definition: js_parser.hh:108
mutable_automaton< Context > make_mutable_automaton(const Context &ctx)
Definition: mutable_automaton.hh:915
transition_t new_epsilon_trans(Aut a, state_t src, state_t dst, weight_t_of< Aut > w)
Helper to create a new epsilon transition.
Definition: add_epsilon_trans.hh:165
std::shared_ptr< internal::mutable_automaton_impl< Context > > mutable_automaton
Definition: mutable_automaton.hh:45
void require(bool b, Args &&... args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:55
void js_add_metadata(Aut &aut, json::object_t *p)
Definition: js_parser.hh:168
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
Definition: js_parser.hh:39
labelset_t_of< context_t > labelset_t
Definition: js_parser.hh:42
typename ratexpset_t::value_t ratexp_t
Definition: js_parser.hh:43
RatExpSet ratexpset_t
Definition: js_parser.hh:40
ratexp_t parseNode(json::node_t const *p)
Definition: js_parser.hh:50
js_exp_parser(const ratexpset_t &rs)
Definition: js_parser.hh:46
weightset_t_of< ratexpset_t > weightset_t
Definition: js_parser.hh:44
context_t_of< ratexpset_t > context_t
Definition: js_parser.hh:41