SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
InstructionHandler.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "VariantMap.h"
4 #include "theories/Attribute.h"
5 #include "../ResourceLimitation.h"
6 
7 #include <smtrat-common/model.h>
8 #include <smtrat-qe/smtrat-qe.h>
9 
10 #include <fstream>
11 #include <iostream>
12 #include <queue>
13 
14 namespace smtrat {
15 namespace parser {
16 
18 inline std::ostream& operator<<(std::ostream& os, OptimizationType ot) {
19  switch (ot) {
20  case OptimizationType::Maximize: return os << "maximize";
21  case OptimizationType::Minimize: return os << "minimize";
22  }
23  return os << "???";
24 }
25 
27  std::ostream out;
28  std::string pre;
29  std::string suf;
30 public:
31  OutputWrapper(std::ostream& o, const std::string& prefix, const std::string& suffix)
32  : out(o.rdbuf()), pre(prefix), suf(suffix) {
33  this->out << pre;
34  }
35  OutputWrapper(const OutputWrapper&& o) : out(o.out.rdbuf()), pre(o.pre), suf(o.suf) {}
37  this->out << suf;
38  }
39  template<typename T>
40  OutputWrapper& operator<<(const T& t) {
41  this->out << t;
42  return *this;
43  }
44 };
45 
47 public:
49 
50 private:
51  std::queue<std::function<void()>> instructionQueue;
52  std::vector<smtrat::ModelVariable> mArtificialVariables;
53 public:
54  void addInstruction(std::function<void()> bind) {
55  this->instructionQueue.push(bind);
56  }
57  bool hasInstructions() const {
58  return !instructionQueue.empty();
59  }
60  void runInstructions() {
61  while (!this->instructionQueue.empty()) {
62  this->instructionQueue.front()();
63  this->instructionQueue.pop();
64  }
65  }
66  void setArtificialVariables(std::vector<smtrat::ModelVariable>&& vars) {
67  mArtificialVariables = std::move(vars);
68  }
69  void cleanModel(smtrat::Model& model) const {
70  for (const auto& v: mArtificialVariables) {
71  model.erase(v);
72  }
73  }
74 protected:
77 public:
78  bool has_info(const std::string& key) const {
79  return infos.find(key) != infos.end();
80  }
81  const auto& get_info(const std::string& key) const {
82  return infos.find(key)->second;
83  }
84  template<typename T>
85  T option(const std::string& key) const {
86  return this->options.get<T>(key);
87  }
88  bool printInstruction() const {
89  if (!this->options.has<bool>("print-instruction")) return false;
90  return this->options.get<bool>("print-instruction");
91  }
92 protected:
93  std::ostream mRegular;
94  std::ostream mDiagnostic;
95  std::map<std::string, std::ofstream> streams;
96 
97  void setStream(const std::string& s, std::ostream& os) {
98  if (s == "stdout") os.rdbuf(std::cout.rdbuf());
99  else if (s == "stderr") os.rdbuf(std::cerr.rdbuf());
100  else if (s == "stdlog") os.rdbuf(std::clog.rdbuf());
101  else {
102  if (this->streams.count(s) == 0) {
103  this->streams[s].open(s);
104  }
105  os.rdbuf(this->streams[s].rdbuf());
106  }
107  }
108 public:
109  InstructionHandler(): mRegular(std::cout.rdbuf()), mDiagnostic(std::cerr.rdbuf()) {
110  Attribute attr("print-instruction", Value(false));
111  this->setOption(attr);
112  smtrat::resource::Limiter::getInstance().initialize();
113  }
115  for (auto& it: this->streams) it.second.close();
116  }
117 
118  std::ostream& diagnostic() {
119  return this->mDiagnostic;
120  }
121  void diagnostic(const std::string& s) {
122  this->setStream(s, this->mDiagnostic);
123  }
124  std::ostream& regular() {
125  return this->mRegular;
126  }
127  void regular(const std::string& s) {
128  this->setStream(s, this->mRegular);
129  }
131  return OutputWrapper(mDiagnostic, "(error \"", "\")\n");
132  }
134  return OutputWrapper(mDiagnostic, "(warn \"", "\")\n");
135  }
137  return OutputWrapper(mRegular, "(info \"", "\")\n");
138  }
139  virtual void add(const FormulaT& f) = 0;
140  virtual void addSoft(const FormulaT& f, Rational weight, const std::string& id) = 0;
141  virtual void annotateName(const FormulaT& f, const std::string& name) = 0;
142  virtual void check() = 0;
143  virtual void declareFun(const carl::Variable&) = 0;
144  virtual void declareSort(const std::string&, const unsigned&) = 0;
145  virtual void defineSort(const std::string&, const std::vector<std::string>&, const carl::Sort&) = 0;
146  virtual void echo(const std::string& s) {
147  regular() << s << std::endl;
148  }
149  virtual void qe() = 0;
150  virtual void exit() = 0;
151  virtual void getAllModels() = 0;
152  virtual void getAssertions() = 0;
153  virtual void getAssignment() = 0;
154  void getInfo(const std::string& key) {
155  if (this->infos.count(key) > 0) regular() << "(:" << key << " " << this->infos[key] << ")" << std::endl;
156  else error() << "no info set for :" << key;
157  }
158  virtual void getModel() = 0;
159  virtual void getObjectives() = 0;
160  void getOption(const std::string& key) {
161  if (this->options.count(key) > 0) regular() << "(:" << key << " " << this->options[key] << ")" << std::endl;
162  else error() << "no option set for :" << key;
163  }
164  virtual void getProof() = 0;
165  virtual void getUnsatCore() = 0;
166  virtual void getValue(const std::vector<carl::Variable>&) = 0;
167  virtual void addObjective(const Poly& p, OptimizationType ot) = 0;
168  virtual void pop(std::size_t) = 0;
169  virtual void push(std::size_t) = 0;
170  virtual void reset() {
171  this->infos.clear();
172  this->options.clear();
173  };
174  virtual void resetAssertions() = 0;
175  void setInfo(const Attribute& attr) {
176  if (this->infos.count(attr.key) > 0) warn() << "overwriting info for :" << attr.key;
177  if (attr.key == "name" || attr.key == "authors" || attr.key == "version") error() << "The info :" << attr.key << " is read-only.";
178  else this->infos[attr.key] = attr.value;
179  }
180  virtual void setLogic(const carl::Logic&) = 0;
181  void setOption(const Attribute& option) {
182  std::string key = option.key;
183  if (this->options.count(key) > 0) warn() << "overwriting option for :" << key;
184  this->options[key] = option.value;
185  if (key == "diagnostic-output-channel") this->diagnostic(this->options.get<std::string>(key));
186  else if (key == "expand-definitions") this->error() << "The option :expand-definitions is not supported.";
187  else if (key == "interactive-mode") {
188  this->options.assertType<bool>("interactive-mode", std::bind(&InstructionHandler::error, this));
189  }
190  else if (key == "print-instruction") {
191  this->options.assertType<bool>("print-instruction", std::bind(&InstructionHandler::error, this));
192  }
193  else if (key == "print-success") {
194  this->options.assertType<bool>("print-success", std::bind(&InstructionHandler::error, this));
195  }
196  else if (key == "produce-assignments") {
197  this->options.assertType<bool>("produce-assignments", std::bind(&InstructionHandler::error, this));
198  }
199  else if (key == "produce-models") {
200  this->options.assertType<bool>("produce-models", std::bind(&InstructionHandler::error, this));
201  }
202  else if (key == "produce-proofs") {
203  this->error() << "The option :produce-proofs is not supported.";
204  }
205  else if (key == "produce-unsat-cores") {
206  this->options.assertType<bool>("produce-unsat-cores", std::bind(&InstructionHandler::error, this));
207  }
208  else if (key == "random-seed") {
209  this->error() << "The option :random-seed is not supported.";
210  }
211  else if (key == "regular-output-channel") this->regular(this->options.get<std::string>(key));
212  else if (key == "verbosity") {
213  this->options.assertType<Rational>("verbosity", std::bind(&InstructionHandler::error, this));
214  }
215  else if (key == "timeout") {
216  this->options.assertType<Rational>("timeout", std::bind(&InstructionHandler::error, this));
217  carl::uint timeout = carl::to_int<carl::uint>(options.get<Rational>("timeout"));
218  this->info() << "Setting timeout to " << timeout << " seconds";
219  smtrat::resource::Limiter::getInstance().setTimeout(timeout);
220  }
221  else if (key == "memout") {
222  this->options.assertType<Rational>("memout", std::bind(&InstructionHandler::error, this));
223  smtrat::resource::Limiter::getInstance().setMemout(carl::to_int<carl::uint>(options.get<Rational>("memout")));
224  }
225  }
226 };
227 
228 }
229 }
const T & get(const Key &key) const
Returns the value associated with the given key as type T.
Definition: VariantMap.h:120
bool has(const Key &key) const
Checks if there is a value of the specified type for the given key.
Definition: VariantMap.h:108
void assertType(const Key &key, Output out) const
Asserts that the value that is associated with the given key has a specified type.
Definition: VariantMap.h:90
Represents an Attribute.
Definition: Attribute.h:14
AttributeValue value
Definition: Attribute.h:18
void setOption(const Attribute &option)
virtual void add(const FormulaT &f)=0
void getOption(const std::string &key)
void getInfo(const std::string &key)
virtual void addSoft(const FormulaT &f, Rational weight, const std::string &id)=0
std::vector< smtrat::ModelVariable > mArtificialVariables
virtual void defineSort(const std::string &, const std::vector< std::string > &, const carl::Sort &)=0
void setArtificialVariables(std::vector< smtrat::ModelVariable > &&vars)
virtual void declareSort(const std::string &, const unsigned &)=0
void setInfo(const Attribute &attr)
void cleanModel(smtrat::Model &model) const
virtual void echo(const std::string &s)
void addInstruction(std::function< void()> bind)
void diagnostic(const std::string &s)
virtual void declareFun(const carl::Variable &)=0
bool has_info(const std::string &key) const
const auto & get_info(const std::string &key) const
virtual void setLogic(const carl::Logic &)=0
virtual void pop(std::size_t)=0
T option(const std::string &key) const
virtual void push(std::size_t)=0
VariantMap< std::string, Value > options
virtual void annotateName(const FormulaT &f, const std::string &name)=0
void regular(const std::string &s)
std::queue< std::function< void()> > instructionQueue
VariantMap< std::string, Value > infos
virtual void getValue(const std::vector< carl::Variable > &)=0
std::map< std::string, std::ofstream > streams
void setStream(const std::string &s, std::ostream &os)
virtual void addObjective(const Poly &p, OptimizationType ot)=0
OutputWrapper(std::ostream &o, const std::string &prefix, const std::string &suffix)
OutputWrapper(const OutputWrapper &&o)
OutputWrapper & operator<<(const T &t)
std::vector< T > prefix(const std::vector< T > vars, std::size_t prefixSize)
Definition: utils.h:349
carl::mpl_variant_of< AttributeTypes >::type AttributeValue
Variant type for all attributes.
Definition: TheoryTypes.h:177
std::ostream & operator<<(std::ostream &os, OptimizationType ot)
std::vector< carl::Variable > vars(const std::pair< QuantifierType, std::vector< carl::Variable >> &p)
Definition: QEQuery.h:43
Class to create the formulas for axioms.
carl::Formula< Poly > FormulaT
Definition: types.h:37
carl::Model< Rational, Poly > Model
Definition: model.h:13
carl::MultivariatePolynomial< Rational > Poly
Definition: types.h:25
mpq_class Rational
Definition: types.h:19