carl  24.04
Computer ARithmetic Library
DIMACSImporter.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <fstream>
4 #include <iostream>
5 #include <regex>
6 #include <sstream>
7 #include <string>
8 
11 
12 namespace carl::io {
13 
14 /**
15  * Parser for the DIMACS format.
16  *
17  * Allows for solving multiple formulas from one file by adding lines that only contain "reset".
18  */
19 template<typename Pol>
21 private:
22  std::ifstream in;
23  std::vector<Formula<Pol>> variables;
24  std::regex headerRegex;
25 
26  Formula<Pol> parseLine(const std::string& line) const {
27  std::vector<Formula<Pol>> vars;
28  const char* begin = line.c_str();
29  char* end = nullptr;
30  long long id;
31  while (true) {
32  id = std::strtoll(begin, &end, 10);
33  begin = end;
34  if (id == 0) break;
35  Formula<Pol> v = variables.at(std::size_t(std::abs(id)-1));
36  if (id > 0) vars.emplace_back(v);
37  else vars.emplace_back(NOT, v);
38  }
39  return std::move(Formula<Pol>(OR, std::move(vars)));
40  }
41 
43  std::string line;
44  std::vector<Formula<Pol>> formulas;
45  while (!in.eof()) {
46  std::getline(in, line);
47  if (line == "") continue;
48  if (line == "reset") break;
49  if (line.front() == 'c') continue;
50  if (line.front() == 'p') {
51  std::smatch m;
52  if (!std::regex_match(line, m, headerRegex)) {
53  CARL_LOG_ERROR("carl.formula", "DIMACS line starting with \"p\" does not match header format: \"" << line << "\".");
54  }
55  std::size_t varCount = std::stoull(m[1]);
56  variables.reserve(varCount);
57  while (variables.size() < varCount) {
58  variables.emplace_back(fresh_boolean_variable());
59  }
60  continue;
61  }
62  formulas.push_back(parseLine(line));
63  }
64  return Formula<Pol>(AND, std::move(formulas));
65  }
66 
67 public:
68  /// Load the given file.
69  DIMACSImporter(const std::string& filename):
70  in(filename),
71  headerRegex("p cnf (\\d+) (\\d+)")
72  {}
73 
74  /// Checks if there is another formula to parse.
75  bool hasNext() const {
76  return !in.eof();
77  }
78 
79  /// Parses and returns the next formula (until the next reset line).
81  return parseFormula();
82  }
83 };
84 
85 }
A small wrapper that configures logging for carl.
#define CARL_LOG_ERROR(channel, msg)
Definition: carl-logging.h:40
Interval< Number > abs(const Interval< Number > &_in)
Method which returns the absolute value of the passed number.
Definition: Interval.h:1511
Variable fresh_boolean_variable() noexcept
Definition: VariablePool.h:192
Represent an SMT formula, which can be an atom for some background theory or a boolean combination of...
Definition: Formula.h:47
Parser for the DIMACS format.
Formula< Pol > next()
Parses and returns the next formula (until the next reset line).
Formula< Pol > parseLine(const std::string &line) const
Formula< Pol > parseFormula()
bool hasNext() const
Checks if there is another formula to parse.
DIMACSImporter(const std::string &filename)
Load the given file.
std::vector< Formula< Pol > > variables