def test_NOT_doesnt_match_if_fact_is_not_present_and_InitialFact_neither(): from pyknow.rule import NOT from pyknow.factlist import FactList from pyknow.fact import Fact, InitialFact r = NOT(Fact(something=True)) fl = FactList() assert not r.get_activations(fl)
def test_NOT_match_InitialFact_if_fact_is_not_present(): from pyknow.rule import NOT from pyknow.factlist import FactList from pyknow.fact import Fact, InitialFact r = NOT(Fact(something=True)) fl = FactList() fl.declare(InitialFact()) assert r.get_activations(fl)
class Person_KE(KnowledgeEngine): @Rule(NOT(ConflictResolver(resolved=L(True))), AND(Person(name=L("name"), age=C('age')), Person(name=L("name"), age=N("age")))) def same_name(self, age): nonlocal executions executions.append(age)
class Parent(KnowledgeEngine): @Rule(Fact(inherited=L(True))) def inherited(self): executions.append(2) @Rule(NOT(Fact(not_inherited=L(True)))) def not_inherited(self): executions.append(3)
def test_Rule_with_only_one_NOT_match_InitialFact_if_fact_is_not_present(): from pyknow.rule import NOT, Rule from pyknow.factlist import FactList from pyknow.fact import Fact, InitialFact, L from pyknow.match import Capturation r = Rule(NOT(Fact(something=L(True)))) fl = FactList() fl.declare(InitialFact()) assert next(r.get_activations(fl, Capturation()), None) is not None
def test_Rule_and_NOT_nesting(): from pyknow.rule import Rule, NOT from pyknow.factlist import FactList from pyknow.fact import Fact, InitialFact, L r = Rule(Fact(a=L(1)), NOT(Fact(b=L(2)))) fl = FactList() fl.declare(InitialFact()) fl.declare(Fact(a=L(1))) activations = list(r.get_activations(fl)) assert len(activations) == 1 assert {0, 1} == set(activations[0].facts)
class Parent(KnowledgeEngine): @Rule(Fact(initial=L(True))) def shouldnot_run(self): # pytest.set_trace() print("Ran") executions.append(4) @Rule(Fact(inherited=L(True))) def inherited(self): executions.append(2) @Rule(NOT(Fact(not_inherited=L(True)))) def not_inherited(self): executions.append(3)
def test_Rule_with_NOT_DEFINED(): from pyknow.rule import Rule, NOT from pyknow.factlist import FactList from pyknow.fact import Fact, InitialFact, L, W r = Rule(Fact(a=L(1)), NOT(Fact(b=W(True)))) fl = FactList() fl.declare(InitialFact()) fl.declare(Fact(a=L(1))) activations = r.get_activations(fl) assert len(list(activations)) == 1 fl.declare(Fact(b=L('SOMETHING'))) activations = r.get_activations(fl) assert len(list(activations)) == 0
def test_rule_with_NOT_testce(): from pyknow.rule import Rule, NOT from pyknow.factlist import FactList from pyknow.fact import Fact, InitialFact, L, T r = Rule(Fact(a=L(1)), NOT(Fact(b=T(lambda c, x: x.startswith('D'))))) fl = FactList() fl.declare(InitialFact()) fl.declare(Fact(a=L(1))) activations = r.get_activations(fl) assert len(list(activations)) == 1 fl.declare(Fact(b=L('David'))) activations = r.get_activations(fl) assert len(list(activations)) == 0 fl = FactList() fl.declare(InitialFact()) fl.declare(Fact(a=L(1))) fl.declare(Fact(b=L('Penelope'))) activations = r.get_activations(fl) assert len(list(activations)) == 1
class Person_KE(KnowledgeEngine): @Rule(Person(name=C("name")), NOT(Person(surname=V('name')))) def same_name(self, name): nonlocal executions executions.append(name)