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

evalweb: Improve error handling in compare

parent deee3a75
Loading
Loading
Loading
Loading
Loading
+22 −5
Original line number Original line Diff line number Diff line
@@ -2,8 +2,9 @@ from flask import Blueprint, render_template, request
from flask_wtf import FlaskForm  # type: ignore
from flask_wtf import FlaskForm  # type: ignore
from wtforms import RadioField   # type: ignore
from wtforms import RadioField   # type: ignore
from lib.parsing.parser import ParsingError
from lib.parsing.parser import ParsingError
from evalweb.web_checker import WebChecker, Language
from evalweb.web_checker import WebChecker, Language, CompositeException
from evalweb.examples import examples, convert_examples
from evalweb.examples import examples, convert_examples
from typing import Optional




bp = Blueprint('eval', __name__, url_prefix='/reg')
bp = Blueprint('eval', __name__, url_prefix='/reg')
@@ -64,15 +65,31 @@ def compare() -> str:
        teacher_area = teacher_string
        teacher_area = teacher_string


        if student_string == "" or teacher_string == "":
        if student_string == "" or teacher_string == "":
            return render_template('parsing_error.html',
            def ifempty(arg: str) -> Optional[str]:
                                   error="Nebyl zadán vstupní formalismus.")
                if arg == "":
                    return "Nebyl zadán vstupní formalismus."
                return None

            return render_template('compare.html',
                                   error_asgn=ifempty(teacher_string),
                                   error_sol=ifempty(student_string),
                                   student_form=student_form,
                                   teacher_form=teacher_form,
                                   student_area=student_area,
                                   teacher_area=teacher_area)


        checker = WebChecker(student_string=student_string, task=student_type)
        checker = WebChecker(student_string=student_string, task=student_type)
        try:
        try:
            result = checker.compare(teacher_string=teacher_string,
            result = checker.compare(teacher_string=teacher_string,
                                     teacher_type=teacher_type)
                                     teacher_type=teacher_type)
        except ParsingError as ex:
        except CompositeException as ex:
            return render_template('parsing_error.html', error=str(ex))
            return render_template('compare.html',
                                   error_asgn=ex.exceptions[0],
                                   error_sol=ex.exceptions[1],
                                   student_form=student_form,
                                   teacher_form=teacher_form,
                                   student_area=student_area,
                                   teacher_area=teacher_area)


        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
+16 −4
Original line number Original line Diff line number Diff line
@@ -15,7 +15,13 @@
        <label for="teacher_string">Zadání</label>
        <label for="teacher_string">Zadání</label>
        <div>
        <div>
            <div style="width:400px; float:right;">
            <div style="width:400px; float:right;">
            <textarea style="width:400px;" name="teacher_string" id="teacher_string">{{ teacher_area }}</textarea></div>
                <textarea style="width:400px;" name="teacher_string" id="teacher_string">{{ teacher_area }}</textarea>
                {% if error_asgn is defined and error_asgn %}
                <div class="error">
                    {{error_asgn}}
                </div>
                {% endif %}
            </div>
            {{ teacher_form.hidden_tag() }}
            {{ teacher_form.hidden_tag() }}
            <div style="float:left;">{{ teacher_form.make(style="list-style:none") }}</div>
            <div style="float:left;">{{ teacher_form.make(style="list-style:none") }}</div>
        </div>
        </div>
@@ -23,7 +29,13 @@
        <label for="student_string">Řešení</label><br>
        <label for="student_string">Řešení</label><br>
        <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></div>
                <textarea style="width:400px;" name="student_string" id="student_string">{{ student_area }}</textarea>
                {% if error_sol is defined and error_sol %}
                <div class="error">
                    {{error_sol}}
                </div>
                {% endif %}
            </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>
        </div>
        </div>
+0 −11
Original line number Original line Diff line number Diff line
{% extends 'base.html' %}

{% block header %}
    <h1>Výsledek</h1>
{% endblock %}

{% block content %}
    <br>Vstupní formalismus nebyl správně zadán, nastala chyba při parsování.<br><br>
    {{ error }}

{% endblock %}
+18 −2
Original line number Original line Diff line number Diff line
@@ -51,6 +51,12 @@ class Language: # contains information about formalism and its language
        return f"{teacher}-{task}:{dfa}"
        return f"{teacher}-{task}:{dfa}"




class CompositeException(Exception):
    def __init__(self, *args: Optional[Exception]) -> None:
        self.exceptions = args
        super().__init__()


class WebChecker:
class WebChecker:
    def __init__(self, student_string: str, task: str) -> None:
    def __init__(self, student_string: str, task: str) -> None:
        self.student_string = student_string
        self.student_string = student_string
@@ -67,8 +73,18 @@ 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:
        error_teach = None
        try:
            teacher = Language(string=teacher_string, task=teacher_type)
            teacher = Language(string=teacher_string, task=teacher_type)
        except ParsingError as ex:
            error_teach = ex
        try:
            student = Language(string=student_string, task=self.task)
            student = Language(string=student_string, task=self.task)
        except ParsingError as ex:
            raise CompositeException(error_teach, ex)
        if error_teach:
            raise CompositeException(error_teach, None)

        # only for task checking
        # only for task checking
        student_solution = transform(student_string, task)
        student_solution = transform(student_string, task)