Exemplo n.º 1
0
        /// <summary>
        /// Gets the interceptor options.
        /// </summary>
        /// <typeparam name="TOwner">The type of the owner.</typeparam>
        /// <param name="frameworkCommandInfo">The framework command info.</param>
        /// <returns>Options object to add to Command.</returns>
        public object GetInterceptorOptions <TOwner> (IFrameworkCommandInfo frameworkCommandInfo)
        {
            IRuleSelector ruleSelector;

            if (frameworkCommandInfo is IRuleCommandInfo)
            {
                ruleSelector = (frameworkCommandInfo as IRuleCommandInfo).RuleSelector;
            }
            else
            {
                ruleSelector =
                    new SelectAllRulesInRuleSetSelector(
                        frameworkCommandInfo.Name.EndsWith("RuleSet") ? frameworkCommandInfo.Name : frameworkCommandInfo.Name + "RuleSet");
            }

            IRuleEngine <TOwner> ruleEngine = null;
            var triedCreateRuleEngine       = false;

            var context = new RuleEngineContext <TOwner> (( TOwner )frameworkCommandInfo.Owner, ruleSelector);

            var ruleExecutor = new RuleExecutor
            {
                ExecuteRules = o =>
                {
                    if (!triedCreateRuleEngine)
                    {
                        triedCreateRuleEngine = true;
                        ruleEngine            = _ruleEngineFactory.CreateRuleEngine <TOwner>();
                    }
                    if (ruleEngine != null)
                    {
                        context.Refresh();
                        if (o != null)
                        {
                            context.WorkingMemory.AddContextObject(o);
                            if (context.RuleSelector is ITakeParameter)
                            {
                                (context.RuleSelector as ITakeParameter).Parameter = o;
                            }
                        }
                        return(ruleEngine.ExecuteRules(context));
                    }
                    return(new RuleExecutionResult(Enumerable.Empty <RuleViolation> ()));
                }
            };

            return(ruleExecutor);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Exectutes rules selected by the rule selector in the Rule Collection of the Rule Engine.
        /// </summary>
        /// <param name="subject">Subject the rules are run against.</param>
        /// <param name="ruleSelector">The <see cref="IRuleSelector">Rule Selector</see> to determin the list of rules that will be executed.</param>
        /// <returns>A <see cref="RuleExecutionResult"/> containing the results of the execution pass.</returns>
        public RuleExecutionResult ExecuteSelectedRules(TSubject subject, IRuleSelector ruleSelector)
        {
            var ruleEngineContext = new RuleEngineContext <TSubject> (subject, ruleSelector);

            return(ExecuteRules(ruleEngineContext));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Exectues all rules in the Rule Collection of the Rule Engine.
        /// </summary>
        /// <param name="subject">Subject the rules are run against.</param>
        /// <returns>A <see cref="RuleExecutionResult"/> containing the results of the execution pass.</returns>
        public RuleExecutionResult ExecuteAllRules(TSubject subject)
        {
            var ruleEngineContext = new RuleEngineContext <TSubject> (subject);

            return(ExecuteRules(ruleEngineContext));
        }