Awali
Another Weighted Automata library
lift_tdc.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_LIFT_TDC_HH
18 # define AWALI_ALGOS_LIFT_TDC_HH
19 
20 # include <map>
21 
28 
29 namespace awali { namespace sttc {
30 
31 
32  namespace internal
33  {
34 
35  template <typename Tdc>
36  struct tdc_lifter {
39  using o_labelset_t = typename i_labelset_t::template valueset_t<0>;
40  using exp_labelset_t = typename i_labelset_t::template valueset_t<1>;
45 
47 
48  tdc_lifter (const Tdc& tdc) : tdc_(tdc) {}
49 
50  /*------------------.
51  | lift(automaton). |
52  `------------------*/
53 
54  inline
56  lift()
57  {
58  const o_labelset_t& o_labelset{std::get<0>(tdc_->context().labelset()->sets())};
59  const exp_labelset_t exp_labelset{std::get<1>(tdc_->context().labelset()->sets())};
60  exp_context_t exp_context{std::get<1>(tdc_->context().labelset()->sets()), *(tdc_->context().weightset())};
61  o_weightset_t o_weightset{exp_context, rat::identities::series};
62  o_context_t o_context{o_labelset, o_weightset};
63  res = make_mutable_automaton(o_context);
64  in_out_map[tdc_->pre()] = res->pre();
65  in_out_map[tdc_->post()] = res->post();
66  for (auto s: tdc_->states())
67  in_out_map[s] = res->add_state();
68 
69  for (auto t: tdc_->all_transitions())
70  if (tdc_->src_of(t) == tdc_->pre())
71  res->add_initial(in_out_map[tdc_->dst_of(t)],
72  o_weightset.lmul(tdc_->weight_of(t), o_weightset.one()));
73  else if (tdc_->dst_of(t) == tdc_->post())
74  res->add_final(in_out_map[tdc_->src_of(t)],
75  o_weightset.lmul(tdc_->weight_of(t), o_weightset.one()));
76  else if (exp_labelset.is_one(std::get<1>(tdc_->label_of(t))))
77  {
78  res->add_transition
79  (in_out_map[tdc_->src_of(t)], in_out_map[tdc_->dst_of(t)],
80  std::get<0>(tdc_->label_of(t)),
81  o_weightset.lmul(tdc_->weight_of(t), o_weightset.one()));
82  }
83  else
84  res->add_transition
85  (in_out_map[tdc_->src_of(t)], in_out_map[tdc_->dst_of(t)],
86  std::get<0>(tdc_->label_of(t)),
87  o_weightset.lmul(tdc_->weight_of(t), o_weightset.atom(std::get<1>(tdc_->label_of(t)))));
88  return res;
89  }
90 
91  void set_history() {
92  auto history = std::make_shared<single_history<Tdc>>(tdc_);
93  res->set_history(history);
94  for (auto p: in_out_map)
95  history->add_state(p.second, p.first);
96  }
97 
98 
99  const Tdc& tdc_;
101  std::map<state_t, state_t> in_out_map;
102  };
103  }
104 
105  template <typename Tdc>
106  inline
107  auto
108  lift_tdc(const Tdc& tdc, bool keep_history=true) -> typename internal::tdc_lifter<Tdc>::automaton_t
109  {
110  auto tl = internal::tdc_lifter<Tdc>(tdc);
111  tl.lift();
112  if(keep_history)
113  tl.set_history();
114  return tl.res;
115  }
116 
117 }}//end of ns awali::stc
118 
119 #endif // !AWALI_ALGOS_LIFT_TDC_HH
carries the algebraic settings of automata
Definition: context.hh:40
@ series
Trivial identities plus series identities.
auto lift_tdc(const Tdc &tdc, bool keep_history=true) -> typename internal::tdc_lifter< Tdc >::automaton_t
Definition: lift_tdc.hh:108
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:915
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: lift_tdc.hh:36
labelset_t_of< Tdc > i_labelset_t
Definition: lift_tdc.hh:37
mutable_automaton< o_context_t > lift()
Definition: lift_tdc.hh:56
mutable_automaton< o_context_t > automaton_t
Definition: lift_tdc.hh:46
typename i_labelset_t::template valueset_t< 0 > o_labelset_t
Definition: lift_tdc.hh:39
automaton_t res
Definition: lift_tdc.hh:100
weightset_t_of< Tdc > i_weightset_t
Definition: lift_tdc.hh:38
std::map< state_t, state_t > in_out_map
Definition: lift_tdc.hh:101
void set_history()
Definition: lift_tdc.hh:91
typename i_labelset_t::template valueset_t< 1 > exp_labelset_t
Definition: lift_tdc.hh:40
const Tdc & tdc_
Definition: lift_tdc.hh:99
tdc_lifter(const Tdc &tdc)
Definition: lift_tdc.hh:48
Provide a variadic mul on top of a binary mul(), and one().
Definition: weightset.hh:38