def test_FactRules_deduce2(): # pos/neg/zero, but the rules are not sufficient to derive all relations f = FactRules(['pos -> ~neg', 'pos -> ~z']) def D(facts): kb = FactKB(f) kb.deduce_all_facts(facts) return kb assert D({'pos': T}) == {'pos': T, 'neg': F, 'z': F} assert D({'pos': F}) == {'pos': F} assert D({'neg': T}) == {'pos': F, 'neg': T} assert D({'neg': F}) == {'neg': F} assert D({'z': T}) == {'pos': F, 'z': T} assert D({'z': F}) == {'z': F} # pos/neg/zero. rules are sufficient to derive all relations f = FactRules(['pos -> ~neg', 'neg -> ~pos', 'pos -> ~z', 'neg -> ~z']) assert D({'pos': T}) == {'pos': T, 'neg': F, 'z': F} assert D({'pos': F}) == {'pos': F} assert D({'neg': T}) == {'pos': F, 'neg': T, 'z': F} assert D({'neg': F}) == {'neg': F} assert D({'z': T}) == {'pos': F, 'neg': F, 'z': T} assert D({'z': F}) == {'z': F}
def test_FactRules_parse(): f = FactRules('a -> b') assert f.prereq == {'b': {'a'}, 'a': {'b'}} f = FactRules('a -> ~b') assert f.prereq == {'b': {'a'}, 'a': {'b'}} f = FactRules('~a -> b') assert f.prereq == {'b': {'a'}, 'a': {'b'}} f = FactRules('~a -> ~b') assert f.prereq == {'b': {'a'}, 'a': {'b'}} f = FactRules('~z == nz') assert f.prereq == {'z': {'nz'}, 'nz': {'z'}} pytest.raises(ValueError, lambda: FactRules('a ? b')) f = FactRules('a -> b & ~b') # trivial test assert not f.prereq # tautologies assert not FactRules('a -> a | b').prereq # XXX: We don't support non-atomic left operands, so use Prover directly p = Prover() p.process_rule(And('a', 'b'), 'a') p.process_rule(Or('a', 'b'), 'a') assert not p.proved_rules
def test_FactRules_deduce_multiple2(): f = FactRules(['real == neg | zero | pos']) def D(facts): kb = FactKB(f) kb.deduce_all_facts(facts) return kb assert D({'real': T}) == {'real': T} assert D({'real': F}) == {'real': F, 'neg': F, 'zero': F, 'pos': F} assert D({'neg': T}) == {'real': T, 'neg': T} assert D({'zero': T}) == {'real': T, 'zero': T} assert D({'pos': T}) == {'real': T, 'pos': T} # --- key tests below --- assert D({'neg': F, 'zero': F, 'pos': F}) == {'real': F, 'neg': F, 'zero': F, 'pos': F} assert D({'real': T, 'neg': F}) == {'real': T, 'neg': F} assert D({'real': T, 'zero': F}) == {'real': T, 'zero': F} assert D({'real': T, 'pos': F}) == {'real': T, 'pos': F} assert D({'real': T, 'zero': F, 'pos': F}) == {'real': T, 'neg': T, 'zero': F, 'pos': F} assert D({'real': T, 'neg': F, 'pos': F}) == {'real': T, 'neg': F, 'zero': T, 'pos': F} assert D({'real': T, 'neg': F, 'zero': F }) == {'real': T, 'neg': F, 'zero': F, 'pos': T} assert D({'neg': T, 'zero': F, 'pos': F}) == {'real': T, 'neg': T, 'zero': F, 'pos': F} assert D({'neg': F, 'zero': T, 'pos': F}) == {'real': T, 'neg': F, 'zero': T, 'pos': F} assert D({'neg': F, 'zero': F, 'pos': T}) == {'real': T, 'neg': F, 'zero': F, 'pos': T}
def test_inconsistent_assumptions(): kb = FactKB(FactRules(['npos -> ~pos', 'pos -> ~npos'])) with pytest.raises(InconsistentAssumptions) as err: kb.deduce_all_facts({'pos': T, 'npos': T}) assert str(err.value) in [ '{\n\tnpos: False,\n\tpos: True}, npos=True', '{\n\tnpos: True,\n\tpos: False}, pos=True' ]
def test_FactRules_deduce_staticext(): # verify that static beta-extensions deduction takes place f = FactRules([ 'real == neg | zero | pos', 'neg -> real & ~zero & ~pos', 'pos -> real & ~zero & ~neg', 'nneg == real & ~neg', 'npos == real & ~pos' ]) assert ('npos', True) in f.full_implications[('neg', True)] assert ('nneg', True) in f.full_implications[('pos', True)] assert ('nneg', True) in f.full_implications[('zero', True)] assert ('npos', True) in f.full_implications[('zero', True)]
def test_FactRules_deduce_base(): # deduction that starts from base f = FactRules(['real == neg | zero | pos', 'neg -> real & ~zero & ~pos', 'pos -> real & ~zero & ~neg']) base = FactKB(f) base.deduce_all_facts({'real': T, 'neg': F}) assert base == {'real': T, 'neg': F} base.deduce_all_facts({'zero': F}) assert base == {'real': T, 'neg': F, 'zero': F, 'pos': T}
def test_FactRules_deduce_multiple(): # deduction that involves _several_ starting points f = FactRules(['real == pos | npos']) def D(facts): kb = FactKB(f) kb.deduce_all_facts(facts) return kb assert D({'real': T}) == {'real': T} assert D({'real': F}) == {'real': F, 'pos': F, 'npos': F} assert D({'pos': T}) == {'real': T, 'pos': T} assert D({'npos': T}) == {'real': T, 'npos': T} # --- key tests below --- assert D({'pos': F, 'npos': F}) == {'real': F, 'pos': F, 'npos': F} assert D({'real': T, 'pos': F}) == {'real': T, 'pos': F, 'npos': T} assert D({'real': T, 'npos': F}) == {'real': T, 'pos': T, 'npos': F} assert D({'pos': T, 'npos': F}) == {'real': T, 'pos': T, 'npos': F} assert D({'pos': F, 'npos': T}) == {'real': T, 'pos': F, 'npos': T}
def test_FactRules_deduce(): f = FactRules(['a -> b', 'b -> c', 'b -> d', 'c -> e']) def D(facts): kb = FactKB(f) kb.deduce_all_facts(facts) return kb assert (str(D({'a': T})) == '{\n\ta: True,\n\tb: True,\n\tc: True,\n\td: True,\n\te: True}') assert D({'a': T}) == {'a': T, 'b': T, 'c': T, 'd': T, 'e': T} assert D({'b': T}) == { 'b': T, 'c': T, 'd': T, 'e': T} assert D({'c': T}) == { 'c': T, 'e': T} assert D({'d': T}) == { 'd': T } assert D({'e': T}) == { 'e': T} assert D({'a': F}) == {'a': F } assert D({'b': F}) == {'a': F, 'b': F } assert D({'c': F}) == {'a': F, 'b': F, 'c': F } assert D({'d': F}) == {'a': F, 'b': F, 'd': F } assert D({'a': U}) == {'a': U} # XXX ok?
def test_FactRules_parse2(): pytest.raises(ValueError, lambda: FactRules('a -> ~a'))