GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/model_manager.cpp Lines: 65 72 90.3 %
Date: 2021-09-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
9926
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
9926
          env, "DefaultModel", options::assignFunctionValues())),
38
      d_modelBuilder(nullptr),
39
      d_modelBuilt(false),
40
19852
      d_modelBuiltSuccess(false)
41
{
42
9926
}
43
44
9923
ModelManager::~ModelManager() {}
45
46
9926
void ModelManager::finishInit(eq::EqualityEngineNotify* notify)
47
{
48
  // construct the model
49
  // Initialize the model and model builder.
50
9926
  if (logicInfo().isQuantified())
51
  {
52
6835
    QuantifiersEngine* qe = d_te.getQuantifiersEngine();
53
6835
    Assert(qe != nullptr);
54
6835
    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
9926
  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
9926
  initializeModelEqEngine(notify);
66
9926
}
67
68
21271
void ModelManager::resetModel()
69
{
70
21271
  d_modelBuilt = false;
71
21271
  d_modelBuiltSuccess = false;
72
  // Reset basic information on the model object
73
21271
  d_model->reset();
74
21271
}
75
76
25689
bool ModelManager::buildModel()
77
{
78
25689
  if (d_modelBuilt)
79
  {
80
    // already computed
81
9004
    return d_modelBuiltSuccess;
82
  }
83
  // reset the flags now
84
16685
  d_modelBuilt = true;
85
16685
  d_modelBuiltSuccess = false;
86
87
  // prepare the model, which is specific to the manager
88
16685
  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
16684
  d_modelBuiltSuccess = finishBuildModel();
96
97
16684
  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
33368
  Trace("model-builder") << "ModelManager: model built success is "
104
16684
                         << d_modelBuiltSuccess << std::endl;
105
106
16684
  return d_modelBuiltSuccess;
107
}
108
109
bool ModelManager::isModelBuilt() const { return d_modelBuilt; }
110
111
7503
void ModelManager::postProcessModel(bool incomplete)
112
{
113
7503
  if (!d_modelBuilt)
114
  {
115
    // model not built, nothing to do
116
6506
    return;
117
  }
118
997
  Trace("model-builder") << "ModelManager: post-process model..." << std::endl;
119
  // model construction should always succeed unless lemmas were added
120
997
  AlwaysAssert(d_modelBuiltSuccess);
121
997
  if (!options::produceModels())
122
  {
123
370
    return;
124
  }
125
  // Do post-processing of model from the theories (used for THEORY_SEP
126
  // to construct heap model)
127
8778
  for (TheoryId theoryId = theory::THEORY_FIRST; theoryId < theory::THEORY_LAST;
128
       ++theoryId)
129
  {
130
8151
    Theory* t = d_te.theoryOf(theoryId);
131
8151
    if (t == nullptr)
132
    {
133
      // theory not active, skip
134
      continue;
135
    }
136
16302
    Trace("model-builder-debug")
137
8151
        << "  PostProcessModel on theory: " << theoryId << std::endl;
138
8151
    t->postProcessModel(d_model.get());
139
  }
140
  // also call the model builder's post-process model
141
627
  d_modelBuilder->postProcessModel(incomplete, d_model.get());
142
}
143
144
7252139
theory::TheoryModel* ModelManager::getModel() { return d_model.get(); }
145
146
16684
bool ModelManager::collectModelBooleanVariables()
147
{
148
16684
  Trace("model-builder") << "  CollectModelInfo boolean variables" << std::endl;
149
  // Get value of the Boolean variables
150
16684
  prop::PropEngine* propEngine = d_te.getPropEngine();
151
33368
  std::vector<TNode> boolVars;
152
16684
  propEngine->getBooleanVariables(boolVars);
153
16684
  std::vector<TNode>::iterator it, iend = boolVars.end();
154
  bool hasValue, value;
155
87261
  for (it = boolVars.begin(); it != iend; ++it)
156
  {
157
141154
    TNode var = *it;
158
70577
    hasValue = propEngine->hasValue(var, value);
159
    // Should we assert that hasValue is true?
160
70577
    if (!hasValue)
161
    {
162
23682
      Trace("model-builder-assertions")
163
11841
          << "    has no value : " << var << std::endl;
164
11841
      value = false;
165
    }
166
141154
    Trace("model-builder-assertions")
167
141154
        << "(assert" << (value ? " " : " (not ") << var
168
70577
        << (value ? ");" : "));") << std::endl;
169
70577
    if (!d_model->assertPredicate(var, value))
170
    {
171
      return false;
172
    }
173
  }
174
16684
  return true;
175
}
176
177
}  // namespace theory
178
29502
}  // namespace cvc5