17 #ifndef AWALI_ALGOS_SYNCHRONIZE_HH
18 #define AWALI_ALGOS_SYNCHRONIZE_HH
25 #include<unordered_map>
35 #ifndef DETAILS_APPEND
36 #define DETAILS_APPEND
37 void append(std::string & s,
char c) {
40 void append(std::string & s,
const std::string t) {
46 template <
typename Tdc>
49 using labelset0_t =
typename labelset_t::template valueset_t<0>;
50 using labelset1_t =
typename labelset_t::template valueset_t<1>;
55 using triple_t = std::tuple<state_t, wordset0_value_t, wordset1_value_t>;
57 static Tdc
algo(
const Tdc& tdc,
bool keep_history=
true) {
59 std::unordered_map<triple_t, state_t>
states;
60 std::stack<triple_t> todo;
65 auto it =
states.find(triple);
68 states.emplace(std::make_pair(triple,
r));
77 std::vector<triple_t> finals;
79 for(
auto i : tdc->initial_transitions()) {
81 auto s = get_state(tu);
82 res->set_initial(s, tdc->weight_of(i));
85 while(!todo.empty()) {
86 auto triple = todo.top();
90 for(
auto tr : tdc->all_out(std::get<0>(triple))) {
92 if(t == tdc->post()) {
93 if(is_epsilon<wordset0_t>(std::get<1>(triple)) && is_epsilon<wordset1_t>(std::get<2>(triple)))
94 res->set_final(s, tdc->weight_of(tr));
96 finals.emplace_back(triple);
99 auto l= tdc->label_of(tr);
103 std::get<0>(t_val)= t;
104 if(!is_epsilon<labelset0_t>(std::get<0>(l))) {
107 if(!is_epsilon<labelset1_t>(std::get<1>(l))) {
110 auto a = get_epsilon<labelset0_t>();
111 auto b = get_epsilon<labelset1_t>();
112 if(!is_epsilon<wordset0_t>(std::get<1>(t_val)) && !is_epsilon<wordset1_t>(std::get<2>(t_val))) {
113 a = std::get<1>(t_val)[0];
114 b = std::get<2>(t_val)[0];
115 std::get<1>(t_val) = std::get<1>(t_val).substr(1);
116 std::get<2>(t_val) = std::get<2>(t_val).substr(1);
119 res->set_transition(s, nt, std::make_tuple(a,
b), tdc->weight_of(tr));
123 std::unordered_map<std::tuple<wordset0_value_t, wordset1_value_t>,
state_t> statesf;
124 if(!finals.empty()) {
125 auto get_statef = [&](std::tuple<wordset0_value_t, wordset1_value_t>& p) ->
state_t {
126 auto it = statesf.find(p);
127 if(it == statesf.end()) {
129 statesf.emplace(std::make_pair(p,
r));
136 for(
auto triple : finals) {
137 s = get_state(triple);
140 while(!is_epsilon<wordset0_t>(std::get<0>(p)) || !is_epsilon<wordset1_t>(std::get<1>(p))) {
141 auto a = get_epsilon<labelset0_t>();
142 auto b = get_epsilon<labelset1_t>();
143 if(!is_epsilon<wordset0_t>(std::get<0>(p))) {
145 std::get<0>(p) = std::get<0>(p).substr(1);
147 if(!is_epsilon<wordset1_t>(std::get<1>(p))) {
149 std::get<1>(p) = std::get<1>(p).substr(1);
153 res->set_transition(s, t, std::make_tuple(a,
b),tdc->get_final_weight(std::get<0>(triple)));
157 res->set_transition(s, t, std::make_tuple(a,
b));
164 auto final_history = std::make_shared<single_history<Tdc>>(tdc);
166 final_history->add_state(pp.second,std::get<0>(pp.first));
167 res->set_history(final_history);
174 template<
typename Tdc>
181 std::unordered_map<unsigned, unsigned> status;
182 std::unordered_map<state_t, int> diff;
187 for(
auto tr : tdc_->initial_transitions()) {
189 auto it = status.find(i);
190 if(it != status.end())
202 for(
auto tr : tdc_->out(s)) {
204 auto it = status.find(t);
205 if(it != status.end() && it->second == 2)
208 if(!is_epsilon<labelset0_t>(std::get<0>(tdc_->label_of(tr))))
210 if(!is_epsilon<labelset1_t>(std::get<1>(tdc_->label_of(tr))))
212 if(it == status.end()) {
228 template <
typename Tdc>
233 template<
typename Tdc>
The Boolean semring.
Definition: b.hh:38
The semiring of floating Numbers.
Definition: r.hh:35
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
Tdc synchronize(const Tdc &tdc, bool keep_history=true)
Definition: synchronize.hh:229
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
void proper_here(Aut &aut, direction_t dir=BACKWARD, bool prune=true)
Eliminate spontaneous transitions in place.
Definition: proper.hh:427
bool is_synchronizable(Tdc tdc)
Definition: synchronize.hh:234
Main namespace of Awali.
Definition: ato.hh:22
unsigned state_t
Definition: types.hh:21
Definition: synchronize.hh:175
bool test_rec(state_t s)
Definition: synchronize.hh:200
typename labelset_t::template valueset_t< 0 > labelset0_t
Definition: synchronize.hh:177
typename labelset_t::template valueset_t< 1 > labelset1_t
Definition: synchronize.hh:178
is_synchronizable_impl(Tdc tdc)
Definition: synchronize.hh:184
bool test()
Definition: synchronize.hh:186
labelset_t_of< Tdc > labelset_t
Definition: synchronize.hh:176
static auto get(const Tuple &t) -> type< Tuple >
Definition: sub_tuple.hh:119
Definition: synchronize.hh:47
std::tuple< state_t, wordset0_value_t, wordset1_value_t > triple_t
Definition: synchronize.hh:55
typename labelset_trait< labelset0_t >::wordset_t wordset0_t
Definition: synchronize.hh:51
typename wordset1_t::value_t wordset1_value_t
Definition: synchronize.hh:54
typename labelset_t::template valueset_t< 1 > labelset1_t
Definition: synchronize.hh:50
labelset_t_of< Tdc > labelset_t
Definition: synchronize.hh:48
typename labelset_trait< labelset1_t >::wordset_t wordset1_t
Definition: synchronize.hh:52
typename wordset0_t::value_t wordset0_value_t
Definition: synchronize.hh:53
static Tdc algo(const Tdc &tdc, bool keep_history=true)
Definition: synchronize.hh:57
typename labelset_t::template valueset_t< 0 > labelset0_t
Definition: synchronize.hh:49
L wordset_t
Definition: traits.hh:38