Example #1
0
        /// <summary>
        /// This will compile the rules and store them to dictionary
        /// </summary>
        /// <typeparam name="T">type of entity</typeparam>
        /// <param name="workflowName">workflow name</param>
        /// <returns>bool result</returns>
        private bool RegisterRule(string workflowName, params RuleParameter[] ruleParams)
        {
            string compileRulesKey = GetCompileRulesKey(workflowName, ruleParams);

            if (_rulesCache.ContainsCompiledRules(compileRulesKey))
            {
                return(true);
            }

            var workflowRules = _rulesCache.GetWorkFlowRules(workflowName);

            if (workflowRules != null)
            {
                var lstFunc = new List <Delegate>();
                foreach (var rule in _rulesCache.GetRules(workflowName))
                {
                    RuleCompiler ruleCompiler = new RuleCompiler(new RuleExpressionBuilderFactory(_reSettings), _logger);
                    lstFunc.Add(ruleCompiler.CompileRule(rule, ruleParams));
                }

                _rulesCache.AddOrUpdateCompiledRule(compileRulesKey, new CompiledRule()
                {
                    CompiledRules = lstFunc
                });
                _logger.LogTrace($"Rules has been compiled for the {workflowName} workflow and added to dictionary");
                return(true);
            }
            else
            {
                return(false);
            }
        }
Example #2
0
 public RulesEngine(ILogger logger = null, ReSettings reSettings = null)
 {
     _logger               = logger ?? new NullLogger <RulesEngine>();
     _reSettings           = reSettings ?? new ReSettings();
     _ruleExpressionParser = new RuleExpressionParser(_reSettings);
     _ruleCompiler         = new RuleCompiler(new RuleExpressionBuilderFactory(_reSettings, _ruleExpressionParser), _reSettings, _logger);
     _actionFactory        = new ActionFactory(GetActionRegistry(_reSettings));
 }
Example #3
0
        /// <summary>
        /// This will compile the rules and store them to dictionary
        /// </summary>
        /// <param name="workflowName">workflow name</param>
        /// <param name="ruleParams">The rule parameters.</param>
        /// <returns>
        /// bool result
        /// </returns>
        private bool RegisterRule(string workflowName, params RuleParameter[] ruleParams)
        {
            string compileRulesKey = _rulesCache.GetRulesCacheKey(workflowName);

            if (_rulesCache.ContainsCompiledRules(compileRulesKey))
            {
                return(true);
            }

            var workflowRules = _rulesCache.GetWorkFlowRules(workflowName);

            if (workflowRules != null)
            {
                var lstFunc      = new List <RuleFunc <RuleResultTree> >();
                var ruleCompiler = new RuleCompiler(new RuleExpressionBuilderFactory(_reSettings), _logger);
                foreach (var rule in _rulesCache.GetRules(workflowName))
                {
                    var compiledParamsKey = GetCompiledParamsCacheKey(workflowName, rule);
                    CompiledRuleParam compiledRuleParam = _compiledParamsCache.GetOrCreate(compiledParamsKey, (entry) => ruleParamCompiler.CompileParamsExpression(rule, ruleParams));

                    var updatedRuleParams = compiledRuleParam != null?ruleParams?.Concat(compiledRuleParam?.RuleParameters) : ruleParams;

                    var compiledRule = ruleCompiler.CompileRule(rule, updatedRuleParams?.ToArray());

                    RuleFunc <RuleResultTree> updatedRule = (object[] paramList) => {
                        var inputs = paramList.AsEnumerable();
                        IEnumerable <CompiledParam> localParams = compiledRuleParam?.CompiledParameters ?? new List <CompiledParam>();
                        foreach (var localParam in localParams)
                        {
                            var evaluatedLocalParam = ruleParamCompiler.EvaluateCompiledParam(localParam.Name, localParam.Value, inputs);
                            inputs = inputs.Append(evaluatedLocalParam.Value);
                        }
                        var result = compiledRule(inputs.ToArray());
                        result.RuleEvaluatedParams = compiledRuleParam?.RuleParameters;
                        return(result);
                    };
                    lstFunc.Add(updatedRule);
                }

                _rulesCache.AddOrUpdateCompiledRule(compileRulesKey, lstFunc);
                _logger.LogTrace($"Rules has been compiled for the {workflowName} workflow and added to dictionary");
                return(true);
            }
            else
            {
                return(false);
            }
        }
Example #4
0
        /// <summary>
        /// This will compile the rules and store them to dictionary
        /// </summary>
        /// <param name="workflowName">workflow name</param>
        /// <param name="ruleParams">The rule parameters.</param>
        /// <returns>
        /// bool result
        /// </returns>
        private bool RegisterCompiledRule(string workflowName, params RuleParameter[] ruleParams)
        {
            string compileRulesKey = _rulesCache.GetRulesCacheKey(workflowName);

            if (_rulesCache.ContainsCompiledRules(compileRulesKey))
            {
                return(true);
            }

            var workflowRules = _rulesCache.GetWorkFlowRules(workflowName);

            if (workflowRules != null)
            {
                var lstFunc      = new List <CompiledRule>();
                var ruleCompiler = new RuleCompiler(new RuleExpressionBuilderFactory(_reSettings), _logger);
                foreach (var rule in _rulesCache.GetRules(workflowName))
                {
                    var compiledParamsKey = _compiledParamsCache.GetCompiledParamsCacheKey(workflowName, rule);
                    CompiledRuleParam compiledRuleParam;
                    if (_compiledParamsCache.ContainsParams(compiledParamsKey))
                    {
                        compiledRuleParam = _compiledParamsCache.GetParams(compiledParamsKey);
                    }
                    else
                    {
                        compiledRuleParam = ruleParamCompiler.CompileParamsExpression(rule, ruleParams);
                        _compiledParamsCache.AddOrUpdateParams(compiledParamsKey, compiledRuleParam);
                    }

                    var updatedRuleParams = compiledRuleParam != null?ruleParams?.Concat(compiledRuleParam?.RuleParameters) : ruleParams;

                    var compiledRule = ruleCompiler.CompileRule(rule, updatedRuleParams?.ToArray());
                    lstFunc.Add(new CompiledRule {
                        Rule = compiledRule, CompiledParameters = compiledRuleParam
                    });
                }

                _rulesCache.AddOrUpdateCompiledRule(compileRulesKey, lstFunc);
                _logger.LogTrace($"Rules has been compiled for the {workflowName} workflow and added to dictionary");
                return(true);
            }
            else
            {
                return(false);
            }
        }