private async Task ExecuteAsyncRules(IEnumerable <IGeneralRule <T> > rules)
        {
            rules = rules.ToList();

            await ExecuteParallelRules(rules);

            foreach (var asyncRule in OrderByExecutionOrder(rules))
            {
                await InvokeNestedRulesAsync(asyncRule.Configuration.InvokeNestedRulesFirst, asyncRule);

                if (asyncRule.CanInvoke(_model, _ruleEngineConfiguration.IsRuleEngineTerminated()))
                {
                    await InvokePreactiveRulesAsync(asyncRule);

                    TraceMessage.Verbose(asyncRule, TraceMessage.BeforeInvokeAsync);
                    await asyncRule.BeforeInvokeAsync();

                    TraceMessage.Verbose(asyncRule, TraceMessage.InvokeAsync);
                    var ruleResult = await asyncRule.InvokeAsync();

                    TraceMessage.Verbose(asyncRule, TraceMessage.AfterInvokeAsync);
                    await asyncRule.AfterInvokeAsync();

                    asyncRule.UpdateRuleEngineConfiguration(_ruleEngineConfiguration);

                    await InvokeReactiveRulesAsync(asyncRule);

                    ruleResult.AssignRuleName(asyncRule.GetType().Name);
                    AddToAsyncRuleResults(ruleResult);
                }

                await InvokeNestedRulesAsync(!asyncRule.Configuration.InvokeNestedRulesFirst, asyncRule);
            }
        }
        private async Task ExecuteAsyncRules(IEnumerable <IRuleAsync <T> > rules)
        {
            await ExecuteParallelRules(rules);

            foreach (var rule in OrderByExecutionOrder(rules))
            {
                await InvokeNestedRulesAsync(rule.Configuration.InvokeNestedRulesFirst, rule);

                if (rule.CanInvoke() && !_ruleEngineConfiguration.IsRuleEngineTerminated())
                {
                    try
                    {
                        await InvokeProactiveRulesAsync(rule);

                        AddToAsyncRuleResults(await ExecuteRuleAsync(rule));
                        rule.UpdateRuleEngineConfiguration(_ruleEngineConfiguration);

                        await InvokeReactiveRulesAsync(rule);
                    }
                    catch (Exception exception)
                    {
                        rule.UnhandledException = exception;

                        if (_rxRuleService.GetExceptionRules().ContainsKey(rule.GetType()))
                        {
                            await InvokeExceptionRulesAsync(rule);
                        }
                        else
                        {
                            var globalExceptionHandler = _rules.GetGlobalExceptionHandler();

                            if (globalExceptionHandler is IRuleAsync <T> ruleAsync)
                            {
                                globalExceptionHandler.UnhandledException = exception;
                                await ExecuteAsyncRules(new List <IRuleAsync <T> > {
                                    ruleAsync
                                });
                            }
                            else
                            {
                                throw;
                            }
                        }
                    }
                }

                await InvokeNestedRulesAsync(!rule.Configuration.InvokeNestedRulesFirst, rule);
            }
        }
        private void Execute(IEnumerable <IGeneralRule <T> > rules)
        {
            foreach (var rule in OrderByExecutionOrder(rules))
            {
                InvokeNestedRules(rule.Configuration.InvokeNestedRulesFirst, rule);

                if (rule.CanInvoke(_model, _ruleEngineConfiguration.IsRuleEngineTerminated()))
                {
                    InvokePreactiveRules(rule);

                    TraceMessage.Verbose(rule, TraceMessage.BeforeInvoke);
                    rule.BeforeInvoke();

                    TraceMessage.Verbose(rule, TraceMessage.Invoke);
                    var ruleResult = rule.Invoke();

                    TraceMessage.Verbose(rule, TraceMessage.AfterInvoke);
                    rule.AfterInvoke();

                    AddToRuleResults(ruleResult, rule.GetType().Name);

                    rule.UpdateRuleEngineConfiguration(_ruleEngineConfiguration);

                    InvokeReactiveRules(rule);
                }

                InvokeNestedRules(!rule.Configuration.InvokeNestedRulesFirst, rule);
            }
        }
Exemple #4
0
        private void Execute(IList <IRule <T> > rules)
        {
            foreach (var rule in OrderByExecutionOrder(rules))
            {
                InvokeNestedRules(rule.Configuration.InvokeNestedRulesFirst, rule);

                if (rule.CanInvoke(_model, _ruleEngineConfiguration.IsRuleEngineTerminated()))
                {
                    InvokeProactiveRules(rule);

                    try
                    {
                        TraceMessage.Verbose(rule, TraceMessage.BeforeInvoke);
                        rule.BeforeInvoke();

                        TraceMessage.Verbose(rule, TraceMessage.Invoke);
                        var ruleResult = rule.Invoke();

                        TraceMessage.Verbose(rule, TraceMessage.AfterInvoke);
                        rule.AfterInvoke();

                        AddToRuleResults(ruleResult, rule.GetType().Name);
                    }

                    catch (Exception exception)
                    {
                        rule.UnhandledException = exception;
                        if (_rxRuleService.GetExceptionRules().ContainsKey(rule.GetType()))
                        {
                            InvokeExceptionRules(rule);
                        }
                        else
                        {
                            var globalExceptionHandler = _rules.GetGlobalExceptionHandler();

                            if (globalExceptionHandler is IRule <T> )
                            {
                                globalExceptionHandler.UnhandledException = exception;
                                Execute(new List <IRule <T> > {
                                    (IRule <T>)globalExceptionHandler
                                });
                            }

                            throw;
                        }
                    }

                    rule.UpdateRuleEngineConfiguration(_ruleEngineConfiguration);

                    InvokeReactiveRules(rule);
                }

                InvokeNestedRules(!rule.Configuration.InvokeNestedRulesFirst, rule);
            }
        }