Exemple #1
0
        public void testTwoBinding()
        {
            Defclass    dc    = new Defclass(typeof(TestBean2));
            Deftemplate dtemp = dc.createDeftemplate("testBean2");

            Slot[] slts = dtemp.AllSlots;

            Binding bn = new Binding();

            bn.LeftRow    = (0);
            bn.LeftIndex  = (0);
            bn.RightIndex = (0);

            Binding bn2 = new Binding();

            bn2.LeftRow    = (0);
            bn2.LeftIndex  = (2);
            bn2.RightIndex = (2);

            Binding[]     binds  = { bn, bn2 };
            HashedEqBNode btnode = new HashedEqBNode(1);

            btnode.Bindings = (binds);

            Console.WriteLine("betaNode::" + btnode.toPPString());
            Assert.IsNotNull(btnode.toPPString());
        }
        /// <summary> method compiles ObjectConditions, which include NOTCE
        /// </summary>
        public override BaseJoin compileJoin(ICondition condition, int position, Rule.IRule rule)
        {
            Binding[]       binds    = getBindings(condition, rule, position);
            ObjectCondition oc       = (ObjectCondition)condition;
            BaseJoin        joinNode = null;

            //deal with the CE which is not NOT CE.
            if (!oc.Negated)
            {
                if (binds.Length > 0 && oc.HasPredicateJoin)
                {
                    joinNode = new PredicateBNode(ruleCompiler.Engine.nextNodeId());
                }
                else if (binds.Length > 0 && oc.HasNotEqual)
                {
                    joinNode = new HashedNotEqBNode(ruleCompiler.Engine.nextNodeId());
                }
                else if (binds.Length > 0)
                {
                    joinNode = new HashedEqBNode(ruleCompiler.Engine.nextNodeId());
                }
                else if (binds.Length == 0)
                {
                    joinNode = new ZJBetaNode(ruleCompiler.Engine.nextNodeId());
                }
            }

            //deal with the CE which is NOT CE.
            if (oc.Negated)
            {
                if (binds.Length > 0 && oc.HasPredicateJoin)
                {
                    joinNode = new NotJoin(ruleCompiler.Engine.nextNodeId());
                }
                else if (oc.HasNotEqual)
                {
                    joinNode = new HashedNotEqNJoin(ruleCompiler.Engine.nextNodeId());
                }
                else
                {
                    joinNode = new HashedEqNJoin(ruleCompiler.Engine.nextNodeId());
                }
            }

            if (joinNode != null)
            {
                joinNode.Bindings = binds;
            }
            return(joinNode);
        }
Exemple #3
0
        public void testSingleBinding()
        {
            Defclass dc = new Defclass(typeof (TestBean2));
            Deftemplate dtemp = dc.createDeftemplate("testBean2");

            Slot[] slts = dtemp.AllSlots;

            Binding bn = new Binding();
            bn.LeftRow = (0);
            bn.LeftIndex = (0);
            bn.RightIndex = (0);

            Binding[] binds = {bn};
            HashedEqBNode btnode = new HashedEqBNode(1);
            btnode.Bindings = (binds);

            Console.WriteLine("betaNode::" + btnode.toPPString());
            Assert.IsNotNull(btnode.toPPString());
        }
Exemple #4
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
        }
Exemple #5
0
        public void testPropogateChange()
        {
            Console.WriteLine("testPropogateChange");
            // 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 = 2;
            ArrayList data = new ArrayList();
            for (int idx = 0; idx < count; idx++)
            {
                TestBean2 bean = new TestBean2();
                bean.Attr1 = ("random");
                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);
                }
                catch (AssertException e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            IGenericMap<Object, Object> lbmem = (IGenericMap<Object, Object>) engine.WorkingMemory.getBetaLeftMemory(nj);
            Assert.AreEqual(count, lbmem.Count);

            IGenericMap<Object, Object> lbmem2 = (IGenericMap<Object, Object>) engine.WorkingMemory.getBetaLeftMemory(bn2);
            Assert.AreEqual(2, lbmem2.Count);

            itr = data.GetEnumerator();
            while (itr.MoveNext())
            {
                try
                {
                    IFact f1 = (IFact) itr.Current;
                    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);

            // once the facts are asserted to the right, there should be no
            // facts in successor. this makes sure that assertRight correctly
            // results in a retract.
            lbmem2 = (IGenericMap<Object, Object>) engine.WorkingMemory.getBetaLeftMemory(bn2);
            Assert.AreEqual(0, lbmem2.Count);
            engine.close();
        }
Exemple #6
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
        }
Exemple #7
0
        public void testPropogateChange()
        {
            Console.WriteLine("testPropogateChange");
            // 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 = 2;
            ArrayList data  = new ArrayList();

            for (int idx = 0; idx < count; idx++)
            {
                TestBean2 bean = new TestBean2();
                bean.Attr1 = ("random");
                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);
                }
                catch (AssertException e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            IGenericMap <Object, Object> lbmem = (IGenericMap <Object, Object>)engine.WorkingMemory.getBetaLeftMemory(nj);

            Assert.AreEqual(count, lbmem.Count);

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

            Assert.AreEqual(2, lbmem2.Count);

            itr = data.GetEnumerator();
            while (itr.MoveNext())
            {
                try
                {
                    IFact f1 = (IFact)itr.Current;
                    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);

            // once the facts are asserted to the right, there should be no
            // facts in successor. this makes sure that assertRight correctly
            // results in a retract.
            lbmem2 = (IGenericMap <Object, Object>)engine.WorkingMemory.getBetaLeftMemory(bn2);
            Assert.AreEqual(0, lbmem2.Count);
            engine.close();
        }
Exemple #8
0
        /// <summary> method compiles ObjectConditions, which include NOTCE
        /// </summary>
        public override BaseJoin compileJoin(ICondition condition, int position, Rule.IRule rule)
        {
            Binding[] binds = getBindings(condition, rule, position);
            ObjectCondition oc = (ObjectCondition) condition;
            BaseJoin joinNode = null;
            //deal with the CE which is not NOT CE.
            if (!oc.Negated)
            {
                if (binds.Length > 0 && oc.HasPredicateJoin)
                {
                    joinNode = new PredicateBNode(ruleCompiler.Engine.nextNodeId());
                }
                else if (binds.Length > 0 && oc.HasNotEqual)
                {
                    joinNode = new HashedNotEqBNode(ruleCompiler.Engine.nextNodeId());
                }
                else if (binds.Length > 0)
                {
                    joinNode = new HashedEqBNode(ruleCompiler.Engine.nextNodeId());
                }
                else if (binds.Length == 0)
                {
                    joinNode = new ZJBetaNode(ruleCompiler.Engine.nextNodeId());
                }
            }

            //deal with the CE which is NOT CE.
            if (oc.Negated)
            {
                if (binds.Length > 0 && oc.HasPredicateJoin)
                {
                    joinNode = new NotJoin(ruleCompiler.Engine.nextNodeId());
                }
                else if (oc.HasNotEqual)
                {
                    joinNode = new HashedNotEqNJoin(ruleCompiler.Engine.nextNodeId());
                }
                else
                {
                    joinNode = new HashedEqNJoin(ruleCompiler.Engine.nextNodeId());
                }
            }

            if (joinNode != null)
                joinNode.Bindings = binds;
            return joinNode;
        }