Graph¶
Graph is the primary aggregate root of the domain. It owns collections of Node and Edge instances and exposes structural query helpers. Unlike Node and Edge, Graph is mutable — adding or removing elements changes its internal state and increments its version.
Lifecycle¶
stateDiagram-v2
[*] --> Empty : Graph.create()
Empty --> HasNodes : add_node()
HasNodes --> HasNodes : add_node() / update_node() / remove_node()
HasNodes --> HasEdges : add_edge()
HasEdges --> HasEdges : add_edge() / remove_edge()
HasEdges --> HasNodes : remove_edge()
HasNodes --> Empty : remove_node() (last)
Creating a Graph¶
Prefer using GraphEngine.create_graph() or GraphService.create_graph() rather than calling Graph.create() directly — the higher-level methods also persist the record and register the graph in cache.
from knowledge_platform.core.graph import Graph
from knowledge_platform.core.identifiers import new_workspace_id
ws_id = new_workspace_id()
graph = Graph.create(ws_id, type_name="outline", name="My Document")
print(graph.node_count()) # 0
print(graph.version) # 1
Node Operations¶
from knowledge_platform.core.node import Node
node = Node.create(graph.id, "OutlineItem", {"title": "Intro"})
graph.add_node(node)
print(graph.node_count()) # 1
# Retrieve a specific node
same_node = graph.get_node(node.id)
# Iterate all nodes of a specific type
for n in graph.nodes(type_name="OutlineItem"):
print(n.attributes["title"])
# Remove cascades incident edges automatically
graph.remove_node(node.id)
Edge Operations¶
from knowledge_platform.core.edge import Edge
edge = Edge.create(graph.id, parent.id, child.id, "ParentOf")
graph.add_edge(edge)
# Query edges by type
for e in graph.edges(type_name="ParentOf"):
print(f"{e.source_id} -> {e.target_id}")
# Adjacency helpers
for e in graph.outgoing_edges(parent.id, type_name="ParentOf"):
print(e.target_id)
for e in graph.incoming_edges(child.id):
print(e.source_id)
Error Handling¶
| Method | Raises | Condition |
|---|---|---|
add_node(node) |
ValueError |
Node belongs to a different graph or already exists |
update_node(node) |
KeyError |
Node not found |
remove_node(node_id) |
KeyError |
Node not found |
get_node(node_id) |
KeyError |
Node not found |
add_edge(edge) |
ValueError |
Edge belongs to a different graph or already exists |
add_edge(edge) |
KeyError |
Source or target node not found |
remove_edge(edge_id) |
KeyError |
Edge not found |
get_edge(edge_id) |
KeyError |
Edge not found |
API Reference¶
knowledge_platform.core.graph.Graph
dataclass
¶
An in-memory graph governed by a single graph type.
:class:Graph is the primary aggregate root of the domain. It owns
collections of :class:~knowledge_platform.core.node.Node and
:class:~knowledge_platform.core.edge.Edge instances and exposes
structural query helpers.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
GraphId
|
Unique identifier. |
workspace_id |
WorkspaceId
|
Owning workspace. |
type_name |
str
|
The graph type controlling semantics. |
name |
str
|
Human-readable display name. |
version |
int
|
Incremented on each structural change. |
created_at |
datetime
|
UTC creation timestamp. |
updated_at |
datetime
|
UTC last-modified timestamp. |
Source code in src/knowledge_platform/core/graph.py
14 15 16 17 18 19 20 21 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 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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | |
Functions¶
add_edge ¶
Insert edge into this graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge
|
Edge
|
Must reference nodes within this graph. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the edge belongs to a different graph or already exists. |
KeyError
|
If either endpoint node is missing. |
Source code in src/knowledge_platform/core/graph.py
add_node ¶
Insert node into this graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
Node
|
Must have :attr: |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the node belongs to a different graph or already exists. |
Source code in src/knowledge_platform/core/graph.py
create
classmethod
¶
Create a new empty graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
workspace_id
|
WorkspaceId
|
Owning workspace. |
required |
type_name
|
str
|
Semantic graph type name. |
required |
name
|
str
|
Optional display name. |
''
|
Returns:
| Name | Type | Description |
|---|---|---|
Empty |
'Graph'
|
class: |
Source code in src/knowledge_platform/core/graph.py
edge_count ¶
edges ¶
Iterate over edges, optionally filtered by type_name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_name
|
str | None
|
If given, yield only edges with this type. |
None
|
Yields:
| Name | Type | Description |
|---|---|---|
Matching |
Edge
|
class: |
Source code in src/knowledge_platform/core/graph.py
get_edge ¶
Retrieve an edge by ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_id
|
EdgeId
|
Target edge identifier. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
The |
Edge
|
class: |
Raises:
| Type | Description |
|---|---|
KeyError
|
If not found. |
Source code in src/knowledge_platform/core/graph.py
get_node ¶
Retrieve a node by ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id
|
NodeId
|
Target node identifier. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
The |
Node
|
class: |
Raises:
| Type | Description |
|---|---|
KeyError
|
If not found. |
Source code in src/knowledge_platform/core/graph.py
incoming_edges ¶
Yield edges where node_id is the target.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id
|
NodeId
|
Target node identifier. |
required |
type_name
|
str | None
|
Optional type filter. |
None
|
Yields:
| Type | Description |
|---|---|
Edge
|
Matching incoming :class: |
Source code in src/knowledge_platform/core/graph.py
node_count ¶
nodes ¶
Iterate over nodes, optionally filtered by type_name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_name
|
str | None
|
If given, yield only nodes with this type. |
None
|
Yields:
| Name | Type | Description |
|---|---|---|
Matching |
Node
|
class: |
Source code in src/knowledge_platform/core/graph.py
outgoing_edges ¶
Yield edges where node_id is the source.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id
|
NodeId
|
Source node identifier. |
required |
type_name
|
str | None
|
Optional type filter. |
None
|
Yields:
| Type | Description |
|---|---|
Edge
|
Matching outgoing :class: |
Source code in src/knowledge_platform/core/graph.py
remove_edge ¶
Delete an edge by ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_id
|
EdgeId
|
Target edge identifier. |
required |
Raises:
| Type | Description |
|---|---|
KeyError
|
If not found. |
Source code in src/knowledge_platform/core/graph.py
remove_node ¶
Delete a node and all its incident edges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id
|
NodeId
|
ID of the node to remove. |
required |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the node does not exist. |
Source code in src/knowledge_platform/core/graph.py
update_node ¶
Replace an existing node with an updated version.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
Node
|
Updated node (same ID, higher version). |
required |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the node does not exist in this graph. |