Ejemplo n.º 1
0
def test_yaml_symbol_name_not_a_dictionary():
    for i in ['Integer', 'String', 'pred', ['a', 'b']]:
        yaml_input = """
        predicate: %s             
        """ % i
        with pytest.raises(ValueError):
            YamlValidation.validate(yaml.safe_load(yaml_input))
Ejemplo n.º 2
0
def test_yaml_invalid_root():
    for i in ['Integer', 'VALASP', 10, ['a', 'b']]:
        yaml_input = """
                %s
                """ % i
        with pytest.raises(ValueError):
            YamlValidation.validate(yaml.safe_load(yaml_input))
Ejemplo n.º 3
0
def test_yaml_symbol_name():
    for i in {
            'predicate', '_predicate', '_preDicate', '___predicate', '"on"',
            '"off"'
    }:
        yaml_input = """
        %s:
            term1: Integer
        """ % i
        YamlValidation.validate(yaml.safe_load(yaml_input))
Ejemplo n.º 4
0
def test_yaml_valasp_root():
    yaml_input = """
    valasp:
        python: |+
            for i in range(10):
                print(i)
        asp: |+
            code
    """
    YamlValidation.validate(yaml.safe_load(yaml_input))
Ejemplo n.º 5
0
def test_yaml_valasp_in_user_defined_module_2():
    for i in {"Integer", "String", "Alpha", "Any"}:
        for oper in {
                "==", "!=", ">", ">=", "<", "<=", " == ", " !=", "> ", "  >=",
                "<  ", "  <=  "
        }:
            yaml_input = """
             predicate:
                 first: %s
                 second: %s
                 valasp:
                     having:
                        - first%ssecond                  
             """ % (i, i, oper)
            YamlValidation.validate(yaml.safe_load(yaml_input))
Ejemplo n.º 6
0
    def convert2python(self) -> List[str]:
        YamlValidation.validate(self.__content)
        self.__read_valasp()
        self.__read_symbols()

        newline = '\n'
        slash_slash = '\\'

        all_import = """
import clingo
import valasp
import valasp.core
import base64
import re
import sys
from valasp.domain.primitive_types import Alpha, Any, Integer, String

def _(x):
    return base64.b64decode(x).decode()
"""
        template = f"""
def main(files, with_solve=True, stdout=sys.stdout, stderr=sys.stderr):
    try:
        context = valasp.core.Context(wrap=[{', '.join(self.__valasp_wrap)}], max_arity={self.__valasp_max_arity})

        {f'{newline}        '.join(self.__output)}

        control = clingo.Control()
        for file_ in files:
            control.load(file_)
        try:
            context.valasp_run(
                control, 
                on_validation_done=lambda: print("ALL VALID!{slash_slash}n==========", file=stdout), 
                on_model=lambda m: print(f"Answer: {{m}}{slash_slash}n==========", file=stdout), 
                aux_program=[_({self.__valasp_asp})],
                with_solve=with_solve,
            )
        except RuntimeError as e:
            raise ValueError(context.valasp_extract_error_message(e)) from None
    except Exception as e:
        print('VALIDATION FAILED', file=stdout)
        print('=================', file=stdout)
        print(e, file=stdout)
        print('=================', file=stdout)
"""

        return [all_import, self.__valasp_python, template]
Ejemplo n.º 7
0
def test_yaml_symbol_name_invalid_names():
    for i in [
            'Name', 'Valasp', 'valasp', 'on', 'off', 1, -2, '"1"', "_Predicate"
    ]:
        yaml_input = f"""
        {i}:
            term1: Integer
        """
        with pytest.raises(ValueError):
            YamlValidation.validate(yaml.safe_load(yaml_input))
    for i in [['name'], {'name'}]:
        yaml_input = f"""
                {i}:
                    term1: Integer
                """
        with pytest.raises(ConstructorError):
            yaml.safe_load(yaml_input)