|
| 1 | +"""Validation utilities for `ProcessSpec` objects. |
| 2 | +
|
| 3 | +Provides functions to validate process topology including: |
| 4 | +- Checking that all component inputs are connected |
| 5 | +- Checking that input events have matching output event producers |
| 6 | +- Checking for circular connections that require initial values |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +from collections import defaultdict |
| 12 | +import typing as _t |
| 13 | + |
| 14 | +from ._graph import simple_cycles |
| 15 | + |
| 16 | + |
| 17 | +if _t.TYPE_CHECKING: |
| 18 | + from .component import ComponentSpec |
| 19 | + from .connector import ConnectorSpec |
| 20 | + |
| 21 | + |
| 22 | +class ValidationError(Exception): |
| 23 | + """Raised when a process specification fails validation.""" |
| 24 | + |
| 25 | + pass |
| 26 | + |
| 27 | + |
| 28 | +def _build_component_graph( |
| 29 | + connectors: list[ConnectorSpec], |
| 30 | +) -> dict[str, set[str]]: |
| 31 | + """Build a directed graph of component connections from connector specs. |
| 32 | +
|
| 33 | + Args: |
| 34 | + connectors: List of connector specifications. |
| 35 | +
|
| 36 | + Returns: |
| 37 | + A dictionary mapping source component names to sets of target component names. |
| 38 | + """ |
| 39 | + graph: dict[str, set[str]] = defaultdict(set) |
| 40 | + for conn in connectors: |
| 41 | + source_entity = conn.source.entity |
| 42 | + target_entity = conn.target.entity |
| 43 | + if source_entity != target_entity: |
| 44 | + graph[source_entity].add(target_entity) |
| 45 | + # Ensure target is in graph even with no outgoing edges |
| 46 | + if target_entity not in graph: |
| 47 | + graph[target_entity] = set() |
| 48 | + return dict(graph) |
| 49 | + |
| 50 | + |
| 51 | +def _get_edges_in_cycle( |
| 52 | + cycle: list[str], |
| 53 | + connectors: list[ConnectorSpec], |
| 54 | +) -> list[ConnectorSpec]: |
| 55 | + """Get all connector specs that form edges within a cycle. |
| 56 | +
|
| 57 | + Args: |
| 58 | + cycle: List of component names forming a cycle. |
| 59 | + connectors: All connector specifications. |
| 60 | +
|
| 61 | + Returns: |
| 62 | + List of connector specs that are part of the cycle. |
| 63 | + """ |
| 64 | + cycle_edges: list[ConnectorSpec] = [] |
| 65 | + cycle_set = set(cycle) |
| 66 | + for i, node in enumerate(cycle): |
| 67 | + next_node = cycle[(i + 1) % len(cycle)] |
| 68 | + for conn in connectors: |
| 69 | + if conn.source.entity == node and conn.target.entity == next_node: |
| 70 | + cycle_edges.append(conn) |
| 71 | + return [c for c in cycle_edges if c.source.entity in cycle_set and c.target.entity in cycle_set] |
| 72 | + |
| 73 | + |
| 74 | +def validate_all_inputs_connected( |
| 75 | + components: dict[str, dict[str, _t.Any]], |
| 76 | + connectors: list[ConnectorSpec], |
| 77 | +) -> list[str]: |
| 78 | + """Check that all component inputs are connected. |
| 79 | +
|
| 80 | + Args: |
| 81 | + components: Dictionary mapping component names to their IO info. |
| 82 | + Each value must have an ``"inputs"`` key with a list of input field names. |
| 83 | + connectors: List of connector specifications. |
| 84 | +
|
| 85 | + Returns: |
| 86 | + List of error messages for unconnected inputs. |
| 87 | + """ |
| 88 | + # Build mapping of which component inputs are connected |
| 89 | + connected_inputs: dict[str, set[str]] = defaultdict(set) |
| 90 | + for conn in connectors: |
| 91 | + target_name = conn.target.entity |
| 92 | + target_field = conn.target.descriptor |
| 93 | + connected_inputs[target_name].add(target_field) |
| 94 | + |
| 95 | + errors: list[str] = [] |
| 96 | + for comp_name, comp_info in components.items(): |
| 97 | + all_inputs = set(comp_info.get("inputs", [])) |
| 98 | + connected = connected_inputs.get(comp_name, set()) |
| 99 | + unconnected = all_inputs - connected |
| 100 | + if unconnected: |
| 101 | + errors.append(f"Component '{comp_name}' has unconnected inputs: {sorted(unconnected)}") |
| 102 | + return errors |
| 103 | + |
| 104 | + |
| 105 | +def validate_input_events( |
| 106 | + components: dict[str, dict[str, _t.Any]], |
| 107 | +) -> list[str]: |
| 108 | + """Check that all components with input events have a matching output event producer. |
| 109 | +
|
| 110 | + Args: |
| 111 | + components: Dictionary mapping component names to their IO info. |
| 112 | + Each value must have ``"input_events"`` and ``"output_events"`` keys |
| 113 | + with lists of event type strings. |
| 114 | +
|
| 115 | + Returns: |
| 116 | + List of error messages for unmatched input events. |
| 117 | + """ |
| 118 | + # Collect all output event types across all components |
| 119 | + all_output_events: set[str] = set() |
| 120 | + for comp_info in components.values(): |
| 121 | + all_output_events.update(comp_info.get("output_events", [])) |
| 122 | + |
| 123 | + errors: list[str] = [] |
| 124 | + for comp_name, comp_info in components.items(): |
| 125 | + input_events = set(comp_info.get("input_events", [])) |
| 126 | + unmatched = input_events - all_output_events |
| 127 | + if unmatched: |
| 128 | + errors.append( |
| 129 | + f"Component '{comp_name}' has input events with no producer: {sorted(unmatched)}" |
| 130 | + ) |
| 131 | + return errors |
| 132 | + |
| 133 | + |
| 134 | +def validate_no_unresolved_cycles( |
| 135 | + components: list[ComponentSpec], |
| 136 | + connectors: list[ConnectorSpec], |
| 137 | +) -> list[str]: |
| 138 | + """Check for circular connections that are not resolved by initial values. |
| 139 | +
|
| 140 | + Circular loops are only valid if there are ``initial_values`` set on an |
| 141 | + appropriate component input within the loop. |
| 142 | +
|
| 143 | + Args: |
| 144 | + components: List of component specifications. |
| 145 | + connectors: List of connector specifications. |
| 146 | +
|
| 147 | + Returns: |
| 148 | + List of error messages for unresolved circular connections. |
| 149 | + """ |
| 150 | + graph = _build_component_graph(connectors) |
| 151 | + if not graph: |
| 152 | + return [] |
| 153 | + |
| 154 | + # Build lookup of component initial_values by name |
| 155 | + initial_values_by_comp: dict[str, set[str]] = {} |
| 156 | + for comp in components: |
| 157 | + if comp.args.initial_values: |
| 158 | + initial_values_by_comp[comp.args.name] = set(comp.args.initial_values.keys()) |
| 159 | + |
| 160 | + errors: list[str] = [] |
| 161 | + for cycle in simple_cycles(graph): |
| 162 | + # Check if any edge in the cycle targets a component input with initial_values |
| 163 | + cycle_edges = _get_edges_in_cycle(cycle, connectors) |
| 164 | + cycle_resolved = False |
| 165 | + for edge in cycle_edges: |
| 166 | + target_comp = edge.target.entity |
| 167 | + target_field = edge.target.descriptor |
| 168 | + if target_comp in initial_values_by_comp: |
| 169 | + if target_field in initial_values_by_comp[target_comp]: |
| 170 | + cycle_resolved = True |
| 171 | + break |
| 172 | + if not cycle_resolved: |
| 173 | + cycle_str = " -> ".join(cycle + [cycle[0]]) |
| 174 | + errors.append( |
| 175 | + f"Circular connection detected without initial values: {cycle_str}. " |
| 176 | + f"Set initial_values on a component input within the loop to resolve." |
| 177 | + ) |
| 178 | + return errors |
0 commit comments