/*
         * public virtual ObjectTypeNode findObjectTypeNode(String templateName)
         * {
         *  IEnumerator itr = inputnodes.keySet().GetEnumerator();
         *  Template tmpl = null;
         *  while (itr.MoveNext())
         *  {
         *      tmpl = (Template)itr.Current;
         *      if (tmpl.Name.Equals(templateName))
         *      {
         *          break;
         *      }
         *  }
         *  if (tmpl != null)
         *  {
         *      return (ObjectTypeNode)inputnodes.Get(tmpl);
         *  }
         *  else
         *  {
         *      log.Debug(Messages.getString("RuleCompiler.deftemplate.error")); //$NON-NLS-1$
         *      return null;
         *  }
         * }
         */

        /// <summary>
        /// method compiles a literalConstraint
        /// </summary>
        /// <param name="cnstr">The CNSTR.</param>
        /// <param name="templ">The templ.</param>
        /// <param name="rule">The rule.</param>
        /// <returns></returns>
        public virtual BaseAlpha2 compileConstraint(LiteralConstraint cnstr, ITemplate templ, Rule.IRule rule)
        {
            BaseAlpha2 current = null;

            if (templ.getSlot(cnstr.Name) != null)
            {
                Slot   sl   = (Slot)templ.getSlot(cnstr.Name).Clone();
                Object sval = ConversionUtils.convert(sl.ValueType, cnstr.Value);
                sl.Value = sval;
                if (rule.RememberMatch)
                {
                    current = new AlphaNode(engine.nextNodeId());
                }
                else
                {
                    current = new NoMemANode(engine.nextNodeId());
                }
                current.Slot     = sl;
                current.Operator = Constants.EQUAL;
                current.incrementUseCount();
                // we increment the node use count when when create a new
                // AlphaNode for the LiteralConstraint
                templ.getSlot(sl.Id).incrementNodeCount();
            }
            return(current);
        }
Beispiel #2
0
 /// <summary> Add a successor node
 /// </summary>
 public override void addSuccessorNode(BaseNode node, Rete engine, IWorkingMemory mem)
 {
     if (!containsNode(successorNodes, node) && !successor2.Contains(node))
     {
         if (node is BaseJoin || node is TerminalNode)
         {
             successor2.Add(node);
         }
         else
         {
             // we test to see if the operator is ==, nil, not nil
             // if the node isn't BaseJoin, it should be BaseAlpha
             BaseAlpha ba = (BaseAlpha)node;
             if (ba.Operator == Constants.LESS || ba.Operator == Constants.GREATER || ba.Operator == Constants.LESSEQUAL || ba.Operator == Constants.GREATEREQUAL || ba.Operator == Constants.NOTEQUAL || ba.Operator == Constants.NOTNILL)
             {
                 successor2.Add(node);
             }
             else
             {
                 addNode(node);
             }
         }
         if (gauranteeUnique && node is AlphaNode)
         {
             // now we use CompositeIndex instead of HashString
             AlphaNode anode = (AlphaNode)node;
             entries.Put(anode.HashIndex, node);
             // we increment the node count for the slot
             deftemplate.getSlot(anode.slot.Id).incrementNodeCount();
         }
         // if there are matches, we propogate the facts to
         // the new successor only
         IAlphaMemory alpha = (IAlphaMemory)mem.getAlphaMemory(this);
         if (alpha.size() > 0)
         {
             IEnumerator itr = alpha.GetEnumerator();
             while (itr.MoveNext())
             {
                 IFact f = (IFact)itr.Current;
                 if (node is BaseAlpha)
                 {
                     BaseAlpha next = (BaseAlpha)node;
                     next.assertFact(f, engine, mem);
                 }
                 else if (node is BaseJoin)
                 {
                     BaseJoin next = (BaseJoin)node;
                     next.assertRight(f, engine, mem);
                 }
                 else if (node is TerminalNode)
                 {
                     TerminalNode t   = (TerminalNode)node;
                     Index        inx = new Index(new IFact[] { f });
                     t.assertFacts(inx, engine, mem);
                 }
             }
         }
     }
 }
Beispiel #3
0
 public void testOneSlot()
 {
     Defclass dc = new Defclass(typeof (TestBean2));
     Deftemplate dtemp = dc.createDeftemplate("testBean2");
     TestBean2 bean = new TestBean2();
     Slot[] slts = dtemp.AllSlots;
     ObjectTypeNode otn = new ObjectTypeNode(1, dtemp);
     AlphaNode an = new AlphaNode(1);
     slts[0].Value = ConversionUtils.convert(110);
     an.Operator = Constants.EQUAL;
     an.Slot = (slts[0]);
     Console.WriteLine("node::" + an.ToString());
     Assert.IsNotNull(an.ToString(), "Should have a value.");
 }
Beispiel #4
0
        public void testTwoSlots()
        {
            Defclass dc = new Defclass(typeof (TestBean2));
            Deftemplate dtemp = dc.createDeftemplate("testBean2");
            TestBean2 bean = new TestBean2();
            Slot[] slts = dtemp.AllSlots;
            ObjectTypeNode otn = new ObjectTypeNode(1, dtemp);
            AlphaNode an1 = new AlphaNode(1);
            AlphaNode an2 = new AlphaNode(1);

            slts[0].Value = ("testString");
            slts[1].Value = (ConversionUtils.convert(999));

            an1.Slot = (slts[0]);
            an1.Operator = (Constants.EQUAL);
            Console.WriteLine("node::" + an1.toPPString());
            Assert.IsNotNull(an1.toPPString());

            an2.Slot = (slts[1]);
            an2.Operator = (Constants.GREATER);
            Console.WriteLine("node::" + an2.toPPString());
            Assert.IsNotNull(an2.toPPString());
        }
Beispiel #5
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;
        }
Beispiel #6
0
 /*
 public virtual ObjectTypeNode findObjectTypeNode(String templateName)
 {
     IEnumerator itr = inputnodes.keySet().GetEnumerator();
     Template tmpl = null;
     while (itr.MoveNext())
     {
         tmpl = (Template)itr.Current;
         if (tmpl.Name.Equals(templateName))
         {
             break;
         }
     }
     if (tmpl != null)
     {
         return (ObjectTypeNode)inputnodes.Get(tmpl);
     }
     else
     {
         log.Debug(Messages.getString("RuleCompiler.deftemplate.error")); //$NON-NLS-1$
         return null;
     }
 }
 */
 /// <summary>
 /// method compiles a literalConstraint
 /// </summary>
 /// <param name="cnstr">The CNSTR.</param>
 /// <param name="templ">The templ.</param>
 /// <param name="rule">The rule.</param>
 /// <returns></returns>
 public virtual BaseAlpha2 compileConstraint(LiteralConstraint cnstr, ITemplate templ, Rule.IRule rule)
 {
     BaseAlpha2 current = null;
     if (templ.getSlot(cnstr.Name) != null)
     {
         Slot sl = (Slot) templ.getSlot(cnstr.Name).Clone();
         Object sval = ConversionUtils.convert(sl.ValueType, cnstr.Value);
         sl.Value = sval;
         if (rule.RememberMatch)
         {
             current = new AlphaNode(engine.nextNodeId());
         }
         else
         {
             current = new NoMemANode(engine.nextNodeId());
         }
         current.Slot = sl;
         current.Operator = Constants.EQUAL;
         current.incrementUseCount();
         // we increment the node use count when when create a new
         // AlphaNode for the LiteralConstraint
         templ.getSlot(sl.Id).incrementNodeCount();
     }
     return current;
 }
        /// <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);
        }