Example #1
0
        public static async Task RunTestWithConfig(ILogger logger, string logFilePath, string configDir)
        {
            //string rootPath = configDir;
            string repositoryConfigPath     = $"{configDir}{Path.DirectorySeparatorChar}repository.xml.config";
            string machineSubscriptionsPath = $"{configDir}{Path.DirectorySeparatorChar}machines.xml.config";


            string _loggingPath = $"{logFilePath}{Path.DirectorySeparatorChar}RulesEngine{Path.DirectorySeparatorChar}";

            ILogger sublogger = new LoggerConfiguration()
                                .WriteTo.Async(a => a.Logger(logger))
                                .WriteTo.Async(a => a.File($"{_loggingPath}Log_{DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss")}.log").MinimumLevel.Verbose())
                                .MinimumLevel.Verbose()
                                .CreateLogger();

            sublogger.Information("{Class}.{Method} is Starting...", $"{nameof(RAL)}{nameof(RulesEngine)}{nameof(ConsoleTest)}{nameof(Program)}", nameof(RunTestNonConfig));

            RulesManager rulesManager = new RulesManager();

            var RepositoryConfig = new RepositoryFactory().LoadFrom(repositoryConfigPath).Config;


            var rulesFactory = new RulesFactory(sublogger)
                               .LoadFromFile(machineSubscriptionsPath)
                               .WithLogger(sublogger)
                               .WithRepository(
                new RepositoryForRules(
                    new MachineRepository(
                        RepositoryConfig.DatabaseIPaddress,
                        RepositoryConfig.DatabaseName,
                        Convert.ToInt32(RepositoryConfig.DatabasePort)
                        )
                    )
                );

            var rules = rulesFactory.Build();

            rules = rulesManager.Rules = rules;


            await rulesManager.StartAsync();


            sublogger.Information("{Class}.{Method} Waiting Indefinitely...", $"{nameof(RAL)}{nameof(RulesEngine)}{nameof(ConsoleTest)}{nameof(Program)}", nameof(RunTestNonConfig));
            await Task.Run(() => rulesManager.WaitForStop());

            //await rulesManager.StopAsync();
            sublogger.Information("{Class}.{Method} Closing...", $"{nameof(RAL)}{nameof(RulesEngine)}{nameof(ConsoleTest)}{nameof(Program)}", nameof(RunTestNonConfig));
        }
Example #2
0
        public async Task <bool> Save(Policy policy)
        {
            var type = await _policyTypeRepository.GetById(policy.PolicyTypeId);

            var rules = await _ruleRepository.GetAll();


            rules.Where(x => x.RiskTypeId == policy.RiskTypeId).ToList()
            .ForEach(r =>
            {
                var rule = RulesFactory.Build(r.Type, r.Value);

                var x = rule.Invoke(type.Coverage);

                if (x)
                {
                    throw new PolicyInvalidException($"{r.Type} {r.Value}");
                }
            });


            return(await _policyRepository.Save(policy));
        }