Esempio n. 1
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
Esempio n. 2
0
 def __call__(self, obj, attr, obj_ref):
     from textx.scoping.tools import get_referenced_object, \
         get_list_of_concatenated_objects
     from textx.scoping import Postponed
     from textx import get_model
     try:
         # print("DEBUG: ExtRelativeName.__call__(...{})".
         #      format(obj_ref.obj_name))
         one_def_obj = get_referenced_object(None, obj,
                                             self.path_to_definition_object)
         def_obj_list = get_list_of_concatenated_objects(
             one_def_obj, self.path_to_extension, [])
         # print("DEBUG: {}".format(def_obj_list))
         for def_obj in def_obj_list:
             if type(def_obj) is Postponed:
                 self.postponed_counter += 1
                 return def_obj
             res = get_referenced_object(
                 None, def_obj,
                 self.path_to_target + "." + obj_ref.obj_name, obj_ref.cls)
             if res:
                 return res  # may be Postponed
         return None
     except TypeError as e:
         from textx.scoping.tools import get_parser
         line, col = get_parser(obj).pos_to_linecol(obj_ref.position)
         raise TextXSemanticError('ExtRelativeName: {}'.format(str(e)),
                                  line=line,
                                  col=col,
                                  filename=get_model(obj)._tx_filename)
Esempio n. 3
0
 def __call__(self, parser, obj, attr, obj_ref):
     from textx.scoping.tools import get_referenced_object, \
         get_list_of_concatenated_objects
     from textx.scoping import Postponed
     try:
         one_def_obj = get_referenced_object(None, obj,
                                             self.path_to_definition_object,
                                             parser)
         def_obj_list = get_list_of_concatenated_objects(
             one_def_obj, self.path_to_extension, parser, [])
         for def_obj in def_obj_list:
             if type(def_obj) is Postponed:
                 self.postponed_counter += 1
                 return def_obj
             res = get_referenced_object(
                 None, def_obj,
                 self.path_to_target + "." + obj_ref.obj_name, parser,
                 obj_ref.cls)
             if res:
                 return res  # may be Postponed
         return None
     except TypeError as e:
         line, col = parser.pos_to_linecol(obj_ref.position)
         raise TextXSemanticError('ExtRelativeName: {}'.format(str(e)),
                                  line=line,
                                  col=col)
Esempio n. 4
0
    def get_reference_propositions(self, obj, attr, name_part):
        """
        retrieve a list of reference propositions.
        Args:
            obj: parent
            attr: attribute
            name_part: The name is used to build the list
                (e.g. using a substring-like logic).
        Returns:
            the list of objects representing the proposed references
        """
        from textx.scoping.tools import resolve_model_path
        from textx import textx_isinstance
        # find all all "connected" objects
        # (e.g. find all classes: the most derived
        # class, its base, the base of its base, etc.)
        from textx.scoping.tools import get_list_of_concatenated_objects
        def_obj = resolve_model_path(obj, self.path_to_definition_object)
        def_objs = get_list_of_concatenated_objects(def_obj,
                                                    self.path_to_extension)
        # for all containing classes, collect all
        # objects to be looked up (e.g. methods)
        obj_list = []
        for def_obj in def_objs:
            if type(def_obj) is Postponed:
                self.postponed_counter += 1
                return def_obj

            tmp_list = resolve_model_path(def_obj, self.path_to_target)
            assert tmp_list is not None
            # expected to point to  alist
            if not isinstance(tmp_list, list):
                from textx.exceptions import TextXError
                raise TextXError(
                    "expected path to list in the model ({})".format(
                        self.path_to_target))
            tmp_list = list(
                filter(
                    lambda x: textx_isinstance(x, attr.cls) and x.name.find(
                        name_part) >= 0, tmp_list))
            obj_list = obj_list + tmp_list

        return list(obj_list)
Esempio n. 5
0
    def get_reference_propositions(self, obj, attr, name_part):
        """
        retrieve a list of reference propositions.
        Args:
            obj: parent
            attr: attribute
            name_part: The name is used to build the list
                (e.g. using a substring-like logic).
        Returns:
            the list of objects representing the proposed references
        """
        from textx.scoping.tools import resolve_model_path
        from textx import textx_isinstance
        # find all all "connected" objects
        # (e.g. find all classes: the most derived
        # class, its base, the base of its base, etc.)
        from textx.scoping.tools import get_list_of_concatenated_objects
        def_obj = resolve_model_path(obj, self.path_to_definition_object)
        def_objs = get_list_of_concatenated_objects(
            def_obj, self.path_to_extension)
        # for all containing classes, collect all
        # objects to be looked up (e.g. methods)
        obj_list = []
        for def_obj in def_objs:
            if type(def_obj) is Postponed:
                self.postponed_counter += 1
                return def_obj

            tmp_list = resolve_model_path(def_obj, self.path_to_target)
            assert tmp_list is not None
            # expected to point to  alist
            if not isinstance(tmp_list, list):
                from textx.exceptions import TextXError
                raise TextXError(
                    "expected path to list in the model ({})".format(
                        self.path_to_target))
            tmp_list = list(filter(
                lambda x: textx_isinstance(x, attr.cls) and
                x.name.find(name_part) >= 0, tmp_list))
            obj_list = obj_list + tmp_list

        return list(obj_list)