コード例 #1
0
ファイル: ncc_test.py プロジェクト: Cybernetic1/GILR
def test_ncc():
    net = Network()
    c0 = Has('$x', 'on', '$y')
    c1 = Has('$y', 'left-of', '$z')
    c2 = Neg('$z', 'color', 'red')  # YKY: allowed to have Neg inside Ncc
    c3 = Has('$z', 'on', '$w')

    p0 = net.add_production(Rule(c0, c1, Ncc(c2, c3)))
    save_Rete_graph(net, 'rete-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'),
    ]
    for wme in wmes:
        net.add_wme(wme)
    print("# of results [2] = ", len(p0.items))
    # assert len(p0.items) == 2
    net.add_wme(WME('B3', 'color', 'red'))
    print("# of results [1] = ", len(p0.items))
コード例 #2
0
ファイル: test_network.py プロジェクト: stjordanis/naive-rete
def init_network():
    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))
    return net
コード例 #3
0
ファイル: ncc_test.py プロジェクト: Cybernetic1/GILR
def test_neg():
    # setup
    net = Network()
    c0 = Has('$x', 'on', '$y')
    c1 = Has('$y', 'left-of', '$z')
    c2 = Neg('$z', 'color', 'red')
    p0 = net.add_production(Rule(c0, c1, c2))
    # 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'),
        # WME('B4', 'color', 'blue'),
    ]
    for wme in wmes:
        net.add_wme(wme)
    assert p0.items[0].wmes == [
        WME('B1', 'on', 'B3'),
        WME('B3', 'left-of', 'B4'), None
    ]
コード例 #4
0
def add_tree_to_Rete(rete_net, rule):
	""" This code is still unfinished """
	op = rule[0]
	if rule[0] in op_map:
		op = op_map[rule[0]]
	if op == 'X' or op == 'O':
		return Has(op, str(rule[1]), str(rule[2]))
	elif op == '⋀':
		return Rule( \
			add_tree_to_Rete(rule[1]), \
			add_tree_to_Rete(rule[2]))
	else:
		return Rule( \
			add_tree_to_Rete(rule[1]))

	c1 = Has('O', '$x', '$x')
	c2 = Has('□', '$y', '$z')
	c3 = Has('>', '$y', '$z')
	rete_net.add_production(Rule(c1, c2, c3))
コード例 #5
0
    def test__case_0(self):
        for i, (rule, wmes, exp) in enumerate([
            (Rule(Has('x', 'id', '1'), Has('x', 'kind',
                                           '8')), [WME('x', 'id', '1')], 0),
            (Rule(Has('x', 'id', '1'), Has('x', 'kind',
                                           '8')), [WME('x', 'kind', '8')], 0),
            (Rule(Has('x', 'id', '1'),
                  Has('x', 'kind',
                      '8')), [WME('x', 'id', '1'),
                              WME('x', 'kind', '8')], 1),
        ]):
            with self.subTest(i=i, rule=rule, wmes=wmes, exp=exp):
                network = Network()
                production = network.add_production(rule)
                for wme in wmes:
                    network.add_wme(wme)
                result = len(production.memory)

                assert_that(result, 'case 0').is_equal_to(exp)
コード例 #6
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
コード例 #7
0
ファイル: test_beta.py プロジェクト: stjordanis/naive-rete
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
コード例 #8
0
def add_rule_to_Rete(rete_net, rule):
    """ Format of a rule:
		[ => pre-condition post-condition ]
		Format of a pre-condition:
		[ [ literals ... ] [ NC-atoms ... ] ]
	"""
    # print("rule[1] = ", rule[1])
    # print("rule[2] = ", rule[2])
    # print("rule[3] = ", rule[3])
    conjunction = []
    for literal in rule[0]:
        conjunction.append(get_Rete_literal(literal))
    conjunction2 = []
    for literal in rule[1]:
        conjunction2.append(get_Rete_literal(literal))
    if conjunction2 != []:
        p = rete_net.add_production(Rule(*conjunction, Ncc(*conjunction2)))
    elif conjunction != []:
        p = rete_net.add_production(Rule(*conjunction))
    else:
        return None
    p.postcondition = get_Rete_literal(rule[2])
    return p
コード例 #9
0
ファイル: test_beta.py プロジェクト: stjordanis/naive-rete
def test_multi_productions():
    net = Network()
    c0 = Has('$x', 'on', '$y')
    c1 = Has('$y', 'left-of', '$z')
    c2 = Has('$z', 'color', 'red')
    c3 = Has('$z', 'on', 'table')
    c4 = Has('$z', 'left-of', 'B4')

    p0 = net.add_production(Rule(c0, c1, c2))
    p1 = net.add_production(Rule(c0, c1, c3, c4))

    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
    p2 = net.add_production(Rule(c0, c1, c3, c2))

    assert len(p0.items) == 1
    assert len(p1.items) == 1
    assert len(p2.items) == 1
    assert p0.items[0].wmes == [wmes[0], wmes[4], wmes[8]]
    assert p1.items[0].wmes == [wmes[0], wmes[4], wmes[7], wmes[6]]
    assert p2.items[0].wmes == [wmes[0], wmes[4], wmes[7], wmes[8]]

    net.remove_production(p2)
    assert len(p2.items) == 0
コード例 #10
0
ファイル: test_beta.py プロジェクト: stjordanis/naive-rete
def test_network_case0():
    net = Network()
    c0 = Has('x', 'id', '1')
    c1 = Has('x', 'kind', '8')
    p0 = net.add_production(Rule(c0, c1))

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

    net.add_wme(w0)
    assert not p0.items

    net.remove_wme(w0)
    net.add_wme(w1)
    assert not p0.items

    net.add_wme(w0)
    net.add_wme(w1)
    assert p0.items
コード例 #11
0
ファイル: test_beta.py プロジェクト: stjordanis/naive-rete
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
コード例 #12
0
ファイル: test_beta.py プロジェクト: stjordanis/naive-rete
def test_ncc():
    net = Network()
    c0 = Has('$x', 'on', '$y')
    c1 = Has('$y', 'left-of', '$z')
    c2 = Has('$z', 'color', 'red')
    c3 = Has('$z', 'on', '$w')

    p0 = net.add_production(Rule(c0, c1, Ncc(c2, c3)))
    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(p0.items) == 2
    net.add_wme(WME('B3', 'color', 'red'))
    assert len(p0.items) == 1
コード例 #13
0
ファイル: ncc_test.py プロジェクト: Cybernetic1/GILR
def test_black_white():
    net = Network()
    c1 = Has('$item', 'cat', '$cid')
    c2 = Has('$item', 'shop', '$sid')
    white = Ncc(
        Has('$item', 'cat', '100'),
        Neg('$item', 'cat', '101'),
        Neg('$item', 'cat', '102'),
    )
    n1 = Neg('$item', 'shop', '1')
    n2 = Neg('$item', 'shop', '2')
    n3 = Neg('$item', 'shop', '3')
    p0 = net.add_production(Rule(c1, c2, white, n1, n2, n3))
    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(p0.items) == 1
    assert p0.items[0].get_binding('$item') == 'item:1'
コード例 #14
0
import sys
import os

from rete.common import Has, Rule, WME, Neg, Ncc, Token
from rete.network import Network

print("\n\x1b[32m——`—,—{\x1b[31;1m@\x1b[0m\n")  # Genifer logo ——`—,—{@

net = Network()

# Can win a vertical column:
# X($y, $x) ^ X($z, $x) ^ □($w, $x) ^ ($y != $z) => playX($w, $x)
p0 = net.add_production(
    Rule(
        Has('X', '$y', '$x'),
        Has('X', '$z', '$x'),
        Has('□', '$w', '$x'),
        Has('!=', '$y', '$z'),
    ))
p0.postcondition = Has("oldX", '$w', '$x')

# Can win a horizontal row:
# X($x, $y) ^ X($x, $z) ^ □($x, $w) ^ ($y != $z) => playX($x, $w)

# Can win a diagonal:
# X($x, $x) ^ X($y, $y) ^ □($z, $z) ^ ($x != $y) ^ ($y != $z) ^ ($z != $x) => playX($z, $z)

# Can win a backward diagonal: (0,2) (1,1) (2,0)
# X($x, $z) ^ X($y, $y) ^ □($z, $x) ^ ($x != $y) ^ ($y != $z) ^ ($z != $x) => playX($z, $z)

# If enemy can win, we need to block it:
コード例 #15
0
ファイル: genifer_lovers.py プロジェクト: luisfelipe18/GILR
from rete.common import Has, Rule, WME, Neg, Ncc
from rete.network import Network

net = Network()

c1 = Has('male', '$a')
c2 = Has('love', '$a', '$b')
c3 = Has('female', '$b')

# net.add_production(Rule(Ncc(c1, Ncc(c2, c3))))
# net.add_production(Rule(Ncc(c2, Ncc(c3))))
# net.add_production(Rule(c1, Ncc(c2)))
# net.add_production(Rule(c1, Ncc(c2, c3)))
# net.add_production(Rule(c2, c3))
p0 = net.add_production(Rule(c3, Ncc(c2, c1)))

wmes = [
    WME('female', 'Mary'),
    WME('female', 'Ann'),
    WME('love', 'John', 'Pete'),  # 基
    WME('love', 'John', 'John'),  # 自恋
    WME('love', 'Pete', 'Mary'),  # 所谓正常
    WME('love', 'Pete', 'John'),  # 互基
    WME('love', 'Mary', 'Ann'),  # Lesbian
    WME('male', 'John'),
    WME('male', 'Pete'),
]
for wme in wmes:
    net.add_wme(wme)
コード例 #16
0
ファイル: tic_tac_toe_2.py プロジェクト: Cybernetic1/GILR
p = []  # list of p-Nodes

# **** General strategy ****
# - if can win, play it
# - about to lose, play it
# - if center not occupied, play it
# - if able to 'double-fork', play it
# - play randomly

# row 0 win:
q = net.add_production(
    Rule(
        Has('□', '$x'),
        Has('X', '$y'),
        Has('X', '$z'),
        Has('!=', '$y', '$z'),
        Has('π0', '$x', 0),
        Has('π0', '$y', 0),
        Has('π0', '$z', 0),
    ))
q.postcondition = Has("row_0_win", '$x')
q.truth = 1.0
p.append(q)

# row 1 win:
q = net.add_production(
    Rule(
        Has('□', '$x'),
        Has('X', '$y'),
        Has('X', '$z'),
        Has('!=', '$y', '$z'),
コード例 #17
0
ファイル: genifer.py プロジェクト: Cybernetic1/GILR
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
import os

from rete.common import Has, Rule, WME, Neg, Ncc, Token
from rete.network import Network

rete_net = Network()

c01 = Has('O', '$x', '$x')
c02 = Has('□', '$y', '$z')
c03 = Has('>', '$y', '$z')
p0 = rete_net.add_production(Rule(c01, c02, c03))

wmes = [
    WME('X', '0', '2'),
    WME('X', '1', '1'),
    WME('X', '2', '1'),
    WME('O', '0', '0'),
    WME('O', '1', '0'),
    WME('O', '1', '2'),
    WME('O', '2', '2'),
    WME('□', '0', '1'),
    WME('□', '2', '0'),
]
for wme in wmes:
    rete_net.add_wme(wme)

print("# of results = ", len(p0.items))