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-2023 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] = s;
68  res->add_state(s);
69  if(tdc_->has_name(s))
70  res->set_state_name(s, tdc_->get_state_name(s));
71  }
72  for (auto t: tdc_->all_transitions())
73  if (tdc_->src_of(t) == tdc_->pre())
74  res->add_initial(in_out_map[tdc_->dst_of(t)],
75  o_weightset.lmul(tdc_->weight_of(t), o_weightset.one()));
76  else if (tdc_->dst_of(t) == tdc_->post())
77  res->add_final(in_out_map[tdc_->src_of(t)],
78  o_weightset.lmul(tdc_->weight_of(t), o_weightset.one()));
79  else if (exp_labelset.is_one(std::get<1>(tdc_->label_of(t))))
80  {
81  res->add_transition
82  (in_out_map[tdc_->src_of(t)], in_out_map[tdc_->dst_of(t)],
83  std::get<0>(tdc_->label_of(t)),
84  o_weightset.lmul(tdc_->weight_of(t), o_weightset.one()));
85  }
86  else
87  res->add_transition
88  (in_out_map[tdc_->src_of(t)], in_out_map[tdc_->dst_of(t)],
89  std::get<0>(tdc_->label_of(t)),
90  o_weightset.lmul(tdc_->weight_of(t), o_weightset.atom(std::get<1>(tdc_->label_of(t)))));
91  return res;
92  }
93 
94  void set_history() {
95  auto history = std::make_shared<single_history<Tdc>>(tdc_);
96  res->set_history(history);
97  for (auto p: in_out_map)
98  history->add_state(p.second, p.first);
99  }
100 
101 
102  const Tdc& tdc_;
104  std::map<state_t, state_t> in_out_map;
105  };
106  }
107 
108  template <typename Tdc>
109  inline
110  auto
111  lift_tdc(const Tdc& tdc, bool keep_history=true) -> typename internal::tdc_lifter<Tdc>::automaton_t
112  {
113  auto tl = internal::tdc_lifter<Tdc>(tdc);
114  tl.lift();
115  if(keep_history)
116  tl.set_history();
117  return tl.res;
118  }
119 
120 }}//end of ns awali::stc
121 
122 #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:111
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:931
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:103
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:104
void set_history()
Definition: lift_tdc.hh:94
typename i_labelset_t::template valueset_t< 1 > exp_labelset_t
Definition: lift_tdc.hh:40
const Tdc & tdc_
Definition: lift_tdc.hh:102
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