24#include "catalog/pg_type.h"
25#include "utils/uuid.h"
26#include "executor/spi.h"
54 (
pg_uuid_t token,
pg_uuid_t variable,
const std::string &method,
const std::string &args,
bool banzhaf)
60 provsql_error(
"Computing Shapley/Banzhaf values is ill-defined for circuits with multivalued (mulinput) gates");
87 if(PG_ARGISNULL(0) || PG_ARGISNULL(1))
90 Datum token = PG_GETARG_DATUM(0);
91 Datum variable = PG_GETARG_DATUM(1);
94 if(!PG_ARGISNULL(2)) {
95 text *t = PG_GETARG_TEXT_P(2);
96 method = string(VARDATA(t),VARSIZE(t)-VARHDRSZ);
100 if(!PG_ARGISNULL(3)) {
101 text *t = PG_GETARG_TEXT_P(3);
102 args = string(VARDATA(t),VARSIZE(t)-VARHDRSZ);
105 bool banzhaf =
false;
106 if(!PG_ARGISNULL(4)) {
107 banzhaf = PG_GETARG_BOOL(4);
110 PG_RETURN_FLOAT8(
shapley_internal(*DatumGetUUIDP(token), *DatumGetUUIDP(variable), method, args, banzhaf));
111 }
catch(
const std::exception &e) {
123 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
125 MemoryContext per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
126 MemoryContext oldcontext = MemoryContextSwitchTo(per_query_ctx);
128 TupleDesc tupdesc = rsinfo->expectedDesc;
129 Tuplestorestate *tupstore = tuplestore_begin_heap(rsinfo->allowedModes & SFRM_Materialize_Random,
false, work_mem);
131 rsinfo->returnMode = SFRM_Materialize;
132 rsinfo->setResult = tupstore;
134 if(!PG_ARGISNULL(0)) {
135 pg_uuid_t token = *DatumGetUUIDP(PG_GETARG_DATUM(0));
138 if(!PG_ARGISNULL(1)) {
139 text *t = PG_GETARG_TEXT_P(1);
140 method = string(VARDATA(t),VARSIZE(t)-VARHDRSZ);
144 if(!PG_ARGISNULL(2)) {
145 text *t = PG_GETARG_TEXT_P(2);
146 args = string(VARDATA(t),VARSIZE(t)-VARHDRSZ);
149 bool banzhaf =
false;
150 if(!PG_ARGISNULL(3)) {
151 banzhaf = PG_GETARG_BOOL(3);
159 provsql_error(
"Computing Shapley/Banzhaf values is ill-defined for circuits with multivalued (mulinput) gates");
166 for(
auto &v_circuit_gate: c.
getInputs()) {
167 auto var_uuid_string = c.
getUUID(v_circuit_gate);
168 auto var_gate=dd.
getGate(var_uuid_string);
180 UUIDPGetDatum(uuidp), Float8GetDatum(result)
182 bool nulls[
sizeof(values)] = {0, 0};
184 tuplestore_putvalues(tupstore, tupdesc, values, nulls);
188 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.
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>.
Fix gettext macro conflicts between PostgreSQL and the C++ STL.
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.
Constructs a d-DNNF from a Boolean circuit and its tree decomposition.
#define provsql_error(fmt,...)
Report a fatal ProvSQL error and abort the current transaction.
Shared-memory segment and inter-process pipe management.
Core types, constants, and utilities shared across ProvSQL.
#define UUID_LEN
Number of bytes in a UUID.
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.