/// <summary>
        /// Load the rules engine from the Persistent Store and compile the conditions and actions.
        /// </summary>
        public void Load(XmlDocument theRuleXml, TestEnum test)
        {
            // Clearout the current type list.
            _typeList.Clear();

            try
            {
                var theRule = new Rule <TestContext>
                {
                    Name     = "TEST", IsDefault = false,
                    IsExempt = false, Description = "TEST"
                };

                var ruleNode =
                    CollectionUtils.SelectFirst <XmlNode>(theRuleXml.ChildNodes,
                                                          delegate(XmlNode child) { return(child.Name.Equals("rule")); });

                var specCompiler   = GetSpecificationCompiler();
                var actionCompiler = GetActionCompiler(test);
                theRule.Compile(ruleNode, specCompiler, actionCompiler);

                RuleTypeCollection <TestContext, TestEnum> typeCollection;

                if (!_typeList.ContainsKey(test))
                {
                    typeCollection = new RuleTypeCollection <TestContext, TestEnum>(test);
                    _typeList.Add(test, typeCollection);
                }
                else
                {
                    typeCollection = _typeList[test];
                }

                typeCollection.AddRule(theRule);
            }
            catch (Exception e)
            {
                // something wrong with the rule...
                Platform.Log(LogLevel.Warn, e, "Unable to add rule to the engine. It will be skipped");
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Load the rules engine from the Persistent Store and compile the conditions and actions.
        /// </summary>
        public void Load()
        {
            Statistics.LoadTime.Start();

            // Clearout the current type list.
            _typeList.Clear();

            using (IReadContext read = PersistentStoreRegistry.GetDefaultStore().OpenReadContext())
            {
                IServerRuleEntityBroker broker = read.GetBroker <IServerRuleEntityBroker>();

                ServerRuleSelectCriteria criteria = new ServerRuleSelectCriteria();
                criteria.Enabled.EqualTo(true);
                criteria.ServerRuleApplyTimeEnum.EqualTo(_applyTime);
                criteria.ServerPartitionKey.EqualTo(_serverPartitionKey);

                // Add ommitted or included rule types, as appropriate
                if (_omitList.Count > 0)
                {
                    criteria.ServerRuleTypeEnum.NotIn(_omitList.ToArray());
                }
                else if (_includeList.Count > 0)
                {
                    criteria.ServerRuleTypeEnum.In(_includeList.ToArray());
                }

                IList <ServerRule> list = broker.Find(criteria);

                // Create the specification and action compilers
                // We'll compile the rules right away
                var specCompiler = GetSpecificationCompiler();

                foreach (ServerRule serverRule in list)
                {
                    try
                    {
                        var theRule = new Rule <ServerActionContext>();
                        theRule.Name        = serverRule.RuleName;
                        theRule.IsDefault   = serverRule.DefaultRule;
                        theRule.IsExempt    = serverRule.ExemptRule;
                        theRule.Description = serverRule.ServerRuleApplyTimeEnum.Description;

                        XmlNode ruleNode =
                            CollectionUtils.SelectFirst <XmlNode>(serverRule.RuleXml.ChildNodes,
                                                                  delegate(XmlNode child) { return(child.Name.Equals("rule")); });


                        var actionCompiler = GetActionCompiler(serverRule.ServerRuleTypeEnum);
                        theRule.Compile(ruleNode, specCompiler, actionCompiler);

                        RuleTypeCollection <ServerActionContext, ServerRuleTypeEnum> typeCollection;

                        if (!_typeList.ContainsKey(serverRule.ServerRuleTypeEnum))
                        {
                            typeCollection = new RuleTypeCollection <ServerActionContext, ServerRuleTypeEnum>(serverRule.ServerRuleTypeEnum);
                            _typeList.Add(serverRule.ServerRuleTypeEnum, typeCollection);
                        }
                        else
                        {
                            typeCollection = _typeList[serverRule.ServerRuleTypeEnum];
                        }

                        typeCollection.AddRule(theRule);
                    }
                    catch (Exception e)
                    {
                        // something wrong with the rule...
                        Platform.Log(LogLevel.Warn, e, "Unable to add rule {0} to the engine. It will be skipped",
                                     serverRule.RuleName);
                    }
                }
            }

            Statistics.LoadTime.End();
        }