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)
84 if(PG_ARGISNULL(0) || PG_ARGISNULL(1))
87 Datum token = PG_GETARG_DATUM(0);
88 Datum variable = PG_GETARG_DATUM(1);
91 if(!PG_ARGISNULL(2)) {
92 text *t = PG_GETARG_TEXT_P(2);
93 method = string(VARDATA(t),VARSIZE(t)-VARHDRSZ);
97 if(!PG_ARGISNULL(3)) {
98 text *t = PG_GETARG_TEXT_P(3);
99 args = string(VARDATA(t),VARSIZE(t)-VARHDRSZ);
102 bool banzhaf =
false;
103 if(!PG_ARGISNULL(4)) {
104 banzhaf = PG_GETARG_BOOL(4);
107 PG_RETURN_FLOAT8(
shapley_internal(*DatumGetUUIDP(token), *DatumGetUUIDP(variable), method, args, banzhaf));
108 }
catch(
const std::exception &e) {
120 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
122 MemoryContext per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
123 MemoryContext oldcontext = MemoryContextSwitchTo(per_query_ctx);
125 TupleDesc tupdesc = rsinfo->expectedDesc;
126 Tuplestorestate *tupstore = tuplestore_begin_heap(rsinfo->allowedModes & SFRM_Materialize_Random,
false, work_mem);
128 rsinfo->returnMode = SFRM_Materialize;
129 rsinfo->setResult = tupstore;
131 if(!PG_ARGISNULL(0)) {
132 pg_uuid_t token = *DatumGetUUIDP(PG_GETARG_DATUM(0));
135 if(!PG_ARGISNULL(1)) {
136 text *t = PG_GETARG_TEXT_P(1);
137 method = string(VARDATA(t),VARSIZE(t)-VARHDRSZ);
141 if(!PG_ARGISNULL(2)) {
142 text *t = PG_GETARG_TEXT_P(2);
143 args = string(VARDATA(t),VARSIZE(t)-VARHDRSZ);
146 bool banzhaf =
false;
147 if(!PG_ARGISNULL(3)) {
148 banzhaf = PG_GETARG_BOOL(3);
160 for(
auto &v_circuit_gate: c.
getInputs()) {
161 auto var_uuid_string = c.
getUUID(v_circuit_gate);
162 auto var_gate=dd.
getGate(var_uuid_string);
174 UUIDPGetDatum(uuidp), Float8GetDatum(result)
176 bool nulls[
sizeof(values)] = {0, 0};
178 tuplestore_putvalues(tupstore, tupdesc, values, nulls);
182 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(pg_uuid_t token, gate_t &gate)
Build a BooleanCircuit 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>.
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.
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.