GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/model_manager.cpp Lines: 65 72 90.3 %
Date: 2021-11-07 Branches: 78 186 41.9 %

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
15273
ModelManager::ModelManager(Env& env, TheoryEngine& te, 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
15273
          env, "DefaultModel", options().theory.assignFunctionValues)),
38
      d_modelBuilder(nullptr),
39
      d_modelBuilt(false),
40
30546
      d_modelBuiltSuccess(false)
41
{
42
15273
}
43
44
15268
ModelManager::~ModelManager() {}
45
46
15273
void ModelManager::finishInit(eq::EqualityEngineNotify* notify)
47
{
48
  // construct the model
49
  // Initialize the model and model builder.
50
15273
  if (logicInfo().isQuantified())
51
  {
52
12150
    QuantifiersEngine* qe = d_te.getQuantifiersEngine();
53
12150
    Assert(qe != nullptr);
54
12150
    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
15273
  if (d_modelBuilder == nullptr)
60
  {
61
3123
    d_alocModelBuilder.reset(new TheoryEngineModelBuilder(d_env));
62
3123
    d_modelBuilder = d_alocModelBuilder.get();
63
  }
64
  // notice that the equality engine of the model has yet to be assigned.
65
15273
  initializeModelEqEngine(notify);
66
15273
}
67
68
29793
void ModelManager::resetModel()
69
{
70
29793
  d_modelBuilt = false;
71
29793
  d_modelBuiltSuccess = false;
72
  // Reset basic information on the model object
73
29793
  d_model->reset();
74
29793
}
75
76
45497
bool ModelManager::buildModel()
77
{
78
45497
  if (d_modelBuilt)
79
  {
80
    // already computed
81
20507
    return d_modelBuiltSuccess;
82
  }
83
  // reset the flags now
84
24990
  d_modelBuilt = true;
85
24990
  d_modelBuiltSuccess = false;
86
87
  // prepare the model, which is specific to the manager
88
24990
  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
24989
  d_modelBuiltSuccess = finishBuildModel();
96
97
24989
  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
49978
  Trace("model-builder") << "ModelManager: model built success is "
104
24989
                         << d_modelBuiltSuccess << std::endl;
105
106
24989
  return d_modelBuiltSuccess;
107
}
108
109
bool ModelManager::isModelBuilt() const { return d_modelBuilt; }
110
111
9679
void ModelManager::postProcessModel(bool incomplete)
112
{
113
9679
  if (!d_modelBuilt)
114
  {
115
    // model not built, nothing to do
116
8476
    return;
117
  }
118
1203
  Trace("model-builder") << "ModelManager: post-process model..." << std::endl;
119
  // model construction should always succeed unless lemmas were added
120
1203
  AlwaysAssert(d_modelBuiltSuccess);
121
1203
  if (!options().smt.produceModels)
122
  {
123
392
    return;
124
  }
125
  // Do post-processing of model from the theories (used for THEORY_SEP
126
  // to construct heap model)
127
11354
  for (TheoryId theoryId = theory::THEORY_FIRST; theoryId < theory::THEORY_LAST;
128
       ++theoryId)
129
  {
130
10543
    Theory* t = d_te.theoryOf(theoryId);
131
10543
    if (t == nullptr)
132
    {
133
      // theory not active, skip
134
      continue;
135
    }
136
21086
    Trace("model-builder-debug")
137
10543
        << "  PostProcessModel on theory: " << theoryId << std::endl;
138
10543
    t->postProcessModel(d_model.get());
139
  }
140
  // also call the model builder's post-process model
141
811
  d_modelBuilder->postProcessModel(incomplete, d_model.get());
142
}
143
144
18218494
theory::TheoryModel* ModelManager::getModel() { return d_model.get(); }
145
146
24989
bool ModelManager::collectModelBooleanVariables()
147
{
148
24989
  Trace("model-builder") << "  CollectModelInfo boolean variables" << std::endl;
149
  // Get value of the Boolean variables
150
24989
  prop::PropEngine* propEngine = d_te.getPropEngine();
151
49978
  std::vector<TNode> boolVars;
152
24989
  propEngine->getBooleanVariables(boolVars);
153
24989
  std::vector<TNode>::iterator it, iend = boolVars.end();
154
  bool hasValue, value;
155
83114
  for (it = boolVars.begin(); it != iend; ++it)
156
  {
157
116250
    TNode var = *it;
158
58125
    hasValue = propEngine->hasValue(var, value);
159
    // Should we assert that hasValue is true?
160
58125
    if (!hasValue)
161
    {
162
23836
      Trace("model-builder-assertions")
163
11918
          << "    has no value : " << var << std::endl;
164
11918
      value = false;
165
    }
166
116250
    Trace("model-builder-assertions")
167
116250
        << "(assert" << (value ? " " : " (not ") << var
168
58125
        << (value ? ");" : "));") << std::endl;
169
58125
    if (!d_model->assertPredicate(var, value))
170
    {
171
      return false;
172
    }
173
  }
174
24989
  return true;
175
}
176
177
}  // namespace theory
178
31137
}  // namespace cvc5