formula2bdd.cc 4.36 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright (C) 2003  Laboratoire d'Informatique de Paris 6 (LIP6),
// dpartement Systmes Rpartis Coopratifs (SRC), Universit Pierre
// et Marie Curie.
//
// 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 2 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 Spot; see the file COPYING.  If not, write to the Free
// Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.

#include "formula2bdd.hh"
#include "ltlast/allnodes.hh"
#include "ltlast/visitor.hh"
#include "misc/minato.hh"
#include "ltlvisit/clone.hh"

namespace spot
{
  using namespace ltl;

  class formula_to_bdd_visitor : public ltl::const_visitor
  {
  public:
    formula_to_bdd_visitor(bdd_dict* d, void* owner)
      : d_(d), owner_(owner)
    {
    }

    virtual
    ~formula_to_bdd_visitor()
    {
    }

    virtual void
    visit(const atomic_prop* node)
    {
      res_ = bdd_ithvar(d_->register_proposition(node, owner_));
    }

    virtual void
    visit(const constant* node)
    {
      switch (node->val())
	{
	case constant::True:
	  res_ = bddtrue;
	  return;
	case constant::False:
	  res_ = bddfalse;
	  return;
	}
      /* Unreachable code.  */
      assert(0);
    }

    virtual void
    visit(const unop* node)
    {
      switch (node->op())
	{
	case unop::F:
	case unop::G:
	case unop::X:
	  assert(!"unsupported operator");
	case unop::Not:
	  {
	    res_ = bdd_not(recurse(node->child()));
	    return;
	  }
	}
      /* Unreachable code.  */
      assert(0);
    }

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

      switch (node->op())
	{
	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);
	  return;
	case binop::U:
	case binop::R:
	  assert(!"unsupported operator");
	}
      /* Unreachable code.  */
      assert(0);
    }

    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;
	}
      assert(op != -1);
      unsigned s = node->size();
      for (unsigned n = 0; n < s; ++n)
	{
	  res_ = bdd_apply(res_, recurse(node->nth(n)), op);
	}
    }

    bdd
    result() const
    {
      return res_;
    }

    bdd
    recurse(const formula* f) const
    {
      return formula_to_bdd(f, d_, owner_);
    }

  private:
    bdd_dict* d_;
    void* owner_;
    bdd res_;
  };

  bdd
  formula_to_bdd(const formula* f, bdd_dict* d, void* for_me)
  {
    formula_to_bdd_visitor v(d, for_me);
    f->accept(v);
    return v.result();
  }

  // Convert a BDD which is known to be a conjonction into a formula.
  static ltl::formula*
  conj_to_formula(bdd b, const bdd_dict* d)
  {
    if (b == bddfalse)
      return constant::false_instance();
    multop::vec* v = new multop::vec;
    while (b != bddtrue)
      {
	int var = bdd_var(b);
	bdd_dict::vf_map::const_iterator isi = d->var_formula_map.find(var);
	assert(isi != d->var_formula_map.end());
	formula* res = clone(isi->second);

	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);
  }

  const formula*
  bdd_to_formula(bdd f, const bdd_dict* d)
  {
    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);
  }
}