Exemplo n.º 1
0
        public static Object generatePredicateValue(PredicateConstraint pc)
        {
            String     fname         = pc.FunctionName;
            Object     value_Renamed = null;
            IParameter p             = null;
            // first find the literal value
            IList prms = pc.Parameters;

            for (int idx = 0; idx < prms.Count; idx++)
            {
                if (prms[idx] is ValueParam)
                {
                    p = (IParameter)pc.Parameters[1];
                }
            }
            if (fname.Equals(">") || fname.Equals(">="))
            {
                value_Renamed = Decimal.Add(p.BigDecimalValue, new Decimal(1));
            }
            else if (fname.Equals("<") || fname.Equals("<="))
            {
                value_Renamed = Decimal.Subtract(p.BigDecimalValue, new Decimal(1));
            }
            return(value_Renamed);
        }
Exemplo n.º 2
0
        /// <summary>
        /// </summary>
        /// <param name="">cond
        /// </param>
        /// <param name="">templ
        /// </param>
        /// <param name="">engine
        /// </param>
        /// <returns>
        ///
        /// </returns>
        public static IFact generateDeffact(ObjectCondition cond, Deftemplate templ, Rete.Rete engine)
        {
            List <object> list = new List <Object>();

            IConstraint[] cnstr = cond.Constraints;
            for (int idx = 0; idx < cnstr.Length; idx++)
            {
                IConstraint cn = cnstr[idx];
                if (cn is LiteralConstraint)
                {
                    Slot s = new Slot(cn.Name, cn.Value);
                    list.Add(s);
                }
                else if (cn is PredicateConstraint)
                {
                    PredicateConstraint pc = (PredicateConstraint)cn;
                    Object val             = generatePredicateValue(pc);
                    Slot   s = new Slot(cn.Name, val);
                    list.Add(s);
                }
                else if (cn is BoundConstraint)
                {
                    // for now we do the simple thing and just set
                    // any bound slots to 1
                    Slot s = new Slot(cn.Name, 1);
                    list.Add(s);
                }
            }
            IFact f = templ.createFact(list, engine.nextFactId());

            return(f);
        }
        public void RecusiveCallExceptionExpected()
        {
            var mockAttributeBag = new Mock <IAttributeBag>();

            // Note: Creating a re-entrant constraint is now much more difficult than
            //       before because constraints are immutable.  Even the code findBy |= findBy
            //       will not create a re-entrant constraint, it will just be an Or constraint
            //       with two identical clauses.  Given this change in constraint construction
            //       we should consider removing the re-entrance checks in the future.  -- Jeff.
            Constraint findBy = Find.By("tag", "value");

            findBy |= new PredicateConstraint <IAttributeBag>(bag => findBy.Matches(bag, new ConstraintContext()));

            ConstraintContext context = new ConstraintContext();

            mockAttributeBag.Expect(bag => bag.GetAttributeValue("tag")).Returns("val");
            mockAttributeBag.Expect(bag => bag.GetAdapter <IAttributeBag>()).Returns(mockAttributeBag.Object);

            findBy.Matches(mockAttributeBag.Object, context);

            mockAttributeBag.VerifyAll();
        }
        public void RecusiveCallExceptionExpected()
        {
            var mockAttributeBag = new Mock<IAttributeBag>();

            // Note: Creating a re-entrant constraint is now much more difficult than
            //       before because constraints are immutable.  Even the code findBy |= findBy
            //       will not create a re-entrant constraint, it will just be an Or constraint
            //       with two identical clauses.  Given this change in constraint construction
            //       we should consider removing the re-entrance checks in the future.  -- Jeff.
            Constraint findBy = Find.By("tag", "value");
            findBy |=  new PredicateConstraint<IAttributeBag>(bag => findBy.Matches(bag, new ConstraintContext()));

            ConstraintContext context = new ConstraintContext();
            mockAttributeBag.Expect(bag => bag.GetAttributeValue("tag")).Returns("val");
            mockAttributeBag.Expect(bag => bag.GetAdapter<IAttributeBag>()).Returns(mockAttributeBag.Object);

            findBy.Matches(mockAttributeBag.Object, context);

            mockAttributeBag.VerifyAll();
        }
Exemplo n.º 5
0
 public void SetUp()
 {
     theConstraint = new PredicateConstraint<int>((x) => x < 5 );
     expectedDescription = @"value matching lambda expression";
     stringRepresentation = "<predicate>";
 }
        /// <summary> the paramList should be clean and
        /// other codes surrounding this method in subclass may be removed into this method.
        /// Houzhanbin 10/16/2007
        /// </summary>
        /// <param name="">condition
        /// </param>
        /// <param name="">rule
        /// </param>
        /// <param name="">Constraints
        /// </param>
        /// <param name="">position
        /// </param>
        /// <param name="">hasNotEqual
        /// </param>
        /// <param name="">hasPredicateJoin
        /// </param>
        /// <returns>
        ///
        /// </returns>
        internal Binding[] getBindings(ICondition condition, Rule.IRule rule, int position)
        {
            ObjectCondition oc          = getObjectCondition(condition);
            IList           Constraints = oc.BindConstraints;
            Deftemplate     tmpl        = oc.Deftemplate;

            Binding[] binds = new Binding[Constraints.Count];
            for (int idz = 0; idz < Constraints.Count; idz++)
            {
                Object cst = Constraints[idz];
                if (cst is BoundConstraint)
                {
                    BoundConstraint bc  = (BoundConstraint)cst;
                    Binding         cpy = rule.copyBinding(bc.VariableName);
                    if (cpy.LeftRow >= position)
                    {
                        binds = new Binding[0];
                        break;
                    }
                    else
                    {
                        binds[idz] = cpy;
                        int rinx = tmpl.getColumnIndex(bc.Name);
                        // we increment the count to make sure the
                        // template isn't removed if it is being used
                        tmpl.incrementColumnUseCount(bc.Name);
                        binds[idz].RightIndex = rinx;
                        binds[idz].Negated    = bc.Negated;
                        if (bc.Negated)
                        {
                            oc.HasNotEqual = true;
                        }
                    }
                }
                else if (cst is PredicateConstraint)
                {
                    PredicateConstraint pc = (PredicateConstraint)cst;
                    if (pc.Value is BoundParam)
                    {
                        oc.HasPredicateJoin = true;
                        BoundParam bpm = (BoundParam)pc.Value;
                        String     var = bpm.VariableName;
                        int        op  = ConversionUtils.getOperatorCode(pc.FunctionName);
                        // check and make sure the function isn't user defined
                        if (op != Constants.USERDEFINED)
                        {
                            // if the first binding in the function is from the object type
                            // we reverse the operator
                            if (pc.Parameters[0] != bpm)
                            {
                                op = ConversionUtils.getOppositeOperatorCode(op);
                            }
                            binds[idz] = rule.copyPredicateBinding(var, op);
                            ((Binding2)binds[idz]).RightVariable = pc.VariableName;
                        }
                        else
                        {
                            binds[idz] = rule.copyPredicateBinding(var, op);
                        }
                        binds[idz].PredJoin = true;
                        int rinx = tmpl.getColumnIndex(pc.Name);
                        // we increment the count to make sure the
                        // template isn't removed if it is being used
                        tmpl.incrementColumnUseCount(pc.Name);
                        binds[idz].RightIndex = rinx;
                    }
                    else if (pc.Value is FunctionParam)
                    {
                        // this means there is a nested function
                    }
                }
            }
            return(binds);
        }
Exemplo n.º 7
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);
        }
Exemplo n.º 8
0
 public static Object generatePredicateValue(PredicateConstraint pc)
 {
     String fname = pc.FunctionName;
     Object value_Renamed = null;
     IParameter p = null;
     // first find the literal value
     IList prms = pc.Parameters;
     for (int idx = 0; idx < prms.Count; idx++)
     {
         if (prms[idx] is ValueParam)
         {
             p = (IParameter) pc.Parameters[1];
         }
     }
     if (fname.Equals(">") || fname.Equals(">="))
     {
         value_Renamed = Decimal.Add(p.BigDecimalValue, new Decimal(1));
     }
     else if (fname.Equals("<") || fname.Equals("<="))
     {
         value_Renamed = Decimal.Subtract(p.BigDecimalValue, new Decimal(1));
     }
     return value_Renamed;
 }
Exemplo n.º 9
0
 public void SetUp()
 {
     theConstraint        = new PredicateConstraint <int>((x) => x < 5);
     expectedDescription  = @"value matching lambda expression";
     stringRepresentation = "<predicate>";
 }