private ReteNode BuildOrShareJoinNode(ReteNode parent, Alpha_Memory am, List <Test_At_Join_Node> tests) { // look for an existing node to share foreach (ReteNode child in parent.Children) { Join_Node j_node = child as Join_Node; if (null != j_node) { if (j_node.Amem.Equals(am) && Test_At_Join_Node.IsListEquals(j_node.Tests, tests)) { return(child); } } } Join_Node new_j_node = new Join_Node(); new_j_node.Parent = parent; // insert new at the head of the list parent.children parent.Children.Insert(0, new_j_node); // foamliu, 2008/11/29, new object, no need. //new_j_node.Children.Clear(); new_j_node.Tests = tests; new_j_node.Amem = am; am.Successors.Insert(0, new_j_node); return(new_j_node); }
public void JoinNodeLeftActivation(Join_Node node, Token t) { foreach (WME w in node.Amem.Items) { if (PerformJoinTests(node.Tests, t, w)) { foreach (ReteNode child in node.Children) { //if (child is Beta_Memory) //{ // // left-activation (child, t, w) // Beta_Memory_Left_Activation((Beta_Memory)child, t, w); //} //else if (child is P_Node) //{ // P_Node_Left_Activation((P_Node)child, t, w); //} // foamliu, 2008/08/26, use a general method here // to support both join-node and negative-node. // foamliu, 2008/11/21, and ncc-node and ncc-partner-node. // LeftActivation(child, t, w); } } } }
public void JoinNodeRightActivation(Join_Node node, WME w) { // parent is the beta memory node Beta_Memory parent = (Beta_Memory)node.Parent; foreach (Token t in parent.Items) { //if (t is DummyTopToken) //{ // Handle_Dummy_Top_Token(node, w); // continue; //} if (PerformJoinTests(node.Tests, t, w)) { foreach (ReteNode child in node.Children) { //if (child is Beta_Memory) //{ // // left-activation (child, t, w) // Beta_Memory_Left_Activation((Beta_Memory)child, t, w); //} //else if (child is P_Node) //{ // P_Node_Left_Activation((P_Node)child, t, w); //} // foamliu, 2008/08/26, use a general method here // to support both join-node and negative-node. LeftActivation(child, t, w); } } } }
private void HandleDummyTopToken(Join_Node j_node, WME w) { foreach (ReteNode j_child in j_node.Children) { Beta_Memory b_node = j_child as Beta_Memory; if (null != b_node) { Token new_token = new Token(); new_token.WME = w; b_node.Items.Insert(0, new_token); // for each child in node.children do left-activation (child, new-token) foreach (ReteNode b_child in b_node.Children) { JoinNodeLeftActivation((Join_Node)b_child, new_token); } } } }
private void DeleteNodeAndAnyUnusedAncestors(ReteNode node) { // foamliu, 2008/11/20, for NCC nodes, delete the partner node too NCC_Node ncc_node = node as NCC_Node; if (null != ncc_node) { DeleteNodeAndAnyUnusedAncestors(ncc_node.Partner); } // foamliu, 2008/11/20, clean up any tokens the node contains switch (node.Type) { case ReteNodeType.Beta_Memory: Beta_Memory b_node = (Beta_Memory)node; while (b_node.Items.Count != 0 && !(b_node.Items[0] is Dummy_Top_Token)) { DeleteTokenAndDescendents(b_node.Items[0]); } break; case ReteNodeType.Negative_Node: Negative_Node n_node = (Negative_Node)node; while (n_node.Items.Count != 0) { DeleteTokenAndDescendents(n_node.Items[0]); } break; case ReteNodeType.NCC_Node: while (ncc_node.Items.Count != 0) { DeleteTokenAndDescendents(ncc_node.Items[0]); } break; default: break; } NCC_Partner_Node par_node = node as NCC_Partner_Node; if (null != par_node) { while (par_node.New_Result_Buffer.Count != 0) { DeleteTokenAndDescendents(par_node.New_Result_Buffer[0]); } } // foamliu, 2008/11/20, for join and negative nodes, deal with the alpha memory // Join_Node j_node = node as Join_Node; if (null != j_node) { j_node.Amem.Successors.Remove(node); if (j_node.Amem.Successors.Count == 0) { DeleteAlphaMemory(j_node.Amem); } } else if (node.Type == ReteNodeType.Negative_Node) { Negative_Node n_node = (Negative_Node)node; n_node.Amem.Successors.Remove(node); if (n_node.Amem.Successors.Count == 0) { DeleteAlphaMemory(n_node.Amem); } } // foamliu, 2008/11/21, fix a null-ref bug. if (null != node.Parent) { node.Parent.Children.Remove(node); if (node.Parent.Children.Count == 0) { DeleteNodeAndAnyUnusedAncestors(node.Parent); } } node = null; }