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