babylon.models.graph
Graph abstraction layer data models.
Slice 1.7: The Graph Bridge
These Pydantic models define the type-safe boundary between Systems and the graph backend. All models are frozen (immutable) for thread safety.
- Models:
EdgeFilter: Filter specification for edge traversal NodeFilter: Filter specification for node inclusion GraphNode: Frozen Pydantic model for node representation GraphEdge: Frozen Pydantic model for edge representation TraversalQuery: Generic traversal query specification TraversalResult: Result of traversal query execution
- Design Principles:
Backend-agnostic: Works with NetworkX now, DuckDB later
Set-oriented: Think in tables, not just objects (DuckDB-ready)
Minimal but complete: Just enough to cover all System needs
Lazy evaluation: Return iterators/generators where possible
Classes
|
Filter specification for edge traversal. |
|
Type-safe graph edge representation. |
|
Type-safe graph node representation. |
|
Filter specification for node inclusion. |
|
Generic traversal query specification. |
|
Result of traversal query execution. |
- class babylon.models.graph.EdgeFilter(**data)[source]
Bases:
BaseModelFilter specification for edge traversal.
Used to select which edges to traverse during graph operations. All fields are optional - None means no filter (match all).
- edge_types
Set of edge types to include (None = all types).
- min_weight
Minimum weight threshold (inclusive).
- max_weight
Maximum weight threshold (inclusive).
Example
>>> f = EdgeFilter(edge_types={"SOLIDARITY"}, min_weight=0.5) >>> f.edge_types {'SOLIDARITY'}
- model_config: ClassVar[ConfigDict] = {'frozen': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- edge_types: set[str] | None
- min_weight: float | None
- max_weight: float | None
- class babylon.models.graph.NodeFilter(**data)[source]
Bases:
BaseModelFilter specification for node inclusion.
Used to select which nodes to include during graph operations. All fields are optional - None means no filter (match all).
- node_types
Set of node types to include (None = all types).
- attribute_predicates
Dict of attribute equality filters.
Example
>>> f = NodeFilter(node_types={"social_class"}) >>> f.node_types {'social_class'}
- model_config: ClassVar[ConfigDict] = {'frozen': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- node_types: set[str] | None
- attribute_predicates: dict[str, Any] | None
- class babylon.models.graph.GraphNode(**data)[source]
Bases:
BaseModelType-safe graph node representation.
Frozen Pydantic model for node data at the protocol boundary. Adapters convert to/from their internal representation.
- Parameters:
- id
Unique node identifier.
- node_type
Discriminator for polymorphism (e.g., ‘social_class’, ‘territory’).
- attributes
Type-specific attributes stored as dict.
Example
>>> node = GraphNode(id="C001", node_type="social_class", attributes={"wealth": 100.0}) >>> node.wealth 100.0
- model_config: ClassVar[ConfigDict] = {'frozen': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- id: str
- node_type: str
- attributes: dict[str, Any]
- class babylon.models.graph.GraphEdge(**data)[source]
Bases:
BaseModelType-safe graph edge representation.
Frozen Pydantic model for edge data at the protocol boundary. Adapters convert to/from their internal representation.
- Parameters:
- source_id
Origin node ID.
- target_id
Destination node ID.
- edge_type
Edge category (e.g., ‘SOLIDARITY’, ‘EXPLOITATION’).
- weight
Generic weight (default 1.0).
- attributes
Type-specific attributes stored as dict.
Example
>>> edge = GraphEdge(source_id="C001", target_id="C002", edge_type="SOLIDARITY") >>> edge.weight 1.0
- model_config: ClassVar[ConfigDict] = {'frozen': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- source_id: str
- target_id: str
- edge_type: str
- weight: float
- attributes: dict[str, Any]
- class babylon.models.graph.TraversalQuery(**data)[source]
Bases:
BaseModelGeneric traversal query specification.
Specifies what traversal to execute and what to collect. This is the generic hook for complex operations like percolation analysis, pathfinding, and component detection.
- Parameters:
query_type (Literal['bfs', 'dfs', 'shortest_path', 'connected_components', 'percolation', 'reachability'])
edge_filter (EdgeFilter | None)
node_filter (NodeFilter | None)
max_depth (int | None)
- query_type
Type of traversal algorithm to use.
- start_nodes
Starting node IDs (None = all nodes).
- target_nodes
Target node IDs (for shortest_path, reachability).
- edge_filter
Filter which edges to traverse.
- node_filter
Filter which nodes to include.
- max_depth
Maximum traversal depth.
- collect
What to include in result.
Example
>>> query = TraversalQuery(query_type="bfs", start_nodes=["C001"]) >>> query.query_type 'bfs'
- model_config: ClassVar[ConfigDict] = {'frozen': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- query_type: Literal['bfs', 'dfs', 'shortest_path', 'connected_components', 'percolation', 'reachability']
- start_nodes: list[str] | None
- target_nodes: list[str] | None
- edge_filter: EdgeFilter | None
- node_filter: NodeFilter | None
- max_depth: int | None
- collect: list[str]
- class babylon.models.graph.TraversalResult(**data)[source]
Bases:
BaseModelResult of traversal query execution.
Contains all data collected during traversal based on query.collect.
- Parameters:
- nodes
Node IDs in traversal result.
- edges
Edge tuples (source, target, type) in result.
- paths
Paths found (for shortest_path queries).
- components
Connected components (for component queries).
- component_sizes
Sizes of connected components (sorted descending).
- metadata
Query-specific metadata.
Example
>>> result = TraversalResult(component_sizes=[10, 5, 3]) >>> result.largest_component_size 10
- model_config: ClassVar[ConfigDict] = {'frozen': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- nodes: list[str]
- edges: list[tuple[str, str, str]]
- paths: list[list[str]]
- components: list[list[str]]
- component_sizes: list[int]
- metadata: dict[str, Any]