Example #1
0
        private ReteNode BuildOrShareBetaMemoryNode(ReteNode parent)
        {
            // foamliu, 2008/12/10, the first is positive, the beta is not necessary
            if (parent is Dummy_Top_Node)
            {
                return(parent);
            }

            // look for an existing node to share
            foreach (ReteNode child in parent.Children)
            {
                if (child.Type == ReteNodeType.Beta_Memory)
                {
                    return(child);
                }
            }

            Beta_Memory new_beta = new Beta_Memory();

            //new_beta.Type = ReteNodeType.Beta_Memory;
            new_beta.Parent = parent;
            // insert new at the head of the list parent.children
            parent.Children.Insert(0, new_beta);
            //new_beta.Children.Clear();
            //new_beta.Items.Clear();
            UpdateNewNodeWithMatchesFromAbove(new_beta);
            return(new_beta);
        }
Example #2
0
 private void DeleteToken(ReteNode node, Token token)
 {
     if (node is Beta_Memory)
     {
         Beta_Memory b_node = (Beta_Memory)node;
         b_node.Items.Remove(token);
     }
     else if (node is P_Node)
     {
         P_Node p_node = (P_Node)node;
         //p_node.Items.Remove(tok);
         // foamliu, 2008/11/23, for publishing (de)activation events.
         p_node.RemoveToken(token);
     }
     else if (node is Negative_Node)
     {
         Negative_Node n_node = (Negative_Node)node;
         n_node.Items.Remove(token);
     }
     else if (node is NCC_Node)
     {
         NCC_Node ncc_node = (NCC_Node)node;
         ncc_node.Items.Remove(token);
     }
 }
Example #3
0
        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);
                    }
                }
            }
        }
Example #4
0
        public void BetaMemoryLeftActivation(Beta_Memory node, Token token, WME wme)
        {
            //Token new_token = new Token();
            //new_token.Parent = t;
            //new_token.WME = w;

            Token new_token = Make_Token(node, token, wme);

            node.Items.Insert(0, new_token);

            // for each child in node.children do left-activation (child, new-token)
            foreach (ReteNode child in node.Children)
            {
                JoinNodeLeftActivation((Join_Node)child, new_token);
            }
        }
Example #5
0
        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);
                    }
                }
            }
        }
Example #6
0
        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;
        }