Ejemplo n.º 1
0
def test_filter_second():
    net = ReteNetwork()
    net.add_fact(Fact())

    @Production(Fact() & Filter(lambda: True) & Fact())
    def test():
        pass

    net.add_production(test)

    assert len(list(net.matches)) == 1
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
def fire_counting():
    net = ReteNetwork()

    @Production(
        Fact(number=V('x')) & ~Fact(before=V('x'))
        & Bind(lambda x: str(int(x) + 1), V('y')))
    def add1(net, x, y):
        f = Fact(number=y, before=x)
        net.add_fact(f)

    net.add_production(add1)
    assert len(net.wmes) == 0

    net.add_fact(Fact(number='1'))
    assert len(net.wmes) == 2

    print(net)

    for i in range(5):
        net.run(1)
        assert len(net.wmes) == (3 * (i + 1)) + 2
Ejemplo n.º 4
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()
Ejemplo n.º 5
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
Ejemplo n.º 6
0
def add_to_depth():
    net = ReteNetwork()

    @Production(
        Fact(number=V('x'), depth=V('xd')) & Fact(number=V('y'), depth=V('yd'))
        & Filter(lambda xd, yd: xd + yd < 1))
    def add(net, x, y, xd, yd):
        f = Fact(number=x + y, depth=xd + yd + 1)
        net.add_fact(f)

    net.add_fact(Fact(name="1", number=1, depth=0))
    net.add_fact(Fact(name="2", number=2, depth=0))
    # net.add_fact(Fact(name="3", number=3, depth=0))
    # net.add_fact(Fact(name="5", number=5, depth=0))
    # net.add_fact(Fact(name="7", number=7, depth=0))

    net.add_production(add)

    while len(list(net.new_matches)) > 0:
        # print(len(list(net.new_matches)))
        m = net.get_new_match()
        m.fire()