Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functionality to identify the type of the constraints #68

Open
jmhorcas opened this issue Jul 12, 2022 · 0 comments
Open

Add functionality to identify the type of the constraints #68

jmhorcas opened this issue Jul 12, 2022 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@jmhorcas
Copy link
Contributor

This has been extracted from the Constraint class in the FM_METAMODEL, and it should be included in a kind of ConstraintsHelper.
This can be implemented using the AST directly like for the following "is_requires_constraint" :

def is_simple_constraint(self) -> bool:
        return self.is_requires_constraint() or self.is_excludes_constraint()

    def is_complex_constraint(self) -> bool:
        return self.is_pseudocomplex_constraint() or self.is_strictcomplex_constraint()

    def is_requires_constraint(self) -> bool:
        root_op = self._ast.root
        if root_op.is_binary_op():
            if root_op.data in [ASTOperation.REQUIRES, ASTOperation.IMPLIES]:
                return root_op.left.is_term() and root_op.right.is_term()
            elif root_op.data == ASTOperation.OR:
                neg_left = root_op.left.data == ASTOperation.NOT and root_op.left.left.is_term()
                neg_right = root_op.right.data == ASTOperation.NOT and root_op.right.left.is_term()
                return neg_left and root_op.right.is_term() or neg_right and root_op.left.is_term()
        return False

or using the CNF form of the AST as follows:

def is_requires_constraint(self) -> bool:
        if len(self._clauses) == 1 and len(self._clauses[0]) == 2:
            nof_negative_clauses = sum(var.startswith('-') for var in self._clauses[0])
            return nof_negative_clauses == 1
        return False

    def is_excludes_constraint(self) -> bool:
        if len(self._clauses) == 1 and len(self._clauses[0]) == 2:
            nof_negative_clauses = sum(var.startswith('-') for var in self._clauses[0])
            return nof_negative_clauses == 2
        return False

    def is_strictcomplex_constraint(self) -> bool:
        if len(self._clauses) == 1 and len(self._clauses[0]) == 2:
            nof_negative_clauses = sum(var.startswith('-') for var in self._clauses[0])
            return nof_negative_clauses == 0
        strictcomplex = False
        i = iter(self._clauses)
        while not strictcomplex and (cls := next(i, None)) is not None:
            if len(cls) != 2:
                strictcomplex = True
            else:
                nof_negative_clauses = sum(var.startswith('-') for var in cls)
                if nof_negative_clauses not in [1, 2]:
                    strictcomplex = True
        return strictcomplex

    def is_pseudocomplex_constraint(self) -> bool:
        if len(self._clauses) == 1:
            return False
        strictcomplex = False
        i = iter(self._clauses)
        while not strictcomplex and (cls := next(i, None)) is not None:
            if len(cls) != 2:
                strictcomplex = True
            else:
                nof_negative_clauses = sum(var.startswith('-') for var in cls)
                if nof_negative_clauses not in [1, 2]:
                    strictcomplex = True
        return not strictcomplex
@jmhorcas jmhorcas added the enhancement New feature or request label Jul 12, 2022
@jmhorcas jmhorcas added this to the Sprint 3 (After MODEVAR) milestone Jul 12, 2022
@jmhorcas jmhorcas self-assigned this Jul 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant