|
| 1 | +# FSA Validator & Analyzer |
| 2 | + |
| 3 | +A Python-based utility module for the structural validation, property analysis, and string simulation of **Finite State Automata (FSA)**. This module is designed to work with Pydantic-based FSA schemas to ensure mathematical correctness and provide detailed feedback on structural flaws. |
| 4 | + |
| 5 | +## 🚀 Key Features |
| 6 | + |
| 7 | +* **Structural Validation**: Verifies that initial states, accept states, and transitions reference defined states and alphabet symbols. |
| 8 | +* **Property Analysis**: |
| 9 | +* **Determinism**: Checks for multiple transitions from the same state on the same symbol. |
| 10 | +* **Completeness**: Ensures every state has a transition for every symbol in the alphabet. |
| 11 | + |
| 12 | + |
| 13 | +* **State Reachability**: |
| 14 | +* **Unreachable States**: Identifies states that cannot be reached from the initial state using BFS. |
| 15 | +* **Dead States**: Identifies states from which an accepting state can never be reached. |
| 16 | + |
| 17 | + |
| 18 | +* **String Simulation**: Simulates the FSA (NFA or DFA) on a given input string to determine acceptance. |
| 19 | +* **Language Equivalence**: Approximates whether two FSAs are equivalent by testing all possible strings up to a specified maximum length. |
| 20 | + |
| 21 | +--- |
| 22 | + |
| 23 | +## 🛠 Function Reference |
| 24 | + |
| 25 | +### 1. Validation & Properties |
| 26 | + |
| 27 | +| Function | Description | |
| 28 | +| --- | --- | |
| 29 | +| `is_valid_fsa(fsa)` | Returns a list of structural errors (missing states, invalid symbols). | |
| 30 | +| `is_deterministic(fsa)` | Validates if the FSA behaves as a DFA. | |
| 31 | +| `is_complete(fsa)` | Validates if the transition function is total (requires a DFA). | |
| 32 | + |
| 33 | +### 2. Graph Analysis |
| 34 | + |
| 35 | +| Function | Description | |
| 36 | +| --- | --- | |
| 37 | +| `find_unreachable_states(fsa)` | Finds states inaccessible from the start. | |
| 38 | +| `find_dead_states(fsa)` | Finds states that are "trapped" and cannot reach an accept state. | |
| 39 | + |
| 40 | +### 3. Language & Testing |
| 41 | + |
| 42 | +| Function | Description | |
| 43 | +| --- | --- | |
| 44 | +| `accepts_string(fsa, string)` | Tests if the FSA accepts a specific string. Supports non-determinism. | |
| 45 | +| `fsas_accept_same_language(fsa1, fsa2)` | Compares two FSAs for equivalence up to a specific string length. | |
| 46 | + |
| 47 | +--- |
| 48 | + |
| 49 | +## 📋 Data Structure Support |
| 50 | + |
| 51 | +The module expects an `FSA` object (typically a Pydantic model) with the following attributes: |
| 52 | + |
| 53 | +* `states`: `List[str]` |
| 54 | +* `alphabet`: `List[str]` |
| 55 | +* `initial_state`: `str` |
| 56 | +* `accept_states`: `List[str]` |
| 57 | +* `transitions`: `List[Transition]` (where transitions have `from_state`, `to_state`, and `symbol`) |
| 58 | + |
| 59 | +--- |
| 60 | + |
| 61 | +## 💡 Usage Example |
| 62 | + |
| 63 | +```python |
| 64 | +from your_module import is_valid_fsa, find_dead_states |
| 65 | + |
| 66 | +# Example: Checking for dead states |
| 67 | +errors = find_dead_states(my_fsa) |
| 68 | + |
| 69 | +for error in errors: |
| 70 | + print(f"Error Code: {error.code}") |
| 71 | + print(f"Message: {error.message}") |
| 72 | + print(f"Suggestion: {error.suggestion}") |
| 73 | + |
| 74 | +``` |
| 75 | + |
| 76 | +## ⚠️ Error Handling |
| 77 | + |
| 78 | +All functions return a `List[ValidationError]`. An empty list `[]` indicates that the check passed successfully. Each `ValidationError` object contains: |
| 79 | + |
| 80 | +* `message`: Human-readable explanation. |
| 81 | +* `code`: A unique `ErrorCode` for programmatic handling. |
| 82 | +* `severity`: "error" or "warning". |
| 83 | +* `highlight`: Metadata identifying the specific state or transition causing the issue. |
0 commit comments