def test_add_remove_filter(): net = ReteNetwork() @Production(Filter(lambda: True)) def filter_fun(): pass net.add_production(filter_fun) assert len(net.beta_root.children) == 1 net.remove_production(filter_fun) assert len(net.beta_root.children) == 0
def test_add_remove_empty(): net = ReteNetwork() @Production() def empty(): pass net.add_production(empty) assert len(net.beta_root.children) == 1 net.remove_production(empty) assert len(net.beta_root.children) == 0
def test_add_remove_bind(): net = ReteNetwork() @Production(Bind(lambda: 5, V('x'))) def bind(x): return x net.add_production(bind) assert len(net.beta_root.children) == 1 assert list(net.matches)[0].fire() == 5 net.remove_production(bind) assert len(net.beta_root.children) == 0
def test_multi_productions(): net = ReteNetwork() c0 = Cond(V('x'), 'on', V('y')) c1 = Cond(V('y'), 'left-of', V('z')) c2 = Cond(V('z'), 'color', 'red') c3 = Cond(V('z'), 'on', 'table') c4 = Cond(V('z'), 'left-of', 'B4') @Production(AND(c0, c1, c2)) def p0(): pass @Production(AND(c0, c1, c3, c4)) def p1(): pass net.add_production(p0) net.add_production(p1) wmes = [ WME('B1', 'on', 'B2'), WME('B1', 'on', 'B3'), WME('B1', 'color', 'red'), WME('B2', 'on', 'table'), WME('B2', 'left-of', 'B3'), WME('B2', 'color', 'blue'), WME('B3', 'left-of', 'B4'), WME('B3', 'on', 'table'), WME('B3', 'color', 'red'), ] for wme in wmes: net.add_wme(wme) # add product on the fly @Production(AND(c0, c1, c3, c2)) def p2(): pass net.add_production(p2) assert len(list(p0.activations)) == 1 assert len(list(p1.activations)) == 1 assert len(list(p2.activations)) == 1 assert list(p0.activations)[0].wmes == [wmes[0], wmes[4], wmes[8]] assert list(p1.activations)[0].wmes == [wmes[0], wmes[4], wmes[7], wmes[6]] assert list(p2.activations)[0].wmes == [wmes[0], wmes[4], wmes[7], wmes[8]] net.remove_production(p2) print(type(p2)) assert len(list(p2.activations)) == 0
def test_add_remove_not(): net = ReteNetwork() @Production(~Cond('a', 'on', 'b')) def not_fun(): pass net.add_production(not_fun) assert len(net.beta_root.children) == 1 assert len(list(net.matches)) == 1 wme = WME('a', 'on', 'b') net.add_wme(wme) assert len(list(net.matches)) == 0 net.remove_wme(wme) assert len(list(net.matches)) == 1 net.remove_production(not_fun) assert len(net.beta_root.children) == 0
def test_add_remove_ncc(): net = ReteNetwork() @Production(~Fact(first="hello", second="world")) def ncc_fun(): pass net.add_production(ncc_fun) assert len(net.beta_root.children) == 2 assert len(list(net.matches)) == 1 wme = WME('a', 'on', 'b') net.add_wme(wme) f = Fact(first='hello', second='world') net.add_fact(f) assert len(list(net.matches)) == 0 net.remove_fact(f) assert len(list(net.matches)) == 1 net.remove_production(ncc_fun) assert len(net.beta_root.children) == 0