Example #1
0
        internal Rule(

            Define.Rule definition,
            Engine <TModel> EngineRef,
            bool inner
            )
        {
            this.definition = definition;
            this.engineRef  = EngineRef;
            this.inner      = inner;
        }
Example #2
0
        internal virtual void SetResult(LogicDefine.Rule rule, bool result)
        {
            EvaluatorKey key = new EvaluatorKey()
            {
                Context = rule.Context?.ToString(), Id = rule.Id
            };

            if (this.results.ContainsKey(key.ToString()))
            {
                this.results.Remove(key.ToString());
            }
            this.results.Add(key.ToString(), result);
        }
Example #3
0
 internal Expression(Define.Rule definition,
                     Operand condition,
                     IRule <TModel> first,
                     IRule <TModel> second,
                     Engine <TModel> engineRef,
                     bool inner)
 {
     this.definition = definition;
     this.condition  = condition;
     this.first      = first;
     this.second     = second;
     this.engineRef  = engineRef;
     this.inner      = inner;
 }
Example #4
0
        IFluentActivityBuilder <T> IFluentElseActivityBuilder <T> .Else(Action <IFluentActivityBuilder <T> > Then)
        {
            WorkDefine.Activity parent = this.activityStack.Peek();

            int subCount = this.subActivities[parent.Id];

            subCount++;
            this.subActivities[parent.Id] = subCount;

            var lastReaction = parent.Reactions.Last();

            LogicDefine.Rule equationAsRule = lastReaction.Logic;

            string activityId = string.Format("{0}{1}rx{2}", parent.Id, this.config.Convention.Delimeter, subCount);

            WorkDefine.Activity toBuild = new WorkDefine.Activity()
            {
                Id        = activityId,
                Reactions = new List <WorkDefine.Reaction>()
                {
                }
            };



            this.subActivities.Add(activityId, 0);
            activityStack.Push(toBuild);
            this.workflowManager.AddActivity(toBuild);


            //negate
            equationAsRule.TrueCondition = !equationAsRule.TrueCondition;

            string equationId = equationAsRule.ShortHand;

            Then(this);



            parent.Reactions.Add(new WorkDefine.Reaction()
            {
                Logic = equationId, Work = toBuild.Id
            });

            this.activityStack.Pop();

            return(this);
        }
Example #5
0
        internal virtual bool?GetResult(LogicDefine.Rule rule)
        {
            EvaluatorKey key = new EvaluatorKey()
            {
                Id = rule.Id, Context = rule.Context?.ToString()
            };

            if (results.ContainsKey(key.ToString()))
            {
                return(results[key.ToString()]);
            }
            else
            {
                return(null);
            }
        }
Example #6
0
        private ArticulateEvaluator ArticulateEvaluator(LogicDefine.Rule x)
        {
            ArticulateEvaluator toBuild = new ArticulateEvaluator()
            {
                Id = x.Id
            };

            LogicDefine.Evaluator ev = this.workflow.Evaluators.FirstOrDefault(g => g.Id == x.Id);
            toBuild.Literal = ev.Description;

            toBuild.TrueCondition = x.TrueCondition;
            if (x.Context != null)
            {
                ContextDefinition ctxDef = this.workflow.ContextDefinitions.FirstOrDefault(g => g.Name.Equals(x.Context.Name, StringComparison.OrdinalIgnoreCase));

                string ctxLit = string.Join(",", (from i in ctxDef.Items where x.Context.Keys.Contains(i.Key) select i.Literal));

                ArticulateContext articulateContext = new ArticulateContext()
                {
                    Literal = ctxDef.Literal, Value = ctxLit
                };
                //var intent = this.logicIntents.FirstOrDefault(g => g.evaluatorId == x.Id);
                //if (intent != null && intent.Context != null)
                //{
                //    articulateContext.Literal = intent.Context.Literal;
                //    if (intent.Context.Values != null)
                //    {
                //        ContextItem match = intent.Context.Values.FirstOrDefault(k => k.Key == ctx.Key);
                //        if (!string.IsNullOrEmpty(match.Key))
                //        {
                //            ctx.Literal = match.Literal;
                //        }

                //    }


                //}
                //if (string.IsNullOrEmpty(ctx.Literal)) { ctx.Literal = "Inferred"; }
                //if (string.IsNullOrEmpty(articulateContext.Literal)) { articulateContext.Literal = "Inferred"; }
                //articulateContext.Context = ctx;
                toBuild.Context = articulateContext;
            }

            return(toBuild);
        }
Example #7
0
        IEngineLoader <TModel> IEngineLoader <TModel> .PreemptValidation <T>(string key, string context, string comment, string auditCode)
        {
            string typeOfT = typeof(T).Name;

            LogicDefine.Rule rule = new LogicDefine.Rule()
            {
                Id = typeOfT, Context = context
            };

            string normalized = ConventionHelper.ApplyConvention(NamePrefixOptions.Evaluator, rule.RuleIdWithContext, this.Configuration.Convention) + ".";

            key = key.ToLower().Replace(normalized.ToLower(), "");

            string             orideKey = ConventionHelper.ApplyConvention(NamePrefixOptions.Evaluator, rule.RuleIdWithContext + "." + key, this.Configuration.Convention);
            ValidationOverride oride    = new ValidationOverride(orideKey.ToLower(), comment, auditCode);

            this.preOverrides.Add(oride);
            return(this);
        }
Example #8
0
        void IFluentExpressionBuilder <T> .ExpIsFalse(Action <IFluentExpressionBuilder <T> > If)
        {
            string equationId = string.Empty;


            //we are in a sub equation
            if (this.epxressionStack.Count > 0)
            {
                string lastEquationId = this.epxressionStack.Peek().Id;
                string suffix         = (this.epxressionStack.Count % 2 == 0) ? "2" : "1";
                equationId = lastEquationId + this.config.Convention.Delimeter + suffix;
            }
            else //we are at the root
            {
                string lastActivityId = this.activityStack.Peek().Id;
                equationId = ConventionHelper.ChangePrefix(NamePrefixOptions.Activity, NamePrefixOptions.Equation, lastActivityId, this.config.Convention);
            }

            LogicDefine.Equation toAdd = new LogicDefine.Equation()
            {
                Condition = Logic.Operand.And,
                Id        = equationId
            };

            this.epxressionStack.Push(toAdd);
            this.workflowManager.AddEquation(toAdd);


            string firstId, secondId = null;

            If(this);

            LogicDefine.Rule firstRule = this.epxressionStack.Pop().ShortHand;
            firstRule.TrueCondition = !firstRule.TrueCondition;

            firstId = firstRule.ShortHand;

            secondId = ConventionHelper.TrueEvaluator(this.config.Convention);


            toAdd.First  = firstId;
            toAdd.Second = secondId;
        }
Example #9
0
        private IRule <TModel> LoadLogic(string equationId)
        {
            StepTracer <LintTrace>    trace = new StepTracer <LintTrace>();
            StepTraceNode <LintTrace> root  = trace.TraceFirst(new LintTrace(LintStatusOptions.Loading, "Loading Logic", equationId));


            //Lint.... make sure we have everything we need first.
            Func <LogicDefine.Rule, StepTraceNode <LintTrace>, bool, IRule <TModel> > LoadRule = null;

            LoadRule = (rule, parentStep, inner) =>
            {
                StepTraceNode <LintTrace> step     = trace.TraceNext(parentStep, new LintTrace(LintStatusOptions.Inspecting, "Inspecting Rule", rule.Id));
                IRule <TModel>            toReturn = null;
                //if id is an equation, we are creating an expression

                //since we've formalized convention, we can just check that
                if (ConventionHelper.MatchesConvention(NamePrefixOptions.Equation, rule.Id, this.Configuration.Convention))
                {
                    LogicDefine.Equation eq = this.WorkflowManager.GetEquation(rule.Id);

                    IRule <TModel> first  = LoadRule(eq.First, step, true);
                    IRule <TModel> second = LoadRule(eq.Second, step, true);
                    toReturn = new Expression <TModel>(rule, eq.Condition, first, second, this, inner);
                }
                else
                {
                    LogicDefine.Evaluator ev = this.WorkflowManager.GetEvaluator(rule.Id);
                    toReturn = new Rule <TModel>(rule, this, inner);
                }



                return(toReturn);
            };



            LogicDefine.Rule eqRule = equationId;
            IRule <TModel>   loaded = LoadRule(eqRule, root, false);

            return(loaded);
        }
Example #10
0
        //this needs to
        // * ensure reaction rule is an equation
        // * ensure that any evaluators exist in the evaluators list
        private string LoadLogic(WorkDefine.Workflow workFlow, string equationId)
        {
            StepTraceNode <LintTrace> root = this.tracer.Root;


            //load conventions

            LogicDefine.Evaluator trueDef = workFlow.Evaluators.FirstOrDefault(z => z.Id == ConventionHelper.TrueEvaluator(this.config.Convention));
            if (null == trueDef)
            {
                trueDef = new LogicDefine.Evaluator()
                {
                    Id = ConventionHelper.TrueEvaluator(this.config.Convention), Description = "Always True"
                };
                workFlow.Evaluators.Add(trueDef);
            }
            LogicDefine.Equation trueEqDef = workFlow.Equations.FirstOrDefault(z => z.Id == ConventionHelper.TrueEquation(this.config.Convention));
            if (null == trueEqDef)
            {
                trueEqDef = new LogicDefine.Equation()
                {
                    Condition = Logic.Operand.Or, First = trueDef.Id, Second = trueDef.Id, Id = ConventionHelper.TrueEquation(this.config.Convention)
                };
                workFlow.Equations.Add(trueEqDef);
            }



            //Lint.... make sure we have everything we need first.
            Action <LogicDefine.Rule, StepTraceNode <LintTrace>, bool> LoadRule = null;

            LoadRule = (rule, parentStep, isRoot) =>
            {
                StepTraceNode <LintTrace> step = this.tracer.TraceNext(parentStep, new LintTrace(LintStatusOptions.Inspecting, "Inspecting Rule", rule.Id));

                //if id is an equation, we are creating an expression
                LogicDefine.Equation eq = workFlow.Equations.FirstOrDefault(g => g.Id.Equals(rule.Id));
                if (null != eq)
                {
                    if (null != eq.First)
                    {
                        LoadRule(eq.First, step, false);
                    }
                    else
                    {
                        eq.First = new LogicDefine.Rule()
                        {
                            Id = ConventionHelper.TrueEvaluator(this.config.Convention), Context = string.Empty, TrueCondition = true
                        };
                    }

                    if (null != eq.Second)
                    {
                        LoadRule(eq.Second.Id, step, false);
                    }
                    else
                    {
                        eq.Second = new LogicDefine.Rule()
                        {
                            Id = ConventionHelper.TrueEvaluator(this.config.Convention), Context = string.Empty, TrueCondition = true
                        };
                    }

                    if (!rule.TrueCondition)
                    {
                        //create a negation equation.
                        string           negationId = ConventionHelper.NegateEquationName(rule.Id, this.config.Convention);
                        LogicDefine.Rule negated    = (LogicDefine.Rule)rule.Clone();
                        //negated.TrueCondition = false;

                        if (workFlow.Equations.Count(g => g.Id == negationId) == 0)
                        {
                            this.tracer.TraceNext(parentStep, new LintTrace(LintStatusOptions.InferringEquation, string.Format("Inferring negation equation from {0}", rule.Id), negationId));
                            LogicDefine.Equation toAdd = new LogicDefine.Equation()
                            {
                                First     = negated,
                                Id        = negationId,
                                Condition = Logic.Operand.And,
                                Second    = ConventionHelper.TrueEvaluator(this.config.Convention)
                            };
                            workFlow.Equations.Add(toAdd);

                            rule.TrueCondition = true;
                            rule.Id            = negationId;
                        }
                    }
                }
                else
                {
                    //if reaction ruleid is not an equation, create an equation and update reaction

                    LogicDefine.Evaluator ev = workFlow.Evaluators.FirstOrDefault(g => g.Id.Equals(rule.Id));

                    if (null == ev)
                    {
                        this.tracer.TraceNext(parentStep, new LintTrace(LintStatusOptions.LazyDefinition, "No definition found for evaluator", rule.Id));
                        ev = new LogicDefine.Evaluator()
                        {
                            Id          = rule.Id,
                            Description = string.Empty
                        };

                        workFlow.Evaluators.Add(ev);
                    }

                    //if this is the rule referenced by the reaction, then create an equation also,
                    //and  update the equation.  This isn't necessary, but consistent.
                    if (isRoot)
                    {
                        LogicDefine.Rule cloned    = (LogicDefine.Rule)rule.Clone();
                        string           newId     = string.Empty;
                        Logic.Operand    condition = Logic.Operand.And;
                        if (rule.Id == ConventionHelper.TrueEquation(this.config.Convention))
                        {
                            newId     = ConventionHelper.TrueEquation(this.config.Convention);
                            condition = Logic.Operand.Or;
                        }
                        else
                        {
                            newId = ConventionHelper.ChangePrefix(NamePrefixOptions.Evaluator, NamePrefixOptions.Equation, rule.Id, this.config.Convention);
                        }

                        if (!rule.TrueCondition)
                        {
                            newId = ConventionHelper.NegateEquationName(newId, this.config.Convention);
                        }
                        if (workFlow.Equations.Count(g => g.Id == newId) == 0)
                        {
                            this.tracer.TraceNext(parentStep, new LintTrace(LintStatusOptions.InferringEquation, string.Format("Inferring equation from {0}", rule.Id), newId));
                            workFlow.Equations.Add(new LogicDefine.Equation()
                            {
                                Condition = condition,
                                First     = cloned,
                                Second    = ConventionHelper.TrueEvaluator(this.config.Convention),
                                Id        = newId
                            });
                        }
                        rule.Id = newId;
                    }
                }
            };



            LogicDefine.Rule eqRule = equationId;
            LoadRule(eqRule, root, true);

            return(eqRule.Id);
        }
Example #11
0
        void IFluentExpressionBuilder <T> .Rule(Action <IRuleBuilder <T> > action)
        {
            RuleBuilder <T> builderRef = new RuleBuilder <T>(this);

            action.Invoke(builderRef);

            LogicDefine.Rule evaluatorId = builderRef.rule;



            evaluatorId.Id = ConventionHelper.EnsureConvention(NamePrefixOptions.Evaluator, evaluatorId.Id, this.config.Convention);

            string actionName  = builderRef.evaluatorx.GetType().Name;
            string description = ConventionHelper.ParseMethodName(actionName, this.config.Convention.ParseMethodNamesAs).Literal;

            var descAttr = builderRef.evaluatorx.GetType().GetCustomAttributes(typeof(ArticulateOptionsAttribute), true)
                           .OfType <ArticulateOptionsAttribute>()
                           .FirstOrDefault();

            if (descAttr != null)
            {
                description = descAttr.Description;
            }

            this.workflowManager.AddEvaluator(new LogicDefine.Evaluator()
            {
                Id = evaluatorId.Id, Description = description
            });
            bool isRoot = this.epxressionStack.Count == 0;

            if (!this.evaluators.ContainsKey(evaluatorId.Id))
            {
                this.evaluators.Add(evaluatorId.Id, builderRef.evaluatorx);
            }                //if attmpeting to add another implementation with the same id, throw an exception
                             //we can't handle this
            else if (this.evaluators[evaluatorId.Id].GetType() != builderRef.evaluatorx.GetType())
            {
                throw new BuilderException(evaluatorId.Id);
            }

            if (isRoot)
            {
                string equationId = ConventionHelper.ChangePrefix(NamePrefixOptions.Evaluator, NamePrefixOptions.Equation, evaluatorId.Id, this.config.Convention);
                bool   trueCond   = true;;
                if (!evaluatorId.TrueCondition)
                {
                    equationId = ConventionHelper.NegateEquationName(equationId, this.config.Convention);
                    trueCond   = false;
                }
                LogicDefine.Equation toAdd = new LogicDefine.Equation()
                {
                    Condition = Logic.Operand.And,
                    First     = evaluatorId,
                    Id        = equationId,
                    Second    = ConventionHelper.TrueEvaluator(this.config.Convention),
                    //TrueCondition = trueCond
                };
                this.epxressionStack.Push(toAdd);

                this.workflowManager.AddEquation(toAdd);
            }
            else
            {
                this.epxressionStack.Push(evaluatorId);
            }
            //if root... then create euqations
            //otherwise, just use as evaluator
        }