def test_retematcher_changes_are_propagated(TestNode): from experta.engine import KnowledgeEngine from experta.fact import Fact from experta.matchers.rete import ReteMatcher from experta.matchers.rete.token import Token matcher = ReteMatcher(KnowledgeEngine()) tn1 = TestNode() tn2 = TestNode() matcher.root_node.add_child(tn1, tn1.activate) matcher.root_node.add_child(tn2, tn2.activate) f1 = Fact(__factid__=1) f2 = Fact(__factid__=2) f3 = Fact(__factid__=3) f4 = Fact(__factid__=4) matcher.changes(adding=[f1, f2], deleting=[f3, f4]) assert Token.valid(f1) in tn1.added assert Token.valid(f1) in tn2.added assert Token.valid(f2) in tn1.added assert Token.valid(f2) in tn2.added assert Token.invalid(f3) in tn1.added assert Token.invalid(f3) in tn2.added assert Token.invalid(f4) in tn1.added assert Token.invalid(f4) in tn2.added
def test_busnode_remove(TestNode): from experta.matchers.rete.nodes import BusNode from experta.matchers.rete.token import Token from experta.fact import Fact bn = BusNode() tn1 = TestNode() tn2 = TestNode() bn.add_child(tn1, tn1.activate) bn.add_child(tn2, tn2.activate) bn.remove(Fact()) assert tn1.added == [Token.invalid(Fact())] assert tn2.added == [Token.invalid(Fact())]
def test_notnode_right_activate_valid_match_just_one(TestNode): from experta.matchers.rete.nodes import NotNode from experta.matchers.rete.token import Token from experta.fact import Fact nn = NotNode(lambda l, r: True) tn1 = TestNode() tn2 = TestNode() nn.add_child(tn1, tn1.activate) nn.add_child(tn2, tn2.activate) token = Token.valid(Fact(test='data')) nn.left_memory[token.to_info()] = 0 nn.activate_right(token) assert Token.invalid(Fact(test='data')) in tn1.added assert Token.invalid(Fact(test='data')) in tn2.added assert nn.left_memory[token.to_info()] == 1
def test_ordinarymatchnode_left_activate_invalid_remove_from_left_memory(): from experta.matchers.rete.nodes import OrdinaryMatchNode from experta.matchers.rete.token import Token from experta.fact import Fact omn = OrdinaryMatchNode(lambda l, r: True) fact = Fact(test='data') token = Token.valid(fact) omn.left_memory.append(token.to_info()) omn.activate_left(Token.invalid(fact)) assert not omn.left_memory
def test_ordinarymatchnode_left_activate_invalid_build_new_tokens(TestNode): from experta.matchers.rete.nodes import OrdinaryMatchNode from experta.matchers.rete.token import Token from experta.fact import Fact omn = OrdinaryMatchNode(lambda l, r: True) tn1 = TestNode() tn2 = TestNode() omn.add_child(tn1, tn1.activate) omn.add_child(tn2, tn2.activate) rt1 = Token.valid(Fact(rightdata='rightdata1')) rt2 = Token.valid(Fact(rightdata='rightdata2')) omn.right_memory.append(rt1.to_info()) omn.right_memory.append(rt2.to_info()) token = Token.invalid(Fact(leftdata='leftdata')) omn.activate_left(token) assert tn1.added == [ Token.invalid( [Fact(leftdata='leftdata'), Fact(rightdata='rightdata1')]), Token.invalid( [Fact(leftdata='leftdata'), Fact(rightdata='rightdata2')]) ] assert tn2.added == [ Token.invalid( [Fact(leftdata='leftdata'), Fact(rightdata='rightdata1')]), Token.invalid( [Fact(leftdata='leftdata'), Fact(rightdata='rightdata2')]) ]
def test_conflictsetchange_invalid_removes_from_memory(): from experta.fact import Fact from experta.matchers.rete.nodes import ConflictSetNode from experta.matchers.rete.token import Token, TokenInfo from experta.rule import Rule csn = ConflictSetNode(Rule()) f = Fact(test='data') f.__factid__ = 1 csn.memory.add(TokenInfo([f], {'mycontextdata': 'data'})) csn.activate(Token.invalid(f, {'mycontextdata': 'data'})) assert not csn.memory
def test_token_shortcut_invalid(): from experta.matchers.rete.token import Token assert Token.invalid([]) == Token(Token.TagType.INVALID, [])