babylon.engine.adapters
Graph adapters for the Babylon simulation engine.
Slice 1.7: The Graph Bridge
This package contains adapters that implement GraphProtocol for different graph backends:
NetworkXAdapter: Reference implementation using NetworkX (Epoch 1-2) ColumnarAdapter: DuckDB + DuckPGQ implementation (Epoch 3, planned)
All Systems interact with the graph through GraphProtocol, never directly with the backend. This enables backend swapping without changing System code.
- class babylon.engine.adapters.NetworkXAdapter[source]
Bases:
AggregationMixin,QueryMixinReference implementation of GraphProtocol using NetworkX.
Wraps nx.DiGraph and provides all 18 GraphProtocol methods. Node types are stored as ‘_node_type’ attribute, edge types as ‘_edge_type’.
- Inherits from mixins:
AggregationMixin: aggregate()
QueryMixin: query_nodes(), query_edges(), count_nodes(), count_edges()
Example
>>> adapter = NetworkXAdapter() >>> adapter.add_node("C001", "social_class", wealth=100.0) >>> node = adapter.get_node("C001") >>> node.wealth 100.0
- add_edge(source, target, edge_type, weight=1.0, **attributes)[source]
Add directed edge with type and weight.
- execute_traversal(query)[source]
Execute generic traversal query.
- Parameters:
query (
TraversalQuery) – TraversalQuery specifying the traversal.- Return type:
- Returns:
TraversalResult with nodes, edges, paths, or aggregates.
- Raises:
ValueError – If query_type is not supported.
- get_neighborhood(node_id, radius=1, edge_types=None, direction='out')[source]
Get all nodes within radius hops of the source node.
- Parameters:
- Return type:
- Returns:
SubgraphView containing nodes in neighborhood.
- Raises:
KeyError – If node does not exist.
- shortest_path(source, target, edge_types=None, weight_attr=None)[source]
Find shortest path between two nodes.
- property underlying_graph
Access the underlying NetworkX DiGraph.
Used by
WorldState.from_graph()to unwrap the adapter back to a raw graph, and bytopology_monitorfor NetworkX-specific algorithms (copy-and-purge resilience testing) that have no protocol equivalent.- Returns:
The raw nx.DiGraph backing this adapter.
- classmethod wrap(graph)[source]
Wrap an existing nx.DiGraph in a protocol adapter.
Normalizes edge keys: raw graphs from
WorldState.to_graph()storeedge_type(from Pydanticmodel_dump()), but the adapter uses_edge_type. This method adds_edge_typealongside existingedge_typeso protocol methods work correctly. Similarly normalizesnode_type→_node_typefor node type discrimination.The adapter holds a reference to the graph (not a copy), so mutations via protocol methods flow through to the original graph.
- Parameters:
graph – An existing NetworkX DiGraph to wrap.
- Returns:
A NetworkXAdapter backed by the given graph.
- Return type:
- class babylon.engine.adapters.SubgraphView(subgraph)[source]
Bases:
objectRead-only view of a subgraph.
Provides iteration over nodes in a subgraph returned by neighborhood queries. Wraps the underlying NetworkX subgraph to provide GraphNode iteration.
- Parameters:
subgraph (nx.Graph[str])
- _subgraph
The underlying NetworkX subgraph.
Example
>>> from babylon.engine.adapters.inmemory_adapter import NetworkXAdapter >>> adapter = NetworkXAdapter() >>> adapter.add_node("C001", "social_class", wealth=100.0) >>> adapter.add_node("C002", "social_class", wealth=50.0) >>> adapter.add_edge("C001", "C002", "SOLIDARITY") >>> view = adapter.get_neighborhood("C001") >>> list(view.nodes()) [GraphNode(...), GraphNode(...)]
- __init__(subgraph)[source]
Initialize with a NetworkX subgraph.
- Parameters:
subgraph – The NetworkX subgraph to wrap. Can be DiGraph or subgraph view.
Modules
AggregationMixin for graph aggregation operations. |
|
NetworkX-based in-memory graph adapter. |
|
QueryMixin for graph query operations. |
|
SubgraphFilterBuilder for constructing filtered subgraphs. |
|
SubgraphView for read-only graph iteration. |