Ejemplo n.º 1
0
        /// <summary> The implementation is different than EqHashIndex. It ignores
        /// any Bindings that are negated
        /// </summary>
        private void calculateHash()
        {
            Object[] neg = new Object[values.Length];
            int      z   = 0;

            if (values != null && values.Length > 0)
            {
                for (int idx = 0; idx < values.Length; idx++)
                {
                    if (values[idx] != null && !values[idx].negated())
                    {
                        eqhashCode += values[idx].Value.GetHashCode();
                    }
                    else
                    {
                        neg[z] = values[idx].Value;
                        z++;
                    }
                }
            }
            Object[] neg2 = new Object[z];
            Array.Copy(neg, 0, neg2, 0, z);
            negindex = new EqHashIndex(neg2);
            neg      = null;
            neg2     = null;
        }
Ejemplo n.º 2
0
        /// <summary> Assert from the right side is always going to be from an Alpha node.
        ///
        /// </summary>
        /// <param name="">factInstance
        /// </param>
        /// <param name="">engine
        ///
        /// </param>
        public override void assertRight(IFact rfact, Rete engine, IWorkingMemory mem)
        {
            HashedAlphaMemoryImpl rightmem = (HashedAlphaMemoryImpl)mem.getBetaRightMemory(this);
            EqHashIndex           inx      = new EqHashIndex(NodeUtils.getRightValues(binds, rfact));

            rightmem.addPartialMatch(inx, rfact);
            // now that we've added the facts to the list, we
            // proceed with evaluating the fact
            IGenericMap <Object, Object> leftmem = (IGenericMap <Object, Object>)mem.getBetaLeftMemory(this);
            // since there may be key collisions, we iterate over the
            // values of the HashMap. If we used keySet to iterate,
            // we could encounter a ClassCastException in the case of
            // key collision.
            IEnumerator itr = leftmem.Values.GetEnumerator();

            while (itr.MoveNext())
            {
                Index linx = (Index)itr.Current;
                if (evaluate(linx.Facts, rfact))
                {
                    // now we propogate
                    propogateAssert(linx.add(rfact), engine, mem);
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary> Retract from the right works in the following order.
        /// 1. Remove the fact from the right memory
        /// 2. check which left memory matched
        /// 3. propogate the retract
        /// </summary>
        /// <param name="">factInstance
        /// </param>
        /// <param name="">engine
        ///
        /// </param>
        public override void retractRight(IFact rfact, Rete engine, IWorkingMemory mem)
        {
            HashedAlphaMemoryImpl rightmem = (HashedAlphaMemoryImpl)mem.getBetaRightMemory(this);
            EqHashIndex           inx      = new EqHashIndex(NodeUtils.getRightValues(binds, rfact));
            // Remove the fact from the right
            int after = rightmem.removePartialMatch(inx, rfact);

            if (after == 0)
            {
                // now we see the left memory matched and Remove it also
                IGenericMap <Object, Object> leftmem = (IGenericMap <Object, Object>)mem.getBetaLeftMemory(this);
                IEnumerator itr = leftmem.Values.GetEnumerator();
                while (itr.MoveNext())
                {
                    Index linx = (Index)itr.Current;
                    if (evaluate(linx.Facts, rfact))
                    {
                        try
                        {
                            propogateAssert(linx, engine, mem);
                        }
                        catch (AssertException e)
                        {
                            throw new RetractException("NotJion - " + e.Message);
                        }
                    }
                }
                inx = null;
            }
        }
Ejemplo n.º 4
0
        /// <summary> Assert from the right side is always going to be from an Alpha node.
        ///
        /// </summary>
        /// <param name="">factInstance
        /// </param>
        /// <param name="">engine
        ///
        /// </param>
        public override void assertRight(IFact rfact, Rete engine, IWorkingMemory mem)
        {
            // Get the memory for the node
            HashedAlphaMemoryImpl rightmem = (HashedAlphaMemoryImpl)mem.getBetaRightMemory(this);
            EqHashIndex           inx      = new EqHashIndex(NodeUtils.getRightValues(binds, rfact));

            rightmem.addPartialMatch(inx, rfact);
            int after = rightmem.count(inx);
            IGenericMap <Object, Object> leftmem = (IGenericMap <Object, Object>)mem.getBetaLeftMemory(this);
            IEnumerator itr = leftmem.Values.GetEnumerator();

            while (itr.MoveNext())
            {
                Index linx = (Index)itr.Current;
                if (evaluate(linx.Facts, rfact))
                {
                    if (after == 1)
                    {
                        // we have to retract
                        try
                        {
                            propogateRetract(linx, engine, mem);
                        }
                        catch (RetractException e)
                        {
                            throw new AssertException("NotJion - " + e.Message);
                        }
                    }
                }
            }
        }
Ejemplo n.º 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);
                 }
             }
         }
     }
 }
Ejemplo n.º 6
0
 public override void assertLeft(Index linx, Rete engine, IWorkingMemory mem)
 {
     long time = RightTime;
     IGenericMap<Object, Object> leftmem = (IGenericMap<Object, Object>) mem.getBetaLeftMemory(this);
     leftmem.Put(linx, linx);
     EqHashIndex inx = new EqHashIndex(NodeUtils.getLeftValues(binds, linx.Facts));
     TemporalHashedAlphaMem rightmem = (TemporalHashedAlphaMem) mem.getBetaRightMemory(this);
     IEnumerator itr = rightmem.iterator(inx);
     if (itr != null)
     {
         try
         {
             while (itr.MoveNext())
             {
                 IFact vl = (IFact) itr.Current;
                 if (vl != null)
                 {
                     if (vl.timeStamp() > time)
                     {
                         propogateAssert(linx.add(vl), engine, mem);
                     }
                     else
                     {
                         rightmem.removePartialMatch(inx, vl);
                         propogateRetract(linx.add(vl), engine, mem);
                     }
                 }
             }
         }
         catch (RetractException e)
         {
             // there shouldn't be any retract exceptions
         }
     }
 }
Ejemplo n.º 7
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);
                 }
             }
         }
     }
 }
Ejemplo n.º 8
0
 public override void assertRight(IFact rfact, Rete engine, IWorkingMemory mem)
 {
     long time = LeftTime;
     TemporalHashedAlphaMem rightmem = (TemporalHashedAlphaMem) mem.getBetaRightMemory(this);
     EqHashIndex inx = new EqHashIndex(NodeUtils.getRightValues(binds, rfact));
     rightmem.addPartialMatch(inx, rfact);
     // now that we've added the facts to the list, we
     // proceed with evaluating the fact
     IGenericMap<Object, Object> leftmem = (IGenericMap<Object, Object>) mem.getBetaLeftMemory(this);
     // since there may be key collisions, we iterate over the
     // values of the HashMap. If we used keySet to iterate,
     // we could encounter a ClassCastException in the case of
     // key collision.
     IEnumerator itr = leftmem.Values.GetEnumerator();
     try
     {
         while (itr.MoveNext())
         {
             Index linx = (Index) itr.Current;
             if (evaluate(linx.Facts, rfact, time))
             {
                 // now we propogate
                 propogateAssert(linx.add(rfact), engine, mem);
             }
             else
             {
                 propogateRetract(linx.add(rfact), engine, mem);
             }
         }
     }
     catch (RetractException e)
     {
         // we shouldn't Get a retract exception. if we do, it's a bug
     }
 }
Ejemplo n.º 9
0
        /// <summary> assertLeft takes an array of facts. Since the Current join may be
        /// joining against one or more objects, we need to pass all
        /// previously matched facts.
        /// </summary>
        /// <param name="">factInstance
        /// </param>
        /// <param name="">engine
        ///
        /// </param>
        public override void assertLeft(Index linx, Rete engine, IWorkingMemory mem)
        {
            IGenericMap <Object, Object> leftmem = (IGenericMap <Object, Object>)mem.getBetaLeftMemory(this);

            leftmem.Put(linx, linx);
            EqHashIndex           inx      = new EqHashIndex(NodeUtils.getLeftValues(binds, linx.Facts));
            HashedAlphaMemoryImpl rightmem = (HashedAlphaMemoryImpl)mem.getBetaRightMemory(this);

            if (rightmem.count(inx) > 0)
            {
                propogateAssert(linx, engine, mem);
            }
        }
Ejemplo n.º 10
0
        /// <summary> assertLeft takes an array of facts. Since the Current join may be
        /// joining against one or more objects, we need to pass all
        /// previously matched facts.
        /// </summary>
        /// <param name="">factInstance
        /// </param>
        /// <param name="">engine
        ///
        /// </param>
        public override void assertLeft(Index linx, Rete engine, IWorkingMemory mem)
        {
            IGenericMap <Object, Object> leftmem = (IGenericMap <Object, Object>)mem.getBetaLeftMemory(this);

            leftmem.Put(linx, linx);
            EqHashIndex           inx      = new EqHashIndex(NodeUtils.getLeftValues(binds, linx.Facts));
            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)
            {
                propogateAssert(linx, engine, mem);
            }
        }
Ejemplo n.º 11
0
        /// <summary> Return the number of memories of all hash buckets
        /// </summary>
        public override int size()
        {
            IEnumerator itr   = memory.Keys.GetEnumerator();
            int         count = 0;

            while (itr.MoveNext())
            {
                IGenericMap <Object, Object> matches = (IGenericMap <Object, Object>)memory.Get(itr.Current);
                IEnumerator itr2 = matches.Keys.GetEnumerator();
                while (itr2.MoveNext())
                {
                    EqHashIndex ehi = (EqHashIndex)itr2.Current;
                    IGenericMap <Object, Object> submatch = (IGenericMap <Object, Object>)matches.Get(ehi);
                    count += submatch.Count;
                }
            }
            return(count);
        }
Ejemplo n.º 12
0
        /// <summary> Assert from the right side is always going to be from an
        /// Alpha node.
        /// </summary>
        /// <param name="">factInstance
        /// </param>
        /// <param name="">engine
        ///
        /// </param>
        public override void assertRight(IFact rfact, Rete engine, IWorkingMemory mem)
        {
            HashedAlphaMemoryImpl rightmem = (HashedAlphaMemoryImpl)mem.getBetaRightMemory(this);
            EqHashIndex           inx      = new EqHashIndex(NodeUtils.getRightValues(binds, rfact));
            int after = rightmem.addPartialMatch(inx, rfact);
            IGenericMap <Object, Object> leftmem = (IGenericMap <Object, Object>)mem.getBetaLeftMemory(this);
            IEnumerator itr = leftmem.Values.GetEnumerator();

            while (itr.MoveNext())
            {
                Index linx = (Index)itr.Current;
                if (evaluate(linx.Facts, rfact))
                {
                    if (after == 1)
                    {
                        propogateAssert(linx, engine, mem);
                    }
                }
            }
        }
Ejemplo n.º 13
0
        /// <summary> Retract from the right works in the following order. 1. Remove the fact
        /// from the right memory 2. check which left memory matched 3. propogate the
        /// retract
        ///
        /// </summary>
        /// <param name="">factInstance
        /// </param>
        /// <param name="">engine
        ///
        /// </param>
        public override void retractRight(IFact rfact, Rete engine, IWorkingMemory mem)
        {
            EqHashIndex           inx      = new EqHashIndex(NodeUtils.getRightValues(binds, rfact));
            HashedAlphaMemoryImpl rightmem = (HashedAlphaMemoryImpl)mem.getBetaRightMemory(this);

            // first we Remove the fact from the right
            rightmem.removePartialMatch(inx, rfact);
            // now we see the left memory matched and Remove it also
            IGenericMap <Object, Object> leftmem = (IGenericMap <Object, Object>)mem.getBetaLeftMemory(this);
            IEnumerator itr = leftmem.Values.GetEnumerator();

            while (itr.MoveNext())
            {
                Index linx = (Index)itr.Current;
                if (evaluate(linx.Facts, rfact))
                {
                    propogateRetract(linx.add(rfact), engine, mem);
                }
            }
        }
Ejemplo n.º 14
0
        /// <summary> Retracting from the left requires that we propogate the
        ///
        /// </summary>
        /// <param name="">factInstance
        /// </param>
        /// <param name="">engine
        ///
        /// </param>
        public override void retractLeft(Index linx, Rete engine, IWorkingMemory mem)
        {
            IGenericMap <Object, Object> leftmem = (IGenericMap <Object, Object>)mem.getBetaLeftMemory(this);

            leftmem.Remove(linx);
            EqHashIndex           eqinx    = new EqHashIndex(NodeUtils.getLeftValues(binds, linx.Facts));
            HashedAlphaMemoryImpl rightmem = (HashedAlphaMemoryImpl)mem.getBetaRightMemory(this);

            // now we propogate the retract. To do that, we have
            // merge each item in the list with the Fact array
            // and call retract in the successor nodes
            IEnumerator itr = rightmem.iterator(eqinx);

            if (itr != null)
            {
                while (itr.MoveNext())
                {
                    propogateRetract(linx.add((IFact)itr.Current), engine, mem);
                }
            }
        }
Ejemplo n.º 15
0
        /// <summary> assertLeft takes an array of facts. Since the Current join may be joining
        /// against one or more objects, we need to pass all previously matched
        /// facts.
        ///
        /// </summary>
        /// <param name="">factInstance
        /// </param>
        /// <param name="">engine
        ///
        /// </param>
        public override void assertLeft(Index linx, Rete engine, IWorkingMemory mem)
        {
            IGenericMap <Object, Object> leftmem = (IGenericMap <Object, Object>)mem.getBetaLeftMemory(this);

            leftmem.Put(linx, linx);
            EqHashIndex           inx      = new EqHashIndex(NodeUtils.getLeftValues(binds, linx.Facts));
            HashedAlphaMemoryImpl rightmem = (HashedAlphaMemoryImpl)mem.getBetaRightMemory(this);
            IEnumerator           itr      = rightmem.iterator(inx);

            if (itr != null)
            {
                while (itr.MoveNext())
                {
                    IFact vl = (IFact)itr.Current;
                    if (vl != null)
                    {
                        propogateAssert(linx.add(vl), engine, mem);
                    }
                }
            }
        }
Ejemplo n.º 16
0
        /// <summary> The implementation is similar to the index class.
        /// </summary>
        public override bool Equals(Object val)
        {
            if (this == val)
            {
                return(true);
            }
            if (val == null || !(val is EqHashIndex))
            {
                return(false);
            }
            EqHashIndex eval = (EqHashIndex)val;
            bool        eq   = true;

            for (int idx = 0; idx < values.Length; idx++)
            {
                if (!eval.values[idx].Equals(values[idx]))
                {
                    eq = false;
                    break;
                }
            }
            return(eq);
        }
Ejemplo n.º 17
0
        public override void assertLeft(Index linx, Rete engine, IWorkingMemory mem)
        {
            long time = RightTime;
            IGenericMap <Object, Object> leftmem = (IGenericMap <Object, Object>)mem.getBetaLeftMemory(this);

            leftmem.Put(linx, linx);
            EqHashIndex            inx      = new EqHashIndex(NodeUtils.getLeftValues(binds, linx.Facts));
            TemporalHashedAlphaMem rightmem = (TemporalHashedAlphaMem)mem.getBetaRightMemory(this);
            IEnumerator            itr      = rightmem.iterator(inx);

            if (itr != null)
            {
                try
                {
                    while (itr.MoveNext())
                    {
                        IFact vl = (IFact)itr.Current;
                        if (vl != null)
                        {
                            if (vl.timeStamp() > time)
                            {
                                propogateAssert(linx.add(vl), engine, mem);
                            }
                            else
                            {
                                rightmem.removePartialMatch(inx, vl);
                                propogateRetract(linx.add(vl), engine, mem);
                            }
                        }
                    }
                }
                catch (RetractException e)
                {
                    // there shouldn't be any retract exceptions
                }
            }
        }
Ejemplo n.º 18
0
 /// <summary> The implementation is different than EqHashIndex. It ignores
 /// any Bindings that are negated
 /// </summary>
 private void calculateHash()
 {
     Object[] neg = new Object[values.Length];
     int z = 0;
     if (values != null && values.Length > 0)
     {
         for (int idx = 0; idx < values.Length; idx++)
         {
             if (values[idx] != null && !values[idx].negated())
             {
                 eqhashCode += values[idx].Value.GetHashCode();
             }
             else
             {
                 neg[z] = values[idx].Value;
                 z++;
             }
         }
     }
     Object[] neg2 = new Object[z];
     Array.Copy(neg, 0, neg2, 0, z);
     negindex = new EqHashIndex(neg2);
     neg = null;
     neg2 = null;
 }
Ejemplo n.º 19
0
        public override void retractLeft(Index linx, Rete engine, IWorkingMemory mem)
        {
            IGenericMap<Object, Object> leftmem = (IGenericMap<Object, Object>) mem.getBetaLeftMemory(this);
            leftmem.Remove(linx);
            EqHashIndex eqinx = new EqHashIndex(NodeUtils.getLeftValues(binds, linx.Facts));
            TemporalHashedAlphaMem rightmem = (TemporalHashedAlphaMem) mem.getBetaRightMemory(this);

            // now we propogate the retract. To do that, we have
            // merge each item in the list with the Fact array
            // and call retract in the successor nodes
            IEnumerator itr = rightmem.iterator(eqinx);
            if (itr != null)
            {
                while (itr.MoveNext())
                {
                    propogateRetract(linx.add((IFact) itr.Current), engine, mem);
                }
            }
        }
Ejemplo n.º 20
0
        protected internal virtual void printBetaNodes(BaseJoin bjoin, bool detailed, int betaTotal)
        {
            if (bjoin is HashedEqBNode || bjoin is HashedEqNJoin)
            {
                IGenericMap <Object, Object> bm = (IGenericMap <Object, Object>)betaLeftMemories.Get(bjoin);
                // we iterate over the keys in the HashMap
                IEnumerator bitr = bm.Keys.GetEnumerator();
                while (bitr.MoveNext())
                {
                    Index indx = (Index)bm.Get(bitr.Current);
                    if (detailed)
                    {
                        engine.writeMessage(bjoin.toPPString(), Constants.DEFAULT_OUTPUT);
                        HashedAlphaMemoryImpl rightmem = (HashedAlphaMemoryImpl)getBetaRightMemory(bjoin);

                        EqHashIndex eqinx = new EqHashIndex(NodeUtils.getLeftValues(bjoin.binds, indx.Facts));
                        // Add to the total count
                        betaTotal += rightmem.count(eqinx);
                        engine.writeMessage(" count=" + betaTotal + " - " + indx.toPPString() + ": ", Constants.DEFAULT_OUTPUT);
                        IEnumerator ritr = rightmem.iterator(eqinx);
                        if (ritr != null)
                        {
                            StringBuilder buf = new StringBuilder();
                            while (ritr.MoveNext())
                            {
                                buf.Append(((IFact)ritr.Current).FactId + ",");
                            }
                            engine.writeMessage(buf.ToString(), Constants.DEFAULT_OUTPUT);
                        }
                        engine.writeMessage(Constants.LINEBREAK, Constants.DEFAULT_OUTPUT);
                    }
                }
            }
            else if (bjoin is HashedNotEqNJoin || bjoin is HashedNotEqBNode)
            {
                IGenericMap <Object, Object> bm = (IGenericMap <Object, Object>)betaLeftMemories.Get(bjoin);
                // we iterate over the keys in the HashMap
                IEnumerator bitr = bm.Keys.GetEnumerator();
                while (bitr.MoveNext())
                {
                    Index indx = (Index)bm.Get(bitr.Current);
                    if (detailed)
                    {
                        engine.writeMessage(bjoin.toPPString(), Constants.DEFAULT_OUTPUT);
                        HashedNeqAlphaMemory rightmem = (HashedNeqAlphaMemory)getBetaRightMemory(bjoin);

                        EqHashIndex eqinx = new EqHashIndex(NodeUtils.getLeftValues(bjoin.binds, indx.Facts));
                        // Add to the total count
                        betaTotal += rightmem.count(eqinx);
                        engine.writeMessage(" count=" + betaTotal + " - " + indx.toPPString() + ": ", Constants.DEFAULT_OUTPUT);
                        IEnumerator ritr = rightmem.iterator(eqinx);
                        if (ritr != null)
                        {
                            StringBuilder buf = new StringBuilder();
                            while (ritr.MoveNext())
                            {
                                buf.Append(((IFact)ritr.Current).FactId + ",");
                            }
                            engine.writeMessage(buf.ToString(), Constants.DEFAULT_OUTPUT);
                        }
                        engine.writeMessage(Constants.LINEBREAK, Constants.DEFAULT_OUTPUT);
                    }
                }
            }
            else if (bjoin is ExistJoin)
            {
                ExistJoin henj = (ExistJoin)bjoin;
                IGenericMap <Object, Object> bm = (IGenericMap <Object, Object>)betaLeftMemories.Get(henj);
                // we iterate over the keys in the HashMap
                IEnumerator bitr = bm.Keys.GetEnumerator();
                while (bitr.MoveNext())
                {
                    Index indx = (Index)bm.Get(bitr.Current);
                    if (detailed)
                    {
                        engine.writeMessage(bjoin.toPPString(), Constants.DEFAULT_OUTPUT);
                        HashedAlphaMemoryImpl rightmem = (HashedAlphaMemoryImpl)getBetaRightMemory(henj);

                        EqHashIndex eqinx = new EqHashIndex(NodeUtils.getLeftValues(henj.binds, indx.Facts));
                        // Add to the total count
                        betaTotal += rightmem.count(eqinx);
                        engine.writeMessage(" count=" + betaTotal + " - " + indx.toPPString() + ": ", Constants.DEFAULT_OUTPUT);
                        IEnumerator ritr = rightmem.iterator(eqinx);
                        if (ritr != null)
                        {
                            StringBuilder buf = new StringBuilder();
                            while (ritr.MoveNext())
                            {
                                buf.Append(((IFact)ritr.Current).FactId + ",");
                            }
                            engine.writeMessage(buf.ToString(), Constants.DEFAULT_OUTPUT);
                        }
                        engine.writeMessage(Constants.LINEBREAK, Constants.DEFAULT_OUTPUT);
                    }
                }
            }
            else if (bjoin is NotJoin)
            {
                NotJoin nj = (NotJoin)bjoin;
                IGenericMap <Object, Object> bm = (IGenericMap <Object, Object>)getBetaLeftMemory(bjoin);
                IEnumerator bitr = bm.Keys.GetEnumerator();
                while (bitr.MoveNext())
                {
                    Index       indx = (Index)bitr.Current;
                    IBetaMemory bmem = (IBetaMemory)bm.Get(indx);
                    engine.writeMessage(bmem.toPPString());
                }
            }
            else if (bjoin is TemporalEqNode)
            {
                TemporalEqNode ten = (TemporalEqNode)bjoin;
            }
            else
            {
                IGenericMap <Object, Object> bm = (IGenericMap <Object, Object>)betaLeftMemories.Get(bjoin);
                // we iterate over the keys in the HashMap
                IEnumerator bitr = bm.Keys.GetEnumerator();
                while (bitr.MoveNext())
                {
                    Index  indx     = (Index)bm.Get(bitr.Current);
                    Object rightmem = betaRightMemories.Get(bjoin);
                    if (detailed)
                    {
                        if (rightmem is HashedAlphaMemoryImpl)
                        {
                            HashedAlphaMemoryImpl hami = (HashedAlphaMemoryImpl)rightmem;
                            engine.writeMessage(bjoin.toPPString() + " count=" + hami.size() + " - " + indx.toPPString() + Constants.LINEBREAK);
                        }
                        else
                        {
                            IGenericMap <Object, Object> rmap = (IGenericMap <Object, Object>)rightmem;
                            engine.writeMessage(bjoin.toPPString() + " count=" + rmap.Count + " - " + indx.toPPString() + Constants.LINEBREAK);
                        }
                    }
                    if (rightmem is HashedAlphaMemoryImpl)
                    {
                        betaTotal += ((HashedAlphaMemoryImpl)rightmem).size();
                    }
                    else
                    {
                        betaTotal += ((IGenericMap <IFact, IFact>)rightmem).Count;
                    }
                }
            }
        }
Ejemplo n.º 21
0
 /// <summary> Retract from the right works in the following order.
 /// 1. Remove the fact from the right memory
 /// 2. check which left memory matched
 /// 3. propogate the retract
 /// </summary>
 /// <param name="">factInstance
 /// </param>
 /// <param name="">engine
 /// 
 /// </param>
 public override void retractRight(IFact rfact, Rete engine, IWorkingMemory mem)
 {
     HashedAlphaMemoryImpl rightmem = (HashedAlphaMemoryImpl) mem.getBetaRightMemory(this);
     EqHashIndex inx = new EqHashIndex(NodeUtils.getRightValues(binds, rfact));
     // Remove the fact from the right
     int after = rightmem.removePartialMatch(inx, rfact);
     if (after == 0)
     {
         // now we see the left memory matched and Remove it also
         IGenericMap<Object, Object> leftmem = (IGenericMap<Object, Object>) mem.getBetaLeftMemory(this);
         IEnumerator itr = leftmem.Values.GetEnumerator();
         while (itr.MoveNext())
         {
             Index linx = (Index) itr.Current;
             if (evaluate(linx.Facts, rfact))
             {
                 try
                 {
                     propogateAssert(linx, engine, mem);
                 }
                 catch (AssertException e)
                 {
                     throw new RetractException("NotJion - " + e.Message);
                 }
             }
         }
         inx = null;
     }
 }
Ejemplo n.º 22
0
 /// <summary> assertLeft takes an array of facts. Since the Current join may be
 /// joining against one or more objects, we need to pass all
 /// previously matched facts.
 /// </summary>
 /// <param name="">factInstance
 /// </param>
 /// <param name="">engine
 /// 
 /// </param>
 public override void assertLeft(Index linx, Rete engine, IWorkingMemory mem)
 {
     IGenericMap<Object, Object> leftmem = (IGenericMap<Object, Object>) mem.getBetaLeftMemory(this);
     leftmem.Put(linx, linx);
     EqHashIndex inx = new EqHashIndex(NodeUtils.getLeftValues(binds, linx.Facts));
     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)
     {
         propogateAssert(linx, engine, mem);
     }
 }
Ejemplo n.º 23
0
 public override void retractRight(IFact rfact, Rete engine, IWorkingMemory mem)
 {
     long time = LeftTime;
     EqHashIndex inx = new EqHashIndex(NodeUtils.getRightValues(binds, rfact));
     TemporalHashedAlphaMem rightmem = (TemporalHashedAlphaMem) mem.getBetaRightMemory(this);
     // first we Remove the fact from the right
     rightmem.removePartialMatch(inx, rfact);
     // now we see the left memory matched and Remove it also
     IGenericMap<Object, Object> leftmem = (IGenericMap<Object, Object>) mem.getBetaLeftMemory(this);
     IEnumerator itr = leftmem.Values.GetEnumerator();
     while (itr.MoveNext())
     {
         Index linx = (Index) itr.Current;
         if (evaluate(linx.Facts, rfact, time))
         {
             propogateRetract(linx.add(rfact), engine, mem);
         }
     }
 }
Ejemplo n.º 24
0
 /// <summary> Assert from the right side is always going to be from an Alpha node.
 /// 
 /// </summary>
 /// <param name="">factInstance
 /// </param>
 /// <param name="">engine
 /// 
 /// </param>
 public override void assertRight(IFact rfact, Rete engine, IWorkingMemory mem)
 {
     // Get the memory for the node
     HashedAlphaMemoryImpl rightmem = (HashedAlphaMemoryImpl) mem.getBetaRightMemory(this);
     EqHashIndex inx = new EqHashIndex(NodeUtils.getRightValues(binds, rfact));
     rightmem.addPartialMatch(inx, rfact);
     int after = rightmem.count(inx);
     IGenericMap<Object, Object> leftmem = (IGenericMap<Object, Object>) mem.getBetaLeftMemory(this);
     IEnumerator itr = leftmem.Values.GetEnumerator();
     while (itr.MoveNext())
     {
         Index linx = (Index) itr.Current;
         if (evaluate(linx.Facts, rfact))
         {
             if (after == 1)
             {
                 // we have to retract
                 try
                 {
                     propogateRetract(linx, engine, mem);
                 }
                 catch (RetractException e)
                 {
                     throw new AssertException("NotJion - " + e.Message);
                 }
             }
         }
     }
 }
Ejemplo n.º 25
0
        protected internal virtual void printBetaNodes(BaseJoin bjoin, bool detailed, int betaTotal)
        {
            if (bjoin is HashedEqBNode || bjoin is HashedEqNJoin)
            {
                IGenericMap<Object, Object> bm = (IGenericMap<Object, Object>) betaLeftMemories.Get(bjoin);
                // we iterate over the keys in the HashMap
                IEnumerator bitr = bm.Keys.GetEnumerator();
                while (bitr.MoveNext())
                {
                    Index indx = (Index) bm.Get(bitr.Current);
                    if (detailed)
                    {
                        engine.writeMessage(bjoin.toPPString(), Constants.DEFAULT_OUTPUT);
                        HashedAlphaMemoryImpl rightmem = (HashedAlphaMemoryImpl) getBetaRightMemory(bjoin);

                        EqHashIndex eqinx = new EqHashIndex(NodeUtils.getLeftValues(bjoin.binds, indx.Facts));
                        // Add to the total count
                        betaTotal += rightmem.count(eqinx);
                        engine.writeMessage(" count=" + betaTotal + " - " + indx.toPPString() + ": ", Constants.DEFAULT_OUTPUT);
                        IEnumerator ritr = rightmem.iterator(eqinx);
                        if (ritr != null)
                        {
                            StringBuilder buf = new StringBuilder();
                            while (ritr.MoveNext())
                            {
                                buf.Append(((IFact) ritr.Current).FactId + ",");
                            }
                            engine.writeMessage(buf.ToString(), Constants.DEFAULT_OUTPUT);
                        }
                        engine.writeMessage(Constants.LINEBREAK, Constants.DEFAULT_OUTPUT);
                    }
                }
            }
            else if (bjoin is HashedNotEqNJoin || bjoin is HashedNotEqBNode)
            {
                IGenericMap<Object, Object> bm = (IGenericMap<Object, Object>) betaLeftMemories.Get(bjoin);
                // we iterate over the keys in the HashMap
                IEnumerator bitr = bm.Keys.GetEnumerator();
                while (bitr.MoveNext())
                {
                    Index indx = (Index) bm.Get(bitr.Current);
                    if (detailed)
                    {
                        engine.writeMessage(bjoin.toPPString(), Constants.DEFAULT_OUTPUT);
                        HashedNeqAlphaMemory rightmem = (HashedNeqAlphaMemory) getBetaRightMemory(bjoin);

                        EqHashIndex eqinx = new EqHashIndex(NodeUtils.getLeftValues(bjoin.binds, indx.Facts));
                        // Add to the total count
                        betaTotal += rightmem.count(eqinx);
                        engine.writeMessage(" count=" + betaTotal + " - " + indx.toPPString() + ": ", Constants.DEFAULT_OUTPUT);
                        IEnumerator ritr = rightmem.iterator(eqinx);
                        if (ritr != null)
                        {
                            StringBuilder buf = new StringBuilder();
                            while (ritr.MoveNext())
                            {
                                buf.Append(((IFact) ritr.Current).FactId + ",");
                            }
                            engine.writeMessage(buf.ToString(), Constants.DEFAULT_OUTPUT);
                        }
                        engine.writeMessage(Constants.LINEBREAK, Constants.DEFAULT_OUTPUT);
                    }
                }
            }
            else if (bjoin is ExistJoin)
            {
                ExistJoin henj = (ExistJoin) bjoin;
                IGenericMap<Object, Object> bm = (IGenericMap<Object, Object>) betaLeftMemories.Get(henj);
                // we iterate over the keys in the HashMap
                IEnumerator bitr = bm.Keys.GetEnumerator();
                while (bitr.MoveNext())
                {
                    Index indx = (Index) bm.Get(bitr.Current);
                    if (detailed)
                    {
                        engine.writeMessage(bjoin.toPPString(), Constants.DEFAULT_OUTPUT);
                        HashedAlphaMemoryImpl rightmem = (HashedAlphaMemoryImpl) getBetaRightMemory(henj);

                        EqHashIndex eqinx = new EqHashIndex(NodeUtils.getLeftValues(henj.binds, indx.Facts));
                        // Add to the total count
                        betaTotal += rightmem.count(eqinx);
                        engine.writeMessage(" count=" + betaTotal + " - " + indx.toPPString() + ": ", Constants.DEFAULT_OUTPUT);
                        IEnumerator ritr = rightmem.iterator(eqinx);
                        if (ritr != null)
                        {
                            StringBuilder buf = new StringBuilder();
                            while (ritr.MoveNext())
                            {
                                buf.Append(((IFact) ritr.Current).FactId + ",");
                            }
                            engine.writeMessage(buf.ToString(), Constants.DEFAULT_OUTPUT);
                        }
                        engine.writeMessage(Constants.LINEBREAK, Constants.DEFAULT_OUTPUT);
                    }
                }
            }
            else if (bjoin is NotJoin)
            {
                NotJoin nj = (NotJoin) bjoin;
                IGenericMap<Object, Object> bm = (IGenericMap<Object, Object>) getBetaLeftMemory(bjoin);
                IEnumerator bitr = bm.Keys.GetEnumerator();
                while (bitr.MoveNext())
                {
                    Index indx = (Index) bitr.Current;
                    IBetaMemory bmem = (IBetaMemory) bm.Get(indx);
                    engine.writeMessage(bmem.toPPString());
                }
            }
            else if (bjoin is TemporalEqNode)
            {
                TemporalEqNode ten = (TemporalEqNode) bjoin;
            }
            else
            {
                IGenericMap<Object, Object> bm = (IGenericMap<Object, Object>) betaLeftMemories.Get(bjoin);
                // we iterate over the keys in the HashMap
                IEnumerator bitr = bm.Keys.GetEnumerator();
                while (bitr.MoveNext())
                {
                    Index indx = (Index) bm.Get(bitr.Current);
                    Object rightmem = betaRightMemories.Get(bjoin);
                    if (detailed)
                    {
                        if (rightmem is HashedAlphaMemoryImpl)
                        {
                            HashedAlphaMemoryImpl hami = (HashedAlphaMemoryImpl) rightmem;
                            engine.writeMessage(bjoin.toPPString() + " count=" + hami.size() + " - " + indx.toPPString() + Constants.LINEBREAK);
                        }
                        else
                        {
                            IGenericMap<Object, Object> rmap = (IGenericMap<Object, Object>) rightmem;
                            engine.writeMessage(bjoin.toPPString() + " count=" + rmap.Count + " - " + indx.toPPString() + Constants.LINEBREAK);
                        }
                    }
                    if (rightmem is HashedAlphaMemoryImpl)
                    {
                        betaTotal += ((HashedAlphaMemoryImpl) rightmem).size();
                    }
                    else
                    {
                        betaTotal += ((IGenericMap<IFact, IFact>)rightmem).Count;
                    }
                }
            }
        }