Exemple #1
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
Exemple #2
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()