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

            Defclass dc2 = new Defclass(typeof (TestBean3));
            Deftemplate dtemp2 = dc.createDeftemplate("testBean3");

            Slot[] slts = dtemp.AllSlots;
            Slot[] slts2 = dtemp2.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 bn3 = new Binding();
            bn3.LeftRow = (1);
            bn3.LeftIndex = (0);
            bn3.RightIndex = (0);

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

            Console.WriteLine("betaNode::" + btnode.toPPString());
            Assert.IsNotNull(btnode.toPPString());
        }
예제 #2
0
 /// <summary> convienance method for getting the values based on the
 /// bindings
 /// </summary>
 /// <param name="">ft
 /// </param>
 /// <returns>
 /// 
 /// </returns>
 public static BindValue[] getRightBindValues(Binding[] binds, IFact ft)
 {
     BindValue[] vals = new BindValue[binds.Length];
     for (int idx = 0; idx < binds.Length; idx++)
     {
         vals[idx] = new BindValue(ft.getSlotValue(binds[idx].RightIndex), binds[idx].negated());
     }
     return vals;
 }
예제 #3
0
 /// <summary> Get the values from the left side for nodes that do not have
 /// joins with !=
 /// </summary>
 /// <param name="">facts
 /// </param>
 /// <returns>
 /// 
 /// </returns>
 public static Object[] getLeftValues(Binding[] binds, IFact[] facts)
 {
     Object[] vals = new Object[binds.Length];
     for (int idx = 0; idx < binds.Length; idx++)
     {
         vals[idx] = facts[binds[idx].LeftRow].getSlotValue(binds[idx].LeftIndex);
     }
     return vals;
 }
예제 #4
0
 /// <summary> Get the values from the left side
 /// </summary>
 /// <param name="">facts
 /// </param>
 /// <returns>
 /// 
 /// </returns>
 public static BindValue[] getLeftBindValues(Binding[] binds, IFact[] facts)
 {
     BindValue[] vals = new BindValue[binds.Length];
     for (int idx = 0; idx < binds.Length; idx++)
     {
         vals[idx] = new BindValue(facts[binds[idx].LeftRow].getSlotValue(binds[idx].LeftIndex), binds[idx].negated());
     }
     return vals;
 }
예제 #5
0
 /// <summary> convienance method for clonging a binding at rule compilation
 /// time.
 /// </summary>
 public Object Clone()
 {
     Binding bind = new Binding();
     bind.VarName = VarName;
     bind.IsObjectVar = IsObjectVar;
     bind.LeftRow = LeftRow;
     bind.LeftIndex = LeftIndex;
     bind.RightIndex = RightIndex;
     return bind;
 }
예제 #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
        }
예제 #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();
        }
예제 #8
0
        public void testAssertAndRetract()
        {
            // 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);

            int retract = 5;
            try
            {
                for (int idx = 0; idx < retract; idx++)
                {
                    IFact f2 = (IFact) data[idx];
                    bn.retractRight(f2, engine, engine.WorkingMemory);
                }
            }
            catch (RetractException e)
            {
                Console.WriteLine(e.Message);
            }

            rbmem = (IGenericMap<IFact, IFact>)engine.WorkingMemory.getBetaRightMemory(bn);
            Assert.AreEqual(retract, rbmem.Count);

            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;
                Console.WriteLine("match count=" + btm.matchCount() +
                                  " - " + btm.toPPString());
            }
            engine.close();
        }
예제 #9
0
        public void testCreateNode2()
        {
            // 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);
            engine.close();
        }
예제 #10
0
        public void testAssertRightMultiple()
        {
            // 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" + (idx + 1));
                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.assertRight(f1, engine, engine.WorkingMemory);
                }
                catch (AssertException e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            IGenericMap<IFact, IFact> bmem = (IGenericMap<IFact, IFact>)engine.WorkingMemory.getBetaRightMemory(bn);
            Assert.AreEqual(count, bmem.Count);
            engine.close();
        }
예제 #11
0
        public void testAssertLeftOne()
        {
            // 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);

            TestBean2 bean = new TestBean2();
            bean.Attr1 = ("random1");
            bean.Attr2 = (101);
            short s = 10001;
            bean.Attr3 = (s);
            long l = 10101018;
            bean.Attr4 = (l);
            bean.Attr5 = (1010101);
            bean.Attr6 = (1001.1001);
            IFact f1 = dtemp.createFact(bean, dc, engine.nextFactId());
            try
            {
                bn.assertLeft(new Index(new IFact[] {f1}), engine, engine.WorkingMemory);
                IGenericMap<Object, Object> bmem = (IGenericMap<Object, Object>) engine.WorkingMemory.getBetaLeftMemory(bn);
                Assert.AreEqual(1, bmem.Count);
            }
            catch (AssertException e)
            {
                Console.WriteLine(e.Message);
            }
            engine.close();
        }
예제 #12
0
 /// <summary>
 /// Method will only Add the binding if it doesn't already
 /// exist.
 /// </summary>
 /// <param name="key"></param>
 /// <param name="bind"></param>
 public virtual void addBinding(String key, Binding bind)
 {
     if (!bindings.Contains(key))
     {
         bindings.Add(key, bind);
     }
 }
예제 #13
0
 /// <summary> convienance method for getting the values based on the bindings
 /// for nodes that do not have !=
 /// </summary>
 /// <param name="">ft
 /// </param>
 /// <returns>
 /// 
 /// </returns>
 public static Object[] getRightValues(Binding[] binds, IFact ft)
 {
     Object[] vals = new Object[binds.Length];
     for (int idx = 0; idx < binds.Length; idx++)
     {
         vals[idx] = ft.getSlotValue(binds[idx].RightIndex);
     }
     return vals;
 }
예제 #14
0
        /// <summary>
        /// Compiles the constraint.
        /// </summary>
        /// <param name="cnstr">The CNSTR.</param>
        /// <param name="templ">The templ.</param>
        /// <param name="rule">The rule.</param>
        /// <param name="position">The position.</param>
        /// <returns></returns>
        public virtual BaseAlpha2 compileConstraint(PredicateConstraint cnstr, ITemplate templ, Rule.IRule rule, int position)
        {
            BaseAlpha2 current = null;
            // for now we expect the user to write the predicate in this
            // way (> ?bind value), where the binding is first. this
            // needs to be updated so that we look at the order of the
            // parameters and set the node appropriately

            // we only create an AlphaNode if the predicate isn't
            // joining 2 bindings.
            if (!cnstr.PredicateJoin)
            {
                if (ConversionUtils.isPredicateOperatorCode(cnstr.FunctionName))
                {
                    int oprCode = ConversionUtils.getOperatorCode(cnstr.FunctionName);
                    Slot sl = (Slot) templ.getSlot(cnstr.Name).Clone();
                    Object sval = ConversionUtils.convert(sl.ValueType, cnstr.Value);
                    sl.Value = sval;
                    // create the alphaNode
                    if (rule.RememberMatch)
                    {
                        current = new AlphaNode(engine.nextNodeId());
                    }
                    else
                    {
                        current = new NoMemANode(engine.nextNodeId());
                    }
                    current.Slot = sl;
                    current.Operator = oprCode;
                    current.incrementUseCount();
                    // we increment the node use count when when create a new
                    // AlphaNode for the LiteralConstraint
                    templ.getSlot(sl.Id).incrementNodeCount();
                }
                else
                {
                    // the function isn't a built in predicate function that
                    // returns boolean true/false. We look up the function
                    IFunction f = engine.findFunction(cnstr.FunctionName);
                    if (f != null)
                    {
                        // we create the alphaNode if a function is found and
                        // the return type is either boolean primitive or object
                        if (f.ReturnType == Constants.BOOLEAN_PRIM_TYPE || f.ReturnType != Constants.BOOLEAN_OBJECT)
                        {
                            // TODO - need to implement it
                        }
                        else
                        {
                            // the function doesn't return boolean, so we have to notify
                            // the listeners the condition is not valid
                            CompileMessageEventArgs ce = new CompileMessageEventArgs(this, EventType.FUNCTION_INVALID);
                            ce.Message = INVALID_FUNCTION + " " + f.ReturnType; //$NON-NLS-1$
                            notifyListener(ce);
                        }
                    }
                    else
                    {
                        // we need to notify listeners the function wasn't found
                        CompileMessageEventArgs ce = new CompileMessageEventArgs(this, EventType.FUNCTION_NOT_FOUND);
                        ce.Message = FUNCTION_NOT_FOUND + " " + cnstr.FunctionName;
                        notifyListener(ce);
                    }
                }
            }
            Binding bind = new Binding();
            bind.VarName = cnstr.VariableName;
            bind.LeftRow = position;
            bind.LeftIndex = templ.getSlot(cnstr.Name).Id;
            bind.RowDeclared = position;
            // we only Add the binding to the map if it doesn't already exist
            if (rule.getBinding(cnstr.VariableName) == null)
            {
                rule.addBinding(cnstr.VariableName, bind);
            }
            return current;
        }
예제 #15
0
 /// <summary>
 /// method creates Bindings from the bound constraint and adds them to
 /// the Rule.
 /// </summary>
 /// <param name="cnstr">The CNSTR.</param>
 /// <param name="templ">The templ.</param>
 /// <param name="rule">The rule.</param>
 /// <param name="position">The position.</param>
 /// <returns></returns>
 public virtual BaseAlpha2 compileConstraint(BoundConstraint cnstr, ITemplate templ, Rule.IRule rule, int position)
 {
     BaseAlpha2 current = null;
     if (rule.getBinding(cnstr.VariableName) == null)
     {
         // if the HashMap doesn't already contain the binding, we create
         // a new one
         if (cnstr.IsObjectBinding)
         {
             Binding bind = new Binding();
             bind.VarName = cnstr.VariableName;
             bind.LeftRow = position;
             bind.LeftIndex = - 1;
             bind.IsObjectVar = true;
             rule.addBinding(cnstr.VariableName, bind);
         }
         else
         {
             Binding bind = new Binding();
             bind.VarName = cnstr.VariableName;
             bind.LeftRow = position;
             bind.LeftIndex = templ.getSlot(cnstr.Name).Id;
             bind.RowDeclared = position;
             cnstr.FirstDeclaration = true;
             rule.addBinding(cnstr.VariableName, bind);
         }
     }
     return current;
 }