convert.cc 5.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// -*- coding: utf-8 -*-
// Copyright (C) 2015, 2016 Laboratoire de Recherche et Developpement de
// l'Epita (LRDE).
//
// This file is part of Spot, a model checking library.
//
// Spot is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// Spot is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
// License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include <spot/twacube_algos/convert.hh>
#include <assert.h>

namespace spot
{
  spot::cube satone_to_cube(bdd one, cubeset& cubeset,
26
                            std::unordered_map<int, int>& binder)
27
28
29
30
  {
    auto cube = cubeset.alloc();
    while (one != bddtrue)
      {
31
32
33
34
35
36
37
38
39
40
41
42
        if (bdd_high(one) == bddfalse)
          {
            assert(binder.find(bdd_var(one)) != binder.end());
            cubeset.set_false_var(cube, binder[bdd_var(one)]);
            one = bdd_low(one);
          }
        else
          {
            assert(binder.find(bdd_var(one)) != binder.end());
            cubeset.set_true_var(cube, binder[bdd_var(one)]);
            one = bdd_high(one);
          }
43
44
45
46
47
      }
    return cube;
  }

  bdd cube_to_bdd(spot::cube cube, const cubeset& cubeset,
48
                  std::unordered_map<int, int>& reverse_binder)
49
50
51
52
  {
    bdd result = bddtrue;
    for (unsigned int i = 0; i < cubeset.size(); ++i)
      {
53
54
55
56
57
        assert(reverse_binder.find(i) != reverse_binder.end());
        if (cubeset.is_false_var(cube, i))
          result &= bdd_nithvar(reverse_binder[i]);
        if (cubeset.is_true_var(cube, i))
          result &= bdd_ithvar(reverse_binder[i]);
58
59
60
      }
    return result;
  }
61

Etienne Renault's avatar
Etienne Renault committed
62
  spot::twacube_ptr twa_to_twacube(const spot::const_twa_graph_ptr aut)
63
  {
Etienne Renault's avatar
Etienne Renault committed
64
65
66
67
    // Compute the necessary binder and extract atomic propositions
    std::unordered_map<int, int> ap_binder;
    std::vector<std::string>* aps = extract_aps(aut, ap_binder);

68
    // Declare the twa cube
Etienne Renault's avatar
Etienne Renault committed
69
    auto tg =  spot::make_twacube(*aps);
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

    // Fix acceptance
    tg->acc() = aut->acc();

    // This binder maps twagraph indexes to twacube ones.
    std::unordered_map<int, int> st_binder;

    // Fill binder and create corresponding states into twacube
    for (unsigned n = 0; n < aut->num_states(); ++n)
      st_binder.insert({n, tg->new_state()});

    // Fix the initial state
    tg->set_initial(st_binder[aut->get_init_state_number()]);

    // Get the cubeset
    auto cs = tg->get_cubeset();

    // Now just build all transitions of this automaton
    // spot::cube cube(aps);
    for (unsigned n = 0; n < aut->num_states(); ++n)
      for (auto& t: aut->out(n))
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
        {
          bdd cond = t.cond;

          // Special case for bddfalse
          if (cond == bddfalse)
            {
              spot::cube cube = tg->get_cubeset().alloc();
              for (unsigned int i = 0; i < cs.size(); ++i)
                cs.set_false_var(cube, i); // FIXME ! use fill!
              tg->create_transition(st_binder[n], cube,
                                    t.acc, st_binder[t.dst]);
            }
          else
            // Split the bdd into multiple transitions
            while (cond != bddfalse)
              {
                bdd one = bdd_satone(cond);
                cond -= one;
                spot::cube cube =spot::satone_to_cube(one, cs, ap_binder);
                tg->create_transition(st_binder[n], cube, t.acc,
                                      st_binder[t.dst]);
              }
        }
114
115
    // Must be contiguous to support swarming.
    assert(tg->succ_contiguous());
Etienne Renault's avatar
Etienne Renault committed
116
    delete aps;
117
118
119
120
    return tg;
  }

  std::vector<std::string>*
Etienne Renault's avatar
Etienne Renault committed
121
  extract_aps(const spot::const_twa_graph_ptr aut,
122
              std::unordered_map<int, int>& ap_binder)
123
124
125
126
  {
    std::vector<std::string>* aps = new std::vector<std::string>();
    for (auto f: aut->ap())
      {
127
128
129
130
131
132
        int size = aps->size();
        if (std::find(aps->begin(), aps->end(), f.ap_name()) == aps->end())
          {
            aps->push_back(f.ap_name());
            ap_binder.insert({aut->get_dict()->var_map[f], size});
          }
133
134
135
      }
    return aps;
  }
136
137

  spot::twa_graph_ptr
Etienne Renault's avatar
Etienne Renault committed
138
  twacube_to_twa(spot::twacube_ptr twacube)
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
  {
    // Grab necessary variables
    auto& theg = twacube->get_graph();
    spot::cubeset cs = twacube->get_cubeset();

    // Build the resulting graph
    auto d = spot::make_bdd_dict();
    auto res = make_twa_graph(d);

    // Fix the acceptance of the resulting automaton
    res->acc() = twacube->acc();

    // Grep bdd id for each atomic propositions
    std::vector<int> bdds_ref;
    for (auto& ap : twacube->get_ap())
      bdds_ref.push_back(res->register_ap(ap));

    // Build all resulting states
    for (unsigned int i = 0; i < theg.num_states(); ++i)
      {
159
160
161
        unsigned st = res->new_state();
        (void) st;
        assert(st == i);
162
163
164
165
166
      }

    // Build all resulting conditions.
    for (unsigned int i = 1; i <= theg.num_edges(); ++i)
      {
167
168
169
170
171
172
173
174
175
176
177
178
        bdd cond = bddtrue;
        for (unsigned j = 0; j < cs.size(); ++j)
          {
            if (cs.is_true_var(theg.edge_data(i).cube_, j))
              cond &= bdd_ithvar(bdds_ref[j]);
            else if (cs.is_false_var(theg.edge_data(i).cube_, j))
              cond &= bdd_nithvar(bdds_ref[j]);
            // otherwise it 's a free variable do nothing
          }

        res->new_edge(theg.edge_storage(i).src, theg.edge_storage(i).dst,
                      cond, theg.edge_data(i).acc_);
179
180
181
182
183
184
185
      }

    // Fix the initial state
    res->set_init_state(twacube->get_initial());

    return res;
  }
186
}