GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/model_manager.cpp Lines: 98 103 95.1 %
Date: 2021-08-16 Branches: 130 260 50.0 %

Line Exec Source
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
9853
ModelManager::ModelManager(TheoryEngine& te, Env& env, EqEngineManager& eem)
31
    : d_te(te),
32
      d_env(env),
33
      d_eem(eem),
34
      d_modelEqualityEngine(nullptr),
35
      d_modelEqualityEngineAlloc(nullptr),
36
      d_model(new TheoryModel(
37
9853
          env, "DefaultModel", options::assignFunctionValues())),
38
      d_modelBuilder(nullptr),
39
      d_modelBuilt(false),
40
19706
      d_modelBuiltSuccess(false)
41
{
42
9853
}
43
44
9853
ModelManager::~ModelManager() {}
45
46
9853
void ModelManager::finishInit(eq::EqualityEngineNotify* notify)
47
{
48
  // construct the model
49
9853
  const LogicInfo& logicInfo = d_env.getLogicInfo();
50
  // Initialize the model and model builder.
51
9853
  if (logicInfo.isQuantified())
52
  {
53
6763
    QuantifiersEngine* qe = d_te.getQuantifiersEngine();
54
6763
    Assert(qe != nullptr);
55
6763
    d_modelBuilder = qe->getModelBuilder();
56
  }
57
58
  // make the default builder, e.g. in the case that the quantifiers engine does
59
  // not have a model builder
60
9853
  if (d_modelBuilder == nullptr)
61
  {
62
3090
    d_alocModelBuilder.reset(new TheoryEngineModelBuilder);
63
3090
    d_modelBuilder = d_alocModelBuilder.get();
64
  }
65
  // notice that the equality engine of the model has yet to be assigned.
66
9853
  initializeModelEqEngine(notify);
67
9853
}
68
69
21106
void ModelManager::resetModel()
70
{
71
21106
  d_modelBuilt = false;
72
21106
  d_modelBuiltSuccess = false;
73
  // Reset basic information on the model object
74
21106
  d_model->reset();
75
21106
}
76
77
25412
bool ModelManager::buildModel()
78
{
79
25412
  if (d_modelBuilt)
80
  {
81
    // already computed
82
8894
    return d_modelBuiltSuccess;
83
  }
84
  // reset the flags now
85
16518
  d_modelBuilt = true;
86
16518
  d_modelBuiltSuccess = false;
87
88
  // prepare the model, which is specific to the manager
89
16518
  if (!prepareModel())
90
  {
91
2
    Trace("model-builder") << "ModelManager: fail prepare model" << std::endl;
92
2
    return false;
93
  }
94
95
  // now, finish building the model
96
16515
  d_modelBuiltSuccess = finishBuildModel();
97
98
16515
  if (Trace.isOn("model-final"))
99
  {
100
    Trace("model-final") << "Final model:" << std::endl;
101
    Trace("model-final") << d_model->debugPrintModelEqc() << std::endl;
102
  }
103
104
33030
  Trace("model-builder") << "ModelManager: model built success is "
105
16515
                         << d_modelBuiltSuccess << std::endl;
106
107
16515
  return d_modelBuiltSuccess;
108
}
109
110
bool ModelManager::isModelBuilt() const { return d_modelBuilt; }
111
112
7483
void ModelManager::postProcessModel(bool incomplete)
113
{
114
7483
  if (!d_modelBuilt)
115
  {
116
    // model not built, nothing to do
117
6494
    return;
118
  }
119
989
  Trace("model-builder") << "ModelManager: post-process model..." << std::endl;
120
  // model construction should always succeed unless lemmas were added
121
989
  AlwaysAssert(d_modelBuiltSuccess);
122
989
  if (!options::produceModels())
123
  {
124
378
    return;
125
  }
126
  // Do post-processing of model from the theories (used for THEORY_SEP
127
  // to construct heap model)
128
8554
  for (TheoryId theoryId = theory::THEORY_FIRST; theoryId < theory::THEORY_LAST;
129
       ++theoryId)
130
  {
131
7943
    Theory* t = d_te.theoryOf(theoryId);
132
7943
    if (t == nullptr)
133
    {
134
      // theory not active, skip
135
      continue;
136
    }
137
15886
    Trace("model-builder-debug")
138
7943
        << "  PostProcessModel on theory: " << theoryId << std::endl;
139
7943
    t->postProcessModel(d_model.get());
140
  }
141
  // also call the model builder's post-process model
142
611
  d_modelBuilder->postProcessModel(incomplete, d_model.get());
143
}
144
145
288304
theory::TheoryModel* ModelManager::getModel() { return d_model.get(); }
146
147
16515
bool ModelManager::collectModelBooleanVariables()
148
{
149
16515
  Trace("model-builder") << "  CollectModelInfo boolean variables" << std::endl;
150
  // Get value of the Boolean variables
151
16515
  prop::PropEngine* propEngine = d_te.getPropEngine();
152
33030
  std::vector<TNode> boolVars;
153
16515
  propEngine->getBooleanVariables(boolVars);
154
16515
  std::vector<TNode>::iterator it, iend = boolVars.end();
155
  bool hasValue, value;
156
87610
  for (it = boolVars.begin(); it != iend; ++it)
157
  {
158
142190
    TNode var = *it;
159
71095
    hasValue = propEngine->hasValue(var, value);
160
    // Should we assert that hasValue is true?
161
71095
    if (!hasValue)
162
    {
163
23934
      Trace("model-builder-assertions")
164
11967
          << "    has no value : " << var << std::endl;
165
11967
      value = false;
166
    }
167
142190
    Trace("model-builder-assertions")
168
142190
        << "(assert" << (value ? " " : " (not ") << var
169
71095
        << (value ? ");" : "));") << std::endl;
170
71095
    if (!d_model->assertPredicate(var, value))
171
    {
172
      return false;
173
    }
174
  }
175
16515
  return true;
176
}
177
178
122553
void ModelManager::collectAssertedTerms(TheoryId tid,
179
                                        std::set<Node>& termSet,
180
                                        bool includeShared) const
181
{
182
122553
  Theory* t = d_te.theoryOf(tid);
183
  // Collect all terms appearing in assertions
184
122553
  context::CDList<Assertion>::const_iterator assert_it = t->facts_begin(),
185
122553
                                             assert_it_end = t->facts_end();
186
4561787
  for (; assert_it != assert_it_end; ++assert_it)
187
  {
188
2219617
    collectTerms(tid, *assert_it, termSet);
189
  }
190
191
122553
  if (includeShared)
192
  {
193
    // Add terms that are shared terms
194
122553
    context::CDList<TNode>::const_iterator shared_it = t->shared_terms_begin(),
195
                                           shared_it_end =
196
122553
                                               t->shared_terms_end();
197
1380615
    for (; shared_it != shared_it_end; ++shared_it)
198
    {
199
629031
      collectTerms(tid, *shared_it, termSet);
200
    }
201
  }
202
122553
}
203
204
2848648
void ModelManager::collectTerms(TheoryId tid,
205
                                TNode n,
206
                                std::set<Node>& termSet) const
207
{
208
2848648
  const std::set<Kind>& irrKinds = d_model->getIrrelevantKinds();
209
5697296
  std::vector<TNode> visit;
210
5697296
  TNode cur;
211
2848648
  visit.push_back(n);
212
7214961
  do
213
  {
214
10063609
    cur = visit.back();
215
10063609
    visit.pop_back();
216
10063609
    if (termSet.find(cur) != termSet.end())
217
    {
218
      // already visited
219
4917139
      continue;
220
    }
221
5146470
    Kind k = cur.getKind();
222
    // only add to term set if a relevant kind
223
5146470
    if (irrKinds.find(k) == irrKinds.end())
224
    {
225
2466896
      termSet.insert(cur);
226
    }
227
    // traverse owned terms, don't go under quantifiers
228
14262091
    if ((k == kind::NOT || k == kind::EQUAL || Theory::theoryOf(cur) == tid)
229
15095523
        && !cur.isClosure())
230
    {
231
4773094
      visit.insert(visit.end(), cur.begin(), cur.end());
232
    }
233
10063609
  } while (!visit.empty());
234
2848648
}
235
236
}  // namespace theory
237
29340
}  // namespace cvc5