Example #1
0
        /// <summary>
        /// Method for validating proper format of a ServerRule.
        /// </summary>
        /// <param name="type">The type of rule to validate</param>
        /// <param name="rule">The rule to validate</param>
        /// <param name="errorDescription">A failure description on error.</param>
        /// <returns>true on successful validation, otherwise false.</returns>
        public static bool ValidateRule(TTypeEnum type, XmlDocument rule, out string errorDescription)
        {
            var specCompiler   = new XmlSpecificationCompiler("dicom");
            var actionCompiler = new XmlActionCompiler <TActionContext, TTypeEnum>();


            var theRule = new Rule <TActionContext, TTypeEnum>
            {
                Name = string.Empty
            };

            var ruleNode =
                CollectionUtils.SelectFirst(rule.ChildNodes,
                                            (XmlNode child) => child.Name.Equals("rule"));


            try
            {
                theRule.Compile(ruleNode, type, specCompiler, actionCompiler);
            }
            catch (Exception e)
            {
                errorDescription = e.Message;
                return(false);
            }

            errorDescription = "Success";
            return(true);
        }
		private static ValidationRuleSet CompileRuleset(XmlElement rulesetNode)
		{
			var compiler = new XmlSpecificationCompiler(ScriptLanguage);
			var rules = CollectionUtils.Map<XmlElement, ISpecification>(
				rulesetNode.GetElementsByTagName(TagValidationRule), compiler.Compile);

			var applicabilityRuleNode = CollectionUtils.FirstElement(rulesetNode.GetElementsByTagName(TagApplicabililtyRule));
			return applicabilityRuleNode != null ?
				new ValidationRuleSet(rules, compiler.Compile((XmlElement)applicabilityRuleNode))
				: new ValidationRuleSet(rules);
		}
Example #3
0
        private static ValidationRuleSet CompileRuleset(XmlElement rulesetNode)
        {
            var compiler = new XmlSpecificationCompiler(ScriptLanguage);
            var rules    = CollectionUtils.Map <XmlElement, ISpecification>(
                rulesetNode.GetElementsByTagName(TagValidationRule), compiler.Compile);

            var applicabilityRuleNode = CollectionUtils.FirstElement(rulesetNode.GetElementsByTagName(TagApplicabililtyRule));

            return(applicabilityRuleNode != null ?
                   new ValidationRuleSet(rules, compiler.Compile((XmlElement)applicabilityRuleNode))
                                : new ValidationRuleSet(rules));
        }
Example #4
0
        /// <summary>
        /// Compile the rule.
        /// </summary>
        /// <param name="ruleNode">XML representation of the rule.</param>
        /// <param name="ruleType">The type of the rule.</param>
        /// <param name="specCompiler">An <see cref="XmlSpecificationCompiler"/>.</param>
        /// <param name="actionCompiler">An <see cref="XmlActionCompiler{TActionContext,TTypeEnum}"/>.</param>
        public void Compile(XmlNode ruleNode, TTypeEnum ruleType, XmlSpecificationCompiler specCompiler,
                            XmlActionCompiler <TActionContext, TTypeEnum> actionCompiler)
        {
            var conditionNode =
                CollectionUtils.SelectFirst(ruleNode.ChildNodes,
                                            (XmlNode child) => child.Name.Equals("condition"));

            if (conditionNode != null)
            {
                _conditions = specCompiler.Compile(conditionNode as XmlElement, true);
            }
            else if (!IsDefault)
            {
                throw new ApplicationException("No condition element defined for the rule.");
            }
            else
            {
                _conditions = new AndSpecification();
            }

            var actionNode =
                CollectionUtils.SelectFirst(ruleNode.ChildNodes,
                                            (XmlNode child) => child.Name.Equals("action"));

            if (actionNode != null)
            {
                _actions = actionCompiler.Compile(actionNode as XmlElement, ruleType, true);
            }
            else if (!IsExempt)
            {
                throw new ApplicationException("No action element defined for the rule.");
            }
            else
            {
                _actions = new ActionSet <TActionContext>(new List <IActionItem <TActionContext> >());
            }
        }
 /// <summary>
 /// Constructor that allows specifying the expression language to use when evaluating the XML specification.
 /// </summary>
 /// <param name="defaultExpressionLanguage"></param>
 public XmlValidationCompiler(string defaultExpressionLanguage)
 {
     _specCompiler = new XmlSpecificationCompiler(defaultExpressionLanguage);
 }
Example #6
0
 public SpecificationFactory(ISpecificationXmlSource xmlSource)
 {
     _builder = new XmlSpecificationCompiler(this, xmlSource.DefaultExpressionLanguage);
     _cache = new Dictionary<string, ISpecification>();
     _xmlSource = xmlSource;
 }
Example #7
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
                XmlSpecificationCompiler specCompiler = new XmlSpecificationCompiler("dicom");
                XmlActionCompiler <ServerActionContext, ServerRuleTypeEnum> actionCompiler = new XmlActionCompiler <ServerActionContext, ServerRuleTypeEnum>();

                foreach (ServerRule serverRule in list)
                {
                    try
                    {
                        Rule <ServerActionContext, ServerRuleTypeEnum> theRule = new Rule <ServerActionContext, ServerRuleTypeEnum>();
                        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")); });


                        theRule.Compile(ruleNode, serverRule.ServerRuleTypeEnum, 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();
        }
 /// <summary>
 /// Constructor that allows specifying the expression language to use when evaluating the XML specification.
 /// </summary>
 /// <param name="defaultExpressionLanguage"></param>
 public XmlValidationCompiler(string defaultExpressionLanguage)
 {
     _specCompiler = new XmlSpecificationCompiler(defaultExpressionLanguage);
 }