|
| 1 | +from typing import List |
| 2 | +from pydantic import BaseModel, Field |
| 3 | +from .fsa import FSA, Transition |
| 4 | + |
| 5 | +# frontend zod restricts typing, this is the current workaround |
| 6 | + |
| 7 | +class FSAFrontend(BaseModel): |
| 8 | + """ |
| 9 | + Finite State Automaton representation. |
| 10 | +
|
| 11 | + Represents a 5-tuple (Q, Σ, δ, q0, F) where: |
| 12 | + - Q = states |
| 13 | + - Σ = alphabet |
| 14 | + - δ = transitions (transition function) |
| 15 | + - q0 = initial_state |
| 16 | + - F = accept_states |
| 17 | +
|
| 18 | + Example: |
| 19 | + { |
| 20 | + "states": ["q0", "q1", "q2"], |
| 21 | + "alphabet": ["a", "b"], |
| 22 | + "transitions": [ |
| 23 | + {"from_state": "q0", "to_state": "q1", "symbol": "a"}, |
| 24 | + {"from_state": "q1", "to_state": "q2", "symbol": "b"} |
| 25 | + ], |
| 26 | + "initial_state": "q0", |
| 27 | + "accept_states": ["q2"] |
| 28 | + } |
| 29 | + """ |
| 30 | + states: List[str] = Field( |
| 31 | + ..., |
| 32 | + min_length=1, |
| 33 | + description="Q: Set of all state identifiers" |
| 34 | + ) |
| 35 | + |
| 36 | + alphabet: List[str] = Field( |
| 37 | + ..., |
| 38 | + min_length=1, |
| 39 | + description="Σ: Input alphabet symbols (excluding epsilon)" |
| 40 | + ) |
| 41 | + |
| 42 | + transitions: List[str] = Field( |
| 43 | + default_factory=list, |
| 44 | + description="δ: Transition function as a list of (from_state, symbol, to_state) tuples" |
| 45 | + ) |
| 46 | + |
| 47 | + initial_state: str = Field( |
| 48 | + ..., |
| 49 | + description="q0: The starting state" |
| 50 | + ) |
| 51 | + |
| 52 | + accept_states: List[str] = Field( |
| 53 | + default_factory=list, |
| 54 | + description="F: Set of accepting/final states" |
| 55 | + ) |
| 56 | + |
| 57 | + class Config: |
| 58 | + schema_extra = { |
| 59 | + "example": { |
| 60 | + "states": ["q0", "q1", "q2"], |
| 61 | + "alphabet": ["a", "b"], |
| 62 | + "transitions": [ |
| 63 | + "q0|a|q1|", |
| 64 | + "q1|b|q2", |
| 65 | + ], |
| 66 | + "initial_state": "q0", |
| 67 | + "accept_states": ["q2"] |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @classmethod |
| 72 | + def from_flattened(cls, data: dict) -> FSA: |
| 73 | + """ |
| 74 | + Convert frontend FSA payload (with transitions as "from|symbol|to") |
| 75 | + into the FSABackend model with proper Transition objects. |
| 76 | + """ |
| 77 | + states = data.get("states", []) |
| 78 | + alphabet = data.get("alphabet", []) |
| 79 | + initial_state = data.get("initial_state", "q0") |
| 80 | + accept_states = data.get("accept_states", []) |
| 81 | + |
| 82 | + flat_transitions = data.get("transitions", []) |
| 83 | + transitions: List[Transition] = [] |
| 84 | + for t in flat_transitions: |
| 85 | + try: |
| 86 | + from_state, symbol, to_state = t.split("|") |
| 87 | + transitions.append( |
| 88 | + Transition(from_state=from_state, symbol=symbol, to_state=to_state) |
| 89 | + ) |
| 90 | + except ValueError: |
| 91 | + raise ValueError(f"Invalid transition format: '{t}'") |
| 92 | + |
| 93 | + return FSA( |
| 94 | + states=states, |
| 95 | + alphabet=alphabet, |
| 96 | + transitions=transitions, |
| 97 | + initial_state=initial_state, |
| 98 | + accept_states=accept_states, |
| 99 | + ) |
0 commit comments