Core primitives
The drm.base module defines the low-level graph primitives used
throughout the library:
Nodefor root entitiesWeakNodefor hierarchical child entitiesRelationfor typed edgesWeakRelationfor parent-child cascade relations
These classes are the foundation for both backends and for all semantic
entities defined in drm.drm_entities.
See also drm.networkx_graph and drm.neo4j_graph for the concrete
backends that consume these primitives.
Core data structures: Node, Relation, WeakNode, WeakRelation.
These classes represent the graph primitives used by both Neo4jGraph
and NetworkXGraph. Nodes carry a primary key (pk), a main label,
optional alternative labels, and optional parent relationships for
WeakNode hierarchies. Relations connect two nodes with a typed edge.
- class drm.base.Node(pk=<object object>, main_label='', alternative_labels=None, version=5, neo4j_id=None, **kwargs)[source]
Bases:
objectA graph node with a primary key, labels, and optional parent.
Nodes are the fundamental building blocks of the DRM graph. Each node has a
main_label(the primary Cypher label), optionalalternative_labels, and apk(primary key) that uniquely identifies it within its label.When a
parentis provided the node becomes a WeakNode: its primary key is merged with the parent’s key to form a composite key, and a typed edge is created when the node is inserted into a graph store.By default a node must have a
pkor aneo4j_id(or both). If the caller passespk=Noneexplicitly, the node is created with_primary_key = Noneand the backend is expected to assign a real ID later (e.g. Neo4j generates an internal node ID, which is then stored as_primary_key). If the backend never assigns one,_primary_keyremainsNone.- Parameters:
pk (Dict[str, int | str] | None | object) – Primary key — an int is converted to
{"id": pk}, a dict is used as-is. Must be provided (or explicitlyNone) unlessneo4j_idis given.main_label (str) – The primary label used in Cypher queries.
alternative_labels (str | List[str] | None) – Additional labels attached to the node.
version (int) – Neo4j protocol version (default 5).
neo4j_id (int | None) – Internal Neo4j node ID (used when reconstructing a node from a database result).
**kwargs (Any) – Arbitrary additional attributes stored on the node. Special kwargs:
parent(Node),parent_relation(str),is_weak(bool),_propagate(bool),dependencies.
- Raises:
ValueError – If
pkis not provided at all (not even asNone) andneo4j_idis also absent.TypeError – If
pkis neither anintnor adict(whenpkis provided but notNone).
- class drm.base.Relation(src, dst, rel_type, **kwargs)[source]
Bases:
objectA typed edge connecting two nodes in the graph.
Relations store the primary keys of their source and destination nodes and can carry arbitrary edge properties.
- Parameters:
- class drm.base.WeakNode(**kwargs)[source]
Bases:
NodeA node whose identity is tied to its parent node.
WeakNodes form a parent-child hierarchy where the child’s primary key is merged with the parent’s key to produce a composite key. This models document structures such as Document → Section → Page where a child cannot exist without its parent.
When a WeakNode is inserted into a graph store, a typed edge (
WeakRelation) is automatically created linking the parent to the child. This edge carries the_propagate=TRUEflag, which triggers cascade delete: deleting the parent automatically deletes all descendants in the hierarchy.- Parameters:
parent – The parent
Node. Must not be None.**kwargs (Any) – Forwarded to
Node.__init__. Common kwargs includepk(child’s primary key),main_label,alternative_labels,parent_relation(default"HAS"), and_propagate.
- Raises:
AssertionError – If
parentis not aNodeinstance.
- __init__(**kwargs)[source]
Initialize a WeakNode tied to a parent node.
- Parameters:
parent – The parent
Node. Required — must be passed as a kwarg.**kwargs (Any) – Passed to
Node.__init__(pk,main_label,alternative_labels,parent_relation,_propagate, etc.).
- Raises:
AssertionError – If
parentis not provided or is not aNodeinstance.- Return type:
None
- class drm.base.WeakRelation(src, dst, rel_type, **kwargs)[source]
Bases:
RelationA typed edge connecting a parent node to its child (WeakNode).
WeakRelations are automatically created when a WeakNode is inserted. They carry the
_propagate=TRUEflag, which signals to the graph store that deleting the parent should cascade to the child node.- Parameters:
- _propagate
Always
Trueby default. Indicates that deleting the source node should cascade delete to the destination node.