Core
The generic knowledge-graph core.
Typed :class:~ideagraph.kg.node.Node objects connected by directed
:class:~ideagraph.kg.edge.Edge objects in a :class:~ideagraph.kg.graph.KnowledgeGraph,
with a :class:~ideagraph.kg.profile.Profile supplying the vocabulary and rules
for a given domain. Importing this package registers the built-in profiles.
Diagnostic
dataclass
¶
A single validation finding.
Attributes:
| Name | Type | Description |
|---|---|---|
level |
str
|
|
code |
str
|
A stable machine-readable code. |
message |
str
|
A human-readable, self-correction-friendly description. |
ref |
str | None
|
The id of the offending node or edge, if any. |
Source code in src/ideagraph/kg/profile.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | |
to_dict()
¶
Serialise the diagnostic.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A dictionary representation. |
Source code in src/ideagraph/kg/profile.py
40 41 42 43 44 45 46 47 | |
Edge
dataclass
¶
A directed, typed connection between two nodes.
Attributes:
| Name | Type | Description |
|---|---|---|
type |
str
|
The edge's type, from the active profile's edge vocabulary. |
source |
str
|
The id of the source node. |
target |
str
|
The id of the target node (a local id, or a global
|
id |
str
|
Stable unique identifier (UUID4 hex if not supplied). |
properties |
dict[str, Any]
|
Arbitrary structured metadata about the connection. |
created_at |
datetime
|
Timezone-aware creation timestamp (UTC). |
Source code in src/ideagraph/kg/edge.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | |
from_dict(data)
classmethod
¶
Reconstruct an edge from its dictionary representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
A dictionary as produced by :meth: |
required |
Returns:
| Type | Description |
|---|---|
Edge
|
The reconstructed edge. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
Source code in src/ideagraph/kg/edge.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | |
to_dict()
¶
Serialise the edge to a JSON-compatible dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A dictionary representation of the edge. |
Source code in src/ideagraph/kg/edge.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | |
EdgeRule
dataclass
¶
Schema for an edge type.
Attributes:
| Name | Type | Description |
|---|---|---|
type |
str
|
The edge type name. |
source_types |
frozenset[str]
|
Allowed source node types (empty = any). |
target_types |
frozenset[str]
|
Allowed target node types (empty = any). Ignored for cross-article targets, whose node lives in another graph. |
required_properties |
frozenset[str]
|
Property keys every edge of this type must set. |
Source code in src/ideagraph/kg/profile.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | |
KnowledgeGraph
dataclass
¶
An in-memory collection of typed nodes and the edges between them.
Attributes:
| Name | Type | Description |
|---|---|---|
nodes |
dict[str, Node]
|
Nodes held by the graph, keyed by id. |
edges |
dict[str, Edge]
|
Edges held by the graph, keyed by id. |
article_id |
str | None
|
This graph's stable article id. Nodes are addressed globally
as |
metadata |
dict[str, Any]
|
Arbitrary graph-level metadata (e.g. |
Source code in src/ideagraph/kg/graph.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |
add_edge(edge)
¶
Add or replace an edge and update the traversal index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge
|
Edge
|
The edge to store. |
required |
Returns:
| Type | Description |
|---|---|
Edge
|
The stored edge. |
Source code in src/ideagraph/kg/graph.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
add_node(node)
¶
Add or replace a node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
Node
|
The node to store. |
required |
Returns:
| Type | Description |
|---|---|
Node
|
The stored node. |
Source code in src/ideagraph/kg/graph.py
42 43 44 45 46 47 48 49 50 51 52 53 | |
from_dict(data)
classmethod
¶
Reconstruct a graph from its dictionary representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
A dictionary as produced by :meth: |
required |
Returns:
| Type | Description |
|---|---|
KnowledgeGraph
|
The reconstructed graph, with its traversal index rebuilt. |
Source code in src/ideagraph/kg/graph.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |
global_id(node_id)
¶
Return the global address article_id#node_id for a local node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id
|
str
|
A local node id. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The global address. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If this graph has no |
Source code in src/ideagraph/kg/graph.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
incoming(node_id, edge_type=None)
¶
Return edges whose target is node_id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id
|
str
|
The id of the target node. |
required |
edge_type
|
str | None
|
If given, only edges of this type are returned. |
None
|
Returns:
| Type | Description |
|---|---|
list[Edge]
|
The matching edges, in insertion order. |
Source code in src/ideagraph/kg/graph.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | |
nodes_of_type(node_type)
¶
Return all nodes of a given type, in insertion order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_type
|
str
|
The node type to filter by. |
required |
Returns:
| Type | Description |
|---|---|
list[Node]
|
The matching nodes. |
Source code in src/ideagraph/kg/graph.py
118 119 120 121 122 123 124 125 126 127 128 | |
outgoing(node_id, edge_type=None)
¶
Return edges whose source is node_id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id
|
str
|
The id of the source node. |
required |
edge_type
|
str | None
|
If given, only edges of this type are returned. |
None
|
Returns:
| Type | Description |
|---|---|
list[Edge]
|
The matching edges, in insertion order. |
Source code in src/ideagraph/kg/graph.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
to_dict()
¶
Serialise the whole graph to a JSON-compatible dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A dictionary with |
dict[str, Any]
|
|
Source code in src/ideagraph/kg/graph.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 | |
Node
dataclass
¶
A single piece of information in a knowledge graph.
Attributes:
| Name | Type | Description |
|---|---|---|
type |
str
|
The node's type, from the active profile's vocabulary. |
text |
str
|
The human-readable content of the node. |
id |
str
|
Stable unique identifier (UUID4 hex if not supplied). |
tags |
list[str]
|
Free-form labels for grouping and discovery. |
properties |
dict[str, Any]
|
Arbitrary structured, domain-specific fields. |
created_at |
datetime
|
Timezone-aware creation timestamp (UTC). |
updated_at |
datetime
|
Timezone-aware timestamp of the last change (UTC). |
Source code in src/ideagraph/kg/node.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
from_dict(data)
classmethod
¶
Reconstruct a node from its dictionary representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
A dictionary as produced by :meth: |
required |
Returns:
| Type | Description |
|---|---|
Node
|
The reconstructed node. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
Source code in src/ideagraph/kg/node.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
to_dict()
¶
Serialise the node to a JSON-compatible dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A dictionary representation of the node. |
Source code in src/ideagraph/kg/node.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
touch()
¶
Refresh :attr:updated_at to now.
Source code in src/ideagraph/kg/node.py
50 51 52 | |
NodeRule
dataclass
¶
Schema for a node type.
Attributes:
| Name | Type | Description |
|---|---|---|
type |
str
|
The node type name. |
required_properties |
frozenset[str]
|
Property keys every node of this type must set. |
Source code in src/ideagraph/kg/profile.py
50 51 52 53 54 55 56 57 58 59 60 | |
Profile
dataclass
¶
A named schema of node and edge types plus structural rules.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
The profile's stable name. |
node_types |
dict[str, NodeRule]
|
Mapping of node type name to its rule. |
edge_types |
dict[str, EdgeRule]
|
Mapping of edge type name to its rule. |
Source code in src/ideagraph/kg/profile.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | |
allows_edge_type(edge_type)
¶
Report whether an edge type is part of this profile.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_type
|
str
|
The edge type to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/ideagraph/kg/profile.py
107 108 109 110 111 112 113 114 115 116 117 | |
allows_node_type(node_type)
¶
Report whether a node type is part of this profile.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_type
|
str
|
The node type to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/ideagraph/kg/profile.py
95 96 97 98 99 100 101 102 103 104 105 | |
validate(graph)
¶
Structurally validate a graph against this profile.
Checks that every node/edge type is known, that required properties are present, that edges reference existing source nodes, and that endpoint types satisfy the edge rule. Cross-article targets (global ids) are not resolved here — that is a library-level concern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
KnowledgeGraph
|
The graph to validate. |
required |
Returns:
| Type | Description |
|---|---|
list[Diagnostic]
|
A list of diagnostics (empty if the graph conforms). |
Source code in src/ideagraph/kg/profile.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | |
StaleImport
dataclass
¶
A copied node whose origin has drifted since it was extracted.
Attributes:
| Name | Type | Description |
|---|---|---|
node_id |
str
|
The local id of the imported node. |
source_gid |
str
|
The origin's global id ( |
reason |
str
|
|
Source code in src/ideagraph/kg/extract.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 | |
available_profiles()
¶
Return the names of all registered profiles.
Returns:
| Type | Description |
|---|---|
list[str]
|
Registered profile names, sorted. |
Source code in src/ideagraph/kg/profile.py
218 219 220 221 222 223 224 225 | |
dumps_prov(graph, *, indent=2)
¶
Serialise a knowledge graph to a PROV-JSON string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
KnowledgeGraph
|
The graph to export. |
required |
indent
|
int
|
Indentation passed to :func: |
2
|
Returns:
| Type | Description |
|---|---|
str
|
The PROV-JSON document as a string. |
Source code in src/ideagraph/kg/prov.py
141 142 143 144 145 146 147 148 149 150 151 152 | |
extract_subgraph(graph, seeds, *, hops=1, article_id=None, stamp_provenance=True)
¶
Extract the induced subgraph around seeds into a new graph.
The result contains every node within hops edges of a seed, every edge
whose endpoints are both included, and every cross-article edge leaving an
included node. Copies are deep enough to be independent of the source: the
new graph can be mutated or saved without touching the original.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
KnowledgeGraph
|
The source graph to carve from. |
required |
seeds
|
set[str]
|
Seed node ids (ids absent from the source are ignored). |
required |
hops
|
int
|
Number of edge hops to expand from the seeds. |
1
|
article_id
|
str | None
|
|
None
|
stamp_provenance
|
bool
|
If true and the source has an |
True
|
Returns:
| Type | Description |
|---|---|
KnowledgeGraph
|
A new :class: |
Source code in src/ideagraph/kg/extract.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
find_stale_imports(graph, resolve)
¶
Report imported nodes whose origin has changed or disappeared.
Only nodes stamped by :func:extract_subgraph (carrying both a
source_gid and a source_hash) are checked. Local edits to a copied
node do not flag it — the stamp records the origin's text at extraction
time, so only upstream drift is reported.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
KnowledgeGraph
|
The graph holding imported nodes (e.g. a project graph). |
required |
resolve
|
GraphResolver
|
Maps an |
required |
Returns:
| Name | Type | Description |
|---|---|---|
One |
list[StaleImport]
|
class: |
Source code in src/ideagraph/kg/extract.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | |
from_prov(document)
¶
Reconstruct a knowledge graph from a PROV-JSON document.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
document
|
dict[str, Any]
|
A PROV-JSON document as produced by :func: |
required |
Returns:
| Type | Description |
|---|---|
KnowledgeGraph
|
The reconstructed graph. External endpoint stubs (global ids or the |
KnowledgeGraph
|
generic |
KnowledgeGraph
|
as nodes. |
Source code in src/ideagraph/kg/prov.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | |
get_profile(name)
¶
Return a registered profile by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The profile name. |
required |
Returns:
| Type | Description |
|---|---|
Profile
|
The profile. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If no profile with that name is registered. |
Source code in src/ideagraph/kg/profile.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 | |
loads_prov(text)
¶
Deserialise a knowledge graph from a PROV-JSON string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
A PROV-JSON document as produced by :func: |
required |
Returns:
| Type | Description |
|---|---|
KnowledgeGraph
|
The reconstructed graph. |
Source code in src/ideagraph/kg/prov.py
220 221 222 223 224 225 226 227 228 229 230 | |
register_profile(profile)
¶
Register a profile so it can be looked up by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
profile
|
Profile
|
The profile to register. |
required |
Returns:
| Type | Description |
|---|---|
Profile
|
The registered profile. |
Source code in src/ideagraph/kg/profile.py
188 189 190 191 192 193 194 195 196 197 198 199 | |
to_prov(graph)
¶
Convert a knowledge graph to a PROV-JSON document.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
KnowledgeGraph
|
The graph to export. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A PROV-JSON document as a plain dictionary. |
Source code in src/ideagraph/kg/prov.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |