-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresult.py
More file actions
324 lines (268 loc) · 10.3 KB
/
result.py
File metadata and controls
324 lines (268 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
"""
Result Schema
Extended result schema with structured feedback for UI highlighting.
"""
from typing import List, Optional, Literal
from enum import Enum
from pydantic import BaseModel, Field
from evaluation_function.schemas.fsa import FSA
class ErrorCode(str, Enum):
"""
Standardized error codes for FSA validation.
These codes allow the frontend to programmatically handle specific error types.
"""
# Structural errors - invalid elements
INVALID_STATE = "INVALID_STATE"
INVALID_INITIAL = "INVALID_INITIAL"
INVALID_ACCEPT = "INVALID_ACCEPT"
INVALID_SYMBOL = "INVALID_SYMBOL"
# Transition errors
INVALID_TRANSITION_SOURCE = "INVALID_TRANSITION_SOURCE"
INVALID_TRANSITION_DEST = "INVALID_TRANSITION_DEST"
INVALID_TRANSITION_SYMBOL = "INVALID_TRANSITION_SYMBOL"
MISSING_TRANSITION = "MISSING_TRANSITION"
DUPLICATE_TRANSITION = "DUPLICATE_TRANSITION"
# Reachability errors
UNREACHABLE_STATE = "UNREACHABLE_STATE"
DEAD_STATE = "DEAD_STATE"
# Type errors
WRONG_AUTOMATON_TYPE = "WRONG_AUTOMATON_TYPE"
NOT_DETERMINISTIC = "NOT_DETERMINISTIC"
NOT_COMPLETE = "NOT_COMPLETE"
NOT_MINIMAL = "NOT_MINIMAL"
# Language errors
LANGUAGE_MISMATCH = "LANGUAGE_MISMATCH"
TEST_CASE_FAILED = "TEST_CASE_FAILED"
# General errors
EMPTY_STATES = "EMPTY_STATES"
EMPTY_ALPHABET = "EMPTY_ALPHABET"
EVALUATION_ERROR = "EVALUATION_ERROR"
class ElementHighlight(BaseModel):
"""
Specifies which FSA element to highlight in the UI.
The frontend can use this to visually indicate errors on specific
states, transitions, or other FSA components.
"""
type: Literal["state", "transition", "initial_state", "accept_state", "alphabet_symbol"] = Field(
...,
description="Type of FSA element to highlight"
)
# For state-related highlights
state_id: Optional[str] = Field(
default=None,
description="State identifier (for type='state', 'initial_state', 'accept_state')"
)
# For transition-related highlights
from_state: Optional[str] = Field(
default=None,
description="Source state of transition (for type='transition')"
)
to_state: Optional[str] = Field(
default=None,
description="Destination state of transition (for type='transition')"
)
symbol: Optional[str] = Field(
default=None,
description="Transition symbol (for type='transition' or 'alphabet_symbol')"
)
class ValidationError(BaseModel):
"""
A validation error with precise location for UI highlighting.
Example - Missing transition:
{
"message": "Missing transition from state 'q0' on symbol 'a'",
"code": "MISSING_TRANSITION",
"severity": "error",
"highlight": {
"type": "state",
"state_id": "q0"
},
"suggestion": "Add a transition from 'q0' on input 'a'"
}
Example - Invalid transition:
{
"message": "Transition points to non-existent state 'q5'",
"code": "INVALID_TRANSITION_DEST",
"severity": "error",
"highlight": {
"type": "transition",
"from_state": "q0",
"to_state": "q5",
"symbol": "a"
},
"suggestion": "Change destination to an existing state or add state 'q5'"
}
"""
message: str = Field(..., description="Human-readable error message")
code: ErrorCode = Field(
...,
description="Standardized error code from ErrorCode enum"
)
severity: Literal["error", "warning", "info"] = Field(
default="error",
description="Severity level of the issue"
)
highlight: Optional[ElementHighlight] = Field(
default=None,
description="FSA element to highlight in the UI"
)
suggestion: Optional[str] = Field(
default=None,
description="Actionable suggestion for fixing the error"
)
class TestResult(BaseModel):
"""
Result of evaluating the FSA on a single test case.
Shows whether the FSA correctly accepts/rejects a specific input string.
"""
input: str = Field(..., description="The test input string")
expected: bool = Field(..., description="Expected result: true=accept, false=reject")
actual: bool = Field(..., description="Actual result from student's FSA")
passed: bool = Field(..., description="Whether the test passed (actual == expected)")
trace: Optional[List[str]] = Field(
default=None,
description="State sequence during FSA execution (for debugging)"
)
class StructuralInfo(BaseModel):
"""
Structural properties and analysis of the FSA.
Includes information about determinism, completeness, and reachability.
"""
is_deterministic: bool = Field(
...,
description="True if FSA is deterministic (no epsilon transitions, single transition per (state, symbol))"
)
is_complete: bool = Field(
...,
description="True if DFA has transitions defined for all (state, symbol) pairs"
)
num_states: int = Field(..., ge=0, description="Number of states in the FSA")
num_transitions: int = Field(..., ge=0, description="Number of transitions in the FSA")
unreachable_states: List[str] = Field(
default_factory=list,
description="States that cannot be reached from the initial state"
)
dead_states: List[str] = Field(
default_factory=list,
description="States from which no accepting state is reachable"
)
class LanguageComparison(BaseModel):
"""
Result of comparing the student's FSA language with the expected language.
Indicates whether the languages are equivalent and provides a counterexample if not.
"""
are_equivalent: bool = Field(
...,
description="True if student FSA accepts the same language as expected"
)
counterexample: Optional[str] = Field(
default=None,
description="A string where student FSA differs from expected (if languages not equivalent)"
)
counterexample_type: Optional[Literal["should_accept", "should_reject"]] = Field(
default=None,
description="Whether the counterexample should be accepted or rejected"
)
class FSAFeedback(BaseModel):
"""
Structured feedback for FSA evaluation with UI highlighting support.
Contains detailed analysis, errors with element references for highlighting,
and hints for improvement.
"""
summary: str = Field(
default="",
description="Brief summary of the evaluation result"
)
errors: List[ValidationError] = Field(
default_factory=list,
description="List of errors with element references for UI highlighting"
)
warnings: List[ValidationError] = Field(
default_factory=list,
description="List of non-critical warnings"
)
structural: Optional[StructuralInfo] = Field(
default=None,
description="Structural analysis of the FSA (determinism, completeness, etc.)"
)
language: Optional[LanguageComparison] = Field(
default=None,
description="Language equivalence comparison with counterexample if applicable"
)
test_results: List[TestResult] = Field(
default_factory=list,
description="Results of individual test cases"
)
hints: List[str] = Field(
default_factory=list,
description="Helpful hints for improving the FSA"
)
class Result(BaseModel):
"""
Complete evaluation result for FSA validation.
Contains overall correctness, feedback message, optional score,
and structured feedback for UI integration.
Example response:
{
"is_correct": false,
"feedback": "Your FSA rejects 'ab' but it should accept it.",
"score": 0.75,
"fsa_feedback": {
"summary": "Language mismatch - incorrect behavior on some inputs",
"errors": [
{
"message": "Transition from q0 on 'a' goes to wrong state",
"code": "INCORRECT_TRANSITION",
"severity": "error",
"highlight": {
"type": "transition",
"from_state": "q0",
"to_state": "q2",
"symbol": "a"
},
"suggestion": "This transition should go to q1"
}
],
"language": {
"are_equivalent": false,
"counterexample": "ab",
"counterexample_type": "should_accept"
},
"structural": {
"is_deterministic": true,
"is_complete": true,
"num_states": 3,
"num_transitions": 6,
"unreachable_states": [],
"dead_states": []
},
"test_results": [
{"input": "ab", "expected": true, "actual": false, "passed": false}
],
"hints": ["Try tracing through your FSA with the string 'ab'"]
}
}
"""
is_correct: bool = Field(
default=False,
description="Overall correctness: true if FSA is correct, false otherwise"
)
feedback: str = Field(
default="",
description="Human-readable feedback message for the student"
)
score: Optional[float] = Field(
default=None,
ge=0.0,
le=1.0,
description="Normalized score (0.0-1.0) for partial credit, null if not using partial credit"
)
fsa_feedback: Optional[FSAFeedback] = Field(
default=None,
description="Detailed structured feedback with element highlighting for UI"
)
# this is dev only
input_data: Optional[FSA] = Field(
default=None,
description="The parsed FSA input data (for development/debugging purposes only)"
)