コード例 #1
0
def test_globalimports_with_project_root_model_parameter():
    """
    Basic test for the FQNGlobalRepo.
    Tests that "project_root" model parameter has an effect.
    """
    #################################
    # META MODEL DEF
    #################################

    my_meta_model = metamodel_from_file(
        join(abspath(dirname(__file__)), 'interface_model2', 'Interface.tx'))
    my_meta_model.register_scope_providers(
        {"*.*": scoping_providers.FQNGlobalRepo(join("model_a", "*.if"))})

    #################################
    # MODEL PARSING
    #################################

    with raises(IOError):
        _ = my_meta_model.model_from_file(
            join(abspath(dirname(__file__)), "interface_model2", "model_a",
                 "all_in_one.if"))

    _ = my_meta_model.model_from_file(
        join(abspath(dirname(__file__)), "interface_model2", "model_a",
             "all_in_one.if"),
        project_root=join(abspath(dirname(__file__)), "interface_model2"))
コード例 #2
0
def test_globalimports_basic_test_with_distributed_model():
    #################################
    # META MODEL DEF
    #################################

    my_meta_model = metamodel_from_file(
        abspath(dirname(__file__)) + '/interface_model2/Interface.tx')
    my_meta_model.register_scope_providers({
        "*.*":
        scoping_providers.FQNGlobalRepo(
            abspath(dirname(__file__)) + "/interface_model2/model_b/*.if")
    })

    #################################
    # MODEL PARSING
    #################################

    my_model = my_meta_model.model_from_file(
        abspath(dirname(__file__)) + "/interface_model2/model_b/app.if")

    #################################
    # TEST MODEL
    #################################

    # check that "socket" is an interface
    inner_model = my_model._tx_model_repository.all_models.filename_to_model[
        abspath(dirname(__file__)) + "/interface_model2/model_b/base.if"]
    check_unique_named_object_has_class(inner_model, "socket", "Interface")

    # check that "s.s1" is a reference to the socket interface
    a = get_unique_named_object(inner_model, "socket")
    s1 = get_unique_named_object(inner_model, "s1")
    assert a == s1.ref
コード例 #3
0
def test_model_with_local_scope_and_circular_ref_via_two_models():
    """
    Test for FQNGlobalRepo + circular references.
    """
    #################################
    # META MODEL DEF
    #################################

    my_meta_model = metamodel_from_file(
        join(abspath(dirname(__file__)),
             'components_model1', 'Components.tx'),
        global_repository=True)
    global_scope = scoping_providers.FQNGlobalRepo(
        join(abspath(dirname(__file__)),
             "components_model1", "example_?.components"))
    my_meta_model.register_scope_providers({
        "*.*": global_scope,
        "Connection.from_port":
            scoping_providers.RelativeName("from_inst.component.slots"),
        "Connection.to_port":
            scoping_providers.RelativeName("to_inst.component.slots")
    })

    #################################
    # MODEL PARSING
    #################################

    my_model_a = my_meta_model.model_from_file(
        join(abspath(dirname(__file__)),
             "components_model1", "example_A.components"))
    my_model_b = my_meta_model.model_from_file(
        join(abspath(dirname(__file__)),
             "components_model1", "example_B.components"))

    a_my_a = get_unique_named_object(my_model_a, "mya")
    a_my_b = get_unique_named_object(my_model_a, "myb")
    b_my_a = get_unique_named_object(my_model_b, "mya")
    b_my_b = get_unique_named_object(my_model_b, "myb")

    assert a_my_a != b_my_a
    assert a_my_b != b_my_b

    assert a_my_a.component == b_my_a.component  # same component "class"
    assert a_my_b.component == b_my_b.component  # same component "class"

    a_connections = get_children_of_type("Connection", my_model_a)
    b_connections = get_children_of_type("Connection", my_model_b)

    a_connection = list(filter(
        lambda x: x.from_inst == a_my_a and x.to_inst == a_my_b,
        a_connections))
    b_connection = list(filter(
        lambda x: x.from_inst == b_my_a and x.to_inst == b_my_b,
        b_connections))
    assert len(a_connection) == 1
    assert len(b_connection) == 1
コード例 #4
0
def test_globalimports_basic_test_with_single_model_file_and_global_repo():
    """
    Basic test for the FQNGlobalRepo + global_repository.
    Tests that two metamodels create the same objects for the
    same input (when global_repository is used).
    """
    #################################
    # META MODEL DEF
    #################################

    my_meta_model = metamodel_from_file(join(abspath(dirname(__file__)),
                                             'interface_model2',
                                             'Interface.tx'),
                                        global_repository=True)
    my_meta_model.register_scope_providers({
        "*.*":
        scoping_providers.FQNGlobalRepo(
            join(abspath(dirname(__file__)), 'interface_model2', 'model_a',
                 '*.if'))
    })

    #################################
    # MODEL PARSING
    #################################

    my_model = my_meta_model.model_from_file(
        join(abspath(dirname(__file__)), "interface_model2", "model_a",
             "all_in_one.if"))
    my_model2 = my_meta_model.model_from_file(
        join(abspath(dirname(__file__)), "interface_model2", "model_a",
             "all_in_one.if"))

    #################################
    # TEST MODEL
    #################################

    # check that "socket" is an interface
    check_unique_named_object_has_class(my_model, "socket", "Interface")

    # check that "s.s1" is a reference to the socket interface
    a = get_unique_named_object(my_model, "socket")
    s1 = get_unique_named_object(my_model, "s1")
    assert a == s1.ref

    a2 = get_unique_named_object(my_model2, "socket")
    assert a2 == a  # with global repository
コード例 #5
0
def test_globalimports_basic_test_with_distributed_model():
    """
    Basic test for the FQNGlobalRepo.
    Tests that a reference points to the expected (python) object
    located in the model.
    """
    #################################
    # META MODEL DEF
    #################################

    my_meta_model = metamodel_from_file(
        join(abspath(dirname(__file__)), 'interface_model2', 'Interface.tx'))
    my_meta_model.register_scope_providers({
        "*.*":
        scoping_providers.FQNGlobalRepo(
            join(abspath(dirname(__file__)), "interface_model2", "model_b",
                 "*.if"))
    })

    #################################
    # MODEL PARSING
    #################################

    my_model = my_meta_model.model_from_file(
        join(abspath(dirname(__file__)), "interface_model2", "model_b",
             "app.if"))

    #################################
    # TEST MODEL
    #################################

    # check that "socket" is an interface
    inner_model = my_model._tx_model_repository.all_models.filename_to_model[
        join(abspath(dirname(__file__)), "interface_model2", "model_b",
             "base.if")]
    check_unique_named_object_has_class(inner_model, "socket", "Interface")

    # check that "s.s1" is a reference to the socket interface
    a = get_unique_named_object(inner_model, "socket")
    s1 = get_unique_named_object(inner_model, "s1")
    assert a == s1.ref
コード例 #6
0
def test_globalimports_basic_test_with_single_model_file():
    """
    Basic test for the FQNGlobalRepo.
    Tests that two metamodels create different objects for the
    same input.
    """
    #################################
    # META MODEL DEF
    #################################

    my_meta_model = metamodel_from_file(
        abspath(dirname(__file__)) + '/interface_model2/Interface.tx')
    my_meta_model.register_scope_providers({
        "*.*":
        scoping_providers.FQNGlobalRepo(
            abspath(dirname(__file__)) + "/interface_model2/model_a/*.if")
    })

    #################################
    # MODEL PARSING
    #################################

    my_model = my_meta_model.model_from_file(
        abspath(dirname(__file__)) + "/interface_model2/model_a/all_in_one.if")
    my_model2 = my_meta_model.model_from_file(
        abspath(dirname(__file__)) + "/interface_model2/model_a/all_in_one.if")

    #################################
    # TEST MODEL
    #################################

    # check that "socket" is an interface
    check_unique_named_object_has_class(my_model, "socket", "Interface")

    # check that "s.s1" is a reference to the socket interface
    a = get_unique_named_object(my_model, "socket")
    s1 = get_unique_named_object(my_model, "s1")
    assert a == s1.ref

    a2 = get_unique_named_object(my_model2, "socket")
    assert a2 != a  # no global repository
コード例 #7
0
def test_globalimports_basic_test_with_single_model_file_and_global_repo():
    #################################
    # META MODEL DEF
    #################################

    my_meta_model = metamodel_from_file(abspath(dirname(__file__)) +
                                        '/interface_model2/Interface.tx',
                                        global_repository=True)
    my_meta_model.register_scope_providers({
        "*.*":
        scoping_providers.FQNGlobalRepo(
            abspath(dirname(__file__)) + "/interface_model2/model_a/*.if")
    })

    #################################
    # MODEL PARSING
    #################################

    my_model = my_meta_model.model_from_file(
        abspath(dirname(__file__)) + "/interface_model2/model_a/all_in_one.if")
    my_model2 = my_meta_model.model_from_file(
        abspath(dirname(__file__)) + "/interface_model2/model_a/all_in_one.if")

    #################################
    # TEST MODEL
    #################################

    # check that "socket" is an interface
    check_unique_named_object_has_class(my_model, "socket", "Interface")

    # check that "s.s1" is a reference to the socket interface
    a = get_unique_named_object(my_model, "socket")
    s1 = get_unique_named_object(my_model, "s1")
    assert a == s1.ref

    a2 = get_unique_named_object(my_model2, "socket")
    assert a2 == a  # with global repository
コード例 #8
0
def init_metamodel(path):
    this_folder = dirname(abspath(__file__))

    global_repo = GlobalModelRepository()
    global_repo_provider = scoping_providers.FQNGlobalRepo(
        glob_args={"recursive": True})
    global_repo_provider.register_models(path + "/**/*.config")

    all_classes = [
        cl.PExam, cl.PExamContentContainer, cl.PExerciseRef, cl.PExercise,
        cl.PAsciiContent, cl.PCodeContent, cl.PFreeSpaceContent, cl.PImage,
        cl.PLatexContent, cl.PPlantUmlContent
    ]

    mm_exercise = metamodel_from_file(join(this_folder, "Exercise.tx"),
                                      global_repository=global_repo,
                                      use_regexp_group=True,
                                      classes=all_classes)

    mm_exercise.register_obj_processors({
        "MYFLOAT": lambda x: float(x),
        "MYINT": lambda x: int(x),
    })

    def name2file(name):
        return name.replace(".", "/") + ".exercise"

    mm_exam = metamodel_from_file(join(this_folder, "Exam.tx"),
                                  global_repository=global_repo,
                                  use_regexp_group=True,
                                  classes=all_classes)
    mm_exam.register_scope_providers({
        "*.*":
        global_repo_provider,
        "dummy.dummy":
        scoping_providers.FQNImportURI(importURI_converter=name2file,
                                       search_path=[path]),
    })

    mm_exam.register_obj_processors({
        "MYFLOAT": lambda x: float(x),
        "MYINT": lambda x: int(x),
        "PExam": validation.check_exam
    })

    mm_config = metamodel_from_file(join(this_folder, "Config.tx"),
                                    global_repository=global_repo,
                                    use_regexp_group=True)

    clear_language_registrations()
    register_language("exam-exercise-lang",
                      "*.exercise",
                      metamodel=mm_exercise)
    register_language("exam-lang", "*.exam", metamodel=mm_exam)
    register_language("exam-config-lang", "*.config", metamodel=mm_config)

    all_models = global_repo_provider.load_models_in_model_repo().\
        all_models
    configs = get_all(all_models, what='Config')

    if len(configs) > 1:
        raise Exception("found more than one config: {}".format("and ".join(
            map(lambda x: x._tx_filename, configs))))
    if len(configs) != 1:
        raise Exception("found no config")

    return mm_exam, all_models, configs[0]