Awali
Another Weighted Automata library
lift.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_LIFT_HH
18 # define AWALI_ALGOS_LIFT_HH
19 
20 # include <map>
21 
28 
29 namespace awali { namespace sttc {
30 
31 
32  namespace internal
33  {
34  /*--------------.
35  | lift(types). |
36  `--------------*/
37 
38  template <typename ValueSet>
39  struct context_of
40  {
41  using type = typename ValueSet::context_t;
42  };
43 
44  template <typename Context>
45  struct context_of<sttc::mutable_automaton<Context>>
46  {
47  using type = Context;
48  };
49 
50  template <typename ValueSet>
52 
53  template <typename Context>
56 
57  template <typename Aut>
60 
61  template <typename RatExpSet>
64 
65  // lift(ctx) -> ctx
66  template <typename LabelSet, typename WeightSet>
69  {
70  auto rs_in
73  return {oneset{}, rs_in};
74  }
75 
76  // lift(ratexpset) -> ratexpset
77  template <typename Context>
78  lifted_ratexpset_t<ratexpset<Context>>
80  {
81  return {lift_context(rs.context()), rs.identities()};
82  }
83 
84  }
85 
86  /*------------------.
87  | lift(automaton). |
88  `------------------*/
89 
90  template <typename Aut>
91  inline
92  internal::lifted_automaton_t<Aut>
93  lift(const Aut& a, bool keep_history=true)
94  {
95  using auto_in_t = typename Aut::element_type;
96  using ctx_in_t = context_t_of<auto_in_t>;
97 
98  // Produce RatExps of the same context as the original automaton.
99  using rs_in_t = ratexpset_of<ctx_in_t>;
100  rs_in_t rs_in{get_rat_context(a->context()), rs_in_t::identities_t::trivial};
101 
102  auto ctx_out = internal::lift_context(a->context());
103  using auto_out_t = internal::lifted_automaton_t<auto_in_t>;
104  auto_out_t res = make_shared_ptr<auto_out_t>(ctx_out);
105  std::map<state_t, state_t> map;
106  map[a->pre()] = res->pre();
107  map[a->post()] = res->post();
108  for (auto s: a->states())
109  map[s] = res->add_state();
110 
111  for (auto t: a->all_transitions())
112  if (a->src_of(t) == a->pre())
113  res->add_initial(map[a->dst_of(t)],
114  rs_in.lmul(a->weight_of(t), rs_in.one()));
115  else if (a->dst_of(t) == a->post())
116  res->add_final(map[a->src_of(t)],
117  rs_in.lmul(a->weight_of(t), rs_in.one()));
118  else if (a->context().labelset()->is_one(a->label_of(t)))
119  {
120  res->add_transition
121  (map[a->src_of(t)], map[a->dst_of(t)],
122  {},
123  rs_in.lmul(a->weight_of(t), rs_in.one()));
124  }
125  else
126  res->add_transition
127  (map[a->src_of(t)], map[a->dst_of(t)],
128  {},
129  rs_in.lmul(a->weight_of(t), rs_in.atom(a->label_of(t))));
130  if(keep_history) {
131  auto history = std::make_shared<single_history<Aut>>(a);
132  for(auto p : map)
133  history->add_state(p.second,p.first);
134  res->set_history(history);
135  }
136  return res;
137  }
138 
139  /*---------------.
140  | lift(ratexp). |
141  `---------------*/
142 
143  namespace internal
144  {
145  template <typename Exp>
147  typename lifted_context_t<context_t_of<Exp>>::ratexp_t;
148  }
149 
150  template <typename RatExpSet>
151  inline
153  lift(const RatExpSet& rs, const typename RatExpSet::ratexp_t& e)
154  {
155  auto lrs = internal::lift_ratexpset(rs);
156  return lrs.lmul(e, lrs.one());
157  }
158 
159 }}//end of ns awali::stc
160 
161 #endif // !AWALI_ALGOS_LIFT_HH
carries the algebraic settings of automata
Definition: context.hh:40
Implementation of labels are ones: there is a single instance of label.
Definition: oneset.hh:38
auto map(const std::tuple< Ts... > &ts, Fun f) -> decltype(map_tuple_(f, ts, make_index_sequence< sizeof...(Ts)>()))
Map a function on a tuple, return tuple of the results.
Definition: tuple.hh:134
mutable_automaton< lifted_context_t< context_t_of< Aut > >> lifted_automaton_t
Definition: lift.hh:59
typename ValueSet::context_t type
Definition: lift.hh:41
lifted_context_t< context< LabelSet, WeightSet > > lift_context(const context< LabelSet, WeightSet > &ctx)
Definition: lift.hh:68
typename context_of< ValueSet >::type context_of_t
Definition: lift.hh:51
lifted_ratexpset_t< ratexpset< Context > > lift_ratexpset(const ratexpset< Context > &rs)
Definition: lift.hh:79
typename lifted_context_t< context_t_of< Exp > >::ratexp_t lifted_ratexp_t
Definition: lift.hh:147
Definition: lift.hh:40
@ trivial
Trivial identities only.
auto get_rat_context(const Context &ctx) -> context< typename labelset_trait< typename Context::labelset_t >::ratlabelset_t, typename Context::weightset_t >
Definition: traits.hh:238
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
internal::lifted_automaton_t< Aut > lift(const Aut &a, bool keep_history=true)
Definition: lift.hh:93
std::shared_ptr< internal::mutable_automaton_impl< Context > > mutable_automaton
Definition: mutable_automaton.hh:45
Main namespace of Awali.
Definition: ato.hh:22
Provide a variadic mul on top of a binary mul(), and one().
Definition: weightset.hh:38