convert.cc 5.87 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
// Copyright (C) 2015, 2016, 2018, 2020 Laboratoire de Recherche et Developpement de
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 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/>.

20
#include "config.h"
21
22
23
24
25
26
#include <spot/twacube_algos/convert.hh>
#include <assert.h>

namespace spot
{
  spot::cube satone_to_cube(bdd one, cubeset& cubeset,
27
                            std::unordered_map<int, int>& binder)
28
29
30
31
  {
    auto cube = cubeset.alloc();
    while (one != bddtrue)
      {
32
33
34
35
36
37
38
39
40
41
42
43
        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);
          }
44
45
46
47
48
      }
    return cube;
  }

  bdd cube_to_bdd(spot::cube cube, const cubeset& cubeset,
49
                  std::unordered_map<int, int>& reverse_binder)
50
51
52
53
  {
    bdd result = bddtrue;
    for (unsigned int i = 0; i < cubeset.size(); ++i)
      {
54
55
56
57
58
        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]);
59
60
61
      }
    return result;
  }
62

Etienne Renault's avatar
Etienne Renault committed
63
  spot::twacube_ptr twa_to_twacube(const spot::const_twa_graph_ptr aut)
64
  {
65
66
67
    if (aut == nullptr)
      return nullptr;

Etienne Renault's avatar
Etienne Renault committed
68
69
70
71
    // 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);

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

    // 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))
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
        {
          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]);
              }
        }
118
119
    // Must be contiguous to support swarming.
    assert(tg->succ_contiguous());
Etienne Renault's avatar
Etienne Renault committed
120
    delete aps;
121
122
123
124
    return tg;
  }

  std::vector<std::string>*
Etienne Renault's avatar
Etienne Renault committed
125
  extract_aps(const spot::const_twa_graph_ptr aut,
126
              std::unordered_map<int, int>& ap_binder)
127
128
129
130
  {
    std::vector<std::string>* aps = new std::vector<std::string>();
    for (auto f: aut->ap())
      {
131
132
133
134
135
136
        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});
          }
137
138
139
      }
    return aps;
  }
140
141

  spot::twa_graph_ptr
Etienne Renault's avatar
Etienne Renault committed
142
  twacube_to_twa(spot::twacube_ptr twacube)
143
144
145
146
147
148
149
150
151
152
153
154
155
156
  {
    // 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;
Etienne Renault's avatar
Etienne Renault committed
157
    for (auto& ap : twacube->ap())
158
159
160
161
162
      bdds_ref.push_back(res->register_ap(ap));

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

    // Build all resulting conditions.
    for (unsigned int i = 1; i <= theg.num_edges(); ++i)
      {
171
172
173
174
175
176
177
178
179
180
181
182
        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_);
183
184
185
186
187
188
189
      }

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

    return res;
  }
190
}