Esempio n. 1
0
def test_fails():
    """

    Testing invalid requests for :mod:`~backend.logic_layer` that should always fail.

    """

    # Passing invalid name of algorithm
    json_input = XMLConverter.xml_to_json(read_input(REGEXPS + '/regexp.xml'))
    res = logic_layer.simple_algorithm(json_input, 'regexp_trimmm')
    assert res['exception'] == 'Unknown algorithm passed as parameter!'
    assert res['type'] is AltInterfaceException

    # Omitting the optional parameter which is mandatory for some algorithms
    res = logic_layer.simple_algorithm(json_input,
                                       AlgorithmTypes.REGEXP_DERIVATION)
    assert res['exception'] == 'Invalid JSON structure'
    assert res['type'] is XMLConverter.JSONDecodeError

    # Passing invalid input type
    res = logic_layer.simple_algorithm(
        json_input, AlgorithmTypes.AUTOMATON_DETERMINIZATION)
    assert 'Entry overload' in res['exception']
    assert 'not available' in res['exception']
    assert res['type'] is AltInterfaceException

    # Missing 'target' in transformation input
    res = logic_layer.transformation(json_input)
    assert res['exception'] == 'Invalid JSON structure'
    assert res['type'] is XMLConverter.JSONDecodeError

    # Passing invalid target type to transformation
    json_input_transformation = {'source': json_input, 'target': 'NFA'}
    res = logic_layer.transformation(json_input_transformation)
    assert res['exception'] == 'Unknown \'to\' parameter passed as parameter!'
    assert res['type'] is AltInterfaceException

    # Trying to convert ENFA to RG
    json_input = XMLConverter.xml_to_json(
        read_input(AUTOMATA + '/ENFA1.EPSILON.xml'))
    json_input_transformation = {'source': json_input, 'target': 'rg'}
    res = logic_layer.transformation(json_input_transformation)
    assert 'Entry overload' in res['exception']
    assert 'not available' in res['exception']
    assert res['type'] is AltInterfaceException

    # Trying to compare FA and CFG
    json_input2 = XMLConverter.xml_to_json(
        read_input(GRAMMARS + '/CFG1.UNIT.xml'))
    json_input_comparison = {'lhs': json_input, 'rhs': json_input2}
    res = logic_layer.comparison(json_input_comparison)
    assert 'Entry overload' in res['exception']
    assert 'not available' in res['exception']
    assert res['type'] is AltInterfaceException
Esempio n. 2
0
def algorithms(algorithm_name: str) -> Response:
    """

    Algorithms view.

    Checks for correct algorithm name (returns bad request (400) if not valid) and calls given algorithm through
    :func:`backend.logic_layer.simple_algorithm`. Currently supported algorithm names are:

        * ``'automaton_determinization'``
        * ``'automaton_epsilon'``
        * ``'grammar_reduction'``
        * ``'grammar_epsilon'``
        * ``'grammar_unit'``
        * ``'grammar_cnf'``
        * ``'grammar_left_recursion'``
        * ``'regexp_derivation'``

    :param algorithm_name: `string` representing name of the algorithm

    :return: instance of :class:`~flask.Response` representing JSON response (structure of JSON depends on given \
    algorithm)

    """
    if algorithm_name not in AlgorithmTypes():
        return _bad_request('Invalid algorithm_name parameter', 'API')

    response = logic_layer.simple_algorithm(request.get_json(), algorithm_name)
    response = _handle_errors(response)

    return response
Esempio n. 3
0
def test_simple_algorithm_result(input_file: str, algorithm: str,
                                 expected_file: str, optional_param: str):
    """

    Testing given algorithm on given input and comparing the result with expected output.

    .. note::

        Bare on mind that this function is heavily testing comparison. If this test fails, don't forget to check also
        comparison function and wrapper!

    :param input_file: path to XML file containing input
    :param algorithm: algorithm to be run
    :param expected_file: path to XML file containing expected output
    :param optional_param: extra parameter for algorithms that need it (currently only regexp derivation and CYK)

    """
    xml_input = read_input(input_file)
    expected_output = read_input(expected_file)

    json_input = XMLConverter.xml_to_json(xml_input)
    json_output = XMLConverter.xml_to_json(expected_output)

    result = logic_layer.simple_algorithm(json_input, algorithm)

    to_compare = {'lhs': result, 'rhs': json_output}
    res = logic_layer.comparison(to_compare)
    assert res['result'] is True
Esempio n. 4
0
def test_recursion_cnf_run(input_file: str, algorithm: str, result_type: str):
    json_input = XMLConverter.xml_to_json(read_input(input_file))
    res = logic_layer.simple_algorithm(json_input, algorithm)
    assert 'after_reduction' in res.keys()
    assert 'after_epsilon' in res.keys()
    assert 'after_unit_rules' in res.keys()
    assert result_type == res['result']['type']
Esempio n. 5
0
def test_cyk_run(input_file: str, result_type: str, optional_param: str):
    json_input = XMLConverter.xml_to_json(read_input(input_file))
    json_input = {'grammar': json_input, 'cyk_string': optional_param}

    res = logic_layer.simple_algorithm(json_input, AlgorithmTypes.GRAMMAR_CYK)
    assert 'step_table' in res.keys()
    assert 'result' in res.keys()
    assert res['result'] is True or res['result'] is False
Esempio n. 6
0
def test_derivation_run(input_file: str, result_type: str,
                        optional_param: str):
    json_input = XMLConverter.xml_to_json(read_input(input_file))
    json_input = {'regexp': json_input, 'derivation_string': optional_param}

    res = logic_layer.simple_algorithm(json_input,
                                       AlgorithmTypes.REGEXP_DERIVATION)
    assert 'steps' in res.keys()
    assert 'trimmed_steps' in res.keys()
    assert result_type == res['steps'][-1]['type']
Esempio n. 7
0
def test_recursion_cnf_result(input_file: str, algorithm: str,
                              expected_file: str):
    xml_input = read_input(input_file)
    expected_output = read_input(expected_file)

    json_input = XMLConverter.xml_to_json(xml_input)
    expected_json = XMLConverter.xml_to_json(expected_output)

    result = logic_layer.simple_algorithm(json_input, algorithm)

    to_compare = {'lhs': result['result'], 'rhs': expected_json}
    assert result['result'] == expected_json
    res = logic_layer.comparison(to_compare)
    assert res['result'] is True
Esempio n. 8
0
def test_derivation_result(input_file: str, expected_file: str,
                           optional_param: str):
    xml_input = read_input(input_file)
    expected_output = read_input(expected_file)

    json_input = XMLConverter.xml_to_json(xml_input)
    json_output = XMLConverter.xml_to_json(expected_output)

    json_input = {'regexp': json_input, 'derivation_string': optional_param}
    result = logic_layer.simple_algorithm(json_input,
                                          AlgorithmTypes.REGEXP_DERIVATION)
    result = result['steps'][-1]

    to_compare = {'lhs': result, 'rhs': json_output}
    res = logic_layer.comparison(to_compare)
    assert res['result'] is True
Esempio n. 9
0
def test_cyk_result(input_file: str, expected_file: str,
                    expected_file_table: str, optional_param: str):
    xml_input = read_input(input_file)
    expected_output = read_input(expected_file)
    expected_output_table = read_input(expected_file_table)

    json_input = XMLConverter.xml_to_json(xml_input)
    expected_json = XMLConverter.xml_to_json(expected_output,
                                             AlgorithmTypes.GRAMMAR_CYK,
                                             steps=expected_output_table)

    json_input = {'grammar': json_input, 'cyk_string': optional_param}
    result = logic_layer.simple_algorithm(json_input,
                                          AlgorithmTypes.GRAMMAR_CYK)

    assert result['result'] == expected_json['result']
    assert result['step_table'] == expected_json['step_table']
Esempio n. 10
0
def test_epsilon_trim_det_min(automaton: str):
    """

    Testing sequence of automata algorithms: epsilon transition removal, automaton trim, determinization and
    minimization. In each step checking for valid output type.

    :param automaton: path to XML file containing input automaton

    """
    res = XMLConverter.xml_to_json(read_input(automaton))

    for algorithm, result_type in [
        (AlgorithmTypes.AUTOMATON_EPSILON_REMOVAL, 'NFA'),
        (AlgorithmTypes.AUTOMATON_TRIM, 'NFA'),
        (AlgorithmTypes.AUTOMATON_DETERMINIZATION, 'DFA'),
        (AlgorithmTypes.AUTOMATON_MINIMIZATION_NO_VERBOSE, 'DFA')
    ]:
        res = logic_layer.simple_algorithm(res, algorithm)
        assert result_type == res['type']
Esempio n. 11
0
def test_minimization_result(input_file: str, expected_file: str,
                             expected_file_table: str):
    xml_input = read_input(input_file)
    expected_output = read_input(expected_file)
    expected_output_table = read_input(expected_file_table)

    json_input = XMLConverter.xml_to_json(xml_input)
    expected_json = XMLConverter.xml_to_json(
        expected_output,
        AlgorithmTypes.AUTOMATON_MINIMIZATION,
        steps=expected_output_table)

    result = logic_layer.simple_algorithm(
        json_input, AlgorithmTypes.AUTOMATON_MINIMIZATION)

    to_compare = {'lhs': result['result'], 'rhs': expected_json['result']}
    res = logic_layer.comparison(to_compare)
    assert res['result'] is True
    assert result['steps'] == expected_json['steps']
Esempio n. 12
0
def test_simple_algorithm_run(input_file: str, algorithm: str,
                              result_type: str, optional_param: str):
    """

    Simple algorithms test. Just run the given algorithm with given input and checks the output type.

    :param input_file: path to XML file containing input
    :param algorithm: algorithm to be run
    :param result_type: expected type of the output
    :param optional_param: extra parameter for algorithms that need it (currently only regexp derivation and CYK)

    """
    json_input = XMLConverter.xml_to_json(read_input(input_file))
    res = logic_layer.simple_algorithm(json_input, algorithm)

    if algorithm == AlgorithmTypes.AUTOMATON_MINIMIZATION:
        assert 'steps' in res.keys()
        assert result_type == res['result']['type']
    else:
        assert result_type == res['type']