コード例 #1
0
 def get_rete_conds(self):
     if self.pattern is None:
         return ([], )
     disjuncts = compile_disjuncts(self.pattern)
     return [
         list(get_rete_conds(AND(*disjunct))) if isinstance(
             disjunct, tuple) else list(get_rete_conds(AND(disjunct)))
         for disjunct in disjuncts
     ]
コード例 #2
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
コード例 #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_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'
コード例 #5
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
コード例 #6
0
ファイル: test_network.py プロジェクト: cmaclell/py_rete
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
コード例 #7
0
ファイル: test_network.py プロジェクト: cmaclell/py_rete
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 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
    ]
コード例 #9
0
def compile_disjuncts(it, nest: bool = True):
    if isinstance(it, OR):
        return tuple(compile_disjuncts(ele, nest=False) for ele in it)
    elif isinstance(it, (Production, AND)):
        inner = []
        for ele in it:
            if isinstance(ele, NOT):
                inner += compile_disjuncts(ele)
            else:
                inner.append(compile_disjuncts(ele))
        return tuple(product(*inner))
    elif isinstance(it, NOT):
        if len(it) > 1:
            inner = compile_disjuncts(AND(*[ele for ele in it]))
        else:
            inner = compile_disjuncts(it[0])
        return (tuple(
            NOT(*branch) if isinstance(branch, tuple) else NOT(branch)
            for branch in inner), )
    elif nest:
        return (it, )
    else:
        return it
コード例 #10
0
def test_network_case0():
    net = ReteNetwork()
    c0 = Cond('x', 'id', '1')
    c1 = Cond('x', 'kind', '8')

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

    net.add_production(p0)

    w0 = WME('x', 'id', '1')
    w1 = WME('x', 'kind', '8')

    net.add_wme(w0)
    assert len(list(p0.activations)) == 0

    net.remove_wme(w0)
    net.add_wme(w1)
    assert len(list(p0.activations)) == 0

    net.add_wme(w0)
    net.add_wme(w1)
    assert len(list(p0.activations)) > 0
コード例 #11
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