ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
tree_decomposition_dot.cpp
Go to the documentation of this file.
1/**
2 * @file tree_decomposition_dot.cpp
3 * @brief SQL function @c provsql.tree_decomposition_dot() – return a
4 * GraphViz DOT visualisation of the tree decomposition of the
5 * provenance circuit's primal graph.
6 *
7 * The same min-fill decomposition that backs
8 * @c probability_evaluate(..., 'tree-decomposition') is exposed here as
9 * a DOT digraph, with the computed treewidth attached as a
10 * @c "// treewidth=..." comment on the first line.
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(tree_decomposition_dot);
24}
25
26#include "c_cpp_compatibility.h"
27#include "BooleanCircuit.h"
28#include "CircuitFromMMap.h"
29#include "TreeDecomposition.h"
30#include "provsql_utils_cpp.h"
31
32#include <string>
33
34using namespace std;
35
36/**
37 * @brief PostgreSQL-callable entry point.
38 *
39 * Arguments: @c token (uuid).
40 * Returns: DOT text whose first line is @c "// treewidth=<n>".
41 */
42Datum tree_decomposition_dot(PG_FUNCTION_ARGS)
43{
44 try {
45 if(PG_ARGISNULL(0))
46 PG_RETURN_NULL();
47 pg_uuid_t *token = DatumGetUUIDP(PG_GETARG_DATUM(0));
48
49 gate_t root;
50 BooleanCircuit c = getBooleanCircuit(*token, root);
52
53 try {
55 // Two preamble lines before the digraph body:
56 //
57 // // treewidth=<n>
58 // // inputs: <idx>=<uuid> <idx>=<uuid> ...
59 //
60 // The second line maps BooleanCircuit gate indices (the same
61 // indices that appear inside each bag's `{...}` label) back to
62 // the original provenance UUID, when the gate is a tracked input
63 // leaf. Bag indices not present in the map are post-Tseytin
64 // auxiliary gates with no source row. The Studio reads this map
65 // to enrich the bag inspector: input variables get a clickable
66 // chip pointing at /api/leaf/<uuid>, auxiliaries stay opaque.
67 string inputs_line = "// inputs:";
68 for (gate_t in : c.getInputs()) {
69 const string &uuid = c.getUUID(in);
70 if (uuid.empty()) continue;
71 inputs_line += " " + to_string(in) + "=" + uuid;
72 }
73 inputs_line += "\n";
74
75 string dot = "// treewidth=" + to_string(td.getTreewidth()) + "\n"
76 + inputs_line
77 + td.toDot();
78 text *result = (text *) palloc(VARHDRSZ + dot.size());
79 SET_VARSIZE(result, VARHDRSZ + dot.size());
80 memcpy((void *) VARDATA(result), dot.c_str(), dot.size());
81 PG_RETURN_TEXT_P(result);
84 "circuit treewidth exceeds the supported limit (MAX_TREEWIDTH)");
85 }
86 } catch(const std::exception &e) {
87 provsql_error("%s", e.what());
88 } catch(...) {
89 provsql_error("Unknown exception");
90 }
91 PG_RETURN_NULL();
92}
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.
gate_t
Strongly-typed gate identifier.
Definition Circuit.h:49
std::string to_string(gate_t g)
Convert a gate_t to its decimal string representation.
Definition Circuit.h:250
Tree decomposition of a Boolean circuit for knowledge compilation.
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.
void rewriteMultivaluedGates()
Rewrite all MULVAR/MULIN gate clusters into standard AND/OR/NOT circuits.
uuid getUUID(gate_t g) const
Return the UUID string associated with gate g.
Definition Circuit.hpp:46
Exception thrown when a tree decomposition cannot be constructed.
Tree decomposition of a Boolean circuit's primal graph.
unsigned getTreewidth() const
Return the treewidth of this decomposition.
std::string toDot() const
Render the tree decomposition as a GraphViz DOT string.
#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.
Datum tree_decomposition_dot(PG_FUNCTION_ARGS)
PostgreSQL-callable entry point.