Beispiel #1
0
        public void PropagateRetract(IExecutionContext context, TupleFactList tupleFactList)
        {
            if (tupleFactList.Count == 0)
            {
                return;
            }

            IBetaMemory memory    = context.WorkingMemory.GetNodeMemory(this);
            var         toRetract = new List <Tuple>();

            using (var counter = PerfCounter.Retract(context, this))
            {
                var enumerator = tupleFactList.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    Tuple childTuple = memory.FindTuple(enumerator.CurrentTuple, enumerator.CurrentFact);
                    if (childTuple != null)
                    {
                        toRetract.Add(childTuple);
                    }
                }

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

            PropagateRetractInternal(context, memory, toRetract);
        }
Beispiel #2
0
        internal static NodeInfo Create(BetaMemoryNode node, IBetaMemory memory)
        {
            var tuples = memory.Tuples.Select(
                t => string.Join(" || ", t.OrderedFacts().Select(f => f.Value).ToArray()));

            return(new NodeInfo(NodeType.BetaMemory, string.Empty, Empty, Empty, tuples));
        }
Beispiel #3
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);
                 }
             }
         }
     }
 }
Beispiel #4
0
        public void PropagateUpdate(IExecutionContext context, ITupleFactList tupleFactList)
        {
            if (tupleFactList.Count == 0)
            {
                return;
            }
            IBetaMemory memory     = context.WorkingMemory.GetNodeMemory(this);
            var         toAssert   = new List <Tuple>();
            var         toUpdate   = new List <Tuple>();
            var         enumerator = tupleFactList.GetEnumerator();

            while (enumerator.MoveNext())
            {
                Tuple childTuple = memory.FindTuple(enumerator.CurrentTuple, enumerator.CurrentFact);
                if (childTuple == null)
                {
                    childTuple         = new Tuple(enumerator.CurrentTuple, enumerator.CurrentFact);
                    childTuple.GroupId = enumerator.CurrentTuple.Id;
                    toAssert.Add(childTuple);
                }
                else
                {
                    toUpdate.Add(childTuple);
                }
            }

            PropagateAssertInternal(context, memory, toAssert);
            PropagateUpdateInternal(context, toUpdate);
        }
Beispiel #5
0
        public void PropagateAssert(IExecutionContext context, TupleFactList tupleFactList)
        {
            if (tupleFactList.Count == 0)
            {
                return;
            }

            IBetaMemory memory   = context.WorkingMemory.GetNodeMemory(this);
            var         toAssert = new List <Tuple>();

            using (var counter = PerfCounter.Assert(context, this))
            {
                var enumerator = tupleFactList.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    var childTuple = new Tuple(context.IdGenerator.NextTupleId(), enumerator.CurrentTuple,
                                               enumerator.CurrentFact);
                    childTuple.GroupId = enumerator.CurrentTuple.Id;
                    toAssert.Add(childTuple);
                }

                counter.AddItems(tupleFactList.Count);
            }

            PropagateAssertInternal(context, memory, toAssert);
        }
Beispiel #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);
                 }
             }
         }
     }
 }
Beispiel #7
0
        public void Activate(IExecutionContext context)
        {
            var tuple = new Tuple(this);

            IBetaMemory memory = context.WorkingMemory.GetNodeMemory(this);

            memory.Tuples.Add(tuple);

            _sink.PropagateAssert(context, tuple);
        }
Beispiel #8
0
        public void PropagateRetract(IExecutionContext context, Tuple tuple)
        {
            IBetaMemory memory = context.WorkingMemory.GetNodeMemory(this);

            memory.Tuples.Remove(tuple);
            foreach (var sink in _sinks)
            {
                sink.PropagateRetract(context, tuple);
            }
        }
Beispiel #9
0
        public void PropagateAssert(IExecutionContext context, Tuple tuple, Fact fact)
        {
            IBetaMemory memory     = context.WorkingMemory.GetNodeMemory(this);
            var         childTuple = new Tuple(tuple, fact);

            foreach (var sink in _sinks)
            {
                sink.PropagateAssert(context, childTuple);
            }
            memory.Add(childTuple);
        }
Beispiel #10
0
        public void Activate(IExecutionContext context)
        {
            var tuple = new Tuple();

            IBetaMemory memory = context.WorkingMemory.GetNodeMemory(this);

            foreach (ITupleSink sink in _sinks)
            {
                sink.PropagateAssert(context, tuple);
            }
            memory.Add(tuple);
        }
Beispiel #11
0
        /// <summary> Retracting from the left is different than retractRight for couple
        /// of reasons.
        /// <ul>
        /// <li> NotJoin will only propogate the facts from the left</li>
        /// <li> NotJoin never needs to merge the left and right</li>
        /// </ul>
        /// </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);
            // the left memory Contains the fact array, so we
            // retract it.
            IBetaMemory bmem = (IBetaMemory)leftmem.RemoveWithReturn(linx);

            if (bmem != null)
            {
                // if watch is turned on, we send an event
                propogateRetract(linx, engine, mem);
            }
        }
Beispiel #12
0
 private void PropagateAssertInternal(IExecutionContext context, IBetaMemory memory, List <Tuple> tuples)
 {
     if (tuples.Count > 0)
     {
         foreach (var sink in _sinks)
         {
             sink.PropagateAssert(context, tuples);
         }
         foreach (var childTuple in tuples)
         {
             memory.Add(childTuple);
         }
     }
 }
Beispiel #13
0
        public void PropagateRetract(IExecutionContext context, Tuple tuple, Fact fact)
        {
            IBetaMemory memory     = context.WorkingMemory.GetNodeMemory(this);
            Tuple       childTuple = memory.FindTuple(tuple, fact);

            if (childTuple != null)
            {
                foreach (var sink in _sinks)
                {
                    sink.PropagateRetract(context, childTuple);
                }
                memory.Remove(childTuple);
            }
        }
Beispiel #14
0
        public void Activate(IExecutionContext context)
        {
            var tuple     = new Tuple(context.IdGenerator.NextTupleId());
            var tupleList = new List <Tuple>();

            tupleList.Add(tuple);

            IBetaMemory memory = context.WorkingMemory.GetNodeMemory(this);

            foreach (ITupleSink sink in _sinks)
            {
                sink.PropagateAssert(context, tupleList);
            }
            memory.Add(tuple);
        }
Beispiel #15
0
        private void PropagateAssertInternal(IExecutionContext context, IBetaMemory memory, List <Tuple> tuples)
        {
            if (tuples.Count > 0)
            {
                for (int i = 0; i < _sinks.Count; i++)
                {
                    _sinks[i].PropagateAssert(context, tuples);
                }

                using (var counter = PerfCounter.Assert(context, this))
                {
                    memory.Add(tuples);
                    counter.SetCount(memory.TupleCount);
                }
            }
        }
Beispiel #16
0
        private void PropagateRetractInternal(IExecutionContext context, IBetaMemory memory, List <Tuple> tuples)
        {
            if (tuples.Count > 0)
            {
                using (var counter = PerfCounter.Retract(context, this))
                {
                    memory.Remove(tuples);
                    counter.SetCount(memory.TupleCount);
                }

                for (int i = _sinks.Count - 1; i >= 0; i--)
                {
                    _sinks[i].PropagateRetract(context, tuples);
                }
            }
        }
Beispiel #17
0
        public void PropagateUpdate(IExecutionContext context, Tuple tuple, Fact fact)
        {
            IBetaMemory memory     = context.WorkingMemory.GetNodeMemory(this);
            Tuple       childTuple = memory.FindTuple(tuple, fact);

            if (childTuple == null)
            {
                PropagateAssert(context, tuple, fact);
            }
            else
            {
                foreach (var sink in _sinks)
                {
                    sink.PropagateUpdate(context, childTuple);
                }
            }
        }
Beispiel #18
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)
        {
            IGenericMap <Object, Object> rightmem = (IGenericMap <Object, Object>)mem.getBetaRightMemory(this);

            rightmem.Remove(rfact);
            IGenericMap <Object, Object> leftmem = (IGenericMap <Object, Object>)mem.getBetaLeftMemory(this);
            IEnumerator itr = leftmem.Values.GetEnumerator();

            while (itr.MoveNext())
            {
                IBetaMemory bmem = (IBetaMemory)itr.Current;
                if (evaluate(bmem.LeftFacts, rfact, engine))
                {
                    bmem.removeMatch(rfact);
                    propogateRetract(bmem.Index, engine, mem);
                }
            }
        }
        /// <summary> Clear will Clear the lists
        /// </summary>
        public override void clear(IWorkingMemory mem)
        {
            IGenericMap <Object, Object> leftmem  = (IGenericMap <Object, Object>)mem.getBetaLeftMemory(this);
            HashedNeqAlphaMemory         rightmem = (HashedNeqAlphaMemory)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();
            rightmem.clear();
        }
Beispiel #20
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)
        {
            IGenericMap <Object, Object> rightmem = (IGenericMap <Object, Object>)mem.getBetaRightMemory(this);

            rightmem.Put(rfact, rfact);
            IGenericMap <Object, Object> leftmem = (IGenericMap <Object, Object>)mem.getBetaLeftMemory(this);
            IEnumerator itr = leftmem.Values.GetEnumerator();

            while (itr.MoveNext())
            {
                IBetaMemory bmem = (IBetaMemory)itr.Current;
                if (evaluate(bmem.LeftFacts, rfact, engine))
                {
                    // now we propogate
                    bmem.addMatch(rfact);
                    propogateAssert(bmem.Index.add(rfact), engine, mem);
                }
            }
        }
Beispiel #21
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)
        {
            // we only proceed if the fact hasn't already entered
            // the join node
            Index linx = new Index(new IFact[0]);
            IGenericMap <Object, Object> rightmem = (IGenericMap <Object, Object>)mem.getBetaRightMemory(this);

            rightmem.Put(rfact, 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);
            IEnumerator itr = leftmem.Values.GetEnumerator();

            while (itr.MoveNext())
            {
                IBetaMemory bmem      = (IBetaMemory)itr.Current;
                int         prevCount = bmem.matchCount();
                if (evaluate(linx.Facts, rfact, engine))
                {
                    bmem.addMatch(rfact);
                }
                // When facts are asserted from the right, it can only
                // increase the match count, so basically it will never
                // need to propogate to successor nodes.
                if (prevCount == 0 && bmem.matchCount() != 0)
                {
                    linx = new Index(new IFact[0]);
                    // we have to retract
                    try
                    {
                        propogateRetract(linx, engine, mem);
                    }
                    catch (RetractException e)
                    {
                        throw new AssertException("NotJion - " + e.Message);
                    }
                }
            }
        }
Beispiel #22
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);
                 }
             }
         }
     }
 }
Beispiel #23
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)
        {
            Index linx = new Index(new IFact[0]);
            IGenericMap <Object, Object> rightmem = (IGenericMap <Object, Object>)mem.getBetaRightMemory(this);

            if (rightmem.RemoveWithReturn(rfact) != null)
            {
                // 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())
                {
                    IBetaMemory bmem      = (IBetaMemory)itr.Current;
                    int         prevCount = bmem.matchCount();
                    if (bmem.matched(rfact))
                    {
                        // we Remove the fact from the memory
                        bmem.removeMatch(rfact);
                        // since 1 or more matches prevents propogation
                        // we don't need to propogate retract. if the
                        // match count is now zero, we need to propogate
                        // assert
                        if (prevCount != 0 && bmem.matchCount() == 0)
                        {
                            try
                            {
                                propogateAssert(bmem.Index, engine, mem);
                            }
                            catch (AssertException e)
                            {
                                throw new RetractException("NotJion - " + e.Message);
                            }
                        }
                    }
                }
            }
        }
Beispiel #24
0
 /// <summary>
 /// When new Successor nodes are added, we propogate the facts that matched to
 /// the new join node.
 /// </summary>
 /// <param name="node">The node.</param>
 /// <param name="engine">The engine.</param>
 /// <param name="mem">The mem.</param>
 public virtual void addSuccessorNode(BaseJoin 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.GetEnumerator();
         while (itr.MoveNext())
         {
             IBetaMemory bmem = (IBetaMemory)itr.Current;
             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;
                 // now assert in the new join node
                 node.assertLeft(left.add(rfcts), engine, mem);
             }
         }
     }
 }
Beispiel #25
0
 internal static NodeInfo Create(BetaMemoryNode node, IBetaMemory memory)
 {
     var tuples = memory.Tuples.Select(
         t => string.Join(" || ", t.Facts.Reverse().Select(f => f.Object).ToArray()));
     return new NodeInfo(NodeType.BetaMemory, string.Empty, Empty, Empty, tuples);
 }
Beispiel #26
0
        public IEnumerable <Tuple> GetTuples(IExecutionContext context)
        {
            IBetaMemory memory = context.WorkingMemory.GetNodeMemory(this);

            return(memory.Tuples);
        }
Beispiel #27
0
        public void testMatch()
        {
            // first create a rule engine instance
            Rete    engine = new Rete();
            NotJoin bn     = new NotJoin(engine.nextNodeId());

            Assert.IsNotNull(bn);

            // create a defclass
            Defclass dc = new Defclass(typeof(TestBean2));
            // create deftemplate
            Deftemplate dtemp = dc.createDeftemplate("testBean2");

            Assert.IsNotNull(dtemp);
            Binding[] binds = new Binding[1];
            Binding   b1    = new Binding();

            b1.LeftIndex   = (0);
            b1.IsObjectVar = (false);
            b1.LeftRow     = (0);
            b1.RightIndex  = (0);
            b1.VarName     = ("var1");
            binds[0]       = b1;

            // set the binding
            bn.Bindings = (binds);

            int       count = 10;
            ArrayList data  = new ArrayList();

            for (int idx = 0; idx < count; idx++)
            {
                TestBean2 bean = new TestBean2();
                bean.Attr1 = ("random");
                bean.Attr2 = (101);
                short s = 10001;
                bean.Attr3 = (s);
                long l = 10101018;
                bean.Attr4 = (l);
                bean.Attr5 = (1010101);
                bean.Attr6 = (1001.1001);
                IFact fact = dtemp.createFact(bean, dc, engine.nextFactId());
                data.Add(fact);
            }

            IEnumerator itr = data.GetEnumerator();

            while (itr.MoveNext())
            {
                try
                {
                    IFact f1 = (IFact)itr.Current;
                    bn.assertLeft(new Index(new IFact[] { f1 }), engine, engine.WorkingMemory);
                    bn.assertRight(f1, engine, engine.WorkingMemory);
                }
                catch (AssertException e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            IGenericMap <IFact, IFact> rbmem = (IGenericMap <IFact, IFact>)engine.WorkingMemory.getBetaRightMemory(bn);

            Assert.AreEqual(count, rbmem.Count);

            IGenericMap <Object, Object> lbmem = (IGenericMap <Object, Object>)engine.WorkingMemory.getBetaLeftMemory(bn);

            Assert.AreEqual(count, lbmem.Count);

            // now check the BetaMemory has matches
            Console.WriteLine(bn.toPPString());
            IEnumerator mitr = lbmem.Values.GetEnumerator();

            while (mitr.MoveNext())
            {
                IBetaMemory btm = (IBetaMemory)mitr.Current;
                Assert.AreEqual(9, btm.matchCount());
                Console.WriteLine("match count=" + btm.matchCount() +
                                  " - " + btm.toPPString());
            }
            engine.close();
        }
Beispiel #28
0
        public void testPropogateNoMatch()
        {
            Console.WriteLine("testPropogateNoMatch");
            // first create a rule engine instance
            Rete          engine = new Rete();
            NotJoin       nj     = new NotJoin(engine.nextNodeId());
            HashedEqBNode bn2    = new HashedEqBNode(engine.nextNodeId());

            Assert.IsNotNull(nj);

            // create a defclass
            Defclass dc = new Defclass(typeof(TestBean2));
            // create deftemplate
            Deftemplate dtemp = dc.createDeftemplate("testBean2");

            Assert.IsNotNull(dtemp);
            Binding[] binds = new Binding[1];
            Binding   b1    = new Binding();

            b1.LeftIndex   = (0);
            b1.IsObjectVar = (false);
            b1.LeftRow     = (0);
            b1.RightIndex  = (0);
            b1.VarName     = ("var1");
            binds[0]       = b1;

            Binding[] binds2 = new Binding[1];
            Binding   b2     = new Binding();

            b2.LeftIndex   = (1);
            b2.IsObjectVar = (false);
            b2.LeftRow     = (0);
            b2.RightIndex  = (1);
            b2.VarName     = ("var2");
            binds2[0]      = b2;

            // set the binding
            nj.Bindings = (binds);

            bn2.Bindings = (binds2);

            // now add the second Not to the first
            try
            {
                nj.addSuccessorNode(bn2, engine, engine.WorkingMemory);
            }
            catch (AssertException e)
            {
                Console.WriteLine(e.Message);
            }

            int       count = 10;
            ArrayList data  = new ArrayList();

            for (int idx = 0; idx < count; idx++)
            {
                TestBean2 bean = new TestBean2();
                bean.Attr1 = ("random" + idx);
                bean.Attr2 = (101 + idx);
                short s = 10001;
                bean.Attr3 = (s);
                long l = 10101018 + idx;
                bean.Attr4 = (l);
                bean.Attr5 = (1010101);
                bean.Attr6 = (1001.1001);
                IFact fact = dtemp.createFact(bean, dc, engine.nextFactId());
                data.Add(fact);
            }

            IEnumerator itr = data.GetEnumerator();

            while (itr.MoveNext())
            {
                try
                {
                    IFact f1 = (IFact)itr.Current;
                    nj.assertLeft(new Index(new IFact[] { f1 }), engine, engine.WorkingMemory);
                    nj.assertRight(f1, engine, engine.WorkingMemory);
                }
                catch (AssertException e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            IGenericMap <IFact, IFact> rbmem = (IGenericMap <IFact, IFact>)engine.WorkingMemory.getBetaRightMemory(nj);

            Assert.AreEqual(count, rbmem.Count);

            IGenericMap <Object, Object> lbmem = (IGenericMap <Object, Object>)engine.WorkingMemory.getBetaLeftMemory(nj);

            Assert.AreEqual(count, lbmem.Count);

            // now check the BetaMemory has matches
            Console.WriteLine(nj.toPPString());
            IEnumerator mitr = lbmem.Values.GetEnumerator();

            while (mitr.MoveNext())
            {
                IBetaMemory btm = (IBetaMemory)mitr.Current;
                Assert.AreEqual(0, btm.matchCount());
                Console.WriteLine("match count=" + btm.matchCount() +
                                  " - " + btm.toPPString());
            }

            IGenericMap <Object, Object> lbmem2 = (IGenericMap <Object, Object>)engine.WorkingMemory.getBetaLeftMemory(bn2);

            Assert.AreEqual(count, lbmem2.Count);
            Console.WriteLine(bn2.toPPString());
            IEnumerator mitr2 = lbmem2.Values.GetEnumerator();

            engine.close();
            // TODO need to update the test to check the match count
            // by getting the right memory
        }
Beispiel #29
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;
                    }
                }
            }
        }