예제 #1
0
def test_logic_not():
    assert Not('a') == '!a'
    assert Not('!a') == 'a'

    # NOTE: we may want to change default Not behaviour and put this
    # functionality into some method.
    assert Not(And('a', 'b')) == Or('!a', '!b')
    assert Not(Or('a', 'b')) == And('!a', '!b')
예제 #2
0
def test_logic_cmp():
    l1 = And('a', Not('b'))
    l2 = And('a', Not('b'))

    assert hash(l1) == hash(l2)
    assert (l1 == l2) == T
    assert (l1 != l2) == F

    assert And('a', 'b', 'c') == And('b', 'a', 'c')
    assert And('a', 'b', 'c') == And('c', 'b', 'a')
    assert And('a', 'b', 'c') == And('c', 'a', 'b')
예제 #3
0
def test_logic_expand():
    t = And(Or('a','b'), 'c')
    assert t.expand()  == Or(And('a','c'), And('b','c'))

    t = And(Or('a', Not('b')), 'b')
    assert t.expand()  == And('a','b')

    t = And(Or('a','b'), Or('c','d'))
    assert t.expand()  == Or(And('a','c'), And('a','d'), And('b','c'), And('b','d'))
예제 #4
0
def test_deduce_alpha_implications():
    def D(i):
        I = deduce_alpha_implications(i)
        P = rules_2prereq(
            dict(((k, True), set([(v, True) for v in S]))
                 for k, S in I.items()))
        return I, P

    # transitivity
    I, P = D([('a', 'b'), ('b', 'c')])
    assert I == {'a': set(['b', 'c']), 'b': set(['c'])}
    assert P == {'b': set(['a']), 'c': set(['a', 'b'])}

    # see if the output does not contain repeated implications
    I, P = D([('a', 'b'), ('b', 'c'), ('b', 'c')])
    assert I == {'a': set(['b', 'c']), 'b': set(['c'])}
    assert P == {'b': set(['a']), 'c': set(['a', 'b'])}

    # see if it is tolerant to cycles
    assert D([('a', 'a'), ('a', 'a')]) == ({}, {})
    assert D([('a', 'b'), ('b', 'a')]) == ({
        'a': set(['b']),
        'b': set(['a'])
    }, {
        'a': set(['b']),
        'b': set(['a'])
    })

    # see if it catches inconsistency
    raises(ValueError, lambda: D([('a', Not('a'))]))
    raises(ValueError, lambda: D([('a', 'b'), ('b', Not('a'))]))
    raises(ValueError, lambda: D([('a', 'b'), ('b', 'c'), ('b', 'na'),
                                  ('na', Not('a'))]))

    # something related to real-world
    I, P = D([('rat', 'real'), ('int', 'rat')])

    assert I == {'int': set(['rat', 'real']), 'rat': set(['real'])}
    assert P == {'rat': set(['int']), 'real': set(['int', 'rat'])}
예제 #5
0
def test_logic_cmp():
    l1 = And('a', Not('b'))
    l2 = And('a', Not('b'))

    assert hash(l1) == hash(l2)
    assert (l1 == l2) == T
    assert (l1 != l2) == F

    assert And('a', 'b', 'c') == And('b', 'a', 'c')
    assert And('a', 'b', 'c') == And('c', 'b', 'a')
    assert And('a', 'b', 'c') == And('c', 'a', 'b')

    assert Not('a') < Not('b')
    assert (Not('b') < Not('a')) is False
    if PY3:
        assert (Not('a') < 2) is False
예제 #6
0
def test_logic_fromstring():
    S = Logic.fromstring

    assert S('a') == 'a'
    assert S('!a') == Not('a')
    assert S('a & b') == And('a', 'b')
    assert S('a | b') == Or('a', 'b')
    assert S('a | b & c') == And(Or('a', 'b'), 'c')
    assert S('a & b | c') == Or(And('a', 'b'), 'c')
    assert S('a & b & c') == And('a', 'b', 'c')
    assert S('a | b | c') == Or('a', 'b', 'c')

    raises(ValueError, lambda: S('| a'))
    raises(ValueError, lambda: S('& a'))
    raises(ValueError, lambda: S('a | | b'))
    raises(ValueError, lambda: S('a | & b'))
    raises(ValueError, lambda: S('a & & b'))
    raises(ValueError, lambda: S('a |'))
예제 #7
0
def test_logic_xnotx():
    assert And('a', Not('a')) == F
    assert Or('a', Not('a')) == T
예제 #8
0
def test_logic_not():
    assert Not('a') != '!a'
    assert Not('!a') != 'a'
    assert Not(True) == False
    assert Not(False) == True

    # NOTE: we may want to change default Not behaviour and put this
    # functionality into some method.
    assert Not(And('a', 'b')) == Or(Not('a'), Not('b'))
    assert Not(Or('a', 'b')) == And(Not('a'), Not('b'))

    raises(ValueError, lambda: Not(1))
예제 #9
0
def test_deduce_alpha_implications():
    def D(i):
        I = deduce_alpha_implications(i)
        P = rules_2prereq(
            dict(((k, True), {(v, True)
                              for v in S}) for k, S in I.items()))
        return I, P

    # transitivity
    I, P = D([("a", "b"), ("b", "c")])
    assert I == {
        "a": set(["b", "c"]),
        "b": set(["c"]),
        Not("b"): set([Not("a")]),
        Not("c"): set([Not("a"), Not("b")]),
    }
    assert P == {
        "a": set(["b", "c"]),
        "b": set(["a", "c"]),
        "c": set(["a", "b"])
    }

    # Duplicate entry
    I, P = D([("a", "b"), ("b", "c"), ("b", "c")])
    assert I == {
        "a": set(["b", "c"]),
        "b": set(["c"]),
        Not("b"): set([Not("a")]),
        Not("c"): set([Not("a"), Not("b")]),
    }
    assert P == {
        "a": set(["b", "c"]),
        "b": set(["a", "c"]),
        "c": set(["a", "b"])
    }

    # see if it is tolerant to cycles
    assert D([("a", "a"), ("a", "a")]) == ({}, {})
    assert D([("a", "b"), ("b", "a")]) == (
        {
            "a": set(["b"]),
            "b": set(["a"]),
            Not("a"): set([Not("b")]),
            Not("b"): set([Not("a")]),
        },
        {
            "a": set(["b"]),
            "b": set(["a"])
        },
    )

    # see if it catches inconsistency
    raises(ValueError, lambda: D([("a", Not("a"))]))
    raises(ValueError, lambda: D([("a", "b"), ("b", Not("a"))]))
    raises(ValueError, lambda: D([("a", "b"), ("b", "c"), ("b", "na"),
                                  ("na", Not("a"))]))

    # see if it handles implications with negations
    I, P = D([("a", Not("b")), ("c", "b")])
    assert I == {
        "a": set([Not("b"), Not("c")]),
        "b": set([Not("a")]),
        "c": set(["b", Not("a")]),
        Not("b"): set([Not("c")]),
    }
    assert P == {
        "a": set(["b", "c"]),
        "b": set(["a", "c"]),
        "c": set(["a", "b"])
    }
    I, P = D([(Not("a"), "b"), ("a", "c")])
    assert I == {
        "a": set(["c"]),
        Not("a"): set(["b"]),
        Not("b"): set(["a", "c"]),
        Not("c"): set([Not("a"), "b"]),
    }
    assert P == {
        "a": set(["b", "c"]),
        "b": set(["a", "c"]),
        "c": set(["a", "b"])
    }

    # Long deductions
    I, P = D([("a", "b"), ("b", "c"), ("c", "d"), ("d", "e")])
    assert I == {
        "a": set(["b", "c", "d", "e"]),
        "b": set(["c", "d", "e"]),
        "c": set(["d", "e"]),
        "d": set(["e"]),
        Not("b"): set([Not("a")]),
        Not("c"): set([Not("a"), Not("b")]),
        Not("d"): set([Not("a"), Not("b"), Not("c")]),
        Not("e"): set([Not("a"), Not("b"),
                       Not("c"), Not("d")]),
    }
    assert P == {
        "a": set(["b", "c", "d", "e"]),
        "b": set(["a", "c", "d", "e"]),
        "c": set(["a", "b", "d", "e"]),
        "d": set(["a", "b", "c", "e"]),
        "e": set(["a", "b", "c", "d"]),
    }

    # something related to real-world
    I, P = D([("rat", "real"), ("int", "rat")])

    assert I == {
        "int": set(["rat", "real"]),
        "rat": set(["real"]),
        Not("real"): set([Not("rat"), Not("int")]),
        Not("rat"): set([Not("int")]),
    }
    assert P == {
        "rat": set(["int", "real"]),
        "real": set(["int", "rat"]),
        "int": set(["rat", "real"]),
    }
예제 #10
0
def test_apply_beta_to_alpha_route():
    APPLY = apply_beta_to_alpha_route

    # indicates empty alpha-chain with attached beta-rule #bidx
    def Q(bidx):
        return (set(), [bidx])

    # x -> a        &(a,b) -> x     --  x -> a
    A = {"x": set(["a"])}
    B = [(And("a", "b"), "x")]
    assert APPLY(A, B) == {"x": (set(["a"]), []), "a": Q(0), "b": Q(0)}

    # x -> a        &(a,!x) -> b    --  x -> a
    A = {"x": set(["a"])}
    B = [(And("a", Not("x")), "b")]
    assert APPLY(A, B) == {"x": (set(["a"]), []), Not("x"): Q(0), "a": Q(0)}

    # x -> a b      &(a,b) -> c     --  x -> a b c
    A = {"x": set(["a", "b"])}
    B = [(And("a", "b"), "c")]
    assert APPLY(A, B) == {
        "x": (set(["a", "b", "c"]), []),
        "a": Q(0),
        "b": Q(0)
    }

    # x -> a        &(a,b) -> y     --  x -> a [#0]
    A = {"x": set(["a"])}
    B = [(And("a", "b"), "y")]
    assert APPLY(A, B) == {"x": (set(["a"]), [0]), "a": Q(0), "b": Q(0)}

    # x -> a b c    &(a,b) -> c     --  x -> a b c
    A = {"x": set(["a", "b", "c"])}
    B = [(And("a", "b"), "c")]
    assert APPLY(A, B) == {
        "x": (set(["a", "b", "c"]), []),
        "a": Q(0),
        "b": Q(0)
    }

    # x -> a b      &(a,b,c) -> y   --  x -> a b [#0]
    A = {"x": set(["a", "b"])}
    B = [(And("a", "b", "c"), "y")]
    assert APPLY(A, B) == {
        "x": (set(["a", "b"]), [0]),
        "a": Q(0),
        "b": Q(0),
        "c": Q(0)
    }

    # x -> a b      &(a,b) -> c     --  x -> a b c d
    # c -> d                            c -> d
    A = {"x": set(["a", "b"]), "c": set(["d"])}
    B = [(And("a", "b"), "c")]
    assert APPLY(A, B) == {
        "x": (set(["a", "b", "c", "d"]), []),
        "c": (set(["d"]), []),
        "a": Q(0),
        "b": Q(0),
    }

    # x -> a b      &(a,b) -> c     --  x -> a b c d e
    # c -> d        &(c,d) -> e         c -> d e
    A = {"x": set(["a", "b"]), "c": set(["d"])}
    B = [(And("a", "b"), "c"), (And("c", "d"), "e")]
    assert APPLY(A, B) == {
        "x": (set(["a", "b", "c", "d", "e"]), []),
        "c": (set(["d", "e"]), []),
        "a": Q(0),
        "b": Q(0),
        "d": Q(1),
    }

    # x -> a b      &(a,y) -> z     --  x -> a b y z
    #               &(a,b) -> y
    A = {"x": set(["a", "b"])}
    B = [(And("a", "y"), "z"), (And("a", "b"), "y")]
    assert APPLY(A, B) == {
        "x": (set(["a", "b", "y", "z"]), []),
        "a": (set(), [0, 1]),
        "y": Q(0),
        "b": Q(1),
    }

    # x -> a b      &(a,!b) -> c    --  x -> a b
    A = {"x": set(["a", "b"])}
    B = [(And("a", Not("b")), "c")]
    assert APPLY(A, B) == {
        "x": (set(["a", "b"]), []),
        "a": Q(0),
        Not("b"): Q(0)
    }

    # !x -> !a !b   &(!a,b) -> c    --  !x -> !a !b
    A = {Not("x"): set([Not("a"), Not("b")])}
    B = [(And(Not("a"), "b"), "c")]
    assert APPLY(A, B) == {
        Not("x"): (set([Not("a"), Not("b")]), []),
        Not("a"): Q(0),
        "b": Q(0),
    }

    # x -> a b      &(b,c) -> !a    --  x -> a b
    A = {"x": set(["a", "b"])}
    B = [(And("b", "c"), Not("a"))]
    assert APPLY(A, B) == {"x": (set(["a", "b"]), []), "b": Q(0), "c": Q(0)}

    # x -> a b      &(a, b) -> c    --  x -> a b c p
    # c -> p a
    A = {"x": set(["a", "b"]), "c": set(["p", "a"])}
    B = [(And("a", "b"), "c")]
    assert APPLY(A, B) == {
        "x": (set(["a", "b", "c", "p"]), []),
        "c": (set(["p", "a"]), []),
        "a": Q(0),
        "b": Q(0),
    }
예제 #11
0
def test_apply_beta_to_alpha_route():
    APPLY = apply_beta_to_alpha_route

    # indicates empty alpha-chain with attached beta-rule #bidx
    def Q(bidx):
        return (set(), [bidx])

    # x -> a        &(a,b) -> x     --  x -> a
    A = {'x': set(['a'])}
    B = [(And('a', 'b'), 'x')]
    assert APPLY(A, B) == {'x': (set(['a']), []), 'a': Q(0), 'b': Q(0)}

    # x -> a        &(a,!x) -> b    --  x -> a
    A = {'x': set(['a'])}
    B = [(And('a', Not('x')), 'b')]
    assert APPLY(A, B) == {'x': (set(['a']), []), Not('x'): Q(0), 'a': Q(0)}

    # x -> a b      &(a,b) -> c     --  x -> a b c
    A = {'x': set(['a', 'b'])}
    B = [(And('a', 'b'), 'c')]
    assert APPLY(A, B) == \
        {'x': (set(['a', 'b', 'c']), []), 'a': Q(0), 'b': Q(0)}

    # x -> a        &(a,b) -> y     --  x -> a [#0]
    A = {'x': set(['a'])}
    B = [(And('a', 'b'), 'y')]
    assert APPLY(A, B) == {'x': (set(['a']), [0]), 'a': Q(0), 'b': Q(0)}

    # x -> a b c    &(a,b) -> c     --  x -> a b c
    A = {'x': set(['a', 'b', 'c'])}
    B = [(And('a', 'b'), 'c')]
    assert APPLY(A, B) == \
        {'x': (set(['a', 'b', 'c']), []), 'a': Q(0), 'b': Q(0)}

    # x -> a b      &(a,b,c) -> y   --  x -> a b [#0]
    A = {'x': set(['a', 'b'])}
    B = [(And('a', 'b', 'c'), 'y')]
    assert APPLY(A, B) == \
        {'x': (set(['a', 'b']), [0]), 'a': Q(0), 'b': Q(0), 'c': Q(0)}

    # x -> a b      &(a,b) -> c     --  x -> a b c d
    # c -> d                            c -> d
    A = {'x': set(['a', 'b']), 'c': set(['d'])}
    B = [(And('a', 'b'), 'c')]
    assert APPLY(A, B) == {'x': (set(['a', 'b', 'c', 'd']), []),
        'c': (set(['d']), []), 'a': Q(0), 'b': Q(0)}

    # x -> a b      &(a,b) -> c     --  x -> a b c d e
    # c -> d        &(c,d) -> e         c -> d e
    A = {'x': set(['a', 'b']), 'c': set(['d'])}
    B = [(And('a', 'b'), 'c'), (And('c', 'd'), 'e')]
    assert APPLY(A, B) == {'x': (set(['a', 'b', 'c', 'd', 'e']), []),
        'c': (set(['d', 'e']), []), 'a': Q(0), 'b': Q(0), 'd': Q(1)}

    # x -> a b      &(a,y) -> z     --  x -> a b y z
    #               &(a,b) -> y
    A = {'x': set(['a', 'b'])}
    B = [(And('a', 'y'), 'z'), (And('a', 'b'), 'y')]
    assert APPLY(A, B) == {'x': (set(['a', 'b', 'y', 'z']), []),
        'a': (set(), [0, 1]), 'y': Q(0), 'b': Q(1)}

    # x -> a b      &(a,!b) -> c    --  x -> a b
    A = {'x': set(['a', 'b'])}
    B = [(And('a', Not('b')), 'c')]
    assert APPLY(A, B) == \
        {'x': (set(['a', 'b']), []), 'a': Q(0), Not('b'): Q(0)}

    # !x -> !a !b   &(!a,b) -> c    --  !x -> !a !b
    A = {Not('x'): set([Not('a'), Not('b')])}
    B = [(And(Not('a'), 'b'), 'c')]
    assert APPLY(A, B) == \
        {Not('x'): (set([Not('a'), Not('b')]), []), Not('a'): Q(0), 'b': Q(0)}

    # x -> a b      &(b,c) -> !a    --  x -> a b
    A = {'x': set(['a', 'b'])}
    B = [(And('b', 'c'), Not('a'))]
    assert APPLY(A, B) == {'x': (set(['a', 'b']), []), 'b': Q(0), 'c': Q(0)}

    # x -> a b      &(a, b) -> c    --  x -> a b c p
    # c -> p a
    A = {'x': set(['a', 'b']), 'c': set(['p', 'a'])}
    B = [(And('a', 'b'), 'c')]
    assert APPLY(A, B) == {'x': (set(['a', 'b', 'c', 'p']), []),
        'c': (set(['p', 'a']), []), 'a': Q(0), 'b': Q(0)}
예제 #12
0
def test_deduce_alpha_implications():
    def D(i):
        I = deduce_alpha_implications(i)
        P = rules_2prereq(dict(
            ((k, True), set([(v, True) for v in S])) for k, S in I.items()))
        return I, P

    # transitivity
    I, P = D([('a', 'b'), ('b', 'c')])
    assert I == {'a': set(['b', 'c']), 'b': set(['c']), Not('b'):
        set([Not('a')]), Not('c'): set([Not('a'), Not('b')])}
    assert P == {'a': set(['b', 'c']), 'b': set(['a', 'c']), 'c': set(['a', 'b'])}

    # Duplicate entry
    I, P = D([('a', 'b'), ('b', 'c'), ('b', 'c')])
    assert I == {'a': set(['b', 'c']), 'b': set(['c']), Not('b'): set([Not('a')]), Not('c'): set([Not('a'), Not('b')])}
    assert P == {'a': set(['b', 'c']), 'b': set(['a', 'c']), 'c': set(['a', 'b'])}

    # see if it is tolerant to cycles
    assert D([('a', 'a'), ('a', 'a')]) == ({}, {})
    assert D([('a', 'b'), ('b', 'a')]) == (
        {'a': set(['b']), 'b': set(['a']), Not('a'): set([Not('b')]), Not('b'): set([Not('a')])},
        {'a': set(['b']), 'b': set(['a'])})

    # see if it catches inconsistency
    raises(ValueError, lambda: D([('a', Not('a'))]))
    raises(ValueError, lambda: D([('a', 'b'), ('b', Not('a'))]))
    raises(ValueError, lambda: D([('a', 'b'), ('b', 'c'), ('b', 'na'),
           ('na', Not('a'))]))

    # see if it handles implications with negations
    I, P = D([('a', Not('b')), ('c', 'b')])
    assert I == {'a': set([Not('b'), Not('c')]), 'b': set([Not('a')]), 'c': set(['b', Not('a')]), Not('b'): set([Not('c')])}
    assert P == {'a': set(['b', 'c']), 'b': set(['a', 'c']), 'c': set(['a', 'b'])}
    I, P = D([(Not('a'), 'b'), ('a', 'c')])
    assert I == {'a': set(['c']), Not('a'): set(['b']), Not('b'): set(['a',
    'c']), Not('c'): set([Not('a'), 'b']),}
    assert P == {'a': set(['b', 'c']), 'b': set(['a', 'c']), 'c': set(['a', 'b'])}


    # Long deductions
    I, P = D([('a', 'b'), ('b', 'c'), ('c', 'd'), ('d', 'e')])
    assert I == {'a': set(['b', 'c', 'd', 'e']), 'b': set(['c', 'd', 'e']),
        'c': set(['d', 'e']), 'd': set(['e']), Not('b'): set([Not('a')]),
        Not('c'): set([Not('a'), Not('b')]), Not('d'): set([Not('a'), Not('b'),
            Not('c')]), Not('e'): set([Not('a'), Not('b'), Not('c'), Not('d')])}
    assert P == {'a': set(['b', 'c', 'd', 'e']), 'b': set(['a', 'c', 'd',
        'e']), 'c': set(['a', 'b', 'd', 'e']), 'd': set(['a', 'b', 'c', 'e']),
        'e': set(['a', 'b', 'c', 'd'])}

    # something related to real-world
    I, P = D([('rat', 'real'), ('int', 'rat')])

    assert I == {'int': set(['rat', 'real']), 'rat': set(['real']),
        Not('real'): set([Not('rat'), Not('int')]), Not('rat'): set([Not('int')])}
    assert P == {'rat': set(['int', 'real']), 'real': set(['int', 'rat']),
        'int': set(['rat', 'real'])}