Ejemplo n.º 1
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
Ejemplo n.º 2
0
 def left_activation(self, token, wme, binding=None):
     """
     :type wme: WME
     :type token: Token
     :type binding: dict
     """
     new_token = Token(token, wme, node=self, binding=binding)
     self.items.append(new_token)
Ejemplo n.º 3
0
 def left_activation(self, t, w, binding=None):
     """
     :type w: rete.WME
     :type t: rete.Token
     :type binding: dict
     """
     new_token = Token(t, w, self, binding)
     self.items.append(new_token)
     for result in self.partner.new_result_buffer:
         self.partner.new_result_buffer.remove(result)
         new_token.ncc_results.append(result)
         result.owner = new_token
     if not new_token.ncc_results:
         for child in self.children:
             child.left_activation(new_token, None)
Ejemplo n.º 4
0
 def build_or_share_beta_memory(self, parent):
     """
     :type parent: BetaNode
     :rtype: BetaMemory
     """
     for child in parent.children:
         if isinstance(child, BetaMemory):
             return child
     node = BetaMemory(None, parent)
     # dummy top beta memory
     if parent == self.beta_root:
         node.items.append(Token(None, None))
     parent.children.append(node)
     self.update_new_node_with_matches_from_above(node)
     return node
Ejemplo n.º 5
0
 def left_activation(self, token, wme, binding=None):
     """
     :type wme: rete.WME
     :type token: rete.Token
     :type binding: dict
     """
     new_token = Token(token, wme, self, binding)
     self.items.append(new_token)
     for item in self.amem.items:
         if self.perform_join_test(new_token, item):
             jr = NegativeJoinResult(new_token, item)
             new_token.join_results.append(jr)
             item.negative_join_result.append(jr)
     if not new_token.join_results:
         for child in self.children:
             child.left_activation(new_token, None)
Ejemplo n.º 6
0
 def left_activation(self, t, w, binding=None):
     """
     :type w: rete.WME
     :type t: rete.Token
     :type binding: dict
     """
     new_result = Token(t, w, self, binding)
     owners_t = t
     owners_w = w
     for i in range(self.number_of_conditions):
         owners_w = owners_t.wme
         owners_t = owners_t.parent
     for token in self.ncc_node.items:
         if token.parent == owners_t and token.wme == owners_w:
             token.ncc_results.append(new_result)
             new_result.owner = token
             Token.delete_token_and_descendents(token)
     self.new_result_buffer.append(new_result)