dlinear  0.0.1
Delta-complete SMT solver for linear programming
Loading...
Searching...
No Matches
Driver.cpp
1
6#include "Driver.h"
7
8#include <fstream>
9#include <iostream>
10#include <sstream> // IWYU pragma: keep for std::stringstream
11#include <vector>
12
13#include "dlinear/libs/libgmp.h"
14#include "dlinear/solver/SmtSolverOutput.h"
15#include "dlinear/symbolic/PrefixPrinter.h"
16#include "dlinear/util/Box.h"
17#include "dlinear/util/Config.h"
18#include "dlinear/util/ScopedVector.hpp"
19#include "dlinear/util/exception.h"
20
21namespace dlinear {
22
23Driver::Driver(Context& context, const std::string& class_name)
24 : context_{context},
25 debug_scanning_{context_.config().debug_scanning()},
26 debug_parsing_{context_.config().debug_parsing()},
27 stats_{context.config().with_timings(), class_name, "Total time spent in parsing"},
28 timer_{nullptr} {}
29
30bool Driver::ParseStream(std::istream& in, const std::string& sname) {
31 SmtSolverOutput* const output = context_.m_solver_output();
32 // Decide whether to use the external output or the internal stats for the timer
33 if (output != nullptr) {
34 output->parser_stats = stats_;
35 timer_ = &output->parser_stats.m_timer();
36 } else {
38 }
39 DLINEAR_ASSERT(timer_ != nullptr, "Timer must be set.");
40 TimerGuard timer_guard(timer_, stats_.enabled());
41 stream_name_ = sname;
42
43 const bool res = ParseStreamCore(in);
44 // If we used an external output as the timer, we need to update the internal stats
45 if (output != nullptr) stats_ = output->parser_stats;
46 return res;
47}
48
49bool Driver::ParseString(const std::string& input, const std::string& sname) {
50 std::istringstream iss(input);
51 return ParseStream(iss, sname);
52}
53
54bool Driver::ParseFile(const std::string& filename) {
55 std::ifstream in(filename.c_str());
56 if (!in.good()) return false;
57 return ParseStream(in, filename);
58}
59
60void Driver::Error(const std::string& m) { std::cerr << m << std::endl; }
61
63 DLINEAR_ASSERT(timer_ != nullptr, "Timer must be set.");
64 // Don't consider the time spent checking sat in the time spent parsing.
65 timer_->Pause();
66 mpq_class precision = context_.config().precision();
67 context_.CheckSat(&precision);
68 timer_->Resume();
69}
70
72 if (context_.config().silent()) return;
73 std::cout << "(model\n" << context_.model() << "\n)" << std::endl;
74}
75
77 if (context_.config().silent()) return;
78 std::cout << "(assertions\n";
79 for (const Formula& f : context_.assertions()) {
80 std::stringstream ss;
81 PrefixPrinter pp{ss};
82 pp.Print(f);
83 std::cout << "\t" << ss.str() << "\n";
84 }
85 std::cout << ")" << std::endl;
86}
87
88void Driver::GetOption(const std::string& key) const {
89 if (context_.config().silent()) return;
90 std::cout << "get-option ( " << key << " ): " << context_.GetOption(key) << std::endl;
91}
92
93void Driver::GetInfo(const std::string& key) const {
94 if (context_.config().silent()) return;
95 std::cout << "get-info ( " << key << " ): " << context_.GetInfo(key) << std::endl;
96}
97
99 DLINEAR_ASSERT(timer_ != nullptr, "Timer must be set.");
100 // Don't consider the time spent maximizing in the time spent parsing.
101 timer_->Pause();
103 timer_->Resume();
104}
105
107 DLINEAR_ASSERT(timer_ != nullptr, "Timer must be set.");
108 // Don't consider the time spent minimizing in the time spent parsing.
109 timer_->Pause();
111 timer_->Resume();
112}
113
114} // namespace dlinear
const bool & silent() const
Get read-write access to the silent parameter of the configuration.
Definition Config.h:204
const double & precision() const
Get read-write access to the precision parameter of the configuration.
Definition Config.h:191
Context class that holds the set of constraints and provide Assert/Push/Pop/CheckSat functionalities.
Definition Context.h:31
const Config & config() const
Get read-only access to the configuration of the context.
Definition Context.cpp:65
std::string GetInfo(const std::string &key) const
Get the info key.
Definition Context.cpp:58
void Minimize(const Expression &f)
Assert a formula minimizing a cost function f.
Definition Context.cpp:43
std::string GetOption(const std::string &key) const
Get the option key.
Definition Context.cpp:64
void Maximize(const Expression &f)
Assert a formula maximizing a cost function f.
Definition Context.cpp:44
const ScopedVector< Formula > & assertions() const
Get the the asserted formulas.
Definition Context.cpp:71
SatResult CheckSat(mpq_class *actual_precision)
Check the satisfiability of the asserted formulas, and sets actual_precision to the actual max infeas...
Definition Context.cpp:29
const Box & model() const
Get a representation of a model computed by the solver in response to the last invocation of the chec...
Definition Context.cpp:67
void CheckSat()
Call context_.CheckSat() and print proper output messages to the standard output.
Definition Driver.cpp:62
Timer * timer_
Pointer to the timer for the driver. Used to Pause the timer when checking sat.
Definition Driver.h:173
void GetInfo(const std::string &key) const
Callback for the command get-info.
Definition Driver.cpp:93
bool ParseString(const std::string &input, const std::string &sname="string stream")
Invoke the scanner and parser on an input string.
Definition Driver.cpp:49
static void Error(const std::string &m)
General error handling.
Definition Driver.cpp:60
Context & context_
The context filled during parsing of the expressions.
Definition Driver.h:166
void GetModel()
Return a model computed by the solver in response to an invocation of the check-sat.
Definition Driver.cpp:71
Stats stats_
Statistics for the driver.
Definition Driver.h:172
void Minimize(const Expression &f)
Minimize the objective function f.
Definition Driver.cpp:106
Driver(Context &context, const std::string &class_name="Driver")
construct a new parser driver context
Definition Driver.cpp:23
virtual bool ParseFile(const std::string &filename)
Invoke the scanner and parser on a file.
Definition Driver.cpp:54
void GetAssertions() const
Callback for the command get-assertions.
Definition Driver.cpp:76
void GetOption(const std::string &key) const
Callback for the command get-option.
Definition Driver.cpp:88
void Maximize(const Expression &f)
Maximize the objective function f.
Definition Driver.cpp:98
std::string stream_name_
The name of the stream being parsed.
Definition Driver.h:164
virtual bool ParseStreamCore(std::istream &in)=0
Parse the stream.
bool ParseStream(std::istream &in, const std::string &sname="stream input")
Invoke the scanner and parser for a stream.
Definition Driver.cpp:30
Print expressions and formulas in prefix-form.
Timer & m_timer()
Get read-write access to the timer of the stats.
Definition Stats.h:50
bool enabled() const
Check whether the stats is enabled.
Definition Stats.h:48
void Resume()
Resume the timer.
Definition Timer.cpp:37
void Pause()
Pause the timer.
Definition Timer.cpp:29
The TimeGuard wraps a timer object and pauses it when the guard object is destructed.
Definition Timer.h:129
Represents a symbolic form of an expression.
Represents a symbolic form of a first-order logic formula.
Global namespace for the dlinear library.
Data struct containing the output of the solver, such as the result of the computation as well as som...
Stats parser_stats
Statistics about the solver.