Example #1
0
        /// <summary>
        /// Method is used to pass a fact to the successor nodes
        /// </summary>
        /// <param name="inx">The inx.</param>
        /// <param name="engine">The engine.</param>
        /// <param name="mem">The mem.</param>
        protected internal virtual void propogateAssert(Index inx, Rete engine, IWorkingMemory mem)
        {
            for (int idx = 0; idx < successorNodes.Length; idx++)
            {
                BaseJoin baseJoin = successorNodes[idx] as BaseJoin;
                if (baseJoin != null)
                {
                    baseJoin.assertLeft(inx, engine, mem);
                    return;
                }

                TerminalNode terminalNode = successorNodes[idx] as TerminalNode;
                if (terminalNode != null)
                {
                    terminalNode.assertFacts(inx, engine, mem);
                    return;
                }

                //BaseNode node = successorNodes[idx];
                //if (node is BaseJoin)
                //{
                //    ((BaseJoin) node).assertLeft(inx, engine, mem);
                //}
                //else if (node is TerminalNode)
                //{
                //    ((TerminalNode) node).assertFacts(inx, engine, mem);
                //}
            }
        }
Example #2
0
        /// <summary>
        /// method for propogating the retract
        /// </summary>
        /// <param name="inx">The inx.</param>
        /// <param name="engine">The engine.</param>
        /// <param name="mem">The mem.</param>
        protected internal virtual void propogateRetract(Index inx, Rete engine, IWorkingMemory mem)
        {
            for (int idx = 0; idx < successorNodes.Length; idx++)
            {
                BaseJoin node = successorNodes[idx] as BaseJoin;
                if (node != null)
                {
                    node.retractLeft(inx, engine, mem);
                }
                else
                {
                    TerminalNode tnode = successorNodes[idx] as TerminalNode;
                    if (tnode != null)
                    {
                        tnode.retractFacts(inx, engine, mem);
                    }
                }


                //BaseNode node = successorNodes[idx];
                //if (node is BaseJoin)
                //{
                //    ((BaseJoin) node).retractLeft(inx, engine, mem);
                //}
                //else if (node is TerminalNode)
                //{
                //    ((TerminalNode) node).retractFacts(inx, engine, mem);
                //}
            }
        }
Example #3
0
 /// <summary> Set the Current node in the sequence of 1-input nodes.
 /// The Current node can be an AlphaNode or a LIANode.
 /// </summary>
 /// <param name="">node
 ///
 /// </param>
 public override void addSuccessorNode(BaseNode node, Rete engine, IWorkingMemory mem)
 {
     if (addNode(node))
     {
         // if there are matches, we propogate the facts to
         // the new successor only
         IAlphaMemory alpha = (IAlphaMemory)mem.getAlphaMemory(this);
         if (alpha.size() > 0)
         {
             IEnumerator itr = alpha.GetEnumerator();
             while (itr.MoveNext())
             {
                 if (node is BaseAlpha)
                 {
                     BaseAlpha next = (BaseAlpha)node;
                     next.assertFact((IFact)itr.Current, engine, mem);
                 }
                 else if (node is BaseJoin)
                 {
                     BaseJoin next = (BaseJoin)node;
                     next.assertRight((IFact)itr.Current, engine, mem);
                 }
                 else if (node is TerminalNode)
                 {
                     TerminalNode next = (TerminalNode)node;
                     Index        inx  = new Index(new IFact[] { (IFact)itr.Current });
                     next.assertFacts(inx, engine, mem);
                 }
             }
         }
     }
 }
Example #4
0
 /// <summary>
 /// it's unlikely 2 rules are identical, except for the name. The implementation
 /// gets the current memory and propogates, but I wonder how much sense this
 /// makes in a real production environment. An user really shouldn't be deploying
 /// identical rules with different rule name.
 /// </summary>
 /// <param name="node">The node.</param>
 /// <param name="engine">The engine.</param>
 /// <param name="mem">The mem.</param>
 public virtual void addSuccessorNode(TerminalNode node, Rete engine, IWorkingMemory mem)
 {
     if (addNode(node))
     {
         // first, we Get the memory for this node
         IGenericMap <Object, Object> leftmem = (IGenericMap <Object, Object>)mem.getBetaLeftMemory(this);
         // now we iterate over the entry set
         IEnumerator itr = leftmem.Values.GetEnumerator();
         while (itr.MoveNext())
         {
             Object omem = itr.Current;
             if (omem is IBetaMemory)
             {
                 IBetaMemory bmem = (IBetaMemory)omem;
                 Index       left = bmem.Index;
                 // iterate over the matches
                 IGenericMap <Object, Object> rightmem = (IGenericMap <Object, Object>)mem.getBetaRightMemory(this);
                 IEnumerator ritr = rightmem.Keys.GetEnumerator();
                 while (ritr.MoveNext())
                 {
                     IFact rfcts = (IFact)ritr.Current;
                     // merge the left and right fact into a new Array
                     node.assertFacts(left.add(rfcts), engine, mem);
                 }
             }
         }
     }
 }
Example #5
0
 /// <summary> NotJoin has to have a special addSuccessorNode since it needs
 /// to just propogate the left facts if it has zero matches.
 /// </summary>
 public override void addSuccessorNode(TerminalNode node, Rete engine, IWorkingMemory mem)
 {
     if (addNode(node))
     {
         // first, we Get the memory for this node
         IGenericMap <Object, Object> leftmem = (IGenericMap <Object, Object>)mem.getBetaLeftMemory(this);
         // now we iterate over the entry set
         IEnumerator itr = leftmem.Values.GetEnumerator();
         while (itr.MoveNext())
         {
             Object omem = itr.Current;
             if (omem is IBetaMemory)
             {
                 IBetaMemory           bmem     = (IBetaMemory)omem;
                 EqHashIndex           inx      = new EqHashIndex(NodeUtils.getLeftValues(binds, bmem.LeftFacts));
                 HashedAlphaMemoryImpl rightmem = (HashedAlphaMemoryImpl)mem.getBetaRightMemory(this);
                 // we don't bother adding the right fact to the left, since
                 // the right side is already Hashed
                 if (rightmem.count(inx) == 0)
                 {
                     node.assertFacts(bmem.Index, engine, mem);
                 }
             }
         }
     }
 }
Example #6
0
 /// <summary> NotJoin has to have a special addSuccessorNode since it needs
 /// to just propogate the left facts if it has zero matches.
 /// </summary>
 public override void addSuccessorNode(TerminalNode node, Rete engine, IWorkingMemory mem)
 {
     if (addNode(node))
     {
         // first, we Get the memory for this node
         IGenericMap<Object, Object> leftmem = (IGenericMap<Object, Object>) mem.getBetaLeftMemory(this);
         // now we iterate over the entry set
         IEnumerator itr = leftmem.Values.GetEnumerator();
         while (itr.MoveNext())
         {
             Object omem = itr.Current;
             if (omem is IBetaMemory)
             {
                 IBetaMemory bmem = (IBetaMemory) omem;
                 EqHashIndex inx = new EqHashIndex(NodeUtils.getLeftValues(binds, bmem.LeftFacts));
                 HashedAlphaMemoryImpl rightmem = (HashedAlphaMemoryImpl) mem.getBetaRightMemory(this);
                 // we don't bother adding the right fact to the left, since
                 // the right side is already Hashed
                 if (rightmem.count(inx) == 0)
                 {
                     node.assertFacts(bmem.Index, engine, mem);
                 }
             }
         }
     }
 }
Example #7
0
 /// <summary> Add a successor node
 /// </summary>
 public override void addSuccessorNode(BaseNode node, Rete engine, IWorkingMemory mem)
 {
     if (!containsNode(successorNodes, node) && !successor2.Contains(node))
     {
         if (node is BaseJoin || node is TerminalNode)
         {
             successor2.Add(node);
         }
         else
         {
             // we test to see if the operator is ==, nil, not nil
             // if the node isn't BaseJoin, it should be BaseAlpha
             BaseAlpha ba = (BaseAlpha)node;
             if (ba.Operator == Constants.LESS || ba.Operator == Constants.GREATER || ba.Operator == Constants.LESSEQUAL || ba.Operator == Constants.GREATEREQUAL || ba.Operator == Constants.NOTEQUAL || ba.Operator == Constants.NOTNILL)
             {
                 successor2.Add(node);
             }
             else
             {
                 addNode(node);
             }
         }
         if (gauranteeUnique && node is AlphaNode)
         {
             // now we use CompositeIndex instead of HashString
             AlphaNode anode = (AlphaNode)node;
             entries.Put(anode.HashIndex, node);
             // we increment the node count for the slot
             deftemplate.getSlot(anode.slot.Id).incrementNodeCount();
         }
         // if there are matches, we propogate the facts to
         // the new successor only
         IAlphaMemory alpha = (IAlphaMemory)mem.getAlphaMemory(this);
         if (alpha.size() > 0)
         {
             IEnumerator itr = alpha.GetEnumerator();
             while (itr.MoveNext())
             {
                 IFact f = (IFact)itr.Current;
                 if (node is BaseAlpha)
                 {
                     BaseAlpha next = (BaseAlpha)node;
                     next.assertFact(f, engine, mem);
                 }
                 else if (node is BaseJoin)
                 {
                     BaseJoin next = (BaseJoin)node;
                     next.assertRight(f, engine, mem);
                 }
                 else if (node is TerminalNode)
                 {
                     TerminalNode t   = (TerminalNode)node;
                     Index        inx = new Index(new IFact[] { f });
                     t.assertFacts(inx, engine, mem);
                 }
             }
         }
     }
 }
Example #8
0
 /// <summary> propogate the retract
 /// 
 /// </summary>
 /// <param name="">fact
 /// </param>
 /// <param name="">engine
 /// 
 /// </param>
 protected internal override void propogateRetract(IFact fact, Rete engine, IWorkingMemory mem)
 {
     for (int idx = 0; idx < successorNodes.Length; idx++)
     {
         BaseNode nNode = successorNodes[idx];
         if (nNode is BaseJoin)
         {
             BaseJoin next = (BaseJoin) nNode;
             IFact[] newf = new IFact[] {fact};
             next.retractLeft(new Index(newf), engine, mem);
         }
         else if (nNode is TerminalNode)
         {
             TerminalNode next = (TerminalNode) nNode;
             IFact[] newf = new IFact[] {fact};
             next.retractFacts(new Index(newf), engine, mem);
         }
     }
 }
 protected internal virtual void attachTerminalNode(BaseNode last, TerminalNode terminal)
 {
     if (last != null && terminal != null)
     {
         try
         {
             if (last is BaseJoin)
             {
                 ((BaseJoin)last).addSuccessorNode(terminal, engine, memory);
             }
             else if (last is BaseAlpha)
             {
                 ((BaseAlpha)last).addSuccessorNode(terminal, engine, memory);
             }
         }
         catch (AssertException e)
         {
         }
     }
 }
Example #10
0
 /// <summary> Method is used to pass a fact to the successor nodes
 /// </summary>
 /// <param name="">fact
 /// </param>
 /// <param name="">engine
 ///
 /// </param>
 protected internal virtual void propogateAssert(IFact fact, Rete engine, IWorkingMemory mem)
 {
     for (int idx = 0; idx < successorNodes.Length; idx++)
     {
         Object nNode = successorNodes[idx];
         if (nNode is BaseAlpha)
         {
             BaseAlpha next = (BaseAlpha)nNode;
             next.assertFact(fact, engine, mem);
         }
         else if (nNode is BaseJoin)
         {
             BaseJoin next = (BaseJoin)nNode;
             next.assertRight(fact, engine, mem);
         }
         else if (nNode is TerminalNode)
         {
             TerminalNode next = (TerminalNode)nNode;
             Index        inx  = new Index(new IFact[] { fact });
             next.assertFacts(inx, engine, mem);
         }
     }
 }
Example #11
0
 /// <summary> NotJoin has to have a special addSuccessorNode since it needs
 /// to just propogate the left facts if it has zero matches.
 /// </summary>
 public override void addSuccessorNode(TerminalNode node, Rete engine, IWorkingMemory mem)
 {
     if (addNode(node))
     {
         // first, we Get the memory for this node
         IGenericMap <Object, Object> leftmem = (IGenericMap <Object, Object>)mem.getBetaLeftMemory(this);
         // now we iterate over the entry set
         IEnumerator itr = leftmem.Values.GetEnumerator();
         while (itr.MoveNext())
         {
             Object omem = itr.Current;
             if (omem is IBetaMemory)
             {
                 IBetaMemory bmem = (IBetaMemory)omem;
                 // iterate over the matches
                 if (bmem.matchCount() == 0)
                 {
                     node.assertFacts(bmem.Index, engine, mem);
                 }
             }
         }
     }
 }
Example #12
0
 /// <summary> NotJoin has to have a special addSuccessorNode since it needs
 /// to just propogate the left facts if it has zero matches.
 /// </summary>
 public override void addSuccessorNode(TerminalNode node, Rete engine, IWorkingMemory mem)
 {
     if (addNode(node))
     {
         // first, we Get the memory for this node
         IGenericMap<Object, Object> leftmem = (IGenericMap<Object, Object>) mem.getBetaLeftMemory(this);
         // now we iterate over the entry set
         IEnumerator itr = leftmem.Values.GetEnumerator();
         while (itr.MoveNext())
         {
             Object omem = itr.Current;
             if (omem is IBetaMemory)
             {
                 IBetaMemory bmem = (IBetaMemory) omem;
                 // iterate over the matches
                 if (bmem.matchCount() == 0)
                 {
                     node.assertFacts(bmem.Index, engine, mem);
                 }
             }
         }
     }
 }
Example #13
0
 protected internal virtual void attachTerminalNode(BaseNode last, TerminalNode terminal)
 {
     if (last != null && terminal != null)
     {
         try
         {
             if (last is BaseJoin)
             {
                 ((BaseJoin) last).addSuccessorNode(terminal, engine, memory);
             }
             else if (last is BaseAlpha)
             {
                 ((BaseAlpha) last).addSuccessorNode(terminal, engine, memory);
             }
         }
         catch (AssertException e)
         {
         }
     }
 }
        /// <summary> Here is a description of the compilation algorithm.
        /// 1. iterate over the conditional elements
        /// i. generate the alpha nodes
        /// a. literal constraints generate alpha node
        /// b. predicate constaints that compare against a literal generate alpha node
        /// ii. calculate the bindings
        /// a. each binding has a rowId
        /// b. NOT and EXIST CE do not increment the rowId
        /// 2. iterate over the conditional elements
        /// i. generate the beta nodes
        /// ii. attach the Left Input adapater nodes
        /// iii. attach the join nodes to the alpha nodes
        /// 3. create the terminal node and attach to the last
        /// join node.
        ///
        /// This means the rule compiler takes a 2 pass approach to
        /// compiling rules. At the start of the method, it sets 3
        /// attributes to null: prevCE, prevJoinNode, joinNode.
        /// Those attributes are used by the compile join methods,
        /// so it's important to set it to null at the start. If
        /// we don't the Current rule won't compile correctly.
        /// </summary>
        public virtual bool addRule(Rule.IRule rule)
        {
            rule.resolveTemplates(engine);
            if (!validate || (validate && tval.analyze(rule) == Analysis_Fields.VALIDATION_PASSED))
            {
                // we have to set the attributes to null, before we start compiling a rule.

                // we've set the attributes to null, so we can compile now!!

                if (rule.Conditions != null && rule.Conditions.Length > 0)
                {
                    // we check the name of the rule to see if it is for a specific
                    // module. if it is, we have to Add it to that module
                    Module = rule;
                    try
                    {
                        ICondition[] conds = rule.Conditions;
                        // first we create the constraints, before creating the Conditional
                        // elements which include joins
                        // we use a counter and only increment it to make sure the
                        // row index of the bindings are accurate. this makes it simpler
                        // for the rule compiler and compileJoins is cleaner and does
                        // less work.
                        int counter = 0;
                        for (int idx = 0; idx < conds.Length; idx++)
                        {
                            ICondition con = conds[idx];
                            // compile object conditions
                            //implement in the ObjectConditionCompiler.compile or ExistConditionCompiler.compile
                            con.getCompiler(this).compile(con, counter, rule, rule.RememberMatch);

                            if ((con is ObjectCondition) && (!((ObjectCondition)con).Negated))
                            {
                                counter++;
                            }
                        }
                        // now we compile the joins
                        compileJoins(rule);

                        BaseNode     last  = rule.LastNode;
                        TerminalNode tnode = createTerminalNode(rule);

                        attachTerminalNode(last, tnode);
                        // compile the actions
                        compileActions(rule, rule.Actions);
                        // now we pass the bindings to the rule, so that actiosn can
                        // resolve the bindings

                        // now we Add the rule to the module
                        currentMod.addRule(rule);
                        CompileMessageEventArgs ce = new CompileMessageEventArgs(rule, EventType.ADD_RULE_EVENT);
                        ce.Rule = rule;
                        notifyListener(ce);
                        return(true);
                    }
                    catch (AssertException e)
                    {
                        CompileMessageEventArgs ce = new CompileMessageEventArgs(rule, EventType.INVALID_RULE);
                        ce.Message = Messages.getString("RuleCompiler.assert.error"); //$NON-NLS-1$
                        notifyListener(ce);
                        TraceLogger.Instance.Debug(e);
                        return(false);
                    }
                }
                else if (rule.Conditions.Length == 0)
                {
                    Module = rule;
                    // the rule has no LHS, this means it only has actions
                    BaseNode     last  = (BaseNode)inputnodes.Get(engine.initFact);
                    TerminalNode tnode = createTerminalNode(rule);
                    compileActions(rule, rule.Actions);
                    attachTerminalNode(last, tnode);
                    // now we Add the rule to the module
                    currentMod.addRule(rule);
                    CompileMessageEventArgs ce = new CompileMessageEventArgs(rule, EventType.ADD_RULE_EVENT);
                    ce.Rule = rule;
                    notifyListener(ce);
                    return(true);
                }
                return(false);
            }
            else
            {
                // we need to print out a message saying the rule was not valid
                ISummary error = tval.Errors;
                engine.writeMessage("Rule " + rule.Name + " was not added. ", Constants.DEFAULT_OUTPUT); //$NON-NLS-1$ //$NON-NLS-2$
                engine.writeMessage(error.Message, Constants.DEFAULT_OUTPUT);
                ISummary warn = tval.Warnings;
                engine.writeMessage(warn.Message, Constants.DEFAULT_OUTPUT);
                return(false);
            }
        }
Example #15
0
 /// <summary>
 /// it's unlikely 2 rules are identical, except for the name. The implementation
 /// gets the current memory and propogates, but I wonder how much sense this
 /// makes in a real production environment. An user really shouldn't be deploying
 /// identical rules with different rule name.
 /// </summary>
 /// <param name="node">The node.</param>
 /// <param name="engine">The engine.</param>
 /// <param name="mem">The mem.</param>
 public virtual void addSuccessorNode(TerminalNode node, Rete engine, IWorkingMemory mem)
 {
     if (addNode(node))
     {
         // first, we Get the memory for this node
         IGenericMap<Object, Object> leftmem = (IGenericMap<Object, Object>) mem.getBetaLeftMemory(this);
         // now we iterate over the entry set
         IEnumerator itr = leftmem.Values.GetEnumerator();
         while (itr.MoveNext())
         {
             Object omem = itr.Current;
             if (omem is IBetaMemory)
             {
                 IBetaMemory bmem = (IBetaMemory) omem;
                 Index left = bmem.Index;
                 // iterate over the matches
                 IGenericMap<Object, Object> rightmem = (IGenericMap<Object, Object>) mem.getBetaRightMemory(this);
                 IEnumerator ritr = rightmem.Keys.GetEnumerator();
                 while (ritr.MoveNext())
                 {
                     IFact rfcts = (IFact) ritr.Current;
                     // merge the left and right fact into a new Array
                     node.assertFacts(left.add(rfcts), engine, mem);
                 }
             }
         }
     }
 }