コード例 #1
0
ファイル: test_pcabuilder.py プロジェクト: p2424630/PCA
 def test_simple_props(self):
     to_test = [[pcaprop.FalseProp(),
                 pcaprop.FalseProp()],
                [pcaprop.TrueProp(), pcaprop.TrueProp()]]
     self.assertEqual(list(pcabuilder.InitProp('A').interpretations()),
                      to_test)
     to_test = [[pcaprop.FalseProp(),
                 pcaprop.TrueProp()],
                [pcaprop.TrueProp(),
                 pcaprop.FalseProp()]]
     self.assertEqual(
         list(pcabuilder.InitProp('not (not (not A))').interpretations()),
         to_test)
コード例 #2
0
 def test_prop(self):
     a = pcaprop.TrueProp()
     b = pcaprop.NegationOp(a)
     self.assertTrue(a)
     self.assertFalse(b.eval())
     self.assertTrue(pcaprop.DisjunctionOp(a, b.eval()).eval())
     self.assertFalse(pcaprop.ConjunctionOp(b.eval(), a).eval())
コード例 #3
0
ファイル: pcabuilder.py プロジェクト: p2424630/PCA
    def contradiction(self):
        """
        Check if self is a contradiction by returning the opposite of satisfiable.
        :return: bool (Class)
        """

        return pcaprop.FalseProp() if self.satisfiable() else pcaprop.TrueProp(
        )
コード例 #4
0
ファイル: pcabuilder.py プロジェクト: p2424630/PCA
    def tautology(self):
        """
        Check if self is a tautology, this is calculated based on all interpretations.
        :return: bool (Class)
        """

        if len(self.unique_vars()) < 1:
            return self.eval_prop(self._parsed)
        for i in self.interpretations():
            if not i[-1]:
                return pcaprop.FalseProp()
        return pcaprop.TrueProp()
コード例 #5
0
ファイル: test_pcabuilder.py プロジェクト: p2424630/PCA
 def test_disjunction(self):
     to_test = [
         [pcaprop.FalseProp(),
          pcaprop.FalseProp(),
          pcaprop.FalseProp()],
         [pcaprop.FalseProp(),
          pcaprop.TrueProp(),
          pcaprop.TrueProp()],
         [pcaprop.TrueProp(),
          pcaprop.FalseProp(),
          pcaprop.TrueProp()],
         [pcaprop.TrueProp(),
          pcaprop.TrueProp(),
          pcaprop.TrueProp()]
     ]
     self.assertEqual(list(pcabuilder.InitProp('A or B').interpretations()),
                      to_test)
     to_test = [[pcaprop.FalseProp(),
                 pcaprop.TrueProp()],
                [pcaprop.TrueProp(), pcaprop.TrueProp()]]
     self.assertEqual(
         list(pcabuilder.InitProp('A or true').interpretations()), to_test)
コード例 #6
0
ファイル: test_pcabuilder.py プロジェクト: p2424630/PCA
 def test_maximum(self):
     self.assertTrue(
         pcabuilder.InitProp('A or true').maximum() == pcaprop.TrueProp())
     self.assertTrue(
         pcabuilder.InitProp('true or (A iff B)').maximum() ==
         pcaprop.TrueProp())
     self.assertTrue(
         pcabuilder.InitProp('B iff (A and (true or (A iff B)))').maximum()
         == pcaprop.EquivalenceOp(pcaprop.Variable('B'),
                                  pcaprop.Variable('A')))
     self.assertTrue(
         pcabuilder.InitProp('A and true').maximum() == pcaprop.Variable(
             'A'))
     self.assertTrue(
         pcabuilder.InitProp('true and (A iff B)').maximum() == pcaprop.
         EquivalenceOp(pcaprop.Variable('A'), pcaprop.Variable('B')))
     self.assertTrue(
         pcabuilder.InitProp('A and (B and top)').maximum() == pcaprop.
         ConjunctionOp(pcaprop.Variable('A'), pcaprop.Variable('B')))
     self.assertTrue(
         pcabuilder.InitProp('Z iff (A or (B or top))').maximum() ==
         pcaprop.EquivalenceOp(pcaprop.Variable('Z'), pcaprop.TrueProp()))
コード例 #7
0
ファイル: test_pcaparser.py プロジェクト: p2424630/PCA
 def test_prop(self):
     a = pcaparser.PARSER.parse('P or B iff C and A implies (P and C)')
     to_assert = pcaprop.EquivalenceOp(pcaprop.DisjunctionOp(pcaprop.Variable('P'), pcaprop.Variable('B')),
                                       pcaprop.ImplicationOp(
                                           pcaprop.ConjunctionOp(pcaprop.Variable('C'), pcaprop.Variable('A')),
                                           pcaprop.ConjunctionOp(pcaprop.Variable('P'), pcaprop.Variable('C'))))
     self.assertEqual(a, to_assert)
     a = pcaparser.PARSER.parse('true or not A implies false and not not not B or A')
     to_assert = pcaprop.ImplicationOp(
         pcaprop.DisjunctionOp(pcaprop.TrueProp(), pcaprop.NegationOp(pcaprop.Variable('A'))),
         pcaprop.DisjunctionOp(
             pcaprop.ConjunctionOp(pcaprop.FalseProp(), pcaprop.NegationOp(
                 pcaprop.NegationOp(pcaprop.NegationOp(pcaprop.Variable('B'))))),
             pcaprop.Variable('A')))
     self.assertEqual(a, to_assert)
コード例 #8
0
ファイル: pcalaws.py プロジェクト: p2424630/PCA
 def _maximum(op):
     if isinstance(op, (pcaprop.Variable, pcaprop.TrueProp, pcaprop.FalseProp)):
         return op
     if isinstance(op, pcaprop.UnaryOp):
         return op.__class__(_maximum(op.prop))
     if isinstance(op, pcaprop.BinaryOp):
         prop_l = _maximum(op.prop_l)
         prop_r = _maximum(op.prop_r)
         if isinstance(op, pcaprop.DisjunctionOp):
             if any(isinstance(prop, pcaprop.TrueProp) for prop in [prop_l, prop_r]):
                 return pcaprop.TrueProp()
         if isinstance(op, pcaprop.ConjunctionOp):
             if isinstance(prop_l, pcaprop.TrueProp):
                 return prop_r
             if isinstance(prop_r, pcaprop.TrueProp):
                 return prop_l
         return op.__class__(prop_l, prop_r)
コード例 #9
0
ファイル: pcabuilder.py プロジェクト: p2424630/PCA
    def interpretations(self):
        """
        Create all possible interpretations for all unique variables. For each interpretation replace the variables in
        the proposition and evaluate, the result is then appended to the current interpretation's list and yielded.
        :return: iterable
        """
        def _get_interp(op, interp):
            """
            Recursively traverse proposition and replace all instances of variables with the interpretation mapping.
            :param op: Proposition
            :param interp: Dictionary mapping variables to TrueProp or FalseProp.
            :return: Proposition with FalseProp/TrueProp instead of Variables.
            """

            if isinstance(op, (pcaprop.TrueProp, pcaprop.FalseProp)):
                return op
            if isinstance(op, pcaprop.Variable):
                return interp[op]
            if isinstance(op, pcaprop.UnaryOp):
                return op.__class__(_get_interp(op.prop, interp))
            if isinstance(op, pcaprop.BinaryOp):
                prop_l = _get_interp(op.prop_l, interp)
                prop_r = _get_interp(op.prop_r, interp)
                return op.__class__(prop_l, prop_r)

        prop_vars = self.unique_vars()
        len_prop_vars = len(prop_vars)
        if len_prop_vars < 1:
            # Return empty interpretation as this means the proposition is composed without any variables,
            # which means it can only take one, the current, interpretation.
            return
        # List of tuples for all possible interpretations for given number of variables
        combinations = product(
            [pcaprop.FalseProp(), pcaprop.TrueProp()], repeat=len_prop_vars)
        for combination in combinations:
            # Create a dictionary mapping the variables to the current interpretation values.
            interp = dict(zip(prop_vars, combination))
            # Replace the variables in the proposition with the current interpretation mapping.
            interp_prop = _get_interp(self._parsed, interp)
            interp_values = list(interp.values())
            # For the current interpretation evaluate the proposition and append the result in the original
            # interpretation list.
            interp_values.append(self.eval_prop(interp_prop))
            yield interp_values
コード例 #10
0
ファイル: pcaparser.py プロジェクト: p2424630/PCA
 def atom_true(self, value):
     return pcaprop.TrueProp()
コード例 #11
0
ファイル: test_pcabuilder.py プロジェクト: p2424630/PCA
 def test_comb(self):
     to_test = [[
         pcaprop.FalseProp(),
         pcaprop.FalseProp(),
         pcaprop.FalseProp(),
         pcaprop.FalseProp(),
         pcaprop.TrueProp()
     ],
                [
                    pcaprop.FalseProp(),
                    pcaprop.FalseProp(),
                    pcaprop.FalseProp(),
                    pcaprop.TrueProp(),
                    pcaprop.TrueProp()
                ],
                [
                    pcaprop.FalseProp(),
                    pcaprop.FalseProp(),
                    pcaprop.TrueProp(),
                    pcaprop.FalseProp(),
                    pcaprop.FalseProp()
                ],
                [
                    pcaprop.FalseProp(),
                    pcaprop.FalseProp(),
                    pcaprop.TrueProp(),
                    pcaprop.TrueProp(),
                    pcaprop.FalseProp()
                ],
                [
                    pcaprop.FalseProp(),
                    pcaprop.TrueProp(),
                    pcaprop.FalseProp(),
                    pcaprop.FalseProp(),
                    pcaprop.TrueProp()
                ],
                [
                    pcaprop.FalseProp(),
                    pcaprop.TrueProp(),
                    pcaprop.FalseProp(),
                    pcaprop.TrueProp(),
                    pcaprop.TrueProp()
                ],
                [
                    pcaprop.FalseProp(),
                    pcaprop.TrueProp(),
                    pcaprop.TrueProp(),
                    pcaprop.FalseProp(),
                    pcaprop.FalseProp()
                ],
                [
                    pcaprop.FalseProp(),
                    pcaprop.TrueProp(),
                    pcaprop.TrueProp(),
                    pcaprop.TrueProp(),
                    pcaprop.FalseProp()
                ],
                [
                    pcaprop.TrueProp(),
                    pcaprop.FalseProp(),
                    pcaprop.FalseProp(),
                    pcaprop.FalseProp(),
                    pcaprop.FalseProp()
                ],
                [
                    pcaprop.TrueProp(),
                    pcaprop.FalseProp(),
                    pcaprop.FalseProp(),
                    pcaprop.TrueProp(),
                    pcaprop.FalseProp()
                ],
                [
                    pcaprop.TrueProp(),
                    pcaprop.FalseProp(),
                    pcaprop.TrueProp(),
                    pcaprop.FalseProp(),
                    pcaprop.TrueProp()
                ],
                [
                    pcaprop.TrueProp(),
                    pcaprop.FalseProp(),
                    pcaprop.TrueProp(),
                    pcaprop.TrueProp(),
                    pcaprop.FalseProp()
                ],
                [
                    pcaprop.TrueProp(),
                    pcaprop.TrueProp(),
                    pcaprop.FalseProp(),
                    pcaprop.FalseProp(),
                    pcaprop.TrueProp()
                ],
                [
                    pcaprop.TrueProp(),
                    pcaprop.TrueProp(),
                    pcaprop.FalseProp(),
                    pcaprop.TrueProp(),
                    pcaprop.TrueProp()
                ],
                [
                    pcaprop.TrueProp(),
                    pcaprop.TrueProp(),
                    pcaprop.TrueProp(),
                    pcaprop.FalseProp(),
                    pcaprop.FalseProp()
                ],
                [
                    pcaprop.TrueProp(),
                    pcaprop.TrueProp(),
                    pcaprop.TrueProp(),
                    pcaprop.TrueProp(),
                    pcaprop.TrueProp()
                ]]
     self.assertEqual(
         list(
             pcabuilder.InitProp(
                 'A and D or not C iff A implies B').interpretations()),
         to_test)