Ejemplo n.º 1
0
    def edit_connect(self, port_a, port_b, new_port_a=None, new_port_b=None):
        """edit_connect finds all connect clauses that match the pattern
        connect(<port_a>, <port_b>), in that order. If a port is an asterisk, '*',
        then it matches any identifier.

        :param port_a: string, identifier for first port; an asterisk matches all
        :param port_b: string, identifier for second port; an asterisk matches all
        :param new_port_a: string | None, replacement for port a; if None no changes are made
        :param new_port_b: string | None, replacement for port b; if None no changes are made
        """
        # verify the paramaters are sensible
        if (port_a == '*'
                and new_port_a is not None) or (port_b == '*'
                                                and new_port_b is not None):
            raise Exception(
                'Invalid to have a port match a wildcard and replace it (might result in duplicate clauses)'
            )

        # make up to two transformations (one for each replacement)
        if new_port_a is not None:
            selector = (ConnectClauseSelector(port_a, port_b).chain(
                NthChildSelector(2)))
            self.add(
                SimpleTransformation(selector, Edit.make_replace(new_port_a)))

        if new_port_b is not None:
            selector = (ConnectClauseSelector(port_a, port_b).chain(
                NthChildSelector(4)))
            self.add(
                SimpleTransformation(selector, Edit.make_replace(new_port_b)))
Ejemplo n.º 2
0
    def build_edits(self, tree, parser):
        # try to find the model annotation
        model_annotation_xpath = 'stored_definition/class_definition/class_specifier/long_class_specifier/composition/model_annotation'
        model_annotation_node = XPath.XPath.findAll(tree,
                                                    model_annotation_xpath,
                                                    parser)
        if not model_annotation_node:
            # insert the model annotation along with the modifications
            selector = (EquationSectionSelector().chain(
                NthChildSelector(-1)).assert_count(
                    1, 'Failed to find end of the equation section'))

            edit = Edit.make_insert(
                f'\n{config.INDENTATION}annotation({build_modifications(self.modifications, indented=False)});'
            )
            return SimpleTransformation(selector,
                                        edit).build_edits(tree, parser)

        # model annotation exists, recursively update or insert the modifications
        model_annotation_node = model_annotation_node[0]
        return make_edits_for_modifications(
            model_annotation_node.annotation().class_modification(),
            self.modifications,
            parser,
            indented=config.INDENT_INSERTED_ANNOTATION_ARGS,
        )
Ejemplo n.º 3
0
    def transformation(self):
        """transformation creates the transformation required for inserting the
        built component

        :return: Transformation
        """
        if self._insert_index == 0:
            selector = (ElementListSelector().chain(NthChildSelector(0)))
            insert_after = False
        elif self._insert_index < 0:
            # insert after the last child
            selector = (ElementListSelector().chain(NthChildSelector(-1)))
            insert_after = True
        else:
            selector = (ElementListSelector().chain(
                NthChildSelector(self._insert_index)))
            insert_after = True

        edit = Edit.make_insert(self.build(), insert_after=insert_after)
        return SimpleTransformation(selector, edit)
Ejemplo n.º 4
0
    def transformation(self):
        """transformation creates the transformation required for inserting the
        built for loop

        :return: Transformation
        """
        # select the last child of the equation section and insert after it
        selector = (EquationSectionSelector().chain(
            NthChildSelector(-1)).assert_count(
                1, 'Failed to find end of the equation section'))
        edit = Edit.make_insert(self.build(), insert_after=True)
        return SimpleTransformation(selector, edit)
Ejemplo n.º 5
0
    def test_nth_child_selector(self):
        # Setup
        tree, parser = parse(os.path.join(self.data_dir, 'DCMotor.mo'))

        # Act
        selector = (ElementListSelector().chain(NthChildSelector(2)))
        element = selector.apply_to_root(tree, parser)

        # Assert
        self.assertEqual(1, len(element), "should have one element")
        # the third child should be the Inductor line
        # (semicolons are also children of the element_list)
        elementText = element[0].getText()
        self.assertTrue(elementText.startswith('Inductor'))