Edge¶
An Edge is a directed relationship between two Node instances within the same graph. Like Node, it is an immutable frozen dataclass. Use Edge.evolve() to produce an updated copy with a higher version.
Directed Graph Semantics¶
Edges are always directed: they have a source_id (origin) and a target_id (destination). The meaning of direction depends on the edge type declared by the GraphType. For example, in the Outline module a ParentOf edge runs from parent to child.
Creating an Edge¶
Edges are created via Edge.create() and must reference nodes that already exist in the graph:
from knowledge_platform.core.edge import Edge
edge = Edge.create(
graph_id=graph.id,
source_id=parent_node.id,
target_id=child_node.id,
type_name="ParentOf",
)
Validation is done at the service layer
Edge.create() itself does not check that the nodes exist or that the type is valid. Those checks happen in GraphService.add_edge() and Graph.add_edge().
Identity¶
| Field | Type | Meaning |
|---|---|---|
id |
EdgeId |
Globally unique identifier (UUID v4) |
graph_id |
GraphId |
The owning graph |
source_id |
NodeId |
Origin node |
target_id |
NodeId |
Destination node |
type_name |
str |
Semantic type (e.g. "ParentOf") |
API Reference¶
knowledge_platform.core.edge.Edge
dataclass
¶
A directed relationship between two :class:~knowledge_platform.core.node.Node instances.
Like :class:~knowledge_platform.core.node.Node, edges are immutable
value-objects. Use :meth:evolve to produce updated copies.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
EdgeId
|
Unique identifier for this edge. |
graph_id |
GraphId
|
Owning graph identifier. |
source_id |
NodeId
|
Origin node. |
target_id |
NodeId
|
Destination node. |
type_name |
str
|
Semantic relationship type; resolved by the active
:class: |
attributes |
dict[str, object]
|
Arbitrary key-value payload; must be JSON-serialisable. |
version |
int
|
Monotonically increasing change counter (starts at 1). |
created_at |
datetime
|
UTC timestamp of initial creation. |
updated_at |
datetime
|
UTC timestamp of the most recent update. |
Source code in src/knowledge_platform/core/edge.py
Functions¶
create
classmethod
¶
create(
graph_id: GraphId,
source_id: NodeId,
target_id: NodeId,
type_name: str,
attributes: dict[str, object] | None = None,
) -> "Edge"
Factory method that creates a new directed edge with a fresh identity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph_id
|
GraphId
|
Owning graph. |
required |
source_id
|
NodeId
|
Source (origin) node. |
required |
target_id
|
NodeId
|
Target (destination) node. |
required |
type_name
|
str
|
Semantic type registered on the graph's
:class: |
required |
attributes
|
dict[str, object] | None
|
Optional initial attribute payload. |
None
|
Returns:
| Type | Description |
|---|---|
'Edge'
|
A new :class: |
Source code in src/knowledge_platform/core/edge.py
evolve ¶
Return a new :class:Edge with merged attribute_updates.
The version is incremented and updated_at is refreshed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**attribute_updates
|
object
|
Attribute keys/values to merge. |
{}
|
Returns:
| Type | Description |
|---|---|
'Edge'
|
Updated immutable :class: |