Пример #1
0
def test_intra_condition_consistency():
    net = ReteNetwork()
    c = Cond(V('x'), 'b', V('x'))

    @Production(c)
    def test():
        pass

    net.add_production(test)

    wme = WME('a', 'b', 'c')

    net.add_wme(wme)

    assert len(list(net.matches)) == 0

    c2 = Cond(V('x'), 'b', V('z'))

    @Production(c2)
    def test2():
        pass

    net.add_production(test2)

    assert len(list(net.matches)) == 1
Пример #2
0
def test_dup():
    # setup
    net = ReteNetwork()
    c0 = Cond(V('x'), 'self', V('y'))
    c1 = Cond(V('x'), 'color', 'red')
    c2 = Cond(V('y'), 'color', 'red')

    @Production(AND(c0, c1, c2))
    def p0():
        pass

    net.add_production(p0)

    wmes = [
        WME('B1', 'self', 'B1'),
        WME('B1', 'color', 'red'),
    ]
    for wme in wmes:
        net.add_wme(wme)
    # end

    am = net.build_or_share_alpha_memory(c2)
    join_on_value_y = am.successors[1]
    match_for_all = join_on_value_y.children[0]

    assert len(match_for_all.items) == 1
Пример #3
0
def test_readme_productions():
    @Production(Fact(color='red'))
    def alert_something_red():
        print("I found something red")

    @Production(
        AND(OR(Fact(color='red'), Fact(color='blue')),
            NOT(Fact(color='green'))))
    def alert_something_complex():
        print("I found something red or blue without any green present")

    @Production(
        (Fact(color='red') | Fact(color='blue')) & ~Fact(color='green'))
    def alert_something_complex2():
        print("I found something red or blue without any green present")

    @Production(
        Fact(firstname='Chris', lastname=V('lastname'))
        & Fact(first='John', lastname=V('lastname')))
    def found_relatives(lastname):
        print("I found a pair of relatives with the lastname: "
              "{}".format(lastname))

    @Production(
        Fact(value=V('a')) & Fact(value=V('b')) & Filter(lambda a, b: a > b)
        & Fact(value=V('c')) & Filter(lambda b, c: b > c))
    def three_values(a, b, c):
        print("{} is greater than {} is greater than {}".format(a, b, c))
Пример #4
0
def test_activation():
    net = ReteNetwork()
    c0 = Cond(V('x'), 'on', V('y'))
    c1 = Cond(V('y'), 'color', 'red')

    @Production(AND(c0, c1))
    def p():
        pass

    net.add_production(p)

    activations = [p for p in net.matches]
    assert len(activations) == 0

    wmes = [WME('B1', 'on', 'B2'), WME('B2', 'color', 'red')]

    for wme in wmes:
        net.add_wme(wme)

    print(net.working_memory)
    print(net)

    activations = [p for p in net.matches]
    assert len(activations) == 1

    net.remove_wme(wmes[0])

    activations = [p for p in net.matches]
    assert len(activations) == 0
Пример #5
0
 def fire(self, token: Token):
     kwargs = {
         arg: self._rete_net if arg == 'net' else
         self._rete_net.facts[token.binding[V(arg)]] if token.binding[V(
             arg)] in self._rete_net.facts else token.binding[V(arg)]
         for arg in self._wrapped_args
     }
     return self(**kwargs)
Пример #6
0
    def get_function_result(self, token, wme, binding):
        args = inspect.getfullargspec(self.func)[0]
        args = {
            arg: self._rete_net
            if arg == 'net' else self._rete_net.facts[binding[V(arg)]]
            if binding[V(arg)] in self._rete_net.facts else binding[V(arg)]
            for arg in args
        }

        return self.func(**args)
Пример #7
0
def init_network():
    net = ReteNetwork()
    c0 = Cond(V('x'), 'on', V('y'))
    c1 = Cond(V('y'), 'left-of', V('z'))
    c2 = Cond(V('z'), 'color', 'red')

    @Production(AND(c0, c1, c2))
    def test():
        pass

    net.add_production(test)

    return net
Пример #8
0
 def get_function_result(self, binding: Dict[V, Any]):
     """
     Given a binding that maps variables to values, this instantiates the
     arguments for the function and executes it.
     """
     args = inspect.getfullargspec(self.func)[0]
     args = {
         arg: self._rete_net
         if arg == 'net' else self._rete_net.facts[binding[V(arg)]]
         if binding[V(arg)] in self._rete_net.facts else binding[V(arg)]
         for arg in args
     }
     return self.func(**args)
Пример #9
0
def test_negative_condition():
    # setup
    net = ReteNetwork()
    c0 = Cond(V('x'), 'on', V('y'))
    c1 = Cond(V('y'), 'left-of', V('z'))
    c2 = Neg(V('z'), 'color', 'red')

    @Production(AND(c0, c1, c2))
    def p0():
        pass

    net.add_production(p0)

    # end

    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)

    am0 = net.build_or_share_alpha_memory(c0)
    am1 = net.build_or_share_alpha_memory(c1)
    am2 = net.build_or_share_alpha_memory(c2)
    dummy_join = am0.successors[0]
    join_on_value_y = am1.successors[0]
    join_on_value_z = am2.successors[0]
    match_c0 = dummy_join.children[0]
    match_c0c1 = join_on_value_y.children[0]
    match_c0c1c2 = join_on_value_z.children[0]

    print(match_c0.items)
    print(match_c0c1.items)
    print(type(join_on_value_z))
    print(match_c0c1c2.items)

    assert list(p0.activations)[0].wmes == [
        WME('B1', 'on', 'B3'),
        WME('B3', 'left-of', 'B4'), None
    ]
Пример #10
0
def test_ncc():
    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', V('w'))

    # @Production(AND(c0, c1, NOT(AND(c2, c3))))
    @Production(c0 & c1 & ~(c2 & c3))
    def p0():
        pass

    net.add_production(p0)

    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'),
    ]
    for wme in wmes:
        net.add_wme(wme)
    assert len(list(p0.activations)) == 2
    net.add_wme(WME('B3', 'color', 'red'))
    assert len(list(p0.activations)) == 1
Пример #11
0
def test_bind_first():
    net = ReteNetwork()

    @Production(Bind(lambda: 1 + 1, V('x')))
    def test(x):
        pass

    net.add_production(test)

    assert len(list(net.matches)) == 1
Пример #12
0
def test_production():
    class Block(Fact):
        pass

    @Production(Block(name=V('x'), clear=True))
    def is_clear(x):
        print(x)

    net = ReteNetwork()
    net.add_production(is_clear)
Пример #13
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
Пример #14
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()
Пример #15
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()
Пример #16
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
Пример #17
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
Пример #18
0
def test_readme_production_nested_match():
    @Production(Fact(name=V('name'), against__scissors=1, against__paper=-1))
    def what_wins_to_scissors_and_losses_to_paper(name):
        print(name)
Пример #19
0
def test_readme_production_bind():
    @Production(V('name_fact') << Fact(name=V('name')))
    def found_name(name_fact):
        print("I found a name fact {}".format(name_fact))
Пример #20
0
def test_ncc():
    c0 = Cond(V('a'), V('b'), V('c'))
    c1 = Ncc(Cond(V('x'), 'color', 'red'))
    c2 = Ncc(c0, c1)
    assert c2.number_of_conditions == 2
Пример #21
0
def test_condition_test():
    c0 = Cond(V('x'), 'color', 'red')
    w0 = WME('B1', 'color', 'red')
    w1 = WME('B1', 'color', 'blue')
    assert c0.test(w0)
    assert not c0.test(w1)
Пример #22
0
def test_condition_contain():
    c0 = Cond(V('a'), V('b'), V('c'))
    assert c0.contain(V('a'))
    assert not c0.contain(V('d'))
Пример #23
0
def test_condition_vars():
    c0 = Cond(V('x'), 'is', V('y'))
    assert len(c0.vars) == 2
Пример #24
0
def test_network_case1():
    # setup
    net = ReteNetwork()
    c0 = Cond(V('x'), 'on', V('y'))
    c1 = Cond(V('y'), 'left-of', V('z'))
    c2 = Cond(V('z'), 'color', 'red')

    @Production(AND(c0, c1, c2))
    def p0():
        pass

    net.add_production(p0)

    # end

    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)

    am0 = net.build_or_share_alpha_memory(c0)
    am1 = net.build_or_share_alpha_memory(c1)
    am2 = net.build_or_share_alpha_memory(c2)
    dummy_join = am0.successors[0]
    join_on_value_y = am1.successors[0]
    join_on_value_z = am2.successors[0]
    match_c0 = dummy_join.children[0]
    match_c0c1 = join_on_value_y.children[0]
    match_c0c1c2 = join_on_value_z.children[0]

    assert am0.items == [wmes[0], wmes[1], wmes[3], wmes[7]]
    assert am1.items == [wmes[4], wmes[6]]
    assert am2.items == [wmes[2], wmes[8]]
    assert len(match_c0.items) == 4
    assert len(match_c0c1.items) == 2
    assert len(match_c0c1c2.items) == 1

    t0 = Token(Token(None, None), wmes[0])
    t1 = Token(t0, wmes[4])
    t2 = Token(t1, wmes[8])
    assert match_c0c1c2.items[0] == t2

    print(wmes[0].tokens)
    print(match_c0.items)
    print(match_c0c1.items)
    print(match_c0c1c2.items)
    print()

    net.remove_wme(wmes[0])

    print(wmes[0].tokens)
    print(match_c0.items)
    print(match_c0c1.items)
    print(match_c0c1c2.items)

    assert am0.items == [wmes[1], wmes[3], wmes[7]]
    assert am1.items == [wmes[4], wmes[6]]
    assert am2.items == [wmes[2], wmes[8]]
    assert len(match_c0.items) == 3
    assert len(match_c0c1.items) == 1
    assert len(match_c0c1c2.items) == 0
Пример #25
0
def test_black_white():
    net = ReteNetwork()
    c1 = Cond(V('item'), 'cat', V('cid'))
    c2 = Cond(V('item'), 'shop', V('sid'))
    white = Ncc(
        Neg(V('item'), 'cat', '100'),
        Neg(V('item'), 'cat', '101'),
        Neg(V('item'), 'cat', '102'),
    )
    n1 = Neg(V('item'), 'shop', '1')
    n2 = Neg(V('item'), 'shop', '2')
    n3 = Neg(V('item'), 'shop', '3')

    @Production(AND(c1, c2, white, n1, n2, n3))
    def p0():
        pass

    net.add_production(p0)

    wmes = [
        WME('item:1', 'cat', '101'),
        WME('item:1', 'shop', '4'),
        WME('item:2', 'cat', '100'),
        WME('item:2', 'shop', '1'),
    ]
    for wme in wmes:
        net.add_wme(wme)

    assert len(list(p0.activations)) == 1
    assert list(p0.activations)[0].binding[V('item')] == 'item:1'