Exemplo n.º 1
0
        /// <summary> Clear will Clear the lists
        /// </summary>
        public override void clear(IWorkingMemory mem)
        {
            IGenericMap <Object, Object> leftmem  = (IGenericMap <Object, Object>)mem.getBetaLeftMemory(this);
            HashedAlphaMemoryImpl        rightmem = (HashedAlphaMemoryImpl)mem.getBetaRightMemory(this);
            IEnumerator itr = leftmem.Keys.GetEnumerator();

            // first we iterate over the list for each fact
            // and Clear it.
            while (itr.MoveNext())
            {
                IBetaMemory bmem = (IBetaMemory)leftmem.Get(itr.Current);
                bmem.clear();
            }
            // now that we've cleared the list for each fact, we
            // can Clear the Creshendo.rete.util.Map.
            leftmem.Clear();
            // TODO Clear the right memory
            rightmem.clear();
        }
Exemplo n.º 2
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);
                }
            }
        }
Exemplo n.º 3
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);
                    }
                }
            }
        }
Exemplo n.º 4
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);
                    }
                }
            }
        }
Exemplo n.º 5
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);
                }
            }
        }
Exemplo n.º 6
0
        /// <summary> the current implementation will try to find the memory for the node.
        /// If it doesn't find it, it checks the node type and creates the
        /// appropriate AlphaMemory for the node. Since right memories are
        /// hashed, it creates the appropriate type of Hashed memory.
        /// </summary>
        public virtual Object getBetaRightMemory(Object key)
        {
            Object val = betaRightMemories.Get(key);

            if (val != null)
            {
                return(val);
            }
            else
            {
                if (key is HashedEqBNode || key is HashedEqNJoin || key is ExistJoin)
                {
                    String mname = "hnode" + ((BaseNode)key).nodeID;
                    HashedAlphaMemoryImpl alpha = new HashedAlphaMemoryImpl(mname);
                    betaRightMemories.Put(key, alpha);
                    return(alpha);
                }
                else if (key is HashedNotEqBNode || key is HashedNotEqNJoin || key is ExistNeqJoin)
                {
                    String mname = "hneq" + ((BaseNode)key).nodeID;
                    HashedNeqAlphaMemory alpha = new HashedNeqAlphaMemory(mname);
                    betaRightMemories.Put(key, alpha);
                    return(alpha);
                }
                else if (key is TemporalEqNode)
                {
                    String mname = "hnode" + ((BaseNode)key).nodeID;
                    TemporalHashedAlphaMem alpha = new TemporalHashedAlphaMem(mname);
                    betaRightMemories.Put(key, alpha);
                    return(alpha);
                }
                else
                {
                    String mname = "brmem" + ((BaseNode)key).nodeID;
                    IGenericMap <IFact, IFact> right = CollectionFactory.newAlphaMemoryMap(mname);
                    betaRightMemories.Put(key, right);
                    return(right);
                }
            }
        }
Exemplo n.º 7
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;
                    }
                }
            }
        }
Exemplo n.º 8
0
 /// <summary> the current implementation will try to find the memory for the node.
 /// If it doesn't find it, it checks the node type and creates the
 /// appropriate AlphaMemory for the node. Since right memories are
 /// hashed, it creates the appropriate type of Hashed memory.
 /// </summary>
 public virtual Object getBetaRightMemory(Object key)
 {
     Object val = betaRightMemories.Get(key);
     if (val != null)
     {
         return val;
     }
     else
     {
         if (key is HashedEqBNode || key is HashedEqNJoin || key is ExistJoin)
         {
             String mname = "hnode" + ((BaseNode) key).nodeID;
             HashedAlphaMemoryImpl alpha = new HashedAlphaMemoryImpl(mname);
             betaRightMemories.Put(key, alpha);
             return alpha;
         }
         else if (key is HashedNotEqBNode || key is HashedNotEqNJoin || key is ExistNeqJoin)
         {
             String mname = "hneq" + ((BaseNode) key).nodeID;
             HashedNeqAlphaMemory alpha = new HashedNeqAlphaMemory(mname);
             betaRightMemories.Put(key, alpha);
             return alpha;
         }
         else if (key is TemporalEqNode)
         {
             String mname = "hnode" + ((BaseNode) key).nodeID;
             TemporalHashedAlphaMem alpha = new TemporalHashedAlphaMem(mname);
             betaRightMemories.Put(key, alpha);
             return alpha;
         }
         else
         {
             String mname = "brmem" + ((BaseNode) key).nodeID;
             IGenericMap<IFact, IFact> right = CollectionFactory.newAlphaMemoryMap(mname);
             betaRightMemories.Put(key, right);
             return right;
         }
     }
 }