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-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_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("label"))
69  e= rs_.atom(ls_.value_from_json(jo->at("label")));
70  else if(jo->has_child("one"))
71  e = rs_.one();
72  else if(jo->has_child("zero"))
73  e = rs_.zero();
74  else
75  raise("Json parser: ratexp:","none expected field found");
76 
77  if(jo->has_child("lweight"))
78  e=rs_.lmul(ws_.value_from_json(jo->at("lweight")), e);
79  if(jo->has_child("rweight"))
80  e=rs_.rmul(e, ws_.value_from_json(jo->at("lweight")));
81  return e;
82  }
83 
84  private:
85  const ratexpset_t& rs_;
86  weightset_t ws_ = *rs_.weightset();
87  labelset_t ls_ = *rs_.labelset();
88 
89  };
90  }
91 
92  template <typename RatExpSet>
93  inline
94  typename RatExpSet::ratexp_t
95  js_parse_exp_content(const RatExpSet& rs,
96  json::node_t const* p)
97  {
99  return parser.parseNode(p);
100  }
101 
102  template <typename Context>
103  mutable_automaton<Context>
104  js_parse_aut_content(const Context& context, json::object_t const* p)
105  {
106  // p is a pointer on the value of the "Content" field
107  // using aut_t = mutable_automaton<Context>;
108  auto ws = context.weightset();
109  auto ls = context.labelset();
111  std::unordered_map<unsigned,state_t> states;
112  unsigned s;
113  require(p->has_child("states"),"json automaton:","no field states");
114  for(json::node_t * jv : *p->at("states")->array()) {
115  json::object_t* jstate = dynamic_cast<json::object_t*>(jv);
116  require(jstate->has_child("id"),"json automaton:","no state id");
117  s=jstate->at("id")->to_int();
118  states[s]=aut->add_state();
119  if(jstate->has_child("name"))
120  aut->set_state_name(states[s],jstate->at("name")->to_string());
121  if(jstate->has_child("history")) {
122  auto history=aut->history();
123  if(history->get_nature() == history_kind_t::NO_HISTORY) {
124  history=std::make_shared<string_history>();
125  aut->set_history(history);
126  }
127  auto& hs = dynamic_cast<string_history&>(*history);
128  hs.add_state(states[s], jstate->at("history")->to_string());
129  }
130  if(jstate->has_child("initial"))
131  aut->set_initial(states[s] , ws->value_from_json(jstate->at("initial")));
132  if(jstate->has_child("final"))
133  aut->set_final(states[s] , ws->value_from_json(jstate->at("final")));
134  }
135  if(p->has_child("transitions"))
136  for(json::node_t * jv : *p->at("transitions")->array()) {
137  json::object_t* jtr = dynamic_cast<json::object_t*>(jv);
138  require(jtr->has_child("source"),"js automaton:","no transition source");
139  require(jtr->has_child("destination"),"js automaton:","no transition destination");
140  s=jtr->at("source")->to_int();
141  unsigned t=jtr->at("destination")->to_int();
142  if(jtr->has_child("label")) {
143  auto l = ls->value_from_json(jtr->at("label"));
144  if(jtr->has_child("weight")) {
145  auto w = ws->value_from_json(jtr->at("weight"));
146  aut->new_transition(states[s], states[t],l,w);
147  }
148  else
149  aut->new_transition(states[s], states[t],l);
150  }
151  else
152  if(jtr->has_child("weight")) {
153  auto w = ws->value_from_json(jtr->at("weight"));
154  new_epsilon_trans(aut, states[s], states[t],w);
155  }
156  else
157  new_epsilon_trans(aut, states[s], states[t]);
158  }
159  return aut;
160  }
161 
162  template <typename Aut>
163  void
165  if(p->has_child("name"))
166  aut->set_name(p->at("name")->to_string());
167  if(p->has_child("caption"))
168  aut->set_desc(p->at("caption")->to_string());
169  }
170 
171 }}//end of ns awali::stc
172 
173 #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:95
mutable_automaton< Context > js_parse_aut_content(const Context &context, json::object_t const *p)
Definition: js_parser.hh:104
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:164
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