ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
GenericCircuit.h
Go to the documentation of this file.
1/**
2 * @file GenericCircuit.h
3 * @brief Semiring-agnostic in-memory provenance circuit.
4 *
5 * @c GenericCircuit is an in-memory directed acyclic graph whose gate
6 * types use the PostgreSQL @c gate_type enumeration. It is built from
7 * the persistent mmap representation (via @c createGenericCircuit() /
8 * @c getGenericCircuit()) and then evaluated over an arbitrary semiring
9 * using the @c evaluate() template method.
10 *
11 * Beyond the gate/wire data inherited from @c Circuit<gate_type>,
12 * @c GenericCircuit tracks:
13 * - Per-gate integer annotation pairs (@c info1, @c info2) used by
14 * aggregation and semimodule gates.
15 * - Per-gate variable-length string extras (e.g. label strings for
16 * @c gate_value gates).
17 * - A probability vector for probabilistic evaluation.
18 * - The set of input gate IDs (for semiring evaluation traversal).
19 *
20 * The circuit is Boost-serialisable, which is used when sending it as
21 * a blob to an external knowledge-compiler process.
22 */
23#ifndef GENERIC_CIRCUIT_H
24#define GENERIC_CIRCUIT_H
25
26#include <map>
27#include <type_traits>
28
29#include <boost/archive/binary_oarchive.hpp>
30#include <boost/serialization/unordered_map.hpp>
31#include <boost/serialization/map.hpp>
32#include <boost/serialization/set.hpp>
33#include <boost/serialization/vector.hpp>
34
35#include "Circuit.h"
36#include "semiring/Semiring.h"
37
38extern "C" {
39#include "provsql_utils.h"
40}
41
42/**
43 * @brief In-memory provenance circuit with semiring-generic evaluation.
44 *
45 * Gate types are the PostgreSQL @c gate_type values. The circuit is
46 * constructed from the persistent mmap store and then evaluated in-memory.
47 */
48class GenericCircuit : public Circuit<gate_type>
49{
50private:
51std::map<gate_t, std::pair<unsigned,unsigned> > infos; ///< Per-gate (info1, info2) annotations
52std::map<gate_t, std::string> extra; ///< Per-gate string extras
53std::set<gate_t> inputs; ///< Set of input (leaf) gate IDs
54std::vector<double> prob; ///< Per-gate probability values
55
56public:
57/**
58 * @brief Return a placeholder debug string (not intended for display).
59 * @param g Gate identifier (unused).
60 * @return Fixed placeholder string @c "<GenericCircuit>".
61 */
62virtual std::string toString(gate_t g) const override {
63 return "<GenericCircuit>";
64}
65
66/**
67 * @brief Set the integer annotation pair for gate @p g.
68 * @param g Gate identifier.
69 * @param info1 First annotation integer.
70 * @param info2 Second annotation integer.
71 */
72void setInfos(gate_t g, unsigned info1, unsigned info2)
73{
74 infos[g]=std::make_pair(info1, info2);
75}
76
77/**
78 * @brief Return the integer annotation pair for gate @p g.
79 * @param g Gate identifier.
80 * @return @c {info1, info2}, or @c {-1,-1} if not set.
81 */
82std::pair<unsigned,unsigned> getInfos(gate_t g) const
83{
84 auto it = infos.find(g);
85 if(it==infos.end())
86 return std::make_pair(-1, -1);
87 return it->second;
88}
89
90/**
91 * @brief Attach a string extra to gate @p g.
92 * @param g Gate identifier.
93 * @param ex String to store.
94 */
95void setExtra(gate_t g, const std::string &ex)
96{
97 extra[g]=ex;
98}
99
100/**
101 * @brief Return the string extra for gate @p g.
102 * @param g Gate identifier.
103 * @return The stored string, or empty if not set.
104 */
105std::string getExtra(gate_t g) const
106{
107 auto it = extra.find(g);
108 if(it==extra.end())
109 return "";
110 else
111 return it->second;
112}
113
114/** @copydoc Circuit::addGate() */
115gate_t addGate() override;
116/** @copydoc Circuit::setGate(gateType) */
117gate_t setGate(gate_type type) override;
118/** @copydoc Circuit::setGate(const uuid&, gateType) */
119gate_t setGate(const uuid &u, gate_type type) override;
120
121/**
122 * @brief Return the set of input (leaf) gates.
123 * @return Const reference to the set of input gate identifiers.
124 */
125const std::set<gate_t> &getInputs() const {
126 return inputs;
127}
128
129/**
130 * @brief Set the probability for gate @p g.
131 * @param g Gate identifier.
132 * @param p Probability in [0, 1].
133 */
134void setProb(gate_t g, double p) {
135 prob[static_cast<std::underlying_type<gate_t>::type>(g)]=p;
136}
137
138/**
139 * @brief Return the probability for gate @p g.
140 * @param g Gate identifier.
141 * @return The stored probability.
142 */
143double getProb(gate_t g) const {
144 return prob[static_cast<std::underlying_type<gate_t>::type>(g)];
145}
146
147/**
148 * @brief Boost serialisation support.
149 * @param ar Boost archive (input or output).
150 * @param version Archive version (unused).
151 */
152template<class Archive>
153void serialize (Archive & ar, const unsigned int version)
154{
155 ar & uuid2id;
156 ar & id2uuid;
157 ar & gates;
158 ar & wires;
159 ar & infos;
160 ar & extra;
161 ar & inputs;
162 ar & prob;
163}
164
167
168/**
169 * @brief Evaluate the sub-circuit rooted at gate @p g over semiring @p semiring.
170 *
171 * Performs a post-order traversal from @p g, mapping each input gate to
172 * its semiring value via @p provenance_mapping, and combining the results
173 * using the semiring operations.
174 *
175 * @tparam S A concrete @c semiring::Semiring subclass.
176 * @param g Root gate of the sub-circuit to evaluate.
177 * @param provenance_mapping Map from input gate IDs to semiring values.
178 * @param semiring Semiring instance providing @c zero(), @c one(),
179 * @c plus(), @c times(), etc.
180 * @return The semiring value of the circuit at gate @p g.
181 */
182template<typename S, std::enable_if_t<std::is_base_of_v<semiring::Semiring<typename S::value_type>, S>, int> = 0>
183typename S::value_type evaluate(gate_t g, std::unordered_map<gate_t, typename S::value_type> &provenance_mapping, S semiring) const;
184};
185
186#endif /* GENERIC_CIRCUIT_H */
Generic directed-acyclic-graph circuit template and gate identifier.
gate_t
Strongly-typed gate identifier.
Definition Circuit.h:48
Abstract semiring interface for provenance evaluation.
Generic template base class for provenance circuits.
Definition Circuit.h:61
std::string uuid
UUID type used in this circuit (always std::string).
Definition Circuit.h:64
std::unordered_map< gate_t, uuid > id2uuid
Gate index → UUID string.
Definition Circuit.h:68
std::unordered_map< uuid, gate_t > uuid2id
UUID string → gate index.
Definition Circuit.h:67
std::vector< gate_type > gates
Gate type for each gate.
Definition Circuit.h:70
std::vector< std::vector< gate_t > > wires
Child wire lists for each gate.
Definition Circuit.h:71
In-memory provenance circuit with semiring-generic evaluation.
std::map< gate_t, std::pair< unsigned, unsigned > > infos
Per-gate (info1, info2) annotations.
S::value_type evaluate(gate_t g, std::unordered_map< gate_t, typename S::value_type > &provenance_mapping, S semiring) const
Evaluate the sub-circuit rooted at gate g over semiring semiring.
std::map< gate_t, std::string > extra
Per-gate string extras.
gate_t addGate() override
Allocate a new gate with a default-initialised type.
std::set< gate_t > inputs
Set of input (leaf) gate IDs.
void serialize(Archive &ar, const unsigned int version)
Boost serialisation support.
std::vector< double > prob
Per-gate probability values.
void setInfos(gate_t g, unsigned info1, unsigned info2)
Set the integer annotation pair for gate g.
std::string getExtra(gate_t g) const
Return the string extra for gate g.
gate_t setGate(gate_type type) override
Allocate a new gate with type type and no UUID.
double getProb(gate_t g) const
Return the probability for gate g.
friend class boost::serialization::access
const std::set< gate_t > & getInputs() const
Return the set of input (leaf) gates.
std::pair< unsigned, unsigned > getInfos(gate_t g) const
Return the integer annotation pair for gate g.
void setExtra(gate_t g, const std::string &ex)
Attach a string extra to gate g.
virtual std::string toString(gate_t g) const override
Return a placeholder debug string (not intended for display).
void setProb(gate_t g, double p)
Set the probability for gate g.
Builds a d-DNNF from a Boolean circuit using a tree decomposition.
Core types, constants, and utilities shared across ProvSQL.
gate_type
Possible gate type in the provenance circuit.