Awali
Another Weighted Automata library
real_time.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_REALTIME_HH
18 #define AWALI_ALGOS_REALTIME_HH
19 
23 #include <awali/sttc/misc/add_epsilon_trans.hh> // is_epsilon
24 #include<unordered_map>
25 #include<map>
26 #include<stack>
27 #include<tuple>
28 #include<utility>
29 
30 namespace awali {
31  namespace sttc {
32  namespace internal {
33 #ifndef DETAILS_APPEND
34 #define DETAILS_APPEND
35  void append(std::string & s, char c) {
36  s.append(1,c);
37  }
38  void append(std::string & s, const std::string t) {
39  s.append(t);
40  }
41 #endif
42 
43  template <typename Tdc>
44  struct realtimer {
46  using labelset0_t = typename labelset_t::template valueset_t<0>;
47  using labelset1_t = typename labelset_t::template valueset_t<1>;
50  using wordset1_value_t = typename wordset1_t::value_t;
53 
58 
59 
60  using pair_t = std::pair<state_t, wordset1_value_t>;
61 
62  static bool is_realtime(const Tdc& tdc) {
63  for(auto t : tdc->transitions())
64  if(is_epsilon<labelset0_t>(std::get<0>(tdc->label_of(t))))
65  return false;
66  return true;
67  }
68 
69  static bool is_sequential(const Tdc& tdc) {
70  if(!is_realtime(tdc))
71  return false;
72  if (1 < tdc->num_initials())
73  return false;
74  using label0_t = typename labelset0_t::value_t;
75  for(auto s : tdc->states()) {
76  std::unordered_set<label0_t> seen;
77  for(auto t : tdc->out(s))
78  if (!seen.insert(std::get<0>(tdc->label_of(t))).second)
79  return false;
80  }
81  return true;
82  }
83 
84  static automaton_t realtime(const Tdc& tdc, bool keep_history) {
85  std::unordered_map<pair_t, state_t> states;
86  std::stack<pair_t> todo;
87 
88  auto lsets =tdc->context().labelset()->sets();
89  wordset1_t wset1=get_wordset(std::get<1>(lsets));
91  res_labelset_t(std::make_tuple(std::get<0>(lsets), wset1)),*tdc->context().weightset()});
92  auto get_state = [&](pair_t& pair) -> state_t {
93  auto it = states.find(pair);
94  if(it == states.end()) {
95  state_t r = res->add_state();
96  states.emplace(std::make_pair(pair, r));
97  todo.emplace(pair);
98  return r;
99  }
100  else
101  return it->second;
102  };
103 
104  //states to explore:
105  std::vector<pair_t> finals;
106 
107  for(auto i : tdc->initial_transitions()) {
108  pair_t tu{tdc->dst_of(i),wordset1_t::one()};
109  auto s = get_state(tu);
110  res->set_initial(s, tdc->weight_of(i));
111  todo.emplace(tu);
112  }
113  while(!todo.empty()) {
114  auto pair = todo.top();
115  todo.pop();
116  //retrieve the valuation of state s:
117  state_t s = get_state(pair);
118  for(auto tr : tdc->all_out(pair.first)) {
119  state_t t = tdc->dst_of(tr);
120  if(t == tdc->post()) {
121  if(is_epsilon<wordset1_t>(pair.second))
122  res->set_final(s, tdc->weight_of(tr));
123  else
124  finals.emplace_back(pair);
125  continue;
126  }
127  auto l= tdc->label_of(tr);
128  //the valuation of t is the valuation of s...
129  pair_t t_val{pair};
130  //... plus the label of the transition
131  t_val.first= t;
132  if(!is_epsilon<labelset1_t>(std::get<1>(l))) {
133  internal::append(t_val.second,std::get<1>(l));
134  }
135  if(!is_epsilon<labelset0_t>(std::get<0>(l))) {
136  auto w=t_val.second;
137  t_val.second=wordset1_t::one();
138  state_t nt = get_state(t_val);
139  res->set_transition(s, nt, std::make_tuple(std::get<0>(l),w), tdc->weight_of(tr));
140  }
141  else {
142  auto w=wordset1_t::one();
143  state_t nt = get_state(t_val);
144  res->set_transition(s, nt, std::make_tuple(std::get<0>(l),w), tdc->weight_of(tr));
145  }
146  }
147  }
148 
149  if(!finals.empty()) {
150  //state_t r = res->add_state();
151  auto a = labelset0_t::special();
152  for(auto pair : finals) {
153  state_t s=get_state(pair);
154  res->set_transition(s, res->post(), std::make_tuple(a, pair.second),tdc->get_final_weight(pair.first));
155  }
156  //res->set_final(r);
157  }
158  proper_here(res);
159  notnullset0_t nnlset0 =get_not_nullableset(std::get<0>(lsets));
161  ret_labelset_t(std::make_tuple(nnlset0, wset1)),*tdc->context().weightset()});
162  copy_into(res,ret,keep_history);
163  if(keep_history) {
164  auto final_history = std::make_shared<single_history<Tdc>>(tdc);
165  auto & ret_history = ret->history()->template as<single_history<decltype(*res)>>();
166  std::unordered_map<state_t, state_t> inv_states;
167  for(auto pp: states)
168  inv_states[pp.second]=pp.first.first;
169  for(auto s : ret->states())
170  final_history->add_state(s,inv_states[ret_history.get_state(s)]);
171  ret->set_history(final_history);
172  }
173  return ret;
174  }
175  };
176  }
177  //
178  template<typename Tdc>
179  auto realtime(const Tdc& tdc, bool keep_history=true) -> typename internal::realtimer<Tdc>::automaton_t {
180  return internal::realtimer<Tdc>::realtime(tdc, keep_history);
181  }
182 
183  template<typename Tdc>
184  bool is_realtime(const Tdc& tdc) {
186  }
187 
188  template<typename Tdc>
189  bool is_sequential_tdc(const Tdc& tdc) {
191  }
192  }
193 }//end of ns awali::stc
194 
195 #endif
The semiring of complex numbers.
Definition: c.hh:44
carries the algebraic settings of automata
Definition: context.hh:40
The semiring of floating Numbers.
Definition: r.hh:35
specialisation of history_base
Definition: single_history.hh:50
void add_state(state_t s, const state_t &sb)
set the history of state s
Definition: single_history.hh:91
A ValueSet which is a Cartesian product of ValueSets.
Definition: tupleset.hh:80
std::vector< state_t > states(abstract_automaton_t const *aut, bool all)
void append(std::string &s, char c)
Definition: real_time.hh:35
constant< type_t::one, Label, Weight > one
Definition: fwd.hh:116
auto realtime(const Tdc &tdc, bool keep_history=true) -> typename internal::realtimer< Tdc >::automaton_t
Definition: real_time.hh:179
bool is_realtime(const Tdc &tdc)
Definition: real_time.hh:184
void copy_into(const AutIn &in, AutOut &out, Pred keep_state, bool keep_history=true, bool transpose=false)
Copy an automaton.
Definition: copy.hh:144
bool is_sequential_tdc(const Tdc &tdc)
Definition: real_time.hh:189
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
auto get_wordset(const L &labelset) -> typename labelset_trait< L >::wordset_t
Definition: traits.hh:232
auto get_not_nullableset(const L &labelset) -> typename labelset_trait< L >::not_nullable_t
Definition: traits.hh:253
mutable_automaton< Context > make_mutable_automaton(const Context &ctx)
Definition: mutable_automaton.hh:915
void proper_here(Aut &aut, direction_t dir=BACKWARD, bool prune=true)
Eliminate spontaneous transitions in place.
Definition: proper.hh:427
std::shared_ptr< internal::mutable_automaton_impl< Context > > mutable_automaton
Definition: mutable_automaton.hh:45
pair_automaton< Aut > pair(const Aut &aut, bool keep_initials=false)
Definition: synchronizing_word.hh:266
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
unsigned state_t
Definition: types.hh:21
Definition: real_time.hh:44
static bool is_realtime(const Tdc &tdc)
Definition: real_time.hh:62
static automaton_t realtime(const Tdc &tdc, bool keep_history)
Definition: real_time.hh:84
tupleset< labelset0_t, wordset1_t > res_labelset_t
Definition: real_time.hh:51
labelset_t_of< Tdc > labelset_t
Definition: real_time.hh:45
typename labelset_t::template valueset_t< 1 > labelset1_t
Definition: real_time.hh:47
mutable_automaton< ret_context_t > automaton_t
Definition: real_time.hh:57
std::pair< state_t, wordset1_value_t > pair_t
Definition: real_time.hh:60
typename labelset_t::template valueset_t< 0 > labelset0_t
Definition: real_time.hh:46
static bool is_sequential(const Tdc &tdc)
Definition: real_time.hh:69
typename labelset_trait< labelset0_t >::not_nullable_t notnullset0_t
Definition: real_time.hh:54
typename labelset_trait< labelset1_t >::wordset_t wordset1_t
Definition: real_time.hh:48
typename wordset1_t::value_t wordset1_value_t
Definition: real_time.hh:50
weightset_t_of< Tdc > weightset_t
Definition: real_time.hh:49
tupleset< notnullset0_t, wordset1_t > ret_labelset_t
Definition: real_time.hh:55
L not_nullable_t
Definition: traits.hh:36
L wordset_t
Definition: traits.hh:38