19bool parse_weight_line(
const std::string &line,
long &lit,
double &w)
21 std::istringstream iss(line);
22 std::string c, p, weight;
23 if (!(iss >> c >> p >> weight) || c !=
"c" || p !=
"p" || weight !=
"weight")
26 if (!(iss >> lit >> w >> zero))
36 std::map<long, double> weights;
37 std::vector<int> tokens;
38 bool saw_header =
false;
40 std::istringstream in(text);
42 while (std::getline(in, line)) {
44 size_t s = line.find_first_not_of(
" \t\r");
45 if (s == std::string::npos)
50 if (parse_weight_line(line.substr(s), lit, w) && lit > 0)
55 std::istringstream iss(line.substr(s));
63 std::istringstream iss(line.substr(s));
66 tokens.push_back(lit);
69 throw std::runtime_error(
"DIMACS: missing 'p cnf' header");
72 std::map<int, gate_t> in_gate;
73 std::map<int, gate_t> neg_gate;
75 auto literal_gate = [&](
int lit) ->
gate_t {
76 int v = std::abs(lit);
77 auto it = in_gate.find(v);
78 if (it == in_gate.end()) {
81 auto w = weights.find(v);
82 if (w != weights.end()) p = w->second;
85 it = in_gate.emplace(v, g).first;
89 auto n = neg_gate.find(v);
90 if (n == neg_gate.end()) {
93 n = neg_gate.emplace(v, ng).first;
99 unsigned clause_no = 0;
101 bool clause_open =
false;
102 for (
int lit : tokens) {
108 c.
addWire(clause, literal_gate(lit));
@ NOT
Logical negation of a single child gate.
@ OR
Logical disjunction of child gates.
@ AND
Logical conjunction of child gates.
@ IN
Input (variable) gate representing a base tuple.
gate_t
Strongly-typed gate identifier.
Out-of-line template method implementations for Circuit<gateType>.
Boolean circuit for provenance formula evaluation.
gate_t setGate(BooleanGate type) override
Allocate a new gate with type type and no UUID.
void addWire(gate_t f, gate_t t)
Add a directed wire from gate f (parent) to gate t (child).
gate_t parse_dimacs_cnf(const std::string &text, BooleanCircuit &c, bool weighted)
Parse text (a DIMACS CNF) into c and return the root gate.
Parse a DIMACS CNF into a ProvSQL BooleanCircuit.