ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
compile_to_ddnnf_dot.cpp
Go to the documentation of this file.
1/**
2 * @file compile_to_ddnnf_dot.cpp
3 * @brief SQL function @c provsql.compile_to_ddnnf_dot() – render the
4 * compiled d-DNNF of a provenance circuit as GraphViz DOT.
5 *
6 * Builds the Boolean circuit from the mmap-backed store, runs the
7 * requested external knowledge compiler (@c d4 / @c c2d / @c minic2d /
8 * @c dsharp) via @c BooleanCircuit::compilation(), and emits the
9 * resulting @c dDNNF as a DOT digraph. The string can be piped through
10 * @c dot(1) to produce an image, or rendered by Studio.
11 */
12extern "C" {
13#include "postgres.h"
14#include "fmgr.h"
15#if PG_VERSION_NUM >= 160000
16#include "varatt.h"
17#endif
18#include "catalog/pg_type.h"
19#include "utils/uuid.h"
20#include "provsql_shmem.h"
21#include "provsql_utils.h"
22
23PG_FUNCTION_INFO_V1(compile_to_ddnnf_dot);
24}
25
26#include "c_cpp_compatibility.h"
27#include "BooleanCircuit.h"
28#include "CircuitFromMMap.h"
29#include "dDNNF.h"
30#include "provsql_utils_cpp.h"
31#include "tool_registry_sync.h"
32
33#include <string>
34
35using namespace std;
36
37/**
38 * @brief PostgreSQL-callable entry point.
39 *
40 * Arguments: @c token (uuid), @c compiler (text, default @c "d4").
41 * Returns: DOT representation of the compiled d-DNNF.
42 */
43Datum compile_to_ddnnf_dot(PG_FUNCTION_ARGS)
44{
45 provsql_sync_tool_registry(); // honour persisted tool-registry overrides
46 try {
47 if(PG_ARGISNULL(0))
48 PG_RETURN_NULL();
49 pg_uuid_t *token = DatumGetUUIDP(PG_GETARG_DATUM(0));
50
51 string compiler = ""; // empty => preference-ranked best available compiler
52 if(!PG_ARGISNULL(1)) {
53 text *t = PG_GETARG_TEXT_P(1);
54 compiler = string(VARDATA(t), VARSIZE(t)-VARHDRSZ);
55 }
56
57 // The compiler argument selects how the d-D circuit is obtained:
58 // an external compiler (d4 / d4v2 / c2d / minic2d / dsharp /
59 // panini-*) or one of the in-process meta-routes
60 // ("tree-decomposition", "interpret-as-dd", "default"). The single
61 // dispatch point makeDDByName resolves both, shared with
62 // compile_to_ddnnf (NNF) and ddnnf_stats so they cannot drift.
63 // "inversion-free" is special: it needs the per-input order keys on the
64 // generic circuit's annotation markers, so it goes through
65 // buildInversionFreeDDNNF rather than the BooleanCircuit dispatch.
66 dDNNF d;
67 if(compiler == "inversion-free") {
68 d = buildInversionFreeDDNNF(*token);
69 } else {
70 gate_t root;
71 BooleanCircuit c = getBooleanCircuit(*token, root);
73 d = c.makeDDByName(root, compiler);
74 }
75 string dot = d.toDot();
76
77 text *result = (text *) palloc(VARHDRSZ + dot.size());
78 SET_VARSIZE(result, VARHDRSZ + dot.size());
79 memcpy((void *) VARDATA(result), dot.c_str(), dot.size());
80 PG_RETURN_TEXT_P(result);
81 } catch(const std::exception &e) {
82 provsql_error("%s", e.what());
83 } catch(...) {
84 provsql_error("Unknown exception");
85 }
86 PG_RETURN_NULL();
87}
Boolean provenance circuit with support for knowledge compilation.
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.
dDNNF buildInversionFreeDDNNF(pg_uuid_t token)
Compile a query certified inversion-free to its structured d-DNNF.
gate_t
Strongly-typed gate identifier.
Definition Circuit.h:49
Fix gettext macro conflicts between PostgreSQL and the C++ STL.
Boolean circuit for provenance formula evaluation.
void rewriteMultivaluedGates()
Rewrite all MULVAR/MULIN gate clusters into standard AND/OR/NOT circuits.
dDNNF makeDDByName(gate_t g, const std::string &name) const
Build a dDNNF from a single compiler/route name.
A d-DNNF circuit supporting exact probabilistic and game-theoretic evaluation.
Definition dDNNF.h:71
std::string toDot() const
Return a GraphViz DOT representation of the d-DNNF.
Definition dDNNF.cpp:896
Datum compile_to_ddnnf_dot(PG_FUNCTION_ARGS)
PostgreSQL-callable entry point.
Decomposable Deterministic Negation Normal Form circuit.
#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.
C++ utility functions for UUID manipulation.
UUID structure.
void provsql_sync_tool_registry()
Rebuild the in-memory registry as "compiled seed overlaid with the provsql.tool_overrides rows"...
Reload the in-memory external-tool registry from its persistent overrides.