Example #1
0
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))
Example #2
0
def get_Rete_nc(nc):
    """ This is the recursive (nested) algorithm, not used anymore """
    conjunction = []
    for literal_or_NC in nc[1:]:
        if literal_or_NC[0] == 'NC':
            return conjunction.append(get_Rete_nc(literal_or_NC))
        else:
            conjunction.append(get_Rete_literal(literal_or_NC))
    return Ncc(*conjunction)
Example #3
0
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
Example #4
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
Example #5
0
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'
Example #6
0
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)
Example #7
0
def test_ncc():
    c0 = Has('$a', '$b', '$c')
    c1 = Ncc(Has('$x', 'color', 'red'))
    c2 = Ncc(c0, c1)
    assert c2.number_of_conditions == 2