1 /********************* */
2 /*! \file decision_engine.h
4 ** Original author: Kshitij Bansal
5 ** Major contributors: none
6 ** Minor contributors (to current version): Clark Barrett, Dejan Jovanovic, Morgan Deters, Tim King
7 ** This file is part of the CVC4 project.
8 ** Copyright (c) 2009-2014 New York University and The University of Iowa
9 ** See the file COPYING in the top-level source directory for licensing
10 ** information.\endverbatim
12 ** \brief Decision engine
17 #include "cvc4_private.h"
19 #ifndef __CVC4__DECISION__DECISION_ENGINE_H
20 #define __CVC4__DECISION__DECISION_ENGINE_H
24 #include "decision/decision_strategy.h"
26 #include "expr/node.h"
27 #include "prop/cnf_stream.h"
28 #include "prop/prop_engine.h"
29 #include "prop/sat_solver_types.h"
30 #include "theory/decision_attributes.h"
31 #include "util/ite_removal.h"
32 #include "util/output.h"
35 using namespace CVC4::prop
;
36 using namespace CVC4::decision
;
40 class DecisionEngine
{
42 vector
<DecisionStrategy
* > d_enabledStrategies
;
43 vector
<ITEDecisionStrategy
* > d_needIteSkolemMap
;
44 RelevancyStrategy
* d_relevancyStrategy
;
46 typedef context::CDList
<Node
> AssertionsList
;
47 AssertionsList d_assertions
;
49 // PropEngine* d_propEngine;
50 CnfStream
* d_cnfStream
;
51 DPLLSatSolverInterface
* d_satSolver
;
53 context::Context
* d_satContext
;
54 context::UserContext
* d_userContext
;
56 // Does decision engine know the answer?
57 context::CDO
<SatValue
> d_result
;
59 // Disable creating decision engine without required parameters
62 // init/shutdown state
63 unsigned d_engineState
; // 0=pre-init; 1=init,pre-shutdown; 2=shutdown
65 // Necessary functions
68 DecisionEngine(context::Context
*sc
, context::UserContext
*uc
);
70 /** Destructor, currently does nothing */
72 Trace("decision") << "Destroying decision engine" << std::endl
;
75 // void setPropEngine(PropEngine* pe) {
76 // // setPropEngine should not be called more than once
77 // Assert(d_propEngine == NULL);
78 // Assert(pe != NULL);
82 void setSatSolver(DPLLSatSolverInterface
* ss
) {
83 // setPropEngine should not be called more than once
84 Assert(d_satSolver
== NULL
);
89 void setCnfStream(CnfStream
* cs
) {
90 // setPropEngine should not be called more than once
91 Assert(d_cnfStream
== NULL
);
96 /* enables decision stragies based on options */
99 /* clears all of the strategies */
100 void clearStrategies();
104 * This is called by SmtEngine, at shutdown time, just before
105 * destruction. It is important because there are destruction
106 * ordering issues between some parts of the system.
109 Assert(d_engineState
== 1);
112 Trace("decision") << "Shutting down decision engine" << std::endl
;
116 // Interface for External World to use our services
118 /** Gets the next decision based on strategies that are enabled */
119 SatLiteral
getNext(bool &stopSearch
) {
120 Assert(d_cnfStream
!= NULL
,
121 "Forgot to set cnfStream for decision engine?");
122 Assert(d_satSolver
!= NULL
,
123 "Forgot to set satSolver for decision engine?");
125 SatLiteral ret
= undefSatLiteral
;
127 i
< d_enabledStrategies
.size()
128 and ret
== undefSatLiteral
129 and stopSearch
== false; ++i
) {
130 ret
= d_enabledStrategies
[i
]->getNext(stopSearch
);
135 /** Is a sat variable relevant */
136 bool isRelevant(SatVariable var
);
139 * Try to get tell SAT solver what polarity to try for a
140 * decision. Return SAT_VALUE_UNKNOWN if it can't help
142 SatValue
getPolarity(SatVariable var
);
144 /** Is the DecisionEngine in a state where it has solved everything? */
146 Trace("decision") << "DecisionEngine::isDone() returning "
147 << (d_result
!= SAT_VALUE_UNKNOWN
)
148 << (d_result
!= SAT_VALUE_UNKNOWN
? "true" : "false")
150 return (d_result
!= SAT_VALUE_UNKNOWN
);
155 switch(d_result
.get()) {
156 case SAT_VALUE_TRUE
: return Result(Result::SAT
);
157 case SAT_VALUE_FALSE
: return Result(Result::UNSAT
);
158 case SAT_VALUE_UNKNOWN
: return Result(Result::SAT_UNKNOWN
, Result::UNKNOWN_REASON
);
159 default: Assert(false, "d_result is garbage");
165 void setResult(SatValue val
) {
169 // External World helping us help the Strategies
171 /** If one of the enabled strategies needs them */
172 /* bool needIteSkolemMap() { */
173 /* return d_needIteSkolemMap.size() > 0; */
176 /* add a set of assertions */
177 void addAssertions(const vector
<Node
> &assertions
);
180 * add a set of assertions, while providing a mapping between skolem
181 * variables and corresponding assertions. It is assumed that all
182 * assertions after assertionEnd were generated by ite
183 * removal. Hence, iteSkolemMap maps into only these.
185 void addAssertions(const vector
<Node
> &assertions
,
186 unsigned assertionsEnd
,
187 IteSkolemMap iteSkolemMap
);
189 /* add a single assertion */
190 void addAssertion(Node n
);
192 // Interface for Strategies to use stuff stored in Decision Engine
193 // (which was possibly requested by them on initialization)
196 * Get the assertions. Strategies are notified when these are available.
198 AssertionsList
& getAssertions() {
203 // Interface for Strategies to get information about External World
205 bool hasSatLiteral(TNode n
) {
206 return d_cnfStream
->hasLiteral(n
);
208 SatLiteral
getSatLiteral(TNode n
) {
209 return d_cnfStream
->getLiteral(n
);
211 SatValue
getSatValue(SatLiteral l
) {
212 return d_satSolver
->value(l
);
214 SatValue
getSatValue(TNode n
) {
215 return getSatValue(getSatLiteral(n
));
217 Node
getNode(SatLiteral l
) {
218 return d_cnfStream
->getNode(l
);
223 * Enable a particular decision strategy, also updating
224 * corresponding vector<DecisionStrategy*>-s is the engine
226 void enableStrategy(DecisionStrategy
* ds
);
228 };/* DecisionEngine class */
230 }/* CVC4 namespace */
232 #endif /* __CVC4__DECISION__DECISION_ENGINE_H */