/// <summary>
 /// Create a new RuleBookRule object.
 /// </summary>
 /// <param name="id">Initial value of the Id property.</param>
 /// <param name="effectiveDate">Initial value of the EffectiveDate property.</param>
 /// <param name="name">Initial value of the Name property.</param>
 /// <param name="active">Initial value of the Active property.</param>
 /// <param name="ruleBookId">Initial value of the RuleBookId property.</param>
 public static RuleBookRule CreateRuleBookRule(global::System.Int64 id, global::System.DateTime effectiveDate, global::System.String name, global::System.Boolean active, global::System.Int32 ruleBookId)
 {
     RuleBookRule ruleBookRule = new RuleBookRule();
     ruleBookRule.Id = id;
     ruleBookRule.EffectiveDate = effectiveDate;
     ruleBookRule.Name = name;
     ruleBookRule.Active = active;
     ruleBookRule.RuleBookId = ruleBookId;
     return ruleBookRule;
 }
 /// <summary>
 /// Deprecated Method for adding a new object to the RuleBookRules EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToRuleBookRules(RuleBookRule ruleBookRule)
 {
     base.AddObject("RuleBookRules", ruleBookRule);
 }
 //�Private�Methods�(4)
 private RuleEngineRule CreateRatingRule(IRuleContainer container, RuleBookRule rule)
 {
     return new RuleEngineRule()
     {
         Id = rule.Id.ToString(),
         RuleBookId = rule.RuleBookId,
         ChangeId = rule.ChangeId.ToString(),
         Name = rule.Name,
         Parent = rule.ParentChangeId == null ? null : container as RuleEngineRule,
         EffectiveDate = rule.EffectiveDate,
         ParentChangeId = rule.ParentChangeId,
         RuleExpression = rule.RuleExpression,
         LoopContext = rule.Context,
         Tag = rule.Tag,
         Active = rule.Active
     };
 }
        private void FillRules(Predicate<RuleEngineRule> filter, bool validate, IRuleContainer container, IEnumerable<RuleBookRule> rules, RuleBookRule[] allRules)
        {
            foreach (var rule in rules)
            {
                var treeNode = CreateRatingRule(container, rule);

                if (container is RuleEngineRule)
                    treeNode.Parent = (RuleEngineRule)container;
                treeNode.IsOverride = rule.IsOverride;
                treeNode.HasErrors = treeNode.Expression.HasErrors();
                treeNode.IsNew = rule.IsNew;
                treeNode.IsChange = rule.IsChange;
                // Check the rule for any errors
                if (validate && Validator != null)
                {
                    treeNode.ValidationErrors = Validator.ValidateRule(treeNode).ToArray();
                    if (!treeNode.HasErrors)
                        treeNode.HasErrors = treeNode.ValidationErrors.Length > 0;
                }

                RuleBookRule rule1 = rule;
                FillRules(filter, validate, treeNode, allRules.Where(p => p.ParentChangeId == rule1.ChangeId || rule1.BaseRules.Contains(p.ParentChangeId)).ToArray(), allRules);
                if (filter != null && filter(treeNode))
                {
                    container.Children.Add(treeNode);
                }
                else if (filter == null)
                {
                    container.Children.Add(treeNode);
                }
            }

            var children = container.Children.ToArray();
            // Find the exceptions
            var overrides = children.Where(p => p.RuleBookId > 0).ToArray().Select(p => p.Name);
            // Find the common ones to remove
            var remove = children.Where(p => overrides.Contains(p.Name) && p.RuleBookId == 0);
            // Remove any children with the same name
            foreach (var child in remove)
                container.Children.Remove(child);
        }
        public void SaveRule(RuleEngineRule ratingRule, bool forceNew = false)
        {
            var id = Convert.ToInt32(ratingRule.Id);
            var rule = Db.RuleBookRules.FirstOrDefault(p => p.Id == id && p.RuleBookId == ratingRule.RuleBookId);

            if (rule == null || rule.EffectiveDate < EffectiveDate || forceNew)
            {
                rule = new RuleBookRule
                           {
                               ChangeId = new Guid(ratingRule.ChangeId),
                               EffectiveDate = EffectiveDate
                           };

                Db.RuleBookRules.AddObject(rule);
            }

            rule.Context = ratingRule.LoopContext;
            rule.Name = ratingRule.Name;
            rule.RuleBookId = ratingRule.RuleBookId;
            rule.ParentChangeId = ratingRule.ParentChangeId;
            rule.RuleExpression = ratingRule.RuleExpression ?? "true";
            rule.Active = ratingRule.Active;
            Db.SaveChanges();
        }