public string RuleWriter(Rule rule)
        {
            StringWriter writer = new StringWriter();
            XmlSerializer ruleSerializer = null;
            switch (rule.RuleTypeID)
            {
                case RuleType.PaymentMethod:
                    ruleSerializer = new XmlSerializer(typeof(PaymentMethodRule));
                    break;

                case RuleType.Region:
                    ruleSerializer = new XmlSerializer(typeof(RegionRule));
                    break;

                case RuleType.QuantityNeeded:
                    ruleSerializer = new XmlSerializer(typeof(QuantityNeededRule));
                    break;

                case RuleType.Schedule:
                    ruleSerializer = new XmlSerializer(typeof(ScheduleRule));
                    break;
            }

            var xmlnsEmpty = new XmlSerializerNamespaces();
            xmlnsEmpty.Add(string.Empty, string.Empty);
            ruleSerializer.Serialize(writer, rule, xmlnsEmpty);
            return writer.ToString();
        }
Beispiel #2
0
 private RuleFunc <RuleResultTree> CompileRule(Rule rule, RuleParameter[] ruleParams, ScopedParam[] scopedParams)
 {
     return(_ruleCompiler.CompileRule(rule, ruleParams, scopedParams));
 }
        private RuleFunc <RuleResultTree> CompileRule(string workflowName, RuleParameter[] ruleParams, Rule rule)
        {
            if (!_reSettings.EnableLocalParams)
            {
                return(_ruleCompiler.CompileRule(rule, ruleParams));
            }
            var compiledParamsKey = GetCompiledParamsCacheKey(workflowName, rule.RuleName, ruleParams);
            IEnumerable <CompiledParam> compiledParamList = _compiledParamsCache.GetOrCreate(compiledParamsKey, (entry) => _ruleParamCompiler.CompileParamsExpression(rule, ruleParams));
            var compiledRuleParameters = compiledParamList?.Select(c => c.AsRuleParameter()) ?? new List <RuleParameter>();
            var updatedRuleParams      = ruleParams?.Concat(compiledRuleParameters);
            var compiledRule           = _ruleCompiler.CompileRule(rule, updatedRuleParams?.ToArray());

            RuleFunc <RuleResultTree> updatedRule = (RuleParameter[] paramList) =>
            {
                var inputs = paramList.AsEnumerable();
                IEnumerable <CompiledParam> localParams = compiledParamList ?? new List <CompiledParam>();
                var evaluatedParamList = new List <RuleParameter>();
                foreach (var localParam in localParams)
                {
                    var evaluatedLocalParam = _ruleParamCompiler.EvaluateCompiledParam(localParam.Name, localParam.Value, inputs);
                    inputs = inputs.Append(evaluatedLocalParam);
                    evaluatedParamList.Add(evaluatedLocalParam);
                }
                var result = compiledRule(inputs.ToArray());
                result.RuleEvaluatedParams = evaluatedParamList;
                return(result);
            };

            return(updatedRule);
        }