babylon.engine.adapters.subgraph_filter
SubgraphFilterBuilder for constructing filtered subgraphs.
Extracted from inmemory_adapter._build_filtered_subgraph to reduce cyclomatic complexity and improve testability.
Uses the Builder pattern to chain filter operations.
Classes
|
Builds filtered subgraphs from NetworkX DiGraphs. |
- class babylon.engine.adapters.subgraph_filter.SubgraphFilterBuilder(graph)[source]
Bases:
objectBuilds filtered subgraphs from NetworkX DiGraphs.
Uses the Builder pattern to apply node and edge filters incrementally. Each filter method returns self for method chaining.
Example
>>> builder = SubgraphFilterBuilder(graph) >>> subgraph = ( ... builder ... .with_nodes({"C001", "C002"}) ... .with_node_types({"social_class"}) ... .with_edge_types({"SOLIDARITY"}) ... .build() ... )
- Parameters:
graph (nx.DiGraph[str])
- __init__(graph)[source]
Initialize builder with source graph.
- Parameters:
graph – The NetworkX DiGraph to filter.
- with_nodes(nodes)[source]
Filter to specific node IDs.
- with_node_types(node_types)[source]
Filter nodes by type.
- with_edge_types(edge_types)[source]
Filter edges by type.
- with_weight_range(min_weight=None, max_weight=None)[source]
Filter edges by weight range.
- Parameters:
- Return type:
- Returns:
Self for method chaining.
- from_query(query, include_all_nodes=False)[source]
Configure builder from a TraversalQuery.
- Parameters:
query (
TraversalQuery) – TraversalQuery with filters to apply.include_all_nodes (
bool) – If True, include all nodes regardless of start_nodes. Used for BFS/DFS where start_nodes are starting points, not filters.
- Return type:
- Returns:
Self for method chaining.