GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/smt_engine_subsolver.cpp Lines: 41 50 82.0 %
Date: 2021-09-18 Branches: 45 130 34.6 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Andres Noetzli, Gereon Kremer
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
 * Implementation of utilities for initializing subsolvers (copies of
14
 * SmtEngine) during solving.
15
 */
16
17
#include "theory/smt_engine_subsolver.h"
18
19
#include "smt/env.h"
20
#include "smt/smt_engine.h"
21
#include "smt/smt_engine_scope.h"
22
#include "theory/rewriter.h"
23
24
namespace cvc5 {
25
namespace theory {
26
27
// optimization: try to rewrite to constant
28
689
Result quickCheck(Node& query)
29
{
30
689
  query = theory::Rewriter::rewrite(query);
31
689
  if (query.isConst())
32
  {
33
218
    if (!query.getConst<bool>())
34
    {
35
30
      return Result(Result::UNSAT);
36
    }
37
    else
38
    {
39
188
      return Result(Result::SAT);
40
    }
41
  }
42
471
  return Result(Result::SAT_UNKNOWN, Result::REQUIRES_FULL_CHECK);
43
}
44
45
2687
void initializeSubsolver(std::unique_ptr<SmtEngine>& smte,
46
                         const Options& opts,
47
                         const LogicInfo& logicInfo,
48
                         bool needsTimeout,
49
                         unsigned long timeout)
50
{
51
2687
  NodeManager* nm = NodeManager::currentNM();
52
2687
  smte.reset(new SmtEngine(nm, &opts));
53
2687
  smte->setIsInternalSubsolver();
54
2687
  smte->setLogic(logicInfo);
55
  // set the options
56
2687
  if (needsTimeout)
57
  {
58
    smte->setTimeLimit(timeout);
59
  }
60
2687
}
61
2015
void initializeSubsolver(std::unique_ptr<SmtEngine>& smte,
62
                         const Env& env,
63
                         bool needsTimeout,
64
                         unsigned long timeout)
65
{
66
2015
  initializeSubsolver(
67
      smte, env.getOptions(), env.getLogicInfo(), needsTimeout, timeout);
68
2015
}
69
70
Result checkWithSubsolver(std::unique_ptr<SmtEngine>& smte,
71
                          Node query,
72
                          const Options& opts,
73
                          const LogicInfo& logicInfo,
74
                          bool needsTimeout,
75
                          unsigned long timeout)
76
{
77
  Assert(query.getType().isBoolean());
78
  Result r = quickCheck(query);
79
  if (!r.isUnknown())
80
  {
81
    return r;
82
  }
83
  initializeSubsolver(smte, opts, logicInfo, needsTimeout, timeout);
84
  smte->assertFormula(query);
85
  return smte->checkSat();
86
}
87
88
157
Result checkWithSubsolver(Node query,
89
                          const Options& opts,
90
                          const LogicInfo& logicInfo,
91
                          bool needsTimeout,
92
                          unsigned long timeout)
93
{
94
314
  std::vector<Node> vars;
95
314
  std::vector<Node> modelVals;
96
  return checkWithSubsolver(
97
314
      query, vars, modelVals, opts, logicInfo, needsTimeout, timeout);
98
}
99
100
689
Result checkWithSubsolver(Node query,
101
                          const std::vector<Node>& vars,
102
                          std::vector<Node>& modelVals,
103
                          const Options& opts,
104
                          const LogicInfo& logicInfo,
105
                          bool needsTimeout,
106
                          unsigned long timeout)
107
{
108
689
  Assert(query.getType().isBoolean());
109
689
  Assert(modelVals.empty());
110
  // ensure clear
111
689
  modelVals.clear();
112
689
  Result r = quickCheck(query);
113
689
  if (!r.isUnknown())
114
  {
115
218
    if (r.asSatisfiabilityResult().isSat() == Result::SAT)
116
    {
117
      // default model
118
197
      for (const Node& v : vars)
119
      {
120
9
        modelVals.push_back(v.getType().mkGroundTerm());
121
      }
122
    }
123
218
    return r;
124
  }
125
942
  std::unique_ptr<SmtEngine> smte;
126
471
  initializeSubsolver(smte, opts, logicInfo, needsTimeout, timeout);
127
471
  smte->assertFormula(query);
128
471
  r = smte->checkSat();
129
471
  if (r.asSatisfiabilityResult().isSat() == Result::SAT)
130
  {
131
1770
    for (const Node& v : vars)
132
    {
133
2938
      Node val = smte->getValue(v);
134
1469
      modelVals.push_back(val);
135
    }
136
  }
137
471
  return r;
138
}
139
140
}  // namespace theory
141
29574
}  // namespace cvc5