Commit deee3a75 authored by Vladimír Štill's avatar Vladimír Štill
Browse files

evalweb: Fix error handling in convert

parent f629eae4
...@@ -68,10 +68,11 @@ def compare() -> str: ...@@ -68,10 +68,11 @@ def compare() -> str:
error="Nebyl zadán vstupní formalismus.") error="Nebyl zadán vstupní formalismus.")
checker = WebChecker(student_string=student_string, task=student_type) checker = WebChecker(student_string=student_string, task=student_type)
result = checker.compare(teacher_string=teacher_string, try:
teacher_type=teacher_type) result = checker.compare(teacher_string=teacher_string,
if not isinstance(result, bool): teacher_type=teacher_type)
return render_template('parsing_error.html') except ParsingError as ex:
return render_template('parsing_error.html', error=str(ex))
extra_word_ce, missing_word_ce, inf = None, None, None extra_word_ce, missing_word_ce, inf = None, None, None
if not result and checker.eq is not None: # languages not equivalent if not result and checker.eq is not None: # languages not equivalent
...@@ -113,23 +114,26 @@ def compare() -> str: ...@@ -113,23 +114,26 @@ def compare() -> str:
def convert() -> str: def convert() -> str:
student_form = TypeForm(prefix='student_form') student_form = TypeForm(prefix='student_form')
task_form = ConvertForm(prefix='task_form') task_form = ConvertForm(prefix='task_form')
student_area = "" convert_args = {"student_form": student_form,
"task_form": task_form,
"student_area": ""}
if request.method == 'POST' and 'submit_button' in request.form: if request.method == 'POST' and 'submit_button' in request.form:
student_string = request.form['student_string'] student_string = request.form['student_string']
student_type = student_form.make.data student_type = student_form.make.data
task = task_form.make.data task = task_form.make.data
student_area = student_string convert_args["student_area"] = student_string
if student_string == "": if student_string == "":
return render_template('parsing_error.html', return render_template('convert.html',
error="Nebyl zadán vstupní formalismus.") error="Nebyl zadán vstupní formalismus.",
**convert_args)
checker = WebChecker(student_string=student_string, task=task) checker = WebChecker(student_string=student_string, task=task)
try: try:
output = checker.convert(student_type=student_type) output = checker.convert(student_type=student_type)
except ParsingError: except ParsingError as ex:
return render_template('parsing_error.html') return render_template('convert.html', error=ex, **convert_args)
student = Language(string=student_string, task=student_type) student = Language(string=student_string, task=student_type)
is_task = student.gen_is_task(task=task) is_task = student.gen_is_task(task=task)
...@@ -144,10 +148,9 @@ def convert() -> str: ...@@ -144,10 +148,9 @@ def convert() -> str:
if request.method == 'POST' and 'example_button' in request.form: if request.method == 'POST' and 'example_button' in request.form:
student_type = student_form.make.data student_type = student_form.make.data
if student_type in convert_examples: if student_type in convert_examples:
student_area = convert_examples[student_type] convert_args["student_area"] = convert_examples[student_type]
return render_template('convert.html', student_form=student_form, return render_template('convert.html', **convert_args)
task_form=task_form, student_area=student_area)
@bp.route('/userref') @bp.route('/userref')
......
...@@ -168,3 +168,8 @@ code, pre { ...@@ -168,3 +168,8 @@ code, pre {
pre code { pre code {
padding: 0; padding: 0;
} }
.error {
background-color: #f99;
padding: 0.4rem;
}
...@@ -16,6 +16,11 @@ ...@@ -16,6 +16,11 @@
<div> <div>
<div style="width:400px; float:right;"> <div style="width:400px; float:right;">
<textarea style="width:400px;" name="student_string" id="student_string">{{ student_area }}</textarea> <textarea style="width:400px;" name="student_string" id="student_string">{{ student_area }}</textarea>
{% if error is defined %}
<div class="error">
{{error}}
</div>
{% endif %}
</div> </div>
{{ student_form.hidden_tag() }} {{ student_form.hidden_tag() }}
<div style="float:left;">{{ student_form.make(style="list-style:none") }}</div> <div style="float:left;">{{ student_form.make(style="list-style:none") }}</div>
......
...@@ -67,14 +67,10 @@ class WebChecker: ...@@ -67,14 +67,10 @@ class WebChecker:
# nearly equivalent to dfa_task in fja_checker (for IS ROPOTs) # nearly equivalent to dfa_task in fja_checker (for IS ROPOTs)
def dfa_task(self, teacher_type: str, teacher_string: str, def dfa_task(self, teacher_type: str, teacher_string: str,
task: str, student_string: str) -> bool: task: str, student_string: str) -> bool:
try: teacher = Language(string=teacher_string, task=teacher_type)
teacher = Language(string=teacher_string, task=teacher_type) student = Language(string=student_string, task=self.task)
student = Language(string=student_string, task=self.task) # only for task checking
# only for task checking student_solution = transform(student_string, task)
student_solution = transform(student_string, task)
except ParsingError as ex: # Parsing error, don't continue
return ex.args
self.teacher = teacher self.teacher = teacher
self.student = student self.student = student
...@@ -109,48 +105,34 @@ class WebChecker: ...@@ -109,48 +105,34 @@ class WebChecker:
return False return False
def compare(self, teacher_string: str, teacher_type: str) -> bool: def compare(self, teacher_string: str, teacher_type: str) -> bool:
# signal.alarm(50) # from fja_checker: here would kill the web return self.dfa_task(teacher_type=teacher_type,
try: teacher_string=teacher_string,
return self.dfa_task(teacher_type=teacher_type, task=self.task,
teacher_string=teacher_string, student_string=self.student_string)
task=self.task,
student_string=self.student_string)
except ParsingError as ex:
return ex.args
# raise ParsingError(ex.args)
def convert(self, student_type: str) -> str: def convert(self, student_type: str) -> str:
try: parser = Parser()
parser = Parser() if self.task in {"DFA", "TOT", "MIN"}:
if self.task in {"DFA", "TOT", "MIN"}: dfa = dfa_transform(self.student_string, student_type)
dfa = dfa_transform(self.student_string, student_type)
if self.task == "DFA":
if self.task == "DFA": return parser.dfa_to_str(dfa.canonize())
return parser.dfa_to_str(dfa.canonize()) if self.task == "TOT":
if self.task == "TOT": return parser.dfa_to_str(dfa.total().canonize())
return parser.dfa_to_str(dfa.total().canonize()) if self.task == "MIN":
if self.task == "MIN": return parser.dfa_to_str(dfa.minimize().canonize())
return parser.dfa_to_str(dfa.minimize().canonize())
nfa = nfa_transform(self.student_string, student_type)
nfa = nfa_transform(self.student_string, student_type) if self.task == "EFA":
if self.task == "EFA": return parser.nfa_to_str(nfa)
return parser.nfa_to_str(nfa)
nfa = nfa.eliminate_epsilon()
nfa = nfa.eliminate_epsilon() if self.task == "NFA":
if self.task == "NFA": return parser.nfa_to_str(nfa)
return parser.nfa_to_str(nfa)
if self.task == "GRA":
if self.task == "GRA": return parser.reggrammar_to_str(nfa.nfa_to_reggrammar()
return parser.reggrammar_to_str(nfa.nfa_to_reggrammar() .eliminate_useless())
.eliminate_useless())
except ParsingError as ex:
return ex.args # FIXME
# raise ParsingError(ex.args)
except Exception as ex:
print("Error inside of web checker:", ex.args)
def relation(self, eq: bool) -> str: def relation(self, eq: bool) -> str:
assert self.teacher is not None assert self.teacher is not None
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment