示例#1
0
 def test_init(self):
     algebra = BooleanAlgebra()
     self.assertRaises(TypeError, algebra.NOT)
     self.assertRaises(TypeError, algebra.NOT, 'a', 'b')
     algebra.NOT(algebra.Symbol('a'))
     self.assertEqual(algebra.FALSE, (algebra.NOT(algebra.TRUE)).simplify())
     self.assertEqual(algebra.TRUE, (algebra.NOT(algebra.FALSE)).simplify())
示例#2
0
    def test_parse_with_mixed_operators_multilines_and_custom_symbol(self):
        class MySymbol(Symbol):
            pass

        expr_str = '''(a or ~ b +_c  ) and
                      d & ( ! e_
                      | (my * g OR 1 or 0) ) AND that '''

        algebra = BooleanAlgebra(Symbol_class=MySymbol)
        expr = algebra.parse(expr_str)

        expected = algebra.AND(
            algebra.OR(
                algebra.Symbol('a'),
                algebra.NOT(algebra.Symbol('b')),
                algebra.Symbol('_c'),
            ),
            algebra.Symbol('d'),
            algebra.OR(
                algebra.NOT(algebra.Symbol('e_')),
                algebra.OR(
                    algebra.AND(
                        algebra.Symbol('my'),
                        algebra.Symbol('g'),
                    ),
                    algebra.TRUE,
                    algebra.FALSE,
                ),
            ),
            algebra.Symbol('that'),
        )

        self.assertEqual(expected.pretty(), expr.pretty())
        self.assertEqual(expected, expr)
示例#3
0
    def test_simplify_complex_expression_parsed_then_simplified(self):
        # FIXME: THIS SHOULD NOT FAIL

        algebra = BooleanAlgebra()
        a = algebra.Symbol('a')
        b = algebra.Symbol('b')
        c = algebra.Symbol('c')
        d = algebra.Symbol('d')
        parse = algebra.parse

        test_expression_str = ''.join('''
            (~a&~b&~c&~d) | (~a&~b&~c&d) | (~a&b&~c&~d) |
            (~a&b&c&d) | (~a&b&~c&d) | (~a&b&c&~d) |
            (a&~b&~c&d) | (~a&b&c&d) | (a&~b&c&d) | (a&b&c&d)
        '''.split())

        test_expression = ((~a & ~b & ~c & ~d) | (~a & ~b & ~c & d) |
                           (~a & b & ~c & ~d) | (~a & b & c & d) |
                           (~a & b & ~c & d) | (~a & b & c & ~d) |
                           (a & ~b & ~c & d) | (~a & b & c & d) |
                           (a & ~b & c & d) | (a & b & c & d))

        parsed = parse(test_expression_str)
        self.assertEqual(test_expression_str, str(parsed))

        expected = (a & ~b & d) | (~a & b) | (~a & ~c) | (b & c & d)
        self.assertEqual(expected.pretty(),
                         test_expression.simplify().pretty())

        parsed = parse(test_expression_str, simplify=True)

        # FIXME: THIS SHOULD NOT FAIL
        # we have a different simplify behavior for expressions built from python expressions
        # vs. expression built from an object tree vs. expression built from a parse
        self.assertEqual(expected.simplify().pretty(),
                         parsed.simplify().pretty())

        expected_str = '(a&~b&d)|(~a&b)|(~a&~c)|(b&c&d)'
        self.assertEqual(expected_str, str(parsed))

        parsed2 = parse(test_expression_str)
        self.assertEqual(expected.pretty(), parsed2.simplify().pretty())

        self.assertEqual(expected_str, str(parsed2.simplify()))

        expected = algebra.OR(
            algebra.AND(algebra.NOT(algebra.Symbol('a')),
                        algebra.NOT(algebra.Symbol('b')),
                        algebra.NOT(algebra.Symbol('c')),
                        algebra.NOT(algebra.Symbol('d'))),
            algebra.AND(algebra.NOT(algebra.Symbol('a')),
                        algebra.NOT(algebra.Symbol('b')),
                        algebra.NOT(algebra.Symbol('c')), algebra.Symbol('d')),
            algebra.AND(algebra.NOT(algebra.Symbol('a')), algebra.Symbol('b'),
                        algebra.NOT(algebra.Symbol('c')),
                        algebra.NOT(algebra.Symbol('d'))),
            algebra.AND(algebra.NOT(algebra.Symbol('a')), algebra.Symbol('b'),
                        algebra.Symbol('c'), algebra.Symbol('d')),
            algebra.AND(algebra.NOT(algebra.Symbol('a')), algebra.Symbol('b'),
                        algebra.NOT(algebra.Symbol('c')), algebra.Symbol('d')),
            algebra.AND(algebra.NOT(algebra.Symbol('a')), algebra.Symbol('b'),
                        algebra.Symbol('c'), algebra.NOT(algebra.Symbol('d'))),
            algebra.AND(algebra.Symbol('a'), algebra.NOT(algebra.Symbol('b')),
                        algebra.NOT(algebra.Symbol('c')), algebra.Symbol('d')),
            algebra.AND(algebra.NOT(algebra.Symbol('a')), algebra.Symbol('b'),
                        algebra.Symbol('c'), algebra.Symbol('d')),
            algebra.AND(algebra.Symbol('a'), algebra.NOT(algebra.Symbol('b')),
                        algebra.Symbol('c'), algebra.Symbol('d')),
            algebra.AND(algebra.Symbol('a'), algebra.Symbol('b'),
                        algebra.Symbol('c'), algebra.Symbol('d')))

        result = parse(test_expression_str)
        result = result.simplify()
        self.assertEqual(expected, result)
示例#4
0
 def test_isliteral(self):
     algebra = BooleanAlgebra()
     s = algebra.Symbol(1)
     self.assertTrue(algebra.NOT(s).isliteral)
     self.assertFalse(algebra.parse('~(a|b)').isliteral)