def test_adding_removing_facts(): net = ReteNetwork() f = Fact() net.add_fact(f) assert f.id is not None assert len(net.working_memory) > 0 net.remove_fact(f) assert f.id is None assert len(net.working_memory) == 0
def test_readme_network(): net = ReteNetwork() f1 = Fact(light_color="red") net.add_fact(f1) f1['light_color'] = "green" net.update_fact(f1) net.remove_fact(f1) f1 = Fact(light_color="red") @Production(V('fact') << Fact(light_color="red")) def make_green(net, fact): print('making green') fact['light_color'] = 'green' net.update_fact(fact) @Production(V('fact') << Fact(light_color="green")) def make_red(net, fact): print('making red') fact['light_color'] = 'red' net.update_fact(fact) light_net = ReteNetwork() light_net.add_fact(f1) light_net.add_production(make_green) light_net.add_production(make_red) light_net.update_fact(f1) # print(light_net) light_net.run(5) matches = list(light_net.matches) print(matches) new = list(light_net.new_matches) # noqa E262 matches[0].fire()
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