summaryrefslogtreecommitdiff
path: root/src/decision/decision_engine.h
blob: 4d354af2a8335d193a43b3be356a40c76a738674 (plain)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*********************                                                        */
/*! \file decision_engine.h
 ** \verbatim
 ** Original author: kshitij
 ** Major contributors: none
 ** Minor contributors (to current version): barrett, dejan, mdeters
 ** This file is part of the CVC4 prototype.
 ** Copyright (c) 2009-2012  New York University and The University of Iowa
 ** See the file COPYING in the top-level source directory for licensing
 ** information.\endverbatim
 **
 ** \brief Decision engine
 **
 ** Decision engine
 **/

#include "cvc4_private.h"

#ifndef __CVC4__DECISION__DECISION_ENGINE_H
#define __CVC4__DECISION__DECISION_ENGINE_H

#include <vector>

#include "decision/decision_strategy.h"

#include "expr/node.h"
#include "prop/cnf_stream.h"
#include "prop/prop_engine.h"
#include "prop/sat_solver_types.h"
#include "util/ite_removal.h"
#include "util/output.h"

using namespace std;
using namespace CVC4::prop;
using namespace CVC4::decision;

namespace CVC4 {

class DecisionEngine {

  vector <DecisionStrategy* > d_enabledStrategies;
  vector <ITEDecisionStrategy* > d_needIteSkolemMap;
  RelevancyStrategy* d_relevancyStrategy;

  vector <Node> d_assertions;

  // PropEngine* d_propEngine;
  CnfStream* d_cnfStream;
  DPLLSatSolverInterface* d_satSolver;

  context::Context* d_satContext;
  context::Context* d_userContext;

  // Does decision engine know the answer?
  context::CDO<SatValue> d_result;

  // Disable creating decision engine without required parameters
  DecisionEngine() : d_result(NULL) {}

  // init/shutdown state
  unsigned d_engineState;    // 0=pre-init; 1=init,pre-shutdown; 2=shutdown
public:
  // Necessary functions

  /** Constructor */
  DecisionEngine(context::Context *sc, context::Context *uc);

  /** Destructor, currently does nothing */
  ~DecisionEngine() {
    Trace("decision") << "Destroying decision engine" << std::endl;
    for(unsigned i = 0; i < d_enabledStrategies.size(); ++i)
      delete d_enabledStrategies[i];
  }
  
  // void setPropEngine(PropEngine* pe) {
  //   // setPropEngine should not be called more than once
  //   Assert(d_propEngine == NULL);
  //   Assert(pe != NULL);
  //   d_propEngine = pe;
  // }

  void setSatSolver(DPLLSatSolverInterface* ss) {
    // setPropEngine should not be called more than once
    Assert(d_satSolver == NULL);
    Assert(ss != NULL);
    d_satSolver = ss;
  }

  void setCnfStream(CnfStream* cs) {
    // setPropEngine should not be called more than once
    Assert(d_cnfStream == NULL);
    Assert(cs != NULL);
    d_cnfStream = cs;
  }

  /* enables decision stragies based on options */
  void init();

  /**
   * This is called by SmtEngine, at shutdown time, just before
   * destruction.  It is important because there are destruction
   * ordering issues between some parts of the system.  For now,
   * there's nothing to do here in the DecisionEngine.
   */
  void shutdown() {
    Assert(d_engineState == 1);
    d_engineState = 2;

    Trace("decision") << "Shutting down decision engine" << std::endl;
  }

  // Interface for External World to use our services

  /** Gets the next decision based on strategies that are enabled */
  SatLiteral getNext(bool &stopSearch) {
    Assert(d_cnfStream != NULL,
           "Forgot to set cnfStream for decision engine?");
    Assert(d_satSolver != NULL,
           "Forgot to set satSolver for decision engine?");

    SatLiteral ret = undefSatLiteral;
    for(unsigned i = 0; 
        i < d_enabledStrategies.size() 
          and ret == undefSatLiteral
          and stopSearch == false; ++i) {
      ret = d_enabledStrategies[i]->getNext(stopSearch);
    }
    return ret;
  }

  /** Is a sat variable relevant */
  bool isRelevant(SatVariable var);

  /**
   * Try to get tell SAT solver what polarity to try for a
   * decision. Return SAT_VALUE_UNKNOWN if it can't help 
   */
  SatValue getPolarity(SatVariable var);

  /** Is the DecisionEngine in a state where it has solved everything? */
  bool isDone() {
    Trace("decision") << "DecisionEngine::isDone() returning "
		      << (d_result != SAT_VALUE_UNKNOWN)
		      << (d_result != SAT_VALUE_UNKNOWN ? "true" : "false")
		      << std::endl;
    return (d_result != SAT_VALUE_UNKNOWN);
  }

  /** */
  Result getResult() {
    switch(d_result.get()) {
    case SAT_VALUE_TRUE: return Result(Result::SAT);
    case SAT_VALUE_FALSE: return Result(Result::UNSAT);
    case SAT_VALUE_UNKNOWN: return Result(Result::SAT_UNKNOWN, Result::UNKNOWN_REASON);
    default: Assert(false, "d_result is garbage");
    }
    return Result();
  }

  /** */
  void setResult(SatValue val) {
    d_result = val;
  }

  // External World helping us help the Strategies

  /** If one of the enabled strategies needs them  */
  /* bool needIteSkolemMap() { */
  /*   return d_needIteSkolemMap.size() > 0; */
  /* } */

  /* add a set of assertions */
  void addAssertions(const vector<Node> &assertions);

  /**
   * add a set of assertions, while providing a mapping between skolem
   * variables and corresponding assertions. It is assumed that all
   * assertions after assertionEnd were generated by ite
   * removal. Hence, iteSkolemMap maps into only these.
   */
  void addAssertions(const vector<Node> &assertions,
                     unsigned assertionsEnd,
                     IteSkolemMap iteSkolemMap);

  /* add a single assertion */
  void addAssertion(Node n);

  // Interface for Strategies to use stuff stored in Decision Engine
  // (which was possibly requested by them on initialization)

  /**
   * Get the assertions. Strategies are notified when these are available. 
   */
  const vector<Node>& getAssertions() {
    return d_assertions;
  }


  // Interface for Strategies to get information about External World

  bool hasSatLiteral(TNode n) {
    return d_cnfStream->hasLiteral(n);
  }
  SatLiteral getSatLiteral(TNode n) {
    return d_cnfStream->getLiteral(n);
  }
  SatValue getSatValue(SatLiteral l) {
    return d_satSolver->value(l);
  }
  SatValue getSatValue(TNode n) {
    return getSatValue(getSatLiteral(n));
  }
  Node getNode(SatLiteral l) {
    return d_cnfStream->getNode(l);
  }

private:
  /**
   * Enable a particular decision strategy, also updating
   * corresponding vector<DecisionStrategy*>-s is the engine
   */
  void enableStrategy(DecisionStrategy* ds);

};/* DecisionEngine class */

}/* CVC4 namespace */

#endif /* __CVC4__DECISION__DECISION_ENGINE_H */
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback