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-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_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* jo=dynamic_cast<json::object_t*>(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* 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  char c;
112  std::unordered_map<unsigned,state_t> states;
113  unsigned s;
114  require(p->has_child("states"),"json automaton:","no field states");
115  for(json::node_t * jv : *p->at("states")->array()) {
116  json::object_t* jstate = dynamic_cast<json::object_t*>(jv);
117  require(jstate->has_child("id"),"json automaton:","no state id");
118  s=jstate->at("id")->to_int();
119  states[s]=aut->add_state();
120  if(jstate->has_child("name"))
121  aut->set_state_name(states[s],jstate->at("name")->to_string());
122  if(jstate->has_child("history")) {
123  auto history=aut->history();
124  if(history->get_nature() == history_kind_t::NO_HISTORY) {
125  history=std::make_shared<string_history>();
126  aut->set_history(history);
127  }
128  auto& hs = dynamic_cast<string_history&>(*history);
129  hs.add_state(states[s], jstate->at("history")->to_string());
130  }
131  if(jstate->has_child("initial"))
132  aut->set_initial(states[s] , ws->value_from_json(jstate->at("initial")));
133  if(jstate->has_child("final"))
134  aut->set_final(states[s] , ws->value_from_json(jstate->at("final")));
135  }
136  if(p->has_child("transitions"))
137  for(json::node_t * jv : *p->at("transitions")->array()) {
138  json::object_t* jtr = dynamic_cast<json::object_t*>(jv);
139  require(jtr->has_child("source"),"js automaton:","no transition source");
140  require(jtr->has_child("destination"),"js automaton:","no transition destination");
141  s=jtr->at("source")->to_int();
142  unsigned t=jtr->at("destination")->to_int();
143  if(jtr->has_child("label")) {
144  auto l = ls->value_from_json(jtr->at("label"));
145  if(jtr->has_child("weight")) {
146  auto w = ws->value_from_json(jtr->at("weight"));
147  aut->new_transition(states[s], states[t],l,w);
148  }
149  else
150  aut->new_transition(states[s], states[t],l);
151  }
152  else
153  if(jtr->has_child("weight")) {
154  auto w = ws->value_from_json(jtr->at("weight"));
155  new_epsilon_trans(aut, states[s], states[t],w);
156  }
157  else
158  new_epsilon_trans(aut, states[s], states[t]);
159  }
160  return aut;
161  }
162 
163  template <typename Aut>
164  void
166  if(p->has_child("name"))
167  aut->set_name(p->at("name")->to_string());
168  if(p->has_child("caption"))
169  aut->set_desc(p->at("caption")->to_string());
170  }
171 
172 }}//end of ns awali::stc
173 
174 #endif // !AWALI_ALGOS_JS_PARSER_HH
std::vector< node_t * > const & values
Definition: node.hh:429
Definition: node.hh:191
virtual std::string to_string() const
Coerces this node_t to an std::string.
Definition: node.hh:329
virtual int to_int() const
Coerces this node_t to int.
Definition: node.hh:319
node_kind_t const kind
Definition: node.hh:194
virtual array_t const * array() const
Casts this node to array_t.
Definition: node.hh:205
Definition: node.hh:365
virtual bool has_child(std::string const &key) const override
Definition: node.hh:378
virtual node_t * at(std::string const &key) override
The semiring of complex numbers.
Definition: c.hh:43
carries the algebraic settings of automata
Definition: context.hh:40
const weightset_ptr & weightset() const
Definition: context.hh:154
const labelset_ptr & labelset() const
Definition: context.hh:149
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:90
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
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:911
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:167
RatExpSet::ratexp_t js_parse_exp_content(const RatExpSet &rs, json::node_t *p)
Definition: js_parser.hh:95
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:165
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
ratexp_t parseNode(json::node_t *p)
Definition: js_parser.hh:50
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
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