GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/quantifiers/ematching/inst_strategy_e_matching_user.cpp Lines: 52 79 65.8 %
Date: 2021-05-22 Branches: 90 241 37.3 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Morgan Deters, Aina Niemetz
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 e-matching instantiation strategies.
14
 */
15
16
#include "theory/quantifiers/ematching/inst_strategy_e_matching_user.h"
17
18
#include "theory/quantifiers/ematching/pattern_term_selector.h"
19
#include "theory/quantifiers/ematching/trigger_database.h"
20
#include "theory/quantifiers/quantifiers_state.h"
21
22
using namespace cvc5::kind;
23
using namespace cvc5::theory::quantifiers::inst;
24
25
namespace cvc5 {
26
namespace theory {
27
namespace quantifiers {
28
29
6156
InstStrategyUserPatterns::InstStrategyUserPatterns(
30
    inst::TriggerDatabase& td,
31
    QuantifiersState& qs,
32
    QuantifiersInferenceManager& qim,
33
    QuantifiersRegistry& qr,
34
6156
    TermRegistry& tr)
35
6156
    : InstStrategy(td, qs, qim, qr, tr)
36
{
37
6156
}
38
12312
InstStrategyUserPatterns::~InstStrategyUserPatterns() {}
39
40
size_t InstStrategyUserPatterns::getNumUserGenerators(Node q) const
41
{
42
  std::map<Node, std::vector<Trigger*> >::const_iterator it =
43
      d_user_gen.find(q);
44
  if (it == d_user_gen.end())
45
  {
46
    return 0;
47
  }
48
  return it->second.size();
49
}
50
51
Trigger* InstStrategyUserPatterns::getUserGenerator(Node q, size_t i) const
52
{
53
  std::map<Node, std::vector<Trigger*> >::const_iterator it =
54
      d_user_gen.find(q);
55
  if (it == d_user_gen.end())
56
  {
57
    return nullptr;
58
  }
59
  Assert(i < it->second.size());
60
  return it->second[i];
61
}
62
63
84110
std::string InstStrategyUserPatterns::identify() const
64
{
65
84110
  return std::string("UserPatterns");
66
}
67
68
20340
void InstStrategyUserPatterns::processResetInstantiationRound(
69
    Theory::Effort effort)
70
{
71
20340
  Trace("inst-alg-debug") << "reset user triggers" << std::endl;
72
  // reset triggers
73
145720
  for (std::pair<const Node, std::vector<Trigger*> >& u : d_user_gen)
74
  {
75
188097
    for (Trigger* t : u.second)
76
    {
77
62717
      t->resetInstantiationRound();
78
62717
      t->reset(Node::null());
79
    }
80
  }
81
20340
  Trace("inst-alg-debug") << "done reset user triggers" << std::endl;
82
20340
}
83
84
84110
InstStrategyStatus InstStrategyUserPatterns::process(Node q,
85
                                                     Theory::Effort effort,
86
                                                     int e)
87
{
88
84110
  if (e == 0)
89
  {
90
42055
    return InstStrategyStatus::STATUS_UNFINISHED;
91
  }
92
42055
  options::UserPatMode upm = getInstUserPatMode();
93
42055
  int peffort = upm == options::UserPatMode::RESORT ? 2 : 1;
94
42055
  if (e < peffort)
95
  {
96
    return InstStrategyStatus::STATUS_UNFINISHED;
97
  }
98
42055
  if (e != peffort)
99
  {
100
    return InstStrategyStatus::STATUS_UNKNOWN;
101
  }
102
42055
  d_counter[q]++;
103
104
84110
  Trace("inst-alg") << "-> User-provided instantiate " << q << "..."
105
42055
                    << std::endl;
106
42055
  if (upm == options::UserPatMode::RESORT)
107
  {
108
    std::vector<std::vector<Node> >& ugw = d_user_gen_wait[q];
109
    for (size_t i = 0, usize = ugw.size(); i < usize; i++)
110
    {
111
      Trigger* t =
112
          d_td.mkTrigger(q, ugw[i], true, TriggerDatabase::TR_RETURN_NULL);
113
      if (t)
114
      {
115
        d_user_gen[q].push_back(t);
116
      }
117
    }
118
    ugw.clear();
119
  }
120
121
42055
  std::vector<Trigger*>& ug = d_user_gen[q];
122
55771
  for (Trigger* t : ug)
123
  {
124
13716
    if (Trace.isOn("process-trigger"))
125
    {
126
      Trace("process-trigger") << "  Process (user) ";
127
      t->debugPrint("process-trigger");
128
      Trace("process-trigger") << "..." << std::endl;
129
    }
130
13716
    unsigned numInst = t->addInstantiations();
131
27432
    Trace("process-trigger")
132
13716
        << "  Done, numInst = " << numInst << "." << std::endl;
133
13716
    if (d_qstate.isInConflict())
134
    {
135
      // we are already in conflict
136
      break;
137
    }
138
  }
139
42055
  return InstStrategyStatus::STATUS_UNKNOWN;
140
}
141
142
4058
void InstStrategyUserPatterns::addUserPattern(Node q, Node pat)
143
{
144
4058
  Assert(pat.getKind() == INST_PATTERN);
145
  // add to generators
146
8091
  std::vector<Node> nodes;
147
8324
  for (const Node& p : pat)
148
  {
149
4295
    if (std::find(nodes.begin(), nodes.end(), p) != nodes.end())
150
    {
151
      // skip duplicate pattern term
152
4
      continue;
153
    }
154
8557
    Node pat_use = PatternTermSelector::getIsUsableTrigger(p, q);
155
4291
    if (pat_use.isNull())
156
    {
157
50
      Trace("trigger-warn") << "User-provided trigger is not usable : " << pat
158
25
                            << " because of " << p << std::endl;
159
25
      return;
160
    }
161
4266
    nodes.push_back(pat_use);
162
  }
163
4033
  Trace("user-pat") << "Add user pattern: " << pat << " for " << q << std::endl;
164
  // check match option
165
4033
  if (getInstUserPatMode() == options::UserPatMode::RESORT)
166
  {
167
    d_user_gen_wait[q].push_back(nodes);
168
    return;
169
  }
170
4033
  Trigger* t = d_td.mkTrigger(q, nodes, true, TriggerDatabase::TR_MAKE_NEW);
171
4033
  if (t)
172
  {
173
4033
    d_user_gen[q].push_back(t);
174
  }
175
  else
176
  {
177
    Trace("trigger-warn") << "Failed to construct trigger : " << pat
178
                          << " due to variable mismatch" << std::endl;
179
  }
180
}
181
182
}  // namespace quantifiers
183
}  // namespace theory
184
28194
}  // namespace cvc5