예제 #1
0
    def test__case_1(self):
        for i, (rule, wmes, exp) in enumerate([(Rule(
                Has('$x', 'on', '$y'),
                Has('$y', 'left-of', '$z'),
                Has('$z', 'color', 'red'),
        ), [
                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')
        ], [])]):
            with self.subTest(i=i, rule=rule, wmes=wmes, exp=exp):
                network = Network()
                production = network.add_production(rule)

                am0 = network.build_or_share_alpha_memory(rule[0])
                am1 = network.build_or_share_alpha_memory(rule[1])
                am2 = network.build_or_share_alpha_memory(rule[2])

                dummy_join = am0.children[0]

                join_on_value_y = am1.children[0]
                join_on_value_z = am2.children[0]

                match_c0 = dummy_join.children[0]
                match_c0c1 = join_on_value_y.children[0]
                match_c0c1c2 = join_on_value_z.children[0]

                for wme in wmes:
                    network.add_wme(wme)

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

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

                network.remove_wme(wmes[0])
                assert am0.memory == [wmes[1], wmes[3], wmes[7]]
                assert len(match_c0.memory) == 3
                assert len(match_c0c1.memory) == 1
                assert len(match_c0c1c2.memory) == 0
예제 #2
0
def test_network_case1():
    # setup
    net = Network()
    c0 = Has('$x', 'on', '$y')
    c1 = Has('$y', 'left-of', '$z')
    c2 = Has('$z', 'color', 'red')
    net.add_production(Rule(c0, c1, c2))
    # end

    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]

    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)

    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

    net.remove_wme(wmes[0])
    assert am0.items == [wmes[1], wmes[3], wmes[7]]
    assert len(match_c0.items) == 3
    assert len(match_c0c1.items) == 1
    assert len(match_c0c1c2.items) == 0
예제 #3
0
def test_dup():
    # setup
    net = Network()
    c0 = Has('$x', 'self', '$y')
    c1 = Has('$x', 'color', 'red')
    c2 = Has('$y', 'color', 'red')
    net.add_production(Rule(c0, c1, c2))

    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