Awali
Another Weighted Automata library
js_parser_deprecated.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_DEPRECATED_HH
18 # define AWALI_ALGOS_JS_PARSER_DEPRECATED_HH
19 
20 #include <awali/sttc/misc/raise.hh>
22 #include <awali/common/json/parser.cc>
28 # include <stdexcept>
29 # include <stack>
30 # include <iostream>
31 # include <sstream>
32 
33 namespace awali {
34  namespace sttc {
35  namespace deprecated {
36 
37  namespace detail {
38 
39  template<typename RatExpSet>
40  struct js_exp_parser {
41  using ratexpset_t = RatExpSet;
44  using ratexp_t = typename ratexpset_t::value_t;
46 
47  js_exp_parser(const ratexpset_t& rs, std::istream& i)
48  : rs_(rs), i_(i)
49  {}
50 
51  ratexp_t parseNode(const std::string& key) {
52  ratexp_t e;
53  char c;
54  if(key == "Sum") {
55  bool first=true;
57  do {
58  ratexp_t f= parseNode();
59  if(first) {
60  e=f;
61  first=false;
62  }
63  else {
64  e=rs_.add(e,f);
65  }
66  i_ >> c;
67  } while (c==',');
68  if(c!=']')
69  throw std::runtime_error("Json parser: ratexp");
70  }
71  else if(key == "Prod") {
72  bool first=true;
74  do {
75  ratexp_t f= parseNode();
76  if(first) {
77  e=f;
78  first=false;
79  }
80  else
81  e=rs_.mul(e,f);
82  i_ >> c;
83  } while (c==',');
84  if(c!=']')
85  throw std::runtime_error("Json parser: ratexp");
86  }
87  else if(key == "Star") {
88  e= rs_.star(parseNode());
89  }
90  else if(key == "Label") {
91  e= rs_.atom(ls_.value_from_json<0u>(i_));
92  }
93  else if(key == "LWeight") {
94  auto w = ws_.value_from_json<0u>(i_);
95  std::string key2 = ::awali::internal::parsestring(i_);
97  e=rs_.lmul(w,parseNode(key2));
98  }
99  else if(key == "RWeight") {
100  auto w = ws_.value_from_json<0u>(i_);
101  std::string key2 = ::awali::internal::parsestring(i_);
102  ::awali::internal::check(i_,':');
103  e=rs_.rmul(parseNode(key2),w);
104  }
105  else if(key == "One") {
107  if(c!='n')
108  throw std::runtime_error("Json parser: ratexp");
109  e = rs_.one();
110  }
111  else if(key == "Zero") {
113  if(c!='n')
114  throw std::runtime_error("Json parser: ratexp");
115  e = rs_.zero();
116  }
117  else
118  throw std::runtime_error("Json parser: ratexp");
119  return e;
120  }
121 
123  std::string key = ::awali::internal::get_first_attr(i_);
124  ratexp_t e=parseNode(key);
125  ::awali::internal::check(i_,'}');
126  return e;
127  }
128 
129  private:
130  const ratexpset_t& rs_;
131  std::istream& i_;
132  weightset_t ws_ = *rs_.weightset();
133  labelset_t ls_ = *rs_.labelset();
134 
135  };
136  }
137 
138  template <typename RatExpSet>
139  inline
140  typename RatExpSet::ratexp_t
141  js_parse_exp_content(const RatExpSet& rs,
142  std::istream& i)
143  {
145  return parser.parseNode();
146  }
147 
148  template <typename Context>
150  js_parse_aut_content(const Context& context, std::istream& i) {
151  // using aut_t = mutable_automaton<Context>;
152  auto ws = context.weightset();
153  auto ls = context.labelset();
155  char c;
156  std::unordered_map<unsigned,state_t> states;
157  unsigned s;
158  std::string key = ::awali::internal::get_first_attr(i);
159  if(key != "Content")
160  throw std::runtime_error("json: Content");
161  ::awali::internal::check(i, '[');
163  if(key != "States")
164  throw std::runtime_error("json: States");
165  ::awali::internal::check(i, '[');
166  do {
167  if(::awali::internal::peek(i)==']') { //no state
168  i >> c;
169  break;
170  }
172  if(key != "Id")
173  throw std::runtime_error("json: State Id");
175  states[s]=aut->add_state();
177  if(key != "Name")
178  throw std::runtime_error("json: Name");
179  ::awali::internal::check(i, ':');
181  aut->set_state_name(states[s],key);
182  if(::awali::internal::peek(i)!='}') {
183  std::string tmp=::awali::internal::parsestring(i);
184  ::awali::internal::check(i, ':');
185  if(tmp == "History") {
187  auto history=aut->history();
188  if(history->get_nature() == history_kind_t::NO_HISTORY) {
189  history=std::make_shared<string_history>();
190  aut->set_history(history);
191  }
192  auto& hs = dynamic_cast<sttc::string_history&>(*history);
193  hs.add_state(states[s], key);
194  }
195  else
197  }
198  ::awali::internal::check(i, '}');
199  i >> c;
200  } while(c==',');
201  if(c!=']')
202  throw std::runtime_error("json: States ]");
203  ::awali::internal::check(i, '}');
204  if(::awali::internal::peek(i)==']') {
205  ::awali::internal::check(i, ']');
206  ::awali::internal::check(i, '}');
207  return aut;
208  }
209  ::awali::internal::check(i, ',');
211  if(key == "Initial States") {
212  ::awali::internal::check(i, '[');
213  do {
214  if(::awali::internal::peek(i)!='{') {
216  aut->set_initial(states[s]);
217  }
218  else {
220  if(key != "Id")
221  throw std::runtime_error("json: IState Id");
223  if(::awali::internal::peek(i)=='"') {
225  typename Context::labelset_t::value_t l;
226  bool has_label=false;
227  if(key == "Label") {
228  has_label=true;
229  ::awali::internal::check(i, ':');
230  l=ls->template value_from_json<0u>(::awali::json::parse(i));
231  if(::awali::internal::peek(i)!='"') {
232  aut->new_transition(states[s], aut->post(), l);
233  ::awali::internal::check(i, '}');
234  i >> c;
235  continue;
236  }
237  }
238  if(key != "Weight")
239  throw std::runtime_error("json: FWeight");
240  ::awali::internal::check(i, ':');
241  auto w
242  = ws->template value_from_json<0u>(::awali::json::parse(i));
243  if(has_label)
244  aut->new_transition(states[s], aut->post(), l, w);
245  else
246  aut->set_initial(states[s], w);
247  }
248  else
249  aut->set_initial(states[s]);
250  ::awali::internal::check(i, '}');
251  }
252  i >> c;
253  } while(c==',');
254  if(c!=']')
255  throw std::runtime_error("json: IStates ]");
256  ::awali::internal::check(i, '}');
257  if(::awali::internal::peek(i)==']') {
258  ::awali::internal::check(i, ']');
259  ::awali::internal::check(i, '}');
260  ::awali::internal::check(i, ']');
261  ::awali::internal::check(i, '}');
262  return aut;
263  }
266  }
267  if(key == "Final States") {
268  ::awali::internal::check(i, '[');
269  do {
270  if(::awali::internal::peek(i)!='{') {
272  aut->set_final(states[s]);
273  }
274  else {
276  if(key != "Id")
277  throw std::runtime_error("json: FState Id");
279  if(::awali::internal::peek(i)=='"') {
281  typename Context::labelset_t::value_t l;
282  bool has_label=false;
283  if(key == "Label") {
284  has_label=true;
285  ::awali::internal::check(i, ':');
286  l=ls->template value_from_json<0u>(::awali::json::parse(i));
287  if(::awali::internal::peek(i)!='"') {
288  aut->new_transition(states[s], aut->post(), l);
289  ::awali::internal::check(i, '}');
290  i >> c;
291  continue;
292  }
293  }
294  if(key != "Weight")
295  throw std::runtime_error("json: FWeight");
296  ::awali::internal::check(i, ':');
297  auto w=ws->template value_from_json<0u>(::awali::json::parse(i));
298  if(has_label)
299  aut->new_transition(states[s], aut->post(), l, w);
300  else
301  aut->set_final(states[s], w);
302  }
303  else
304  aut->set_final(states[s]);
305  ::awali::internal::check(i, '}');
306  }
307  i >> c;
308  } while(c==',');
309  if(c!=']')
310  throw std::runtime_error("json: FStates ]");
311  ::awali::internal::check(i, '}');
312  if(::awali::internal::peek(i)==']') {
313  ::awali::internal::check(i, ']');
314  ::awali::internal::check(i, '}');
315  ::awali::internal::check(i, ']');
316  ::awali::internal::check(i, '}');
317  return aut;
318  }
321  }
322  if(key == "Transitions") {
323  unsigned t;
324  ::awali::internal::check(i, '[');
325  do {
326  if(::awali::internal::peek(i)==']') { //no transition
327  i >> c;
328  break;
329  }
331  if(key == "Id") {
334  ::awali::internal::check(i, ':');
335  }
336  if(key != "Src")
337  throw std::runtime_error("json: Src");
340  ::awali::internal::check(i, ':');
341  if(key != "Dst")
342  throw std::runtime_error("json: Dst");
344  if(::awali::internal::peek(i)!='"') {
345  new_epsilon_trans(aut, states[s], states[t]);
346  ::awali::internal::check(i, '}');
347  i >> c;
348  continue;
349  }
351  ::awali::internal::check(i, ':');
352  typename Context::labelset_t::value_t l;
353  bool has_label=false;
354  if(key == "Label") {
355  l = ls->template value_from_json<0u>(::awali::json::parse(i));
356  has_label=true;
357  if(::awali::internal::peek(i)!='"') {
358  aut->new_transition(states[s], states[t],l);
359  ::awali::internal::check(i, '}');
360  i >> c;
361  continue;
362  }
364  ::awali::internal::check(i, ':');
365  }
366  if(key != "Weight")
367  throw std::runtime_error("json: Weight");
368  auto w = ws->template value_from_json<0u>(::awali::json::parse(i));
369  if(has_label)
370  aut->new_transition(states[s], states[t],l,w);
371  else
372  new_epsilon_trans(aut, states[s], states[t], w);
373  ::awali::internal::check(i, '}');
374  i >> c;
375  } while(c==',');
376  if(c!=']')
377  throw std::runtime_error("json: Trans ]");
378  ::awali::internal::check(i, '}');// end object Transition
379  ::awali::internal::check(i, ']');// end array Content
380  ::awali::internal::check(i, '}');// end objet Content
381  }
382  return aut;
383  }
384 
385  }
386  }
387 }//end of ns awali::stc
388 
389 #endif // !AWALI_ALGOS_JS_PARSER_HH
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.
bool has_label(automaton_t tdc, unsigned i, std::string l)
std::vector< state_t > states(abstract_automaton_t const *aut, bool all)
static char parsecst(std::istream &i)
Definition: utils.hh:216
static char peek(std::istream &i)
peeks the next character
Definition: utils.hh:139
static std::string parsestring(std::istream &i)
Definition: utils.hh:152
static int parseint(std::istream &i)
Definition: utils.hh:237
static std::string get_first_attr(std::istream &i)
Definition: utils.hh:245
static void parseignore(std::istream &i)
Definition: utils.hh:254
static void check(std::istream &i, char e, std::string oth="")
checks the next character
Definition: utils.hh:116
node_t * parse(std::istream &)
mutable_automaton< Context > js_parse_aut_content(const Context &context, std::istream &i)
Definition: js_parser_deprecated.hh:150
RatExpSet::ratexp_t js_parse_exp_content(const RatExpSet &rs, std::istream &i)
Definition: js_parser_deprecated.hh:141
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 > 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
std::shared_ptr< internal::mutable_automaton_impl< Context > > mutable_automaton
Definition: mutable_automaton.hh:45
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_deprecated.hh:40
weightset_t_of< ratexpset_t > weightset_t
Definition: js_parser_deprecated.hh:45
typename ratexpset_t::value_t ratexp_t
Definition: js_parser_deprecated.hh:44
labelset_t_of< context_t > labelset_t
Definition: js_parser_deprecated.hh:43
ratexp_t parseNode(const std::string &key)
Definition: js_parser_deprecated.hh:51
ratexp_t parseNode()
Definition: js_parser_deprecated.hh:122
js_exp_parser(const ratexpset_t &rs, std::istream &i)
Definition: js_parser_deprecated.hh:47
RatExpSet ratexpset_t
Definition: js_parser_deprecated.hh:41
context_t_of< ratexpset_t > context_t
Definition: js_parser_deprecated.hh:42