protected override COExpression <ClassificationObject> CreateFilteringExpression(ResourceStaticAnalysisRulesConfig.ValueList cultures, ResourceStaticAnalysisRulesConfig.ValueList projects, List <ResourceStaticAnalysisRulesConfig.FilteringExpression> filteringExpressions)
        {
            // This data structure does not support cultres and projects.
            List <COExpression <ClassificationObject> > filteringMethods = new List <COExpression <ClassificationObject> >();

            COExpression <LocResource> mergedFilter = lr => filteringMethods.TrueForAll(method => method(lr));

            return(ExpressionCasting <LocResource> .ExpressionCast(mergedFilter));
        }
 /// <summary>
 /// Creates an instance of a RuleContainer
 /// </summary>
 /// <param name="pathToFile">Path to rule file (.cs or .dll)</param>
 /// <param name="containerType">Type of container: compiled or source code.</param>
 /// <param name="filteringExpressionMethod">An optional filtering expression method.</param>
 public RuleContainer(string pathToFile, RuleContainerType containerType, COExpression <ClassificationObject> filteringExpressionMethod)
 {
     if (String.IsNullOrEmpty(pathToFile))
     {
         throw new ArgumentNullException("pathToFile");
     }
     this.PathToFile                = pathToFile;
     this.ContainerType             = containerType;
     this.FilteringExpressionMethod = filteringExpressionMethod;
 }
Beispiel #3
0
 private void AddOrReplaceFilteringExpression(COExpression <ClassificationObject> filteringExpression, bool replace)
 {
     if (replace)
     {
         this.filteringExpressionMethods.Clear();
     }
     if (filteringExpression != null)
     {
         this.filteringExpressionMethods.Add(filteringExpression);
     }
 }
Beispiel #4
0
        /// <summary>
        /// Perform checking and log result (MessageCreator constructor)
        /// </summary>
        /// <param name="exp">Lambda expression</param>
        /// <param name="mc">String append message creator</param>
        /// <param name="severity"></param>
        /// <returns></returns>
        protected internal bool _AgnosticCheckandLog(COExpression <ClassificationObject> exp, MessageCreator mc, CheckSeverity severity)
        {
            bool   result;
            string message;
            ExecutingRuleException ruleException = null;

            try
            {
                result  = exp(_currentCO);
                message = result ? mc.GetFullMessage() : mc.GetBaseMessage();
            }
            catch (Exception e)
            {
                if (e is OutOfMemoryException || e is StackOverflowException)
                {
                    throw;
                }
                result = false;
                try
                {
                    message = mc.GetBaseMessage();
                }
                catch
                {
                    message = String.Empty;
                    throw;
                }
                message = String.Format(CultureInfo.CurrentCulture, "Rule :{0}, Check() call: \"{1}\" threw an exception.",
                                        this.GetType().Name, message);
                ruleException = new ExecutingRuleException(message, e);
            }
            if (!String.IsNullOrEmpty(message))
            {
                message = Engine.ResourceStaticAnalysisEngine.StringCache.Intern(message);
            }
            else
            {
                message = String.Empty;
            }
            OutputItem oi = new OutputItem {
                Message = message, Result = result, Severity = severity
            };

            currentOutputEntry.AddItem(oi);
            if (ruleException != null)
            {
                throw ruleException;
            }
            return(oi.Result);
        }
        /// <summary>
        /// Identifies any public classes looking like Rule implementation, calls their appropriate constructors,
        /// and registers the constructed objects with this RuleManager.
        /// </summary>
        /// <param name="ruleModule">Either the module loaded from a rule.dll file on disk, or an Assembly compiled from the source code</param>
        /// <param name="filteringExpression">Filtering expression to be applied to objects before processing. Empty string means all objects should be
        /// processed - no filtering.</param>
        /// <param name="disabledRules">Collection of rules within the ruleModule that should be disabled (not run).</param>
        /// <param name="workingFolder">Working folder for the rule, where it should pickup any external configuration or required files for executing.</param>
        private void AddRulesFromAssembly(Assembly ruleModule, string filteringExpression, IEnumerable <string> disabledRules, string workingFolder, IEnumerable <string> enabledRules)
        {
            if (enabledRules != null && enabledRules.Any() && disabledRules != null && disabledRules.Any())
            {
                throw new RuleContainerException("EnabledRules and DisabledRules cannot co-exist in a RuleContainer");
            }

            foreach (var newRule in ruleModule.GetExportedTypes().Where(r => r.IsClass && r.IsSubclassOf(typeof(Rule)) && !r.IsAbstract &&
                                                                        (disabledRules == null || !disabledRules.Contains(r.FullName)) &&
                                                                        ((enabledRules == null || !enabledRules.Any()) || enabledRules.Contains(r.FullName))))
            {
                // create instance of rule
                var rule = (Rule)Activator.CreateInstance(newRule, new object[] { this });
                // check if the rule is applicable to this instance of the Engine
                bool knownCoType = false;
                foreach (var coType in _owningEngine._coTypes)
                {
                    if (rule.TypeOfClassificationObject.Equals(coType.Value))
                    {
                        knownCoType = true;
                        break;
                    }
                }

                if (knownCoType)
                {
                    COExpression <ClassificationObject> filteringMethod = null;
                    if (false == String.IsNullOrEmpty(filteringExpression))
                    {
                        filteringMethod = GetFilteringMethod(filteringExpression, rule);
                    }
                    if (filteringMethod != null)
                    {
                        rule.AddFilteringExpression(filteringMethod);
                    }

                    if (!string.IsNullOrWhiteSpace(workingFolder))
                    {
                        rule.WorkingFolder = workingFolder;
                    }

                    AddRule(rule);
                    Trace.TraceInformation("Adding rule to RuleManager: {0}", rule);
                }
                else
                {
                    Trace.TraceInformation("Rule {0} does not support any ClassificationObject types registered with the engine", rule);
                }
            }
        }
Beispiel #6
0
            /// <summary>
            /// Compile the string expression into a COExpression object.
            /// </summary>
            /// <param name="expression">String value of the expression to compile.</param>
            /// <param name="compiledExpression">Compiled COExpression object.</param>
            /// <param name="exception">The exception that occurred when compiling the expression.</param>
            /// <returns>A value indicating if the expression compiled successfully.</returns>
            public static bool TryCompile <T>(String expression, out COExpression <ClassificationObject> compiledExpression, out Exception exception)
                where T : ClassificationObject
            {
                compiledExpression = null;
                exception          = null;

                try
                {
                    compiledExpression = FilteringExpression.Compile <T>(expression);
                    return(true);
                }
                catch (Exception e)
                {
                    exception = e;
                    return(false);
                }
            }
Beispiel #7
0
            /// <summary>
            /// Compile the string expression into a COExpression object.
            /// </summary>
            /// <param name="expression">String value of the expression to compile.</param>
            /// <param name="compiledExpression"></param>
            /// <returns>A value indicating if the expression compiled successfully.</returns>
            public static bool TryCompile <T>(String expression, out COExpression <ClassificationObject> compiledExpression)
                where T : ClassificationObject
            {
                Exception exception;

                if (!FilteringExpression.TryCompile <T>(expression, out compiledExpression, out exception))
                {
                    if (exception != null)
                    {
                        throw new Exception(String.Format("Failed to compile the Filtering Expression for the string value '{0}'. Reason: {1}",
                                                          expression, (exception.InnerException ?? exception).Message), exception);
                    }

                    return(false);
                }

                return(true);
            }
Beispiel #8
0
 public bool Check(COExpression <LocResource> exp, string message, CheckSeverity severity)
 {
     return(Check(exp, ref message, severity));
 }
 public bool Check(COExpression <IncompatibleObject> exp, MessageCreator mc)
 {
     return(base._AgnosticCheckandLog(ExpressionCaster(exp), mc, CheckSeverity.Normal));
 }
 public bool Check(COExpression <IncompatibleObject> exp, ref string message)
 {
     return(_AgnosticCheckandLog(this.ExpressionCaster(exp), ref message, CheckSeverity.Normal));
 }
Beispiel #11
0
 public bool Check(COExpression <SampleClassificationObjectType> exp, MessageCreator mc)
 {
     return(base._AgnosticCheckandLog(ExpressionCaster(exp), mc, CheckSeverity.Normal));
 }
Beispiel #12
0
 public bool Check(COExpression <SampleClassificationObjectType> exp, ref string message)
 {
     return(_AgnosticCheckandLog(this.ExpressionCaster(exp), ref message, CheckSeverity.Normal));
 }
Beispiel #13
0
 /// <summary>
 /// Call this method from within your Run() method.
 /// Each call to Check() produces one entry in the log.
 /// Use your customized message creator in order to build a dynamic message based on the execution of exp.
 /// </summary>
 /// <param name="exp"></param>
 /// <param name="mc">Message creator is executed after exp executes. This gives you a chance to modify the content of the message in the exp by
 /// passing context information into mc and creating custom logic in your MC implementation of GetMessage().</param>
 /// <param name="severity">Specify the severity of the check.</param>
 /// <returns></returns>
 public bool Check(COExpression <LocResource> exp, MessageCreator mc, CheckSeverity severity)
 {
     return(base._AgnosticCheckandLog(ExpressionCaster(exp), mc, severity));
 }
Beispiel #14
0
 /// <summary>
 /// Call this method from within your Run() method.
 /// Each call to Check() produces one entry in the log.
 /// </summary>
 /// <param name="exp">Your rule's logic in form of a lambda expression</param>
 /// <param name="message">Message to be written out to log. Passed as value, so it cannot be modified by exp.</param>
 /// <returns></returns>
 public bool Check(COExpression <LocResource> exp, string message)
 {
     return(Check(exp, ref message));
 }
Beispiel #15
0
 public bool Check(COExpression <LocResource> exp, ref string message, CheckSeverity severity)
 {
     return(base._AgnosticCheckandLog(this.ExpressionCaster(exp), ref message, severity));
 }
Beispiel #16
0
 /// <summary>
 /// Compiles the filtering expression into a function and replaces any existing filetring expression.
 /// This can be used in a constructor in order to replace filtering conditions for the rule.
 /// </summary>
 /// <param name="filteringExpression">A lambda filtering expression valid for the type of Classification Object supported by the rule.</param>
 public void SetFilteringExpression(COExpression <ClassificationObject> filteringExpression)
 {
     AddOrReplaceFilteringExpression(filteringExpression, true);
 }
Beispiel #17
0
 /// <summary>
 /// Compiles the filtering expression into a function and merges this function with any existing filetring expression
 /// using AND operator. This can be used in a constructor in order to add any filtering conditions in addition to existing ones.
 /// </summary>
 /// <param name="filteringExpression">A lambda filtering expression valid for the type of Classification Object supported by the rule.</param>
 public void AddFilteringExpression(COExpression <ClassificationObject> filteringExpression)
 {
     AddOrReplaceFilteringExpression(filteringExpression, false);
 }