1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Andrew Reynolds |
4 |
|
* |
5 |
|
* This file is part of the cvc5 project. |
6 |
|
* |
7 |
|
* Copyright (c) 2009-2021 by the authors listed in the file AUTHORS |
8 |
|
* in the top-level source directory and their institutional affiliations. |
9 |
|
* All rights reserved. See the file COPYING in the top-level source |
10 |
|
* directory for licensing information. |
11 |
|
* **************************************************************************** |
12 |
|
* |
13 |
|
* Abstract management of models for TheoryEngine. |
14 |
|
*/ |
15 |
|
|
16 |
|
#include "theory/model_manager.h" |
17 |
|
|
18 |
|
#include "options/smt_options.h" |
19 |
|
#include "options/theory_options.h" |
20 |
|
#include "prop/prop_engine.h" |
21 |
|
#include "smt/env.h" |
22 |
|
#include "theory/quantifiers/first_order_model.h" |
23 |
|
#include "theory/quantifiers/fmf/model_builder.h" |
24 |
|
#include "theory/quantifiers_engine.h" |
25 |
|
#include "theory/theory_engine.h" |
26 |
|
|
27 |
|
namespace cvc5 { |
28 |
|
namespace theory { |
29 |
|
|
30 |
9917 |
ModelManager::ModelManager(TheoryEngine& te, Env& env, EqEngineManager& eem) |
31 |
|
: EnvObj(env), |
32 |
|
d_te(te), |
33 |
|
d_eem(eem), |
34 |
|
d_modelEqualityEngine(nullptr), |
35 |
|
d_modelEqualityEngineAlloc(nullptr), |
36 |
|
d_model(new TheoryModel( |
37 |
9917 |
env, "DefaultModel", options::assignFunctionValues())), |
38 |
|
d_modelBuilder(nullptr), |
39 |
|
d_modelBuilt(false), |
40 |
19834 |
d_modelBuiltSuccess(false) |
41 |
|
{ |
42 |
9917 |
} |
43 |
|
|
44 |
9914 |
ModelManager::~ModelManager() {} |
45 |
|
|
46 |
9917 |
void ModelManager::finishInit(eq::EqualityEngineNotify* notify) |
47 |
|
{ |
48 |
|
// construct the model |
49 |
|
// Initialize the model and model builder. |
50 |
9917 |
if (logicInfo().isQuantified()) |
51 |
|
{ |
52 |
6826 |
QuantifiersEngine* qe = d_te.getQuantifiersEngine(); |
53 |
6826 |
Assert(qe != nullptr); |
54 |
6826 |
d_modelBuilder = qe->getModelBuilder(); |
55 |
|
} |
56 |
|
|
57 |
|
// make the default builder, e.g. in the case that the quantifiers engine does |
58 |
|
// not have a model builder |
59 |
9917 |
if (d_modelBuilder == nullptr) |
60 |
|
{ |
61 |
3091 |
d_alocModelBuilder.reset(new TheoryEngineModelBuilder(d_env)); |
62 |
3091 |
d_modelBuilder = d_alocModelBuilder.get(); |
63 |
|
} |
64 |
|
// notice that the equality engine of the model has yet to be assigned. |
65 |
9917 |
initializeModelEqEngine(notify); |
66 |
9917 |
} |
67 |
|
|
68 |
20981 |
void ModelManager::resetModel() |
69 |
|
{ |
70 |
20981 |
d_modelBuilt = false; |
71 |
20981 |
d_modelBuiltSuccess = false; |
72 |
|
// Reset basic information on the model object |
73 |
20981 |
d_model->reset(); |
74 |
20981 |
} |
75 |
|
|
76 |
24794 |
bool ModelManager::buildModel() |
77 |
|
{ |
78 |
24794 |
if (d_modelBuilt) |
79 |
|
{ |
80 |
|
// already computed |
81 |
8396 |
return d_modelBuiltSuccess; |
82 |
|
} |
83 |
|
// reset the flags now |
84 |
16398 |
d_modelBuilt = true; |
85 |
16398 |
d_modelBuiltSuccess = false; |
86 |
|
|
87 |
|
// prepare the model, which is specific to the manager |
88 |
16398 |
if (!prepareModel()) |
89 |
|
{ |
90 |
|
Trace("model-builder") << "ModelManager: fail prepare model" << std::endl; |
91 |
|
return false; |
92 |
|
} |
93 |
|
|
94 |
|
// now, finish building the model |
95 |
16397 |
d_modelBuiltSuccess = finishBuildModel(); |
96 |
|
|
97 |
16397 |
if (Trace.isOn("model-final")) |
98 |
|
{ |
99 |
|
Trace("model-final") << "Final model:" << std::endl; |
100 |
|
Trace("model-final") << d_model->debugPrintModelEqc() << std::endl; |
101 |
|
} |
102 |
|
|
103 |
32794 |
Trace("model-builder") << "ModelManager: model built success is " |
104 |
16397 |
<< d_modelBuiltSuccess << std::endl; |
105 |
|
|
106 |
16397 |
return d_modelBuiltSuccess; |
107 |
|
} |
108 |
|
|
109 |
|
bool ModelManager::isModelBuilt() const { return d_modelBuilt; } |
110 |
|
|
111 |
7481 |
void ModelManager::postProcessModel(bool incomplete) |
112 |
|
{ |
113 |
7481 |
if (!d_modelBuilt) |
114 |
|
{ |
115 |
|
// model not built, nothing to do |
116 |
6481 |
return; |
117 |
|
} |
118 |
1000 |
Trace("model-builder") << "ModelManager: post-process model..." << std::endl; |
119 |
|
// model construction should always succeed unless lemmas were added |
120 |
1000 |
AlwaysAssert(d_modelBuiltSuccess); |
121 |
1000 |
if (!options::produceModels()) |
122 |
|
{ |
123 |
371 |
return; |
124 |
|
} |
125 |
|
// Do post-processing of model from the theories (used for THEORY_SEP |
126 |
|
// to construct heap model) |
127 |
8806 |
for (TheoryId theoryId = theory::THEORY_FIRST; theoryId < theory::THEORY_LAST; |
128 |
|
++theoryId) |
129 |
|
{ |
130 |
8177 |
Theory* t = d_te.theoryOf(theoryId); |
131 |
8177 |
if (t == nullptr) |
132 |
|
{ |
133 |
|
// theory not active, skip |
134 |
|
continue; |
135 |
|
} |
136 |
16354 |
Trace("model-builder-debug") |
137 |
8177 |
<< " PostProcessModel on theory: " << theoryId << std::endl; |
138 |
8177 |
t->postProcessModel(d_model.get()); |
139 |
|
} |
140 |
|
// also call the model builder's post-process model |
141 |
629 |
d_modelBuilder->postProcessModel(incomplete, d_model.get()); |
142 |
|
} |
143 |
|
|
144 |
7221541 |
theory::TheoryModel* ModelManager::getModel() { return d_model.get(); } |
145 |
|
|
146 |
16397 |
bool ModelManager::collectModelBooleanVariables() |
147 |
|
{ |
148 |
16397 |
Trace("model-builder") << " CollectModelInfo boolean variables" << std::endl; |
149 |
|
// Get value of the Boolean variables |
150 |
16397 |
prop::PropEngine* propEngine = d_te.getPropEngine(); |
151 |
32794 |
std::vector<TNode> boolVars; |
152 |
16397 |
propEngine->getBooleanVariables(boolVars); |
153 |
16397 |
std::vector<TNode>::iterator it, iend = boolVars.end(); |
154 |
|
bool hasValue, value; |
155 |
86207 |
for (it = boolVars.begin(); it != iend; ++it) |
156 |
|
{ |
157 |
139620 |
TNode var = *it; |
158 |
69810 |
hasValue = propEngine->hasValue(var, value); |
159 |
|
// Should we assert that hasValue is true? |
160 |
69810 |
if (!hasValue) |
161 |
|
{ |
162 |
23446 |
Trace("model-builder-assertions") |
163 |
11723 |
<< " has no value : " << var << std::endl; |
164 |
11723 |
value = false; |
165 |
|
} |
166 |
139620 |
Trace("model-builder-assertions") |
167 |
139620 |
<< "(assert" << (value ? " " : " (not ") << var |
168 |
69810 |
<< (value ? ");" : "));") << std::endl; |
169 |
69810 |
if (!d_model->assertPredicate(var, value)) |
170 |
|
{ |
171 |
|
return false; |
172 |
|
} |
173 |
|
} |
174 |
16397 |
return true; |
175 |
|
} |
176 |
|
|
177 |
|
} // namespace theory |
178 |
29514 |
} // namespace cvc5 |