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
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
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)
def left_activation(self, token, wme, binding=None): """ :type binding: dict :type wme: WME :type token: Token """ new_token = Token(token, wme, node=self, binding=binding) for child in self.children: child.left_activation(new_token)
def activate_from_left(self, token, wme): """ :param token: :param wme: :return: """ new_token = Token(token, wme, node=self) self.items.append(new_token) for child in self.children: child.activate_from_left(new_token)
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.append_token(Token(None, None)) parent.append_child(node) self.update_new_node_with_matches_from_above(node) return node
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._memory.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)
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)
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.memory: if token.parent == owners_t and token.wme == owners_w: token.ncc_results.append(new_result) new_result.owner = token Token.delete_token_and_descendants(token) self.new_result_buffer.append(new_result)
def left_activation(self, t, w, binding=None): """ :type w: rete.WME :type t: rete.Token :type binding: dict """ DEBUG("NCC left-activate, wme = ", w) new_token = Token(t, w, self, binding) self.items.append(new_token) new_token.ncc_results = [] DEBUG("NCC node.items add token = ", new_token) for result in self.partner.new_result_buffer: self.partner.new_result_buffer.remove(result) result.owner = new_token new_token.ncc_results.append(result) DEBUG(" add to ncc_results: ", result) if not new_token.ncc_results: # if results == [] for child in self.children: child.left_activation(new_token, None)
def left_activation(self, t, w, binding=None): """ :type w: rete.WME :type t: rete.Token :type binding: dict """ DEBUG("NCC partner left-activate, wme = ", w) 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 found = False for token in self.ncc_node.items: if token.parent == owners_t and token.wme == owners_w: DEBUG(" partner add to ncc_results: ", new_result) new_result.owner = token token.ncc_results.append(new_result) Token.delete_descendents_of_token(token) found = True if not found: # new_result.owner = 'buffed' self.new_result_buffer.append(new_result)