babylon.engine.adapters.inmemory_adapter
NetworkX-based in-memory graph adapter.
Slice 1.7: The Graph Bridge
The NetworkXAdapter is the reference implementation of GraphProtocol using NetworkX. It wraps nx.DiGraph and provides the standard interface for all graph operations.
- Key Implementation Details:
Node types stored as ‘_node_type’ attribute (internal)
Edge types stored as ‘_edge_type’ attribute (internal)
Internal attributes (prefixed with ‘_’) are excluded from user-facing data
Thread-safe for read operations (NetworkX is NOT thread-safe for writes)
This adapter is the MVP implementation for Epoch 1 and 2. DuckDB adapter will be added in Epoch 3 for 1000+ node graphs.
Classes
Reference implementation of GraphProtocol using NetworkX. |
- class babylon.engine.adapters.inmemory_adapter.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
- 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:
- 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.
- add_edge(source, target, edge_type, weight=1.0, **attributes)[source]
Add directed edge with type and weight.
- 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.
- 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.
- shortest_path(source, target, edge_types=None, weight_attr=None)[source]
Find shortest path between two nodes.