def test_featurecheck_is_borg_basic_literal(): from experta.matchers.rete.check import FeatureCheck from experta import L assert FeatureCheck('somekey', L(1)) is FeatureCheck('somekey', L(1)) assert FeatureCheck('somekey', L(1)) is not FeatureCheck('somekey', L(2))
def test_featurecheck_is_borg_composed_literal(): from experta.matchers.rete.check import FeatureCheck from experta import L assert FeatureCheck('somekey', L(1) & L(1)) is FeatureCheck('somekey', L(1) & L(1)) assert FeatureCheck('somekey', L(1) | L(1)) is FeatureCheck('somekey', L(1) | L(1)) assert FeatureCheck('somekey', ~L(1)) is FeatureCheck('somekey', ~L(1))
def test_double_not_inside_fact(): from experta import Fact, OR, Rule, L from experta.matchers.rete.dnf import dnf input_ = Rule(Fact(a=~~L(1))) output = Rule(Fact(a=L(1))) result = dnf(input_) assert result == output
def test_or_inside_fact(): from experta import Fact, OR, Rule, L from experta.matchers.rete.dnf import dnf input_ = Rule(Fact(a=L(1) | L(2), b=3)) output = Rule(OR(Fact(a=L(1), b=3), Fact(a=L(2), b=3))) result = dnf(input_) assert set(result[0]) == set(output[0])
def test_not_and_inside_fact(): from experta import Fact, OR, Rule, L from experta.matchers.rete.dnf import dnf input_ = Rule(Fact(a=~(L(1) & L(2)))) output = Rule(OR(Fact(a=~L(1)), Fact(a=~L(2)))) result = dnf(input_) assert result == output
class Person_KE(KnowledgeEngine): @Rule(Person(name=L("David"))) def david(self): self.declare(Person(name="Pepe", apellido="stuff")) @Rule(Person(name=L("Pepe"))) def pepe(self): nonlocal executed executed = True
def test_not_or_inside_fact(): from experta import Fact, OR, Rule, L from experta.fieldconstraint import ANDFC, NOTFC from experta.matchers.rete.dnf import dnf input_ = Rule(Fact(a=~(L(1) | L(2)))) output = Rule(Fact(a=ANDFC(NOTFC(L(1)), NOTFC(L(2))))) result = dnf(input_) assert result == output
def test_featurecheck_call_not_literal(): from experta.matchers.rete.check import FeatureCheck from experta import L, Fact # Positional matching (negated) check = FeatureCheck(0, ~L('somedata')) assert not check(Fact('somedata')) # Positional not matching (negated) check = FeatureCheck(0, ~L('somedata')) assert check(Fact('otherdata'))
def test_featurecheck_call_literal(): from experta.matchers.rete.check import FeatureCheck from experta import L, Fact # Positional field not present check = FeatureCheck(0, L('mydata')) assert not check(Fact()) # Positional field present, matching and not matching check = FeatureCheck(0, L('mydata')) assert check(Fact('mydata')) check = FeatureCheck(0, L('otherdata')) assert not check(Fact('mydata')) # Named field present, matching and not matching check = FeatureCheck('mykey', L('mydata')) assert check(Fact(mykey='mydata')) check = FeatureCheck('mykey', L('mydata')) assert not check(Fact(mykey='myotherdata')) # Named field not present check = FeatureCheck('mykey', L('mydata')) assert not check(Fact(otherkey='mydata')) # Literal with binding, matching and not matching check = FeatureCheck('mykey', L('mydata', __bind__='D')) assert check(Fact(mykey='mydata')) == {'D': 'mydata'} check = FeatureCheck('mykey', L('mydata', __bind__='D')) assert check(Fact(mykey='otherdata')) is False
class Test(KnowledgeEngine): @DefFacts() def tested_deffacts(self): for i in to_declare: yield Fact(something=i) @Rule(Fact(something=L(1)), Fact(something=L(2))) def rule1(self): nonlocal executions executions.append('rule1') @Rule(Fact(something=L(3))) def rule2(self): nonlocal executions executions.append('rule2')
def test_featurecheck_call_and(): from experta.matchers.rete.check import FeatureCheck from experta import L, P, W, Fact # Positional, composed and matching check = FeatureCheck(0, L('mydata') & P(lambda _: True) & W()) assert check(Fact('mydata')) # Positional, composed and matching (with binding) check = FeatureCheck(0, L('mydata') & W('X')) assert check(Fact('mydata')) == {'X': 'mydata'} # Positional, composed and matching (with binding negated) check = FeatureCheck(0, L('mydata') & ~W('X')) assert check(Fact('mydata')) == {(False, 'X'): 'mydata'}
def test_featurecheck_convert_nonPCE_to_PCE(): from experta.matchers.rete.check import FeatureCheck from experta import L def testfunc(): pass fc = FeatureCheck('somekey', testfunc) assert fc.how == L(testfunc)
def test_or_inside_and_inside_fact(): from experta import Fact, OR, Rule, L from experta.fieldconstraint import ANDFC from experta.matchers.rete.dnf import dnf input_ = Rule(Fact(a=L(1) & (L(2) | L(3)))) output = Rule(OR(Fact(a=ANDFC(L(1), L(2))), Fact(a=ANDFC(L(1), L(3))))) result = dnf(input_) assert result == output
def test_or_inside_or_inside_fact(): from experta import Fact, OR, Rule, L from experta.matchers.rete.dnf import dnf input_ = Rule(Fact(a=L(1) | (L(2) | L(3)))) output = Rule(OR(Fact(a=L(1)), Fact(a=L(2)), Fact(a=L(3)))) result = dnf(input_) assert result == output
def test_declare_raises_typeerror_if_conditionalelement_found(): from experta import KnowledgeEngine, L, W, P, Fact ke = KnowledgeEngine() with pytest.raises(TypeError): ke.declare(Fact(L(1))) with pytest.raises(TypeError): ke.declare(Fact(W())) with pytest.raises(TypeError): ke.declare(Fact(P(lambda _: True))) with pytest.raises(TypeError): ke.declare(Fact(~L(1))) with pytest.raises(TypeError): ke.declare(Fact(L(1) | L(2))) with pytest.raises(TypeError): ke.declare(Fact(L(1) & L(2)))
def test_featurecheck_call_or(): """ Or is normally not checked with FeatureCheck because is normalized out during DNF. """ from experta.matchers.rete.check import FeatureCheck from experta import L, P, W, Fact check = FeatureCheck(0, L('mydata') | P(lambda _: True) & W()) assert check(Fact('mydata')) check = FeatureCheck(0, L('mydata') | W('X')) assert check(Fact('mydata')) is True check = FeatureCheck(0, W('X') | L('mydata')) assert check(Fact('mydata')) == {'X': 'mydata'} check = FeatureCheck(0, L('mydata') | ~L('otherdata')) assert check(Fact('mydata')) check = FeatureCheck(0, ~L('otherdata') | L('mydata')) assert check(Fact('mydata')) check = FeatureCheck(0, ~L('mydata') | L('otherdata')) assert not check(Fact('mydata')) check = FeatureCheck(0, L('otherdata') | ~L('mydata')) assert not check(Fact('mydata'))
class Base(KnowledgeEngine): @Rule(Person(name=L('pepe'))) def is_pepe(self): self.declare(Person(drinks="coffee"))
class Test(Base): @Rule(Person(drinks=L("coffee"))) def drinks_coffee(self): nonlocal executed executed = True
class Test(KnowledgeEngine): """ Test KE """ @Rule(OR(Fact(something=L(1)), Fact(something=L(2)))) def rule1(self): """ First rule, something=1 and something=2""" pass