27#include "catalog/pg_type.h"
28#include "utils/tuplestore.h"
29#include "utils/uuid.h"
30#include "executor/spi.h"
43#include <unordered_map>
63 (
pg_uuid_t token,
pg_uuid_t variable,
const std::string &method,
const std::string &args,
bool banzhaf)
74 "not supported -- a conditioned token (X | C) cannot be "
75 "passed to shapley() / banzhaf(). Compute the index on the "
76 "unconditioned token, or use probability_evaluate for the "
77 "conditional probability P(X|C)");
79 std::unordered_map<gate_t, gate_t> gc_to_bc;
83 provsql_error(
"Computing Shapley/Banzhaf values is ill-defined for circuits with multivalued (mulinput) gates");
92 dDNNF dd = (method.empty() || method ==
"default" || method ==
"auto")
94 : c.
makeDD(root, method, args);
118 if(PG_ARGISNULL(0) || PG_ARGISNULL(1))
121 Datum token = PG_GETARG_DATUM(0);
122 Datum variable = PG_GETARG_DATUM(1);
125 if(!PG_ARGISNULL(2)) {
126 text *t = PG_GETARG_TEXT_P(2);
127 method = string(VARDATA(t),VARSIZE(t)-VARHDRSZ);
131 if(!PG_ARGISNULL(3)) {
132 text *t = PG_GETARG_TEXT_P(3);
133 args = string(VARDATA(t),VARSIZE(t)-VARHDRSZ);
136 bool banzhaf =
false;
137 if(!PG_ARGISNULL(4)) {
138 banzhaf = PG_GETARG_BOOL(4);
141 PG_RETURN_FLOAT8(
shapley_internal(*DatumGetUUIDP(token), *DatumGetUUIDP(variable), method, args, banzhaf));
142 }
catch(
const std::exception &e) {
156 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
158 MemoryContext per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
159 MemoryContext oldcontext = MemoryContextSwitchTo(per_query_ctx);
161 TupleDesc tupdesc = rsinfo->expectedDesc;
162 Tuplestorestate *tupstore = tuplestore_begin_heap(rsinfo->allowedModes & SFRM_Materialize_Random,
false, work_mem);
164 rsinfo->returnMode = SFRM_Materialize;
165 rsinfo->setResult = tupstore;
169 if(!PG_ARGISNULL(0))
try {
171 pg_uuid_t token = *DatumGetUUIDP(PG_GETARG_DATUM(0));
174 if(!PG_ARGISNULL(1)) {
175 text *t = PG_GETARG_TEXT_P(1);
176 method = string(VARDATA(t),VARSIZE(t)-VARHDRSZ);
180 if(!PG_ARGISNULL(2)) {
181 text *t = PG_GETARG_TEXT_P(2);
182 args = string(VARDATA(t),VARSIZE(t)-VARHDRSZ);
185 bool banzhaf =
false;
186 if(!PG_ARGISNULL(3)) {
187 banzhaf = PG_GETARG_BOOL(3);
194 "not supported -- a conditioned token (X | C) cannot be "
195 "passed to shapley() / banzhaf(). Compute the index on the "
196 "unconditioned token, or use probability_evaluate for the "
197 "conditional probability P(X|C)");
199 std::unordered_map<gate_t, gate_t> gc_to_bc;
203 provsql_error(
"Computing Shapley/Banzhaf values is ill-defined for circuits with multivalued (mulinput) gates");
205 dDNNF dd = (method.empty() || method ==
"default" || method ==
"auto")
207 : c.
makeDD(root, method, args);
212 for(
auto &v_circuit_gate: c.
getInputs()) {
214 auto var_uuid_string = c.
getUUID(v_circuit_gate);
215 auto var_gate=dd.
getGate(var_uuid_string);
227 UUIDPGetDatum(uuidp), Float8GetDatum(result)
229 bool nulls[
sizeof(values)] = {0, 0};
231 tuplestore_putvalues(tupstore, tupdesc, values, nulls);
233 }
catch(
const std::exception &e) {
241 MemoryContextSwitchTo(oldcontext);
Boolean provenance circuit with support for knowledge compilation.
@ AND
Logical conjunction of child gates.
@ IN
Input (variable) gate representing a base tuple.
BooleanCircuit getBooleanCircuit(GenericCircuit &gc, pg_uuid_t token, gate_t &gate, std::unordered_map< gate_t, gate_t > &gc_to_bc)
Build a BooleanCircuit from an already-loaded GenericCircuit.
GenericCircuit getGenericCircuit(pg_uuid_t token)
Build a GenericCircuit from the mmap store rooted at token.
Build in-memory circuits from the mmap-backed persistent store.
gate_t
Strongly-typed gate identifier.
Out-of-line template method implementations for Circuit<gateType>.
Semiring-agnostic in-memory provenance circuit.
Catalog of probability-evaluation methods (Strategy + registry).
Fix macro conflicts between PostgreSQL headers and the C++ STL/Boost.
Boolean circuit for provenance formula evaluation.
const std::set< gate_t > & getInputs() const
Return the set of input (IN) gate IDs.
dDNNF makeDD(gate_t g, const std::string &method, const std::string &args) const
Dispatch to the appropriate d-DNNF construction method.
bool hasMultivaluedGates() const
Return true if the circuit contains any MULIN gates.
gateType getGateType(gate_t g) const
Return the type of gate g.
uuid getUUID(gate_t g) const
Return the UUID string associated with gate g.
gate_t getGate(const uuid &u)
Return (or create) the gate associated with UUID u.
A d-DNNF circuit supporting exact probabilistic and game-theoretic evaluation.
void makeSmooth()
Make the d-DNNF smooth.
void makeGatesBinary(BooleanGate type)
Rewrite all n-ary AND/OR gates into binary trees.
double shapley(gate_t var) const
Compute the Shapley value of input gate var.
double banzhaf(gate_t var) const
Compute the Banzhaf power index of input gate var.
The SIGINT handler installed for the lifetime of the object, the previous one restored as it ends (a ...
Constructs a d-DNNF from a Boolean circuit and its tree decomposition.
dDNNF makeDDAuto(BooleanCircuit &c, gate_t g)
Cost-select a d-DNNF construction route for gate g of Boolean circuit c and build it – the default ma...
#define PROVSQL_OUT_OF_SCOPE
#define provsql_error(fmt,...)
Report a fatal ProvSQL error and abort the current transaction.
#define provsql_unsupported(scope, tag, fmt,...)
Refuse a query ProvSQL cannot track, and abort the transaction.
void provsql_cancel_if_interrupted(void)
Raise PG's cancel if the evaluation stopped for one; nothing otherwise.
Cancellation of the long C++ evaluation loops.
void provsql_poll_interrupt()
Throw where an evaluation is to stop, its query cancelled.
Shared-memory segment and inter-process pipe management.
Core types, constants, and utilities shared across ProvSQL.
@ gate_conditioned
Conditioning marker with two children [target, evidence]: measure-only, probability_evaluate returns ...
pg_uuid_t string2uuid(const string &source)
Parse a UUID string into a pg_uuid_t.
string uuid2string(pg_uuid_t uuid)
Format a pg_uuid_t as a std::string.
C++ utility functions for UUID manipulation.
static double shapley_internal(pg_uuid_t token, pg_uuid_t variable, const std::string &method, const std::string &args, bool banzhaf)
Core implementation for Shapley and Banzhaf index computation.
Datum shapley(PG_FUNCTION_ARGS)
PostgreSQL-callable wrapper for shapley() and banzhaf().
Datum shapley_all_vars(PG_FUNCTION_ARGS)
PostgreSQL-callable wrapper for shapley_all_vars() set-returning function.