コード例 #1
0
def test_model_with_local_scope_postponed():
    """
    This is a test for the local scope provider which checks that
    the scope resolution is postponed at an intermediate stage.

    This must be the case, since the order of object references is
    exchanged in two differernt metamodels. This, we argue that (in the
    absence of an additional sorting mechanisms) in one of both
    cases the required reference to the "from_instance" must be unresolved
    in the first resolution pass.

    The check is done using white box information (postponed_counter).
    """
    #################################
    # META MODEL DEF
    #################################

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

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

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

    my_meta_model1.model_from_file(
        join(abspath(dirname(__file__)), "components_model1",
             "example.components"))
    my_meta_model2.model_from_file(
        join(abspath(dirname(__file__)), "components_model2",
             "example.components"))

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

    assert sp1.postponed_counter > 0 or sp2.postponed_counter > 0
コード例 #2
0
def test_inheritance_processor():
    """
    Basic test for ExtRelativeName (using an inheritance example)
    """
    #################################
    # 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_inherit3.components")

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

    components = get_children_of_type("Component", my_model)

    expected = """
        Start
        BaseProc
        ExtProc(BaseProc)
        Plus
        ExtProc2(Plus,ExtProc(BaseProc))
        End
        End2
    """

    def myformatter(compo):
        if len(compo.extends) == 0:
            return compo.name
        else:
            return compo.name + "(" + ",".join(map(lambda x: myformatter(x),
                                                   compo.extends)) + ")"

    res = "\n\n"
    # possibly in future version, the order need to be normalized...
    for c in components:
        res += myformatter(c) + "\n"

    print(res)
    assert re.sub(r'\s*', "", expected) == re.sub(r'\s*', "", res)
コード例 #3
0
ファイル: test_local_scope.py プロジェクト: rowhit/textX
def test_model_with_local_scope_and_error():
    #################################
    # 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.RelativeName("from_inst.component.slots"),
        "Connection.to_port":
        scoping_providers.RelativeName("to_inst.component.slots")
    })

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

    with raises(textx.exceptions.TextXSemanticError,
                match=r'.*Unknown objec.*input1.*SlotIn.*'):
        my_model = my_meta_model.model_from_file(
            abspath(dirname(__file__)) +
            "/components_model1/example_err1.components")
コード例 #4
0
ファイル: test_local_scope.py プロジェクト: rowhit/textX
def test_postponed_resolution_error():
    #################################
    # META MODEL DEF
    #################################

    def from_port(parser, obj, attr, obj_ref):
        return scoping.Postponed()

    def to_port(parser, obj, attr, obj_ref):
        return scoping.Postponed()

    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": from_port,
        "Connection.to_port": to_port
    })

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

    with raises(textx.exceptions.TextXSemanticError,
                match=r'.*Unresolvable cross references.*'):
        my_model = my_meta_model.model_from_file(
            abspath(dirname(__file__)) +
            "/components_model1/example.components")
コード例 #5
0
def test_model_with_local_scope_and_bad_model_path():
    """
    This is a basic 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":
            # error (component is not a list)
            scoping_providers.RelativeName("from_inst.component"),
        "Connection.to_port":
            scoping_providers.RelativeName("to_inst.component.slots"),
    })

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

    with raises(textx.exceptions.TextXError,
                match=r'.*expected path to list in the model '
                      + r'\(from_inst.component\).*'):
        my_meta_model.model_from_file(
            join(abspath(dirname(__file__)),
                 "components_model1", "example.components"))
コード例 #6
0
ファイル: test_local_scope.py プロジェクト: fgro93/textX
def test_postponed_resolution_error():
    """
    This test checks that an unresolvable scopre provider induces an exception.
    This is checked by using a scope provider which always returns a postponed
    object.
    """

    #################################
    # META MODEL DEF
    #################################

    def from_port(obj, attr, obj_ref):
        return scoping.Postponed()

    def to_port(obj, attr, obj_ref):
        return scoping.Postponed()

    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": from_port,
        "Connection.to_port": to_port
    })

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

    with raises(textx.exceptions.TextXSemanticError,
                match=r'.*Unresolvable cross references.*'):
        my_model = my_meta_model.model_from_file(
            abspath(dirname(__file__)) +
            "/components_model1/example.components")
コード例 #7
0
def test_model_with_local_scope_and_error():
    """
    This is a basic test for the local scope provider (bad 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
    #################################

    with raises(textx.exceptions.TextXSemanticError,
                match=r'.*Unknown objec.*input1.*SlotIn.*'):
        my_meta_model.model_from_file(
            join(abspath(dirname(__file__)),
                 "components_model1", "example_err1.components"))
コード例 #8
0
def test_fully_qualified_name_ref_type_error():
    """
    This is a basic test for the FQN (positive and negative test).
    """
    #################################
    # META MODEL DEF
    #################################

    my_metamodel = metamodel_from_str(metamodel_str)

    my_metamodel.register_scope_providers({"*.*": scoping_providers.FQN()})

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

    with raises(textx.exceptions.TextXSemanticError, match=r'.*p1.*'):
        my_metamodel.model_from_str('''
        package P1 {
            class Part1 {
            }
        }
        package P2 {
            class Part2 {
                attr C2 rec;
            }
            class C2 {
                attr P1.Part1 p1;
                attr Part2 p2a;
                attr p1 p2b;
            }
        }
        ''')
コード例 #9
0
ファイル: test_local_scope.py プロジェクト: fgro93/textX
def test_model_with_local_scope_wrong_type():
    """
    This is a basic test for the local scope provider
    (basd case with wrong type).
    """
    #################################
    # 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.RelativeName("from_inst.component.slots"),
        "Connection.to_port":
        scoping_providers.RelativeName("to_inst.component.slots"),
    })

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

    with raises(textx.exceptions.TextXSemanticError, match=r'.*wrong_port.*'):
        _ = my_meta_model.model_from_file(
            abspath(dirname(__file__)) +
            "/components_model1/example_wrong_type.components")
コード例 #10
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)
コード例 #11
0
ファイル: test_children.py プロジェクト: rowhit/textX
def test_fully_qualified_name_ref():
    #################################
    # META MODEL DEF
    #################################

    my_metamodel = metamodel_from_str(metamodel_str)

    my_metamodel.register_scope_providers({"*.*": scoping_providers.FQN()})

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

    my_model = my_metamodel.model_from_str('''
    package P1 {
        class Part1 {
        }
    }
    package P2 {
        class Part2 {
            attr C2 rec;
        }
        class C2 {
            attr P1.Part1 p1;
            attr Part2 p2a;
            attr P2.Part2 p2b;
        }
    }
    ''')

    #################################
    # TEST
    #################################

    res = get_children_of_type("Class", my_model)
    res.sort(key=lambda x: x.name)
    assert len(res) == 3
    assert all(map(eq, map(lambda x: x.name, res), ["C2", "Part1", "Part2"]))
    assert not all(
        map(eq, map(lambda x: x.name, res), ["Part1", "Part2", "C2"]))
    for x in res:
        assert x.__class__.__name__ == "Class"

    res = get_children_of_type("Attribute", my_model)
    res.sort(key=lambda x: x.name)
    assert len(res) == 4
    assert all(map(eq, map(lambda x: x.name, res),
                   ["p1", "p2a", "p2b", "rec"]))
    for x in res:
        assert x.__class__.__name__ == "Attribute"

    res = get_children(
        lambda x: hasattr(x, "name") and re.match(".*2.*", x.name), my_model)
    res.sort(key=lambda x: x.name)
    assert len(res) == 5
    assert all(
        map(eq, map(lambda x: x.name, res),
            ["C2", "P2", "Part2", "p2a", "p2b"]))
コード例 #12
0
ファイル: test_local_scope.py プロジェクト: rowhit/textX
def test_model_with_local_scope_postponed():
    #################################
    # META MODEL DEF
    #################################

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

    sp2 = scoping_providers.RelativeName("from_inst.component.slots")
    my_meta_model2 = metamodel_from_file(
        abspath(dirname(__file__)) + '/components_model2/Components.tx')
    my_meta_model2.register_scope_providers({
        "*.*":
        scoping_providers.FQN(),
        "Connection.from_port":
        sp2,
        "Connection.to_port":
        scoping_providers.RelativeName("to_inst.component.slots")
    })

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

    _ = my_meta_model1.model_from_file(
        abspath(dirname(__file__)) + "/components_model1/example.components")
    _ = my_meta_model2.model_from_file(
        abspath(dirname(__file__)) + "/components_model2/example.components")

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

    assert sp1.postponed_counter > 0 or sp2.postponed_counter > 0
コード例 #13
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
コード例 #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
コード例 #15
0
ファイル: test_buildins.py プロジェクト: stjordanis/textX
def test_buildins_fully_qualified_name():
    """
    This test is used to check if a model with buildins works correctly.
    The test uses full quialified name scoping (to check that exchanging the
    scope provider globally does not harm the buildins-feature.

    The test loads
    - one model w/o buildins
    - one model with buildins
    - one model with unknown references (errors)
    """
    #################################
    # META MODEL DEF
    #################################

    type_builtins = {
        'OneThing': Thing(name="OneThing"),
        'OtherThing': Thing(name="OtherThing")
    }
    my_metamodel = metamodel_from_str(metamodel_str,
                                      classes=[Thing],
                                      builtins=type_builtins)
    my_metamodel.register_scope_providers({"*.*": scoping_providers.FQN()})

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

    my_metamodel.model_from_str('''
    thing A {}
    thing B {}
    thing C {A B}
    ''')

    my_metamodel.model_from_str('''
    thing A {}
    thing B {}
    thing C {A B OneThing OtherThing}
    ''')

    with raises(textx.exceptions.TextXSemanticError,
                match=r'.*Unknown object.*UnknownPart.*'):
        my_metamodel.model_from_str('''
        thing A {}
        thing B {}
        thing C {A B OneThing OtherThing UnknownPart}
        ''')
コード例 #16
0
def read_SLCO_model(m):
    """Read, post process, and type check an SLCO model"""

    # create meta-model
    slco_mm = metamodel_from_file(join(this_folder,
                                       '../textx_grammars/slco2.tx'),
                                  autokwd=True,
                                  classes=[
                                      Assignment, Composite, Expression,
                                      ExprPrec1, ExprPrec2, ExprPrec3,
                                      ExprPrec4, Primary, ExpressionRef,
                                      Variable, VariableRef, Type, Action
                                  ])

    # register processors
    slco_mm.register_model_processor(construct_action_set)
    slco_mm.register_model_processor(check_names)
    slco_mm.register_model_processor(add_initial_to_states)
    slco_mm.register_model_processor(add_variable_types)
    #slco_mm.register_model_processor(set_default_type_size)
    slco_mm.register_model_processor(set_default_channel_size)
    slco_mm.register_model_processor(add_taus)
    slco_mm.register_model_processor(fix_references)
    #slco_mm.register_model_processor(simplify_statements)

    # To do: Check receive statements for not receiving multiple values in the same variable
    # To do: Check for absence of arrays (not single elements) as part of messages

    slco_mm.register_scope_providers({
        "*.*":
        providers.FQN(),
        "Initialisation.left":
        providers.RelativeName("parent.type.variables"),
        "Channel.port0":
        providers.RelativeName("source.type.ports"),
        "Channel.port1":
        providers.RelativeName("target.type.ports"),
        "ReceiveSignal.from":
        providers.RelativeName("parent.parent.parent.type.ports"),
        "SendSignal.to":
        providers.RelativeName("parent.parent.parent.type.ports"),
    })

    # parse and return the model
    return slco_mm.model_from_file(m)
コード例 #17
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
コード例 #18
0
def test_fully_qualified_name_ref():
    #################################
    # META MODEL DEF
    #################################

    my_metamodel = metamodel_from_str(metamodel_str)

    my_metamodel.register_scope_providers({"*.*": scoping_providers.FQN()})

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

    my_model = my_metamodel.model_from_str('''
    package P1 {
        class Part1 {
        }
    }
    package P2 {
        class Part2 {
            attr C2 rec;
        }
        class C2 {
            attr P1.Part1 p1;
            attr Part2 p2a;
            attr P2.Part2 p2b;
        }
    }
    ''')

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

    a = get_children(lambda x: hasattr(x, 'name') and x.name == "rec",
                     my_model)
    assert len(a) == 1
    assert a[0].name == "rec"
    assert a[0].ref.__class__.__name__ == "Class"
    assert a[0].ref.name == "C2"

    a = get_children(lambda x: hasattr(x, 'name') and x.name == "p1", my_model)
    assert len(a) == 1
    assert a[0].name == "p1"
    assert a[0].ref.__class__.__name__ == "Class"
    assert a[0].ref.name == "Part1"

    a = get_children(lambda x: hasattr(x, 'name') and x.name == "p2a",
                     my_model)
    assert len(a) == 1
    assert a[0].name == "p2a"
    assert a[0].ref.__class__.__name__ == "Class"
    assert a[0].ref.name == "Part2"

    a = get_children(lambda x: hasattr(x, 'name') and x.name == "p2b",
                     my_model)
    assert len(a) == 1
    assert a[0].name == "p2b"
    assert a[0].ref.__class__.__name__ == "Class"
    assert a[0].ref.name == "Part2"

    ###########################
    # MODEL WITH ERROR
    ############################
    with raises(textx.exceptions.TextXSemanticError,
                match=r'.*Unknown object.*Part1.*'):
        my_model2 = my_metamodel.model_from_str('''
        package P1 {
            class Part1 {
            }
        }
        package P2 {
            class C2 {
                attr Part1 p1;
            }
        }
        ''')
コード例 #19
0
ファイル: test_local_scope.py プロジェクト: fgro93/textX
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
コード例 #20
0
def test_fully_qualified_name_ref():
    """
    This is a basic test for the FQN (positive and negative test).
    """
    #################################
    # META MODEL DEF
    #################################

    my_metamodel = metamodel_from_str(metamodel_str)

    my_metamodel.register_scope_providers({"*.*": scoping_providers.FQN()})

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

    my_model = my_metamodel.model_from_str('''
    package P1 {
        class Part1 {
        }
    }
    package P2 {
        class Part2 {
            attr C2 rec;
        }
        class C2 {
            attr P1.Part1 p1;
            attr Part2 p2a;
            attr P2.Part2 p2b;
        }
    }
    ''')

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

    a = get_children(lambda x: hasattr(x, 'name') and x.name == "rec",
                     my_model)
    assert len(a) == 1
    assert a[0].name == "rec"
    assert a[0].ref.__class__.__name__ == "Class"
    assert a[0].ref.name == "C2"

    a = get_children(lambda x: hasattr(x, 'name') and x.name == "p1", my_model)
    assert len(a) == 1
    assert a[0].name == "p1"
    assert a[0].ref.__class__.__name__ == "Class"
    assert a[0].ref.name == "Part1"

    a = get_children(lambda x: hasattr(x, 'name') and x.name == "p2a",
                     my_model)
    assert len(a) == 1
    assert a[0].name == "p2a"
    assert a[0].ref.__class__.__name__ == "Class"
    assert a[0].ref.name == "Part2"

    a = get_children(lambda x: hasattr(x, 'name') and x.name == "p2b",
                     my_model)
    assert len(a) == 1
    assert a[0].name == "p2b"
    assert a[0].ref.__class__.__name__ == "Class"
    assert a[0].ref.name == "Part2"

    ###########################
    # MODEL WITH ERROR
    ############################
    with raises(textx.exceptions.TextXSemanticError,
                match=r'None:8:.*: Unknown object.*Part1.*'):
        my_metamodel.model_from_str(''' #1
        package P1 { #2
            class Part1 { #3
            } #4
        } #5
        package P2 { #6
            class C2 { #7
                attr Part1 p1; #8
            }
        }
        ''')

    with raises(textx.exceptions.TextXSemanticError,
                match=r'.*test_fully_qualified_name_test_error.model:8:\d+:'
                ' Unknown object.*Part1.*'):
        my_metamodel.model_from_file(
            join(dirname(__file__), "misc",
                 "test_fully_qualified_name_test_error.model"))