Beispiel #1
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);
                 }
             }
         }
     }
 }
Beispiel #2
0
        public void PropagateAssert(IExecutionContext context, Fact fact)
        {
            IAlphaMemory memory = context.WorkingMemory.GetNodeMemory(this);

            memory.Facts.Add(fact);
            _sinks.ForEach(s => s.PropagateAssert(context, fact));
        }
Beispiel #3
0
        public void PropagateRetract(IExecutionContext context, List <Fact> facts)
        {
            IAlphaMemory memory    = context.WorkingMemory.GetNodeMemory(this);
            var          toRetract = new List <Fact>(facts.Count);

            using (var counter = PerfCounter.Retract(context, this))
            {
                foreach (var fact in facts)
                {
                    if (memory.Contains(fact))
                    {
                        toRetract.Add(fact);
                    }
                }

                counter.AddInputs(facts.Count);
                counter.AddOutputs(toRetract.Count);
            }

            if (toRetract.Count > 0)
            {
                foreach (var sink in _sinks)
                {
                    sink.PropagateRetract(context, toRetract);
                }

                using (var counter = PerfCounter.Retract(context, this))
                {
                    memory.Remove(toRetract);
                    counter.SetCount(memory.FactCount);
                }
            }
        }
Beispiel #4
0
        /// <summary> the implementation just propogates the assert down the network
        /// </summary>
        public override void assertFact(IFact fact, Rete engine, IWorkingMemory mem)
        {
            IAlphaMemory alpha = (IAlphaMemory)mem.getAlphaMemory(this);

            alpha.addPartialMatch(fact);
            propogateAssert(fact, engine, mem);
        }
Beispiel #5
0
 /// <summary> Remove a successor node
 /// </summary>
 /// <param name="">node
 /// </param>
 /// <param name="">engine
 /// </param>
 /// <param name="">mem
 /// @throws AssertException
 ///
 /// </param>
 public virtual void removeSuccessorNode(BaseNode node, Rete engine, IWorkingMemory mem)
 {
     if (removeNode(node))
     {
         // we retract the memories first, before removing the node
         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.retractFact((IFact)itr.Current, engine, mem);
                 }
                 else if (node is BaseJoin)
                 {
                     BaseJoin next = (BaseJoin)node;
                     next.retractRight((IFact)itr.Current, engine, mem);
                 }
             }
         }
     }
 }
        public void PropagateUpdate(IExecutionContext context, IList <Fact> facts)
        {
            IAlphaMemory memory   = context.WorkingMemory.GetNodeMemory(this);
            var          toUpdate = new List <Fact>();
            var          toAssert = new List <Fact>();

            foreach (var fact in facts)
            {
                if (memory.Contains(fact))
                {
                    toUpdate.Add(fact);
                }
                else
                {
                    toAssert.Add(fact);
                }
            }
            if (toUpdate.Count > 0)
            {
                foreach (var sink in _sinks)
                {
                    sink.PropagateUpdate(context, toUpdate);
                }
            }
            if (toAssert.Count > 0)
            {
                PropagateAssert(context, toAssert);
            }
        }
Beispiel #7
0
        public virtual void clear()
        {
            IEnumerator amitr = alphaMemories.Values.GetEnumerator();

            while (amitr.MoveNext())
            {
                IAlphaMemory am = (IAlphaMemory)amitr.Current;
                am.clear();
            }
            alphaMemories.Clear();
            // aggressivley Clear the memories
            IEnumerator blitr = betaLeftMemories.Values.GetEnumerator();

            while (blitr.MoveNext())
            {
                Object bval = blitr.Current;
                if (bval is IGenericMap <Object, Object> )
                {
                    IGenericMap <Object, Object> lmem = (IGenericMap <Object, Object>)bval;
                    // now iterate over the betamemories
                    IEnumerator bmitr = lmem.Keys.GetEnumerator();
                    while (bmitr.MoveNext())
                    {
                        Index indx = (Index)bmitr.Current;
                        indx.clear();
                    }
                    lmem.Clear();
                }
            }
            betaLeftMemories.Clear();
            IEnumerator britr = betaRightMemories.Values.GetEnumerator();

            while (britr.MoveNext())
            {
                Object val = britr.Current;
                if (val is HashedAlphaMemoryImpl)
                {
                    ((HashedAlphaMemoryImpl)val).clear();
                }
                else if (val is TemporalHashedAlphaMem)
                {
                    ((TemporalHashedAlphaMem)val).clear();
                }
                else
                {
                    IGenericMap <IFact, IFact> mem = (IGenericMap <IFact, IFact>)val;
                    mem.Clear();
                }
            }
            betaRightMemories.Clear();
            terminalMemories.Clear();
            root.clear();
            focusStack.Clear();
            //contexts.Clear();
            agenda.clear();
            main.clear();
            currentModule.clear();
            addModule(main);
        }
Beispiel #8
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);
                 }
             }
         }
     }
 }
Beispiel #9
0
        /// <summary> Retract simply propogates it down the network
        /// </summary>
        public override void retractFact(IFact fact, Rete engine, IWorkingMemory mem)
        {
            IAlphaMemory alpha = (IAlphaMemory)mem.getAlphaMemory(this);

            if (alpha.removePartialMatch(fact) != null)
            {
                propogateRetract(fact, engine, mem);
            }
        }
Beispiel #10
0
 /// <summary> the implementation will first check to see if the fact already matched.
 /// If it did, the fact stops and doesn't go any further. If it doesn't,
 /// it will attempt to evaluate it and Add the fact if it matches.
 /// </summary>
 /// <param name="">factInstance
 /// </param>
 /// <param name="">engine
 ///
 /// </param>
 public override void assertFact(IFact fact, Rete engine, IWorkingMemory mem)
 {
     if (evaluate(fact))
     {
         IAlphaMemory alpha = (IAlphaMemory)mem.getAlphaMemory(this);
         // we set the time of the last match
         alpha.addPartialMatch(fact);
         propogateAssert(fact, engine, mem);
     }
 }
Beispiel #11
0
        public void PropagateAssert(IExecutionContext context, IList <Fact> facts)
        {
            IAlphaMemory memory = context.WorkingMemory.GetNodeMemory(this);

            foreach (var sink in _sinks)
            {
                sink.PropagateAssert(context, facts);
            }
            memory.Add(facts);
        }
        public void PropagateRetract(IExecutionContext context, Fact fact)
        {
            IAlphaMemory memory = context.WorkingMemory.GetNodeMemory(this);

            foreach (var sink in _sinks)
            {
                sink.PropagateRetract(context, fact);
            }
            memory.Remove(fact);
        }
Beispiel #13
0
        /// <summary> Retract a fact from the node
        /// </summary>
        /// <param name="">factInstance
        /// </param>
        /// <param name="">engine
        ///
        /// </param>
        public override void retractFact(IFact fact, Rete engine, IWorkingMemory mem)
        {
            IAlphaMemory alpha = (IAlphaMemory)mem.getAlphaMemory(this);

            if (alpha.removePartialMatch(fact) != null)
            {
                // if watch is on, we notify the engine. Rather than
                // create an event class here, we let Rete do that.
                propogateRetract(fact, engine, mem);
            }
        }
Beispiel #14
0
 /// <summary> the implementation will first check to see if the fact already matched.
 /// If it did, the fact stops and doesn't go any further. If it doesn't,
 /// it will attempt to evaluate it and Add the fact if it matches.
 /// </summary>
 /// <param name="">factInstance
 /// </param>
 /// <param name="">engine
 ///
 /// </param>
 public override void assertFact(IFact fact, Rete engine, IWorkingMemory mem)
 {
     if (evaluate(fact))
     {
         IAlphaMemory alpha = (IAlphaMemory)mem.getAlphaMemory(this);
         alpha.addPartialMatch(fact);
         // if watch is on, we notify the engine. Rather than
         // create an event class here, we let Rete do that.
         propogateAssert(fact, engine, mem);
     }
 }
Beispiel #15
0
        public void PropagateUpdate(IExecutionContext context, Fact fact)
        {
            IAlphaMemory memory = context.WorkingMemory.GetNodeMemory(this);

            if (memory.Facts.Contains(fact))
            {
                _sinks.ForEach(s => s.PropagateUpdate(context, fact));
            }
            else
            {
                PropagateAssert(context, fact);
            }
        }
        public void PropagateUpdate(IExecutionContext context, Fact fact)
        {
            IAlphaMemory memory = context.WorkingMemory.GetNodeMemory(this);

            if (memory.Contains(fact))
            {
                foreach (var sink in _sinks)
                {
                    sink.PropagateUpdate(context, fact);
                }
            }
            else
            {
                PropagateAssert(context, fact);
            }
        }
Beispiel #17
0
        public void PropagateAssert(IExecutionContext context, List <Fact> facts)
        {
            IAlphaMemory memory = context.WorkingMemory.GetNodeMemory(this);

            foreach (var sink in _sinks)
            {
                sink.PropagateAssert(context, facts);
            }

            using (var counter = PerfCounter.Assert(context, this))
            {
                memory.Add(facts);

                counter.AddItems(facts.Count);
                counter.SetCount(memory.FactCount);
            }
        }
Beispiel #18
0
        public virtual void printWorkingMemory(bool detailed, bool inputNodes)
        {
            engine.writeMessage("AlphaNode count " + alphaMemories.Count + Constants.LINEBREAK);
            IEnumerator itr      = alphaMemories.Keys.GetEnumerator();
            int         memTotal = 0;

            while (itr.MoveNext())
            {
                BaseNode key = (BaseNode)itr.Current;
                if (!(key is ObjectTypeNode) && !(key is LIANode))
                {
                    IAlphaMemory am = (IAlphaMemory)alphaMemories.Get(key);
                    if (detailed)
                    {
                        engine.writeMessage(key.toPPString() + " count=" + am.size() + Constants.LINEBREAK);
                    }
                    memTotal += am.size();
                }
                else
                {
                    if (inputNodes)
                    {
                        IAlphaMemory am = (IAlphaMemory)alphaMemories.Get(key);
                        engine.writeMessage(key.toPPString() + " count=" + am.size() + Constants.LINEBREAK);
                    }
                }
            }
            engine.writeMessage("total AlphaMemories = " + memTotal + Constants.LINEBREAK);

            // now write out the left beta memory
            engine.writeMessage("BetaNode Count " + betaLeftMemories.Count + Constants.LINEBREAK);
            int betaTotal = 0;

            itr = betaLeftMemories.Keys.GetEnumerator();
            while (itr.MoveNext())
            {
                BaseNode key = (BaseNode)itr.Current;
                if (key is BaseJoin)
                {
                    printBetaNodes((BaseJoin)key, detailed, betaTotal);
                }
            }
            engine.writeMessage("total BetaMemories = " + betaTotal + Constants.LINEBREAK);
        }
Beispiel #19
0
        /// <summary>
        /// Printout the memory for the given rule.
        /// </summary>
        /// <param name="rule">The rule.</param>
        public virtual void printWorkingMemory(Rule.IRule rule)
        {
            engine.writeMessage("Memories for " + rule.Name);
            ICondition[] conds    = rule.Conditions;
            int          memTotal = 0;

            for (int idx = 0; idx < conds.Length; idx++)
            {
                ICondition  c   = conds[idx];
                IList       l   = c.Nodes;
                IEnumerator itr = l.GetEnumerator();
                while (itr.MoveNext())
                {
                    BaseNode     key = (BaseNode)itr.Current;
                    IAlphaMemory am  = (IAlphaMemory)alphaMemories.Get(key);
                    engine.writeMessage(key.toPPString() + " count=" + am.size() + Constants.LINEBREAK);
                    memTotal += am.size();
                }
            }
        }
Beispiel #20
0
        public void PropagateRetract(IExecutionContext context, IList <Fact> facts)
        {
            IAlphaMemory memory    = context.WorkingMemory.GetNodeMemory(this);
            var          toRetract = new List <Fact>(facts.Count);

            foreach (var fact in facts)
            {
                if (memory.Contains(fact))
                {
                    toRetract.Add(fact);
                }
            }
            if (toRetract.Count > 0)
            {
                foreach (var sink in _sinks)
                {
                    sink.PropagateRetract(context, toRetract);
                }
                memory.Remove(toRetract);
            }
        }
        public IEnumerable <Fact> GetFacts(IExecutionContext context)
        {
            IAlphaMemory memory = context.WorkingMemory.GetNodeMemory(this);

            return(memory.Facts);
        }
Beispiel #22
0
        /// <summary> Clear the memory. for now the method does not
        /// Remove all the successor nodes. need to think it over a bit.
        /// </summary>
        public override void clear(IWorkingMemory mem)
        {
            IAlphaMemory am = (IAlphaMemory)mem.getAlphaMemory(this);

            am.clear();
        }
Beispiel #23
0
        internal static NodeInfo Create(AlphaMemoryNode node, IAlphaMemory memory)
        {
            var items = memory.Facts.Select(f => f.Object.ToString());

            return(new NodeInfo(NodeType.AlphaMemory, string.Empty, Empty, Empty, items));
        }
Beispiel #24
0
 internal static NodeInfo Create(AlphaMemoryNode node, IAlphaMemory memory)
 {
     return new NodeInfo(NodeType.AlphaMemory, string.Empty, Empty, Empty, memory.Facts.Select(f => f.Object.ToString()));
 }