GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/model_manager.cpp Lines: 65 72 90.3 %
Date: 2021-09-15 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
9942
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
9942
          env, "DefaultModel", options::assignFunctionValues())),
38
      d_modelBuilder(nullptr),
39
      d_modelBuilt(false),
40
19884
      d_modelBuiltSuccess(false)
41
{
42
9942
}
43
44
9939
ModelManager::~ModelManager() {}
45
46
9942
void ModelManager::finishInit(eq::EqualityEngineNotify* notify)
47
{
48
  // construct the model
49
  // Initialize the model and model builder.
50
9942
  if (logicInfo().isQuantified())
51
  {
52
6839
    QuantifiersEngine* qe = d_te.getQuantifiersEngine();
53
6839
    Assert(qe != nullptr);
54
6839
    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
9942
  if (d_modelBuilder == nullptr)
60
  {
61
3103
    d_alocModelBuilder.reset(new TheoryEngineModelBuilder(d_env));
62
3103
    d_modelBuilder = d_alocModelBuilder.get();
63
  }
64
  // notice that the equality engine of the model has yet to be assigned.
65
9942
  initializeModelEqEngine(notify);
66
9942
}
67
68
20985
void ModelManager::resetModel()
69
{
70
20985
  d_modelBuilt = false;
71
20985
  d_modelBuiltSuccess = false;
72
  // Reset basic information on the model object
73
20985
  d_model->reset();
74
20985
}
75
76
24796
bool ModelManager::buildModel()
77
{
78
24796
  if (d_modelBuilt)
79
  {
80
    // already computed
81
8400
    return d_modelBuiltSuccess;
82
  }
83
  // reset the flags now
84
16396
  d_modelBuilt = true;
85
16396
  d_modelBuiltSuccess = false;
86
87
  // prepare the model, which is specific to the manager
88
16396
  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
16395
  d_modelBuiltSuccess = finishBuildModel();
96
97
16395
  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
32790
  Trace("model-builder") << "ModelManager: model built success is "
104
16395
                         << d_modelBuiltSuccess << std::endl;
105
106
16395
  return d_modelBuiltSuccess;
107
}
108
109
bool ModelManager::isModelBuilt() const { return d_modelBuilt; }
110
111
7487
void ModelManager::postProcessModel(bool incomplete)
112
{
113
7487
  if (!d_modelBuilt)
114
  {
115
    // model not built, nothing to do
116
6487
    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
7221391
theory::TheoryModel* ModelManager::getModel() { return d_model.get(); }
145
146
16395
bool ModelManager::collectModelBooleanVariables()
147
{
148
16395
  Trace("model-builder") << "  CollectModelInfo boolean variables" << std::endl;
149
  // Get value of the Boolean variables
150
16395
  prop::PropEngine* propEngine = d_te.getPropEngine();
151
32790
  std::vector<TNode> boolVars;
152
16395
  propEngine->getBooleanVariables(boolVars);
153
16395
  std::vector<TNode>::iterator it, iend = boolVars.end();
154
  bool hasValue, value;
155
86205
  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
16395
  return true;
175
}
176
177
}  // namespace theory
178
29577
}  // namespace cvc5