formula2bdd.cc 4.97 KB
Newer Older
1
2
3
// -*- coding: utf-8 -*-
// Copyright (C) 2009, 2010, 2012, 2014 Laboratoire de Recherche et
// Développement de l'Epita (LRDE).
Guillaume Sadegh's avatar
Guillaume Sadegh committed
4
// Copyright (C) 2003, 2004 Laboratoire d'Informatique de Paris
5
6
// 6 (LIP6), département Systèmes Répartis Coopératifs (SRC),
// Université Pierre et Marie Curie.
7
8
9
10
11
//
// 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
12
// the Free Software Foundation; either version 3 of the License, or
13
14
15
16
17
18
19
20
// (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
21
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
22

23
#include <cassert>
24
#include "formula2bdd.hh"
25
26
#include "ltlast/allnodes.hh"
#include "ltlast/visitor.hh"
27
28
29
30
31
32
#include "misc/minato.hh"

namespace spot
{
  using namespace ltl;

33
  namespace
34
  {
35
    class formula_to_bdd_visitor: public ltl::visitor
36
    {
37
    public:
38
      formula_to_bdd_visitor(const bdd_dict_ptr& d, void* owner)
39
40
41
	: d_(d), owner_(owner)
      {
      }
42

43
44
45
46
      virtual
      ~formula_to_bdd_visitor()
      {
      }
47

48
49
50
51
52
      virtual void
      visit(const atomic_prop* node)
      {
	res_ = bdd_ithvar(d_->register_proposition(node, owner_));
      }
53

54
55
56
57
58
59
60
61
62
63
64
      virtual void
      visit(const constant* node)
      {
	switch (node->val())
	  {
	  case constant::True:
	    res_ = bddtrue;
	    return;
	  case constant::False:
	    res_ = bddfalse;
	    return;
65
	  case constant::EmptyWord:
66
	    SPOT_UNIMPLEMENTED();
67
	  }
68
	SPOT_UNREACHABLE();
69
      }
70

71
72
73
      virtual void
      visit(const bunop*)
      {
74
	SPOT_UNIMPLEMENTED();
75
76
      }

77
78
79
80
81
      virtual void
      visit(const unop* node)
      {
	switch (node->op())
	  {
82
	  case unop::Finish:
83
84
85
	  case unop::F:
	  case unop::G:
	  case unop::X:
86
87
	  case unop::Closure:
	  case unop::NegClosure:
88
	  case unop::NegClosureMarked:
89
	    SPOT_UNIMPLEMENTED();
90
91
92
93
94
95
	  case unop::Not:
	    {
	      res_ = bdd_not(recurse(node->child()));
	      return;
	    }
	  }
96
	SPOT_UNREACHABLE();
97
98
99
100
101
102
103
104
105
      }

      virtual void
      visit(const binop* node)
      {
	bdd f1 = recurse(node->first());
	bdd f2 = recurse(node->second());

	switch (node->op())
106
	  {
107
108
109
110
111
112
113
114
	  case binop::Xor:
	    res_ = bdd_apply(f1, f2, bddop_xor);
	    return;
	  case binop::Implies:
	    res_ = bdd_apply(f1, f2, bddop_imp);
	    return;
	  case binop::Equiv:
	    res_ = bdd_apply(f1, f2, bddop_biimp);
115
	    return;
116
117
	  case binop::U:
	  case binop::R:
118
119
	  case binop::W:
	  case binop::M:
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
120
121
	  case binop::UConcat:
	  case binop::EConcat:
122
	  case binop::EConcatMarked:
123
	    SPOT_UNIMPLEMENTED();
124
	  }
125
	SPOT_UNREACHABLE();
126
      }
127

128
129
130
131
132
133
134
135
136
137
138
139
140
141
      virtual void
      visit(const multop* node)
      {
	int op = -1;
	switch (node->op())
	  {
	  case multop::And:
	    op = bddop_and;
	    res_ = bddtrue;
	    break;
	  case multop::Or:
	    op = bddop_or;
	    res_ = bddfalse;
	    break;
142
	  case multop::Concat:
143
	  case multop::Fusion:
144
	  case multop::AndNLM:
145
146
	  case multop::OrRat:
	  case multop::AndRat:
147
	    SPOT_UNIMPLEMENTED();
148
149
150
151
152
153
154
155
	  }
	assert(op != -1);
	unsigned s = node->size();
	for (unsigned n = 0; n < s; ++n)
	  {
	    res_ = bdd_apply(res_, recurse(node->nth(n)), op);
	  }
      }
156

157
158
159
160
161
      bdd
      result() const
      {
	return res_;
      }
162

163
164
165
166
167
      bdd
      recurse(const formula* f) const
      {
	return formula_to_bdd(f, d_, owner_);
      }
168

169
    private:
170
      bdd_dict_ptr d_;
171
172
173
      void* owner_;
      bdd res_;
    };
174

175
    // Convert a BDD which is known to be a conjonction into a formula.
176
    static const ltl::formula*
177
    conj_to_formula(bdd b, const bdd_dict_ptr d)
178
    {
179
180
181
182
183
184
      if (b == bddfalse)
	return constant::false_instance();
      multop::vec* v = new multop::vec;
      while (b != bddtrue)
	{
	  int var = bdd_var(b);
185
186
187
	  const bdd_dict::bdd_info& i = d->bdd_map[var];
	  assert(i.type == bdd_dict::var);
	  const formula* res = i.f->clone();
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204

	  bdd high = bdd_high(b);
	  if (high == bddfalse)
	    {
	      res = unop::instance(unop::Not, res);
	      b = bdd_low(b);
	    }
	  else
	    {
	      // If bdd_low is not false, then b was not a conjunction.
	      assert(bdd_low(b) == bddfalse);
	      b = high;
	    }
	  assert(b != bddfalse);
	  v->push_back(res);
	}
      return multop::instance(multop::And, v);
205
206
    }

207
  } // anonymous
208
209

  bdd
210
  formula_to_bdd(const formula* f, const bdd_dict_ptr& d, void* for_me)
211
212
213
214
215
216
217
  {
    formula_to_bdd_visitor v(d, for_me);
    f->accept(v);
    return v.result();
  }

  const formula*
218
  bdd_to_formula(bdd f, const bdd_dict_ptr d)
219
220
221
222
223
224
225
226
227
228
229
230
231
232
  {
    if (f == bddfalse)
      return constant::false_instance();

    multop::vec* v = new multop::vec;

    minato_isop isop(f);
    bdd cube;
    while ((cube = isop.next()) != bddfalse)
      v->push_back(conj_to_formula(cube, d));

    return multop::instance(multop::Or, v);
  }
}