Exemplo n.º 1
0
def test_model_with_imports_and_global_repo():
    """
    Basic test for FQNImportURI + global_repository
    """
    #################################
    # META MODEL DEF
    #################################

    my_meta_model = metamodel_from_file(
        join(abspath(dirname(__file__)), 'interface_model1', 'Interface.tx'),
        global_repository=True)
    my_meta_model.register_scope_providers(
        {"*.*": scoping_providers.FQNImportURI()})

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

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

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

    userid = get_unique_named_object(my_model, "userid")
    userid2 = get_unique_named_object(my_model2, "userid")
    assert userid == userid2
    assert userid.ref == userid2.ref
    assert userid.ref.__class__.__name__ == "RawType"
Exemplo n.º 2
0
def test_model_without_imports():
    """
    Basic test for FQNImportURI (with a model not using imports)
    """
    #################################
    # META MODEL DEF
    #################################

    my_meta_model = metamodel_from_file(
        join(abspath(dirname(__file__)),
             'interface_model1', 'Interface.tx'))
    my_meta_model.register_scope_providers(
        {"*.*": scoping_providers.FQNImportURI()})

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

    my_model = my_meta_model.model_from_file(
        join(abspath(dirname(__file__)),
             "interface_model1", "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
Exemplo n.º 3
0
def test_model_with_imports_and_global_repo():
    #################################
    # META MODEL DEF
    #################################

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

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

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

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

    userid = get_unique_named_object(my_model, "userid")
    userid2 = get_unique_named_object(my_model2, "userid")
    assert userid == userid2
    assert userid.ref == userid2.ref
    assert userid.ref.__class__.__name__ == "RawType"
Exemplo n.º 4
0
def test_model_without_imports():
    #################################
    # META MODEL DEF
    #################################

    my_meta_model = metamodel_from_file(
        abspath(dirname(__file__)) + '/interface_model1/Interface.tx')
    my_meta_model.register_scope_providers(
        {"*.*": scoping_providers.FQNImportURI()})

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

    my_model = my_meta_model.model_from_file(
        abspath(dirname(__file__)) + "/interface_model1/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
Exemplo n.º 5
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
Exemplo n.º 6
0
def test_model_without_imports():
    """
    Basic test for FQNImportURI (with a model not using imports)
    """
    #################################
    # META MODEL DEF
    #################################

    my_meta_model = metamodel_from_file(
        join(abspath(dirname(__file__)), 'interface_model1', 'Interface.tx'))
    my_meta_model.register_scope_providers(
        {"*.*": scoping_providers.FQNImportURI()})

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

    my_model = my_meta_model.model_from_file(
        join(abspath(dirname(__file__)), "interface_model1", "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
Exemplo n.º 7
0
def test_model_with_imports_and_global_repo():
    """
    Basic test for FQNImportURI + global_repository
    """
    #################################
    # META MODEL DEF
    #################################

    my_meta_model = metamodel_from_file(join(abspath(dirname(__file__)),
                                             'interface_model1',
                                             'Interface.tx'),
                                        global_repository=True)
    my_meta_model.register_scope_providers(
        {"*.*": scoping_providers.FQNImportURI()})

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

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

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

    userid = get_unique_named_object(my_model, "userid")
    userid2 = get_unique_named_object(my_model2, "userid")
    assert userid == userid2
    assert userid.ref == userid2.ref
    assert userid.ref.__class__.__name__ == "RawType"
Exemplo n.º 8
0
def test_resolve_model_path_with_lists():
    #################################
    # META MODEL DEF
    #################################

    my_meta_model = metamodel_from_file(
        join(abspath(dirname(__file__)),
             'components_model1', 'Components.tx'))
    my_meta_model.register_scope_providers({
        "*.*": scoping_providers.FQN(),
        "Connection.from_port":
            scoping_providers.ExtRelativeName("from_inst.component",
                                              "slots",
                                              "extends"),
        "Connection.to_port":
            scoping_providers.ExtRelativeName("to_inst.component",
                                              "slots",
                                              "extends"),
    })

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

    my_model = my_meta_model.model_from_file(
        join(abspath(dirname(__file__)),
             "components_model1", "example_inherit2.components"))

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

    action2a = resolve_model_path(my_model,
                                  "packages.usage.instances.action2",
                                  True)
    action2b = get_unique_named_object(my_model, "action2")
    assert action2a is action2b

    middle_a = resolve_model_path(my_model,
                                  "packages.base.components.Middle",
                                  True)
    middle_b = get_unique_named_object(my_model, "Middle")
    assert middle_a is middle_b

    # test parent(...) with lists
    action2a_with_parent = resolve_model_path(
        action2a, "parent(Model).packages.usage.instances.action2", True)
    assert action2a_with_parent == action2a

    # test "normal" parent with lists
    action2a_with_parent2 = resolve_model_path(
        action2a, "parent.instances.action2", True)
    assert action2a_with_parent2 == action2a

    with raises(Exception, match=r'.*unexpected: got list in path for '
                                 r'get_referenced_object.*'):
        resolve_model_path(my_model,
                           "packages.usage.instances.action2",
                           False)
def test_reference_to_buildin_attribute():
    # This test demonstrates how to link a textX model to
    # an arbitrary other non textX model.
    #
    # "foreign_model" be an arbitrary object, e.g., a AST from another
    # (non textX) meta model.
    # We can (in combination with a special scope provider)
    # reference (and combine) our textX model with this model from
    # the (non textX) meta model object.
    foreign_model = {"name": "Test", "value": 3}

    # custom scope provider
    def my_scope_provider(obj, attr, attr_ref):
        pyobj = obj.pyobj
        if attr_ref.obj_name in pyobj:
            return pyobj[attr_ref.obj_name]
        else:
            raise Exception("{} not found".format(attr_ref.obj_name))

    # create meta model
    my_metamodel = metamodel_from_str(
        metamodel_str, builtins={'foreign_model': foreign_model})
    my_metamodel.register_scope_providers({
        "Access.pyattr": my_scope_provider,
    })

    # read model
    my_model = my_metamodel.model_from_str('''
    access A1 foreign_model
    access A2 foreign_model.name
    access A3 foreign_model.value
    ''')

    # check that the references are OK
    A2_name = get_unique_named_object(my_model, "A2").pyattr
    assert A2_name is foreign_model["name"]
    A3_val = get_unique_named_object(my_model, "A3").pyattr
    assert A3_val is foreign_model["value"]

    # check error cases
    with raises(Exception, match=r'.*noname not found.*'):
        _ = my_metamodel.model_from_str('''
        access A1 foreign_model
        access A2 foreign_model.noname
        ''')

    with raises(Exception, match=r'.*unknown_model.*'):
        _ = my_metamodel.model_from_str('''
        access A1 foreign_model
        access A2 unknown_model.name
        ''')
    with raises(Exception, match=r'.*unknown_model.*'):
        _ = my_metamodel.model_from_str('''
        access A1 unknown_model
        access A2 foreign_model.name
        ''')

    pass
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
def test_reference_to_nontextx_attribute():
    # This test demonstrates how to link a textX model to
    # an arbitrary other non textX model.
    # Here we use a json file as external model
    # to deomstrate how to load external models on the fly.

    # custom scope provider
    def generic_scope_provider(obj, attr, attr_ref):
        if not obj.pyobj:
            from textx.scoping import Postponed
            return Postponed()
        if not hasattr(obj.pyobj, "data"):
            import json
            obj.pyobj.data = json.load(
                open(abspath(dirname(__file__)) + "/" + obj.pyobj.filename))
        if attr_ref.obj_name in obj.pyobj.data:
            return obj.pyobj.data[attr_ref.obj_name]
        else:
            raise Exception("{} not found".format(attr_ref.obj_name))

    # create meta model
    my_metamodel = metamodel_from_str(metamodel_str)
    my_metamodel.register_scope_providers({
        "Access.pyattr":
        generic_scope_provider,
    })

    # read model
    my_model = my_metamodel.model_from_str('''
    import "test_reference_to_nontextx_attribute/othermodel.json" as data
    access A1 data.name
    access A2 data.gender
    ''')

    # check that the references are OK
    A1_name = get_unique_named_object(my_model, "A1").pyattr
    assert A1_name == "pierre"
    A2_gender = get_unique_named_object(my_model, "A2").pyattr
    assert A2_gender == "male"

    with raises(Exception, match=r'.*noname.*'):
        _ = my_metamodel.model_from_str('''
        import "test_reference_to_nontextx_attribute/othermodel.json" as data
        access A1 data.noname
        access A2 data.gender
        ''')

    with raises(Exception, match=r'.*filenotfound.*'):
        _ = my_metamodel.model_from_str('''
        import "test_reference_to_nontextx_attribute/filenotfound.json" as data
        access A1 data.name
        access A2 data.gender
        ''')

    pass
def test_reference_to_nontextx_attribute():
    # This test demonstrates how to link a textX model to
    # an arbitrary other non textX model.
    # Here we use a json file as external model
    # to demonstrate how to load external models on the fly.

    # custom scope provider
    def generic_scope_provider(obj, attr, attr_ref):
        if not obj.pyobj:
            from textx.scoping import Postponed
            return Postponed()
        if not hasattr(obj.pyobj, "data"):
            import json
            obj.pyobj.data = json.load(open(
                join(abspath(dirname(__file__)), obj.pyobj.filename)))
        if attr_ref.obj_name in obj.pyobj.data:
            return obj.pyobj.data[attr_ref.obj_name]
        else:
            raise Exception("{} not found".format(attr_ref.obj_name))

    # create meta model
    my_metamodel = metamodel_from_str(metamodel_str)
    my_metamodel.register_scope_providers({
        "Access.pyattr": generic_scope_provider,
    })

    # read model
    my_model = my_metamodel.model_from_str('''
    import "test_reference_to_nontextx_attribute/othermodel.json" as data
    access A1 data.name
    access A2 data.gender
    ''')

    # check that the references are OK
    A1_name = get_unique_named_object(my_model, "A1").pyattr
    assert A1_name == "pierre"
    A2_gender = get_unique_named_object(my_model, "A2").pyattr
    assert A2_gender == "male"

    with raises(Exception, match=r'.*noname.*'):
        my_metamodel.model_from_str('''
        import "test_reference_to_nontextx_attribute/othermodel.json" as data
        access A1 data.noname
        access A2 data.gender
        ''')

    with raises(Exception, match=r'.*filenotfound.*'):
        my_metamodel.model_from_str('''
        import "test_reference_to_nontextx_attribute/filenotfound.json" as data
        access A1 data.name
        access A2 data.gender
        ''')

    pass
def test_json_ref_dsl():
    mm = json_ref_dsl.get_metamodel()
    current_dir = os.path.dirname(__file__)
    my_model = mm.model_from_file(
        os.path.join(current_dir, 'models', 'ok.jref'))

    # check that the references are OK
    A1_name = get_unique_named_object(my_model, "A1").pyattr
    assert A1_name == "pierre"
    A2_gender = get_unique_named_object(my_model, "A2").pyattr
    assert A2_gender == "male"
Exemplo n.º 14
0
def test_model_with_local_scope():
    """
    This is a basic test for the local scope provider (good case).
    """
    #################################
    # META MODEL DEF
    #################################

    my_meta_model = metamodel_from_file(
        join(abspath(dirname(__file__)), 'components_model1',
             'Components.tx'))
    my_meta_model.register_scope_providers({
        "*.*": scoping_providers.FQN(),
        "Connection.from_port":
            scoping_providers.RelativeName("from_inst.component.slots"),
        "Connection.to_port":
            scoping_providers.RelativeName("to_inst.component.slots"),
    })

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

    my_model = my_meta_model.model_from_file(
        join(abspath(dirname(__file__)), "components_model1",
             "example.components"))

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

    # test local refs
    action2 = get_unique_named_object(my_model, "action2")
    action3 = get_unique_named_object(my_model, "action3")
    connections = get_children_of_type("Connection", my_model)
    selected_connections = list(filter(
        lambda x: x.from_inst == action2 and x.to_inst == action3,
        connections))
    assert len(selected_connections) == 1

    # test list of formats
    input2 = get_unique_named_object(my_model, "input2")
    assert len(input2.formats) == 3
    format_names = map(lambda x: x.name, input2.formats)
    assert "A" in format_names
    assert "B" in format_names
    assert "C" in format_names
    assert "D" not in format_names
Exemplo n.º 15
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
Exemplo n.º 16
0
def test_model_with_circular_imports():
    """
    Basic test for FQNImportURI + circular imports
    """
    #################################
    # META MODEL DEF
    #################################

    my_meta_model = metamodel_from_file(
        join(abspath(dirname(__file__)),
             'interface_model1', 'Interface.tx'))
    my_meta_model.register_scope_providers(
        {"*.*": scoping_providers.FQNImportURI()})

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

    my_model = my_meta_model.model_from_file(
        join(abspath(dirname(__file__)),
             "interface_model1", "model_c", "A.if"))

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

    imports = get_children_of_type("Import", my_model)
    assert len(imports) > 0
    for i in imports:
        assert 1 == len(i._tx_loaded_models)  # one file / load import
        assert i.importURI in i._tx_loaded_models[0]._tx_filename

    check_unique_named_object_has_class(my_model, "A", "Interface")
    a = get_unique_named_object(my_model, "A")

    a_self = get_children(lambda x: hasattr(x, 'name') and x.name == "self", a)
    assert len(a_self) == 1
    a_self = a_self[0]

    a_other = get_children(
        lambda x: hasattr(x, 'name') and x.name == "other", a)
    assert len(a_other) == 1
    a_other = a_other[0]

    a_other_self = get_children(
        lambda x: hasattr(x, 'name') and x.name == "self", a_other.ref)
    assert len(a_other_self) == 1
    a_other_self = a_other_self[0]

    a_other_other = get_children(
        lambda x: hasattr(x, 'name') and x.name == "other", a_other.ref)
    assert len(a_other_other) == 1
    a_other_other = a_other_other[0]

    assert a_self.ref == a_other_other.ref
    assert a_self.ref != a_other.ref
    assert a_other.ref == a_other_self.ref
    assert a_other.ref != a_other_other.ref
Exemplo n.º 17
0
def test_reference_to_python_attribute():
    # This test demonstrates how to link python objects to
    # a textX model.
    # "access" objects access python attributes.

    from collections import namedtuple
    Person = namedtuple('Person', 'first_name last_name zip_code')
    p1 = Person('Tim', 'Foo', 123)
    p2 = Person('Tom', 'Bar', 456)

    sp = PythonScopeProvider({"p1": p1, "p2": p2})

    # create meta model
    my_metamodel = metamodel_from_str(metamodel_str)
    my_metamodel.register_scope_providers({
        "Access.pyobj": sp,
        "Access.pyattr": sp
    })

    # read model
    my_model = my_metamodel.model_from_str('''
    access A_Tim p1.first_name
    access A_123 p1.zip_code
    access A_456 p2.zip_code
    ''')

    # check that the references are OK
    A_Tim = get_unique_named_object(my_model, "A_Tim").pyattr
    assert A_Tim == "Tim"
    A_123 = get_unique_named_object(my_model, "A_123").pyattr
    assert A_123 == 123
    A_456 = get_unique_named_object(my_model, "A_456").pyattr
    assert A_456 == 456

    with raises(Exception, match=r'.*unknown.*'):
        my_metamodel.model_from_str('''
        access A1 p1.unknown
        ''')

    with raises(Exception, match=r'.*p3.*'):
        my_metamodel.model_from_str('''
        access A1 p3.first_anme
        ''')

    pass
Exemplo n.º 18
0
def test_model_with_imports():
    """
    Basic test for FQNImportURI (good case)
    """
    #################################
    # META MODEL DEF
    #################################

    my_meta_model = metamodel_from_file(
        join(abspath(dirname(__file__)),
             'interface_model1', 'Interface.tx'))
    my_meta_model.register_scope_providers(
        {"*.*": scoping_providers.FQNImportURI()})

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

    my_model = my_meta_model.model_from_file(
        join(abspath(dirname(__file__)),
             "interface_model1", "model_b", "app.if"))
    my_model2 = my_meta_model.model_from_file(
        join(abspath(dirname(__file__)),
             "interface_model1", "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_model1", "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")
    userid = get_unique_named_object(my_model, "userid")
    assert a == s1.ref

    userid2 = get_unique_named_object(my_model2, "userid")
    assert userid != userid2
    assert userid.ref != userid2.ref
    assert userid.ref.__class__.__name__ == "RawType"
Exemplo n.º 19
0
def test_model_with_circular_imports():
    """
    Basic test for FQNImportURI + circular imports
    """
    #################################
    # META MODEL DEF
    #################################

    my_meta_model = metamodel_from_file(
        join(abspath(dirname(__file__)), 'interface_model1', 'Interface.tx'))
    my_meta_model.register_scope_providers(
        {"*.*": scoping_providers.FQNImportURI()})

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

    my_model = my_meta_model.model_from_file(
        join(abspath(dirname(__file__)), "interface_model1", "model_c",
             "A.if"))

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

    imports = get_children_of_type("Import", my_model)
    assert len(imports) > 0
    for i in imports:
        assert 1 == len(i._tx_loaded_models)  # one file / load import
        assert i.importURI in i._tx_loaded_models[0]._tx_filename

    check_unique_named_object_has_class(my_model, "A", "Interface")
    a = get_unique_named_object(my_model, "A")

    a_self = get_children(lambda x: hasattr(x, 'name') and x.name == "self", a)
    assert len(a_self) == 1
    a_self = a_self[0]

    a_other = get_children(lambda x: hasattr(x, 'name') and x.name == "other",
                           a)
    assert len(a_other) == 1
    a_other = a_other[0]

    a_other_self = get_children(
        lambda x: hasattr(x, 'name') and x.name == "self", a_other.ref)
    assert len(a_other_self) == 1
    a_other_self = a_other_self[0]

    a_other_other = get_children(
        lambda x: hasattr(x, 'name') and x.name == "other", a_other.ref)
    assert len(a_other_other) == 1
    a_other_other = a_other_other[0]

    assert a_self.ref == a_other_other.ref
    assert a_self.ref != a_other.ref
    assert a_other.ref == a_other_self.ref
    assert a_other.ref != a_other_other.ref
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
Exemplo n.º 21
0
def test_model_with_imports():
    """
    Basic test for FQNImportURI (good case)
    """
    #################################
    # META MODEL DEF
    #################################

    my_meta_model = metamodel_from_file(
        join(abspath(dirname(__file__)), 'interface_model1', 'Interface.tx'))
    my_meta_model.register_scope_providers(
        {"*.*": scoping_providers.FQNImportURI()})

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

    my_model = my_meta_model.model_from_file(
        join(abspath(dirname(__file__)), "interface_model1", "model_b",
             "app.if"))
    my_model2 = my_meta_model.model_from_file(
        join(abspath(dirname(__file__)), "interface_model1", "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_model1", "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")
    userid = get_unique_named_object(my_model, "userid")
    assert a == s1.ref

    userid2 = get_unique_named_object(my_model2, "userid")
    assert userid != userid2
    assert userid.ref != userid2.ref
    assert userid.ref.__class__.__name__ == "RawType"
Exemplo n.º 22
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
Exemplo n.º 23
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
Exemplo n.º 24
0
def test_get_referenced_object():
    #################################
    # META MODEL DEF
    #################################

    my_meta_model = metamodel_from_file(
        abspath(dirname(__file__)) + '/components_model1/Components.tx')
    my_meta_model.register_scope_providers({
        "*.*": scoping_providers.FQN(),
        "Connection.from_port":
            scoping_providers.ExtRelativeName("from_inst.component",
                                              "slots",
                                              "extends"),
        "Connection.to_port":
            scoping_providers.ExtRelativeName("to_inst.component",
                                              "slots",
                                              "extends"),
    })

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

    my_model = my_meta_model.model_from_file(
        abspath(dirname(__file__)) +
        "/components_model1/example_inherit2.components")

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

    action2a = get_referenced_object(
        None, my_model, "packages.usage.instances.action2")
    action2b = get_unique_named_object(my_model, "action2")
    assert action2a is action2b

    middle_a = get_referenced_object(
        None, my_model, "packages.base.components.Middle")
    middle_b = get_unique_named_object(my_model, "Middle")
    assert middle_a is middle_b
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
Exemplo n.º 26
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
Exemplo n.º 27
0
def test_model_with_circular_imports():
    #################################
    # META MODEL DEF
    #################################

    my_meta_model = metamodel_from_file(
        abspath(dirname(__file__)) + '/interface_model1/Interface.tx')
    my_meta_model.register_scope_providers(
        {"*.*": scoping_providers.FQNImportURI()})

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

    my_model = my_meta_model.model_from_file(
        abspath(dirname(__file__)) + "/interface_model1/model_c/A.if")

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

    check_unique_named_object_has_class(my_model, "A", "Interface")
    a = get_unique_named_object(my_model, "A")

    a_self = get_children(lambda x: hasattr(x, 'name') and x.name == "self", a)
    assert len(a_self) == 1
    a_self = a_self[0]

    a_other = get_children(lambda x: hasattr(x, 'name') and x.name == "other",
                           a)
    assert len(a_other) == 1
    a_other = a_other[0]

    a_other_self = get_children(
        lambda x: hasattr(x, 'name') and x.name == "self", a_other.ref)
    assert len(a_other_self) == 1
    a_other_self = a_other_self[0]

    a_other_other = get_children(
        lambda x: hasattr(x, 'name') and x.name == "other", a_other.ref)
    assert len(a_other_other) == 1
    a_other_other = a_other_other[0]

    assert a_self.ref == a_other_other.ref
    assert a_self.ref != a_other.ref
    assert a_other.ref == a_other_self.ref
    assert a_other.ref != a_other_other.ref
Exemplo n.º 28
0
def test_get_list_of_concatenated_objects():
    #################################
    # META MODEL DEF
    #################################

    my_meta_model = metamodel_from_file(
        join(abspath(dirname(__file__)),
             'components_model1', 'Components.tx'))
    my_meta_model.register_scope_providers({
        "*.*": scoping_providers.FQN(),
        "Connection.from_port":
            scoping_providers.ExtRelativeName("from_inst.component",
                                              "slots",
                                              "extends"),
        "Connection.to_port":
            scoping_providers.ExtRelativeName("to_inst.component",
                                              "slots",
                                              "extends"),
    })

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

    my_model1 = my_meta_model.model_from_file(
        join(abspath(dirname(__file__)),
             "components_model1", "example_inherit1.components"))
    my_model2 = my_meta_model.model_from_file(
        join(abspath(dirname(__file__)),
             "components_model1", "example_inherit2.components"))

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

    # test extends A,B
    start = get_unique_named_object(my_model1, "Start")
    middle = get_unique_named_object(my_model1, "Middle")
    end = get_unique_named_object(my_model1, "End")
    inherited_classes = get_list_of_concatenated_objects(middle, "extends")
    assert len(inherited_classes) == 3
    assert inherited_classes[0] is middle
    assert inherited_classes[1] is start
    assert inherited_classes[2] is end

    # test extends A extends B
    start = get_unique_named_object(my_model2, "Start")
    middle = get_unique_named_object(my_model2, "Middle")
    end = get_unique_named_object(my_model2, "End")
    inherited_classes = get_list_of_concatenated_objects(middle, "extends")
    assert len(inherited_classes) == 3
    assert inherited_classes[0] is middle
    assert inherited_classes[1] is start
    assert inherited_classes[2] is end
Exemplo n.º 29
0
def test_model_with_local_scope_and_inheritance2():
    """
    This is a more complicated test for the local scope provider.
    """
    #################################
    # META MODEL DEF
    #################################

    my_meta_model = metamodel_from_file(
        abspath(dirname(__file__)) + '/components_model1/Components.tx')
    my_meta_model.register_scope_providers({
        "*.*":
        scoping_providers.FQN(),
        "Connection.from_port":
        scoping_providers.ExtRelativeName("from_inst.component", "slots",
                                          "extends"),
        "Connection.to_port":
        scoping_providers.ExtRelativeName("to_inst.component", "slots",
                                          "extends"),
    })

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

    my_model = my_meta_model.model_from_file(
        abspath(dirname(__file__)) +
        "/components_model1/example_inherit1.components")

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

    # test inherited ports are same (direct inheritance)
    action1 = get_unique_named_object(my_model, "action1")
    action2 = get_unique_named_object(my_model, "action2")
    action3 = get_unique_named_object(my_model, "action3")
    end = get_unique_named_object(my_model, "end")
    connections = get_children_of_type("Connection", my_model)
    selected_connections_12 = list(
        filter(lambda x: x.from_inst == action1 and x.to_inst == action2,
               connections))
    selected_connections_3e = list(
        filter(lambda x: x.from_inst == action3 and x.to_inst == end,
               connections))
    assert len(selected_connections_12) == 1
    assert len(selected_connections_3e) == 1
    assert selected_connections_12[0].to_port is selected_connections_3e[0].\
        to_port  # output3 is same

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

    my_model = my_meta_model.model_from_file(
        abspath(dirname(__file__)) +
        "/components_model1/example_inherit2.components")

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

    # test inherited ports are same
    # (indirect inheritance: Middle -> Start -> End)
    action1 = get_unique_named_object(my_model, "action1")
    action2 = get_unique_named_object(my_model, "action2")
    action3 = get_unique_named_object(my_model, "action3")
    end = get_unique_named_object(my_model, "end")
    connections = get_children_of_type("Connection", my_model)
    selected_connections_12 = list(
        filter(lambda x: x.from_inst == action1 and x.to_inst == action2,
               connections))
    selected_connections_3e = list(
        filter(lambda x: x.from_inst == action3 and x.to_inst == end,
               connections))
    assert len(selected_connections_12) == 1
    assert len(selected_connections_3e) == 1
    assert selected_connections_12[0].to_port is selected_connections_3e[0].\
        to_port  # output3 is same
Exemplo n.º 30
0
def test_model_with_local_scope_and_inheritance2():
    """
    This is a more complicated test for the local scope provider.
    """
    #################################
    # META MODEL DEF
    #################################

    my_meta_model = metamodel_from_file(
        join(abspath(dirname(__file__)), 'components_model1',
             'Components.tx'))
    my_meta_model.register_scope_providers({
        "*.*": scoping_providers.FQN(),
        "Connection.from_port":
            scoping_providers.ExtRelativeName("from_inst.component",
                                              "slots",
                                              "extends"),
        "Connection.to_port":
            scoping_providers.ExtRelativeName("to_inst.component",
                                              "slots",
                                              "extends"),
    })

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

    my_model = my_meta_model.model_from_file(
        join(abspath(dirname(__file__)),
             "components_model1", "example_inherit1.components"))

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

    # test inherited ports are same (direct inheritance)
    action1 = get_unique_named_object(my_model, "action1")
    action2 = get_unique_named_object(my_model, "action2")
    action3 = get_unique_named_object(my_model, "action3")
    end = get_unique_named_object(my_model, "end")
    connections = get_children_of_type("Connection", my_model)
    selected_connections_12 = list(filter(
        lambda x: x.from_inst == action1 and x.to_inst == action2,
        connections))
    selected_connections_3e = list(filter(
        lambda x: x.from_inst == action3 and x.to_inst == end,
        connections))
    assert len(selected_connections_12) == 1
    assert len(selected_connections_3e) == 1
    assert selected_connections_12[0].to_port is selected_connections_3e[0].\
        to_port  # output3 is same

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

    my_model = my_meta_model.model_from_file(
        join(abspath(dirname(__file__)),
             "components_model1", "example_inherit2.components"))

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

    # test inherited ports are same
    # (indirect inheritance: Middle -> Start -> End)
    action1 = get_unique_named_object(my_model, "action1")
    action2 = get_unique_named_object(my_model, "action2")
    action3 = get_unique_named_object(my_model, "action3")
    end = get_unique_named_object(my_model, "end")
    connections = get_children_of_type("Connection", my_model)
    selected_connections_12 = list(filter(
        lambda x: x.from_inst == action1 and x.to_inst == action2,
        connections))
    selected_connections_3e = list(filter(
        lambda x: x.from_inst == action3 and x.to_inst == end, connections))
    assert len(selected_connections_12) == 1
    assert len(selected_connections_3e) == 1
    assert selected_connections_12[0].to_port is selected_connections_3e[0].\
        to_port  # output3 is same