Exemple #1
0
        public void LoadDiscoverableRules(Assembly assembly)
        {
            if (assembly == null)
            {
                return;
            }

            //by default use calling assembly to discover
            var rulePairs = RuleDiscoveryProvider.DiscoverRules(new List <Assembly>()
            {
                assembly
            });

            if (rulePairs != null && rulePairs.Count() > 0)
            {
                foreach (var rulePair in rulePairs)
                {
                    if (rulePair.Item2.Enabled)
                    {
                        Rule r = ConstructRule(rulePair.Item1, false); //we want exceptions thrown during load because base Rule constructor validates input subclass info.
                        if (r != null)
                        {
                            AddRule(r);
                        }
                    }
                }
            }
        }
        private void LoadDiscoverableRules(Assembly assembly)
        {
            if (assembly == null)
            {
                return;
            }

            //by default use calling assembly to discover
            var rules = RuleDiscoveryProvider.DiscoverRules(new List <Assembly>()
            {
                assembly
            });

            if (rules != null && rules.Count() > 0)
            {
                var enabledRules = rules.Where(x => x.Enabled);
                foreach (var rule in enabledRules)
                {
                    AddRule(rule.Rule);
                }
            }
        }
        public void Initialize(RuleConfigurationSettings config)
        {
            if (!Initialized)
            {
                lock (this)
                {
                    if (!Initialized)
                    {
                        Debug.WriteLine("Starting: Initializing RulesEngine");
                        if (config != null && config.Enabled)
                        {
                            if (config.DiscoverRules)
                            {
                                if (!String.IsNullOrEmpty(config.DiscoverAssemblies))
                                {
                                    string[] assemblies = config.DiscoverAssemblies.Split(';');
                                    foreach (string assemblyName in assemblies)
                                    {
                                        try
                                        {
                                            var assembly = Assembly.Load(assemblyName);
                                            if (assembly != null)
                                            {
                                                LoadDiscoverableRules(assembly);
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            throw new RuleException(String.Format("Error loading aseembly or discoverable rules in assembly: {0}", assemblyName), ex);
                                        }
                                    }
                                }
                            }
                            //load rules specified in config file
                            if (config.RuleSet != null && config.RuleSet.Count > 0)
                            {
                                foreach (var ruleReference in config.RuleSet)
                                {
                                    if (ruleReference.Enabled)
                                    {
                                        Type t = Type.GetType(ruleReference.Type);
                                        if (t != null)
                                        {
                                            var ruleDescription = t.GetCustomAttribute <RuleDiscoveryAttribute>();
                                            var ruleInfo        = RuleDiscoveryProvider.Map(new DiscoveredType <RuleDiscoveryAttribute>()
                                            {
                                                Type = t, Attribute = ruleDescription
                                            });

                                            //force enabled from config
                                            ruleInfo.Enabled = ruleReference.Enabled;

                                            if (ruleInfo.Enabled)
                                            {
                                                AddRule(ruleInfo.Rule);
                                            }
                                        }
                                        else
                                        {
                                            throw new RuleException(String.Format("Can not load rule type. Type: {0}", ruleReference.Type));
                                        }
                                    }
                                }
                            }
                        }
                        Debug.WriteLine("Finished: Initializing RulesEngine");
                        Initialized = true;
                    }
                }
            }
        }