public void GetMatchingRules_CompositePropertyMatch_RuleReturned() { // Arrange var engine = new RulesService <TestModel>(new RulesCompiler(), new LazyCache.Mocks.MockCachingService()); // Act var matching = engine.GetMatchingRules( new TestModel { Composit = new TestModel.CompositeInnerClass { NumericField = 10 } }, new[] { new RulesConfig { Id = Guid.NewGuid(), RulesOperator = Rule.InterRuleOperatorType.And, RulesGroups = new RulesGroup[] { new RulesGroup { RulesOperator = Rule.InterRuleOperatorType.Or, Rules = new[] { new Rule { ComparisonOperator = Rule.ComparisonOperatorType.GreaterThanOrEqual, ComparisonValue = 4.ToString(), ComparisonPredicate = $"{nameof(TestModel.Composit)}.{nameof(TestModel.Composit.NumericField)}" } } } } } }); // Assert Assert.Single(matching.Data); }
public void GetMatchingRules_MultiRuleOrMatch_RuleReturned() { // Arrange var engine = new RulesService <TestModel>(new RulesCompiler(), new LazyCache.Mocks.MockCachingService()); // Act var matching = engine.GetMatchingRules( new TestModel { TextField = "SomePrefixBlahBlah", NumericField = 10 }, new[] { new RulesConfig { Id = Guid.NewGuid(), RulesOperator = Rule.InterRuleOperatorType.And, RulesGroups = new RulesGroup[] { new RulesGroup { RulesOperator = Rule.InterRuleOperatorType.Or, Rules = new[] { new Rule { ComparisonOperator = Rule.ComparisonOperatorType.StringStartsWith, ComparisonValue = "NOT MATCHING PREFIX", ComparisonPredicate = nameof(TestModel.TextField) }, new Rule { ComparisonOperator = Rule.ComparisonOperatorType.GreaterThan, ComparisonValue = 4.ToString(), ComparisonPredicate = nameof(TestModel.NumericField) } } } } } }); // Assert Assert.Single(matching.Data); }
public void GetMatchingRules_KeyValueCollectionNotMatch_RuleReturned() { // Arrange var engine = new RulesService <TestModel>(new RulesCompiler(), new LazyCache.Mocks.MockCachingService()); // Act var matching = engine.GetMatchingRules( new TestModel { KeyValueCollection = new Dictionary <string, object> { { "DateOfBirth", DateTime.Now } } }, new[] { new RulesConfig { Id = Guid.NewGuid(), RulesOperator = Rule.InterRuleOperatorType.And, RulesGroups = new RulesGroup[] { new RulesGroup { RulesOperator = Rule.InterRuleOperatorType.Or, Rules = new[] { // PredicateType is needed here to be able to determine value type which is string new Rule { ComparisonOperator = Rule.ComparisonOperatorType.LessThan, ComparisonValue = DateTime.Now.AddSeconds(-2).ToString("U"), ComparisonPredicate = $"{nameof(TestModel.KeyValueCollection)}[DateOfBirth]", PredicateType = TypeCode.DateTime } } } } } }); // Assert Assert.Empty(matching.Data); }
public void GetMatchingRules_CaluculatedCollectionNotContainsAnyOfNotMatch_RuleNotReturned() { // Arrange var engine = new RulesService <TestModel>(new RulesCompiler(), new LazyCache.Mocks.MockCachingService()); // Act var matching = engine.GetMatchingRules( new TestModel { CompositeCollection = new List <TestModel.CompositeInnerClass> { new TestModel.CompositeInnerClass { NumericField = 10 } } }, new[] { new RulesConfig { Id = Guid.NewGuid(), RulesOperator = Rule.InterRuleOperatorType.And, RulesGroups = new RulesGroup[] { new RulesGroup { RulesOperator = Rule.InterRuleOperatorType.Or, Rules = new[] { new Rule { ComparisonOperator = Rule.ComparisonOperatorType.CollectionNotContainsAnyOf, ComparisonValue = "10|11|12", ComparisonPredicate = $"{nameof(TestModel.CaluculatedCollection)}" } } } } } }); // Assert Assert.Empty(matching.Data); }
public void GetMatchingRules_PrimitiveNotInCollectionMatch_RuleReturned() { // Arrange var engine = new RulesService <TestModel>(new RulesCompiler(), new LazyCache.Mocks.MockCachingService()); // Act var matching = engine.GetMatchingRules( new TestModel { NumericField = 10 }, new[] { new RulesConfig { Id = Guid.NewGuid(), RulesOperator = Rule.InterRuleOperatorType.And, RulesGroups = new RulesGroup[] { new RulesGroup { RulesOperator = Rule.InterRuleOperatorType.Or, Rules = new[] { new Rule { ComparisonOperator = Rule.ComparisonOperatorType.In, ComparisonValue = "1|2|3|4|5", ComparisonPredicate = $"{nameof(TestModel.NumericField)}" } } } } } }); // Assert Assert.Empty(matching.Data); }
public void GetMatchingRules_EnumValueMatch_RuleReturned() { // Arrange var engine = new RulesService <TestModel>(new RulesCompiler(), new LazyCache.Mocks.MockCachingService()); // Act var matching = engine.GetMatchingRules( new TestModel { SomeEnumValue = TestModel.SomeEnum.Yes }, new[] { new RulesConfig { Id = Guid.NewGuid(), RulesOperator = Rule.InterRuleOperatorType.And, RulesGroups = new RulesGroup[] { new RulesGroup { RulesOperator = Rule.InterRuleOperatorType.Or, Rules = new[] { new Rule { ComparisonOperator = Rule.ComparisonOperatorType.Equal, ComparisonValue = TestModel.SomeEnum.Yes.ToString(), ComparisonPredicate = $"{nameof(TestModel.SomeEnumValue)}" } } } } } }); // Assert Assert.Single(matching.Data); }
public void GetMatchingRulesPrimitiveCollectionNoMatch_RuleNotReturned() { // Arrange var engine = new RulesService <TestModel>(new RulesCompiler(), new LazyCache.Mocks.MockCachingService()); // Act var matching = engine.GetMatchingRules( new TestModel { PrimitivesCollection = new List <int> { 1, 2, 4, 5 } }, new[] { new RulesConfig { Id = Guid.NewGuid(), RulesOperator = Rule.InterRuleOperatorType.And, RulesGroups = new RulesGroup[] { new RulesGroup { RulesOperator = Rule.InterRuleOperatorType.Or, Rules = new[] { new Rule { ComparisonOperator = Rule.ComparisonOperatorType.CollectionContainsAll, ComparisonValue = "1|2|10", ComparisonPredicate = $"{nameof(TestModel.PrimitivesCollection)}" } } } } } }); // Assert Assert.Empty(matching.Data); }
public void GetMatchingRules_NumericValueNotMatch_RuleNotReturned() { // Arrange IRulesService <TestModel> engine = RulesService <TestModel> .CreateDefault(); // Act var numericValueTest = 5; var numericValueOtherValue = 6; var matching = engine.GetMatchingRules( new TestModel { NumericField = numericValueTest }, new[] { new RulesConfig { Id = Guid.NewGuid(), RulesOperator = Rule.InterRuleOperatorType.And, RulesGroups = new RulesGroup[] { new RulesGroup { RulesOperator = Rule.InterRuleOperatorType.And, Rules = new[] { new Rule { ComparisonOperator = Rule.ComparisonOperatorType.Equal, ComparisonValue = numericValueOtherValue.ToString(), ComparisonPredicate = nameof(TestModel.NumericField) } } } } } }); // Assert Assert.Empty(matching.Data); }
static void Main(string[] args) { IDiceService diceService = new DiceService(); IRulesService rulesService = new RulesService(diceService); var rules = rulesService.GetRules(ERules.Percentage); IStrategyService strategyService = new StrategyService(rules); IVillage village = new Village(diceService, strategyService, rules); IList <ICharacter> fighters = new List <ICharacter>(); for (int i = 0; i < 200; i++) { var character = village.GetFighter(EFighterClass.Warrior); fighters.Add(character); } for (int i = 0; i < 10; i++) { var character = village.GetFighter(EFighterClass.Ranger); fighters.Add(character); } IArena arena = new ClassicArena(fighters); arena.StartFight(); }
public void IsLinguisticVariableOutputExcpetionTest() { RulesService.Clear(); LinguisticVariableService.Clear(); LinguisticVariable inputVariable1 = new LinguisticVariable("var1", 1, 10); LinguisticVariable inputVariable2 = new LinguisticVariable("var2", 1, 10); LinguisticVariable inputVariable3 = new LinguisticVariable("var3", 1, 10); LinguisticVariable outputVariable = new LinguisticVariable("outputVar", 1, 10); Term term1 = TermsFactory.Instance.CreateTermForVariable("term1", inputVariable1, new TrapezoidalFunction()); Term term2 = TermsFactory.Instance.CreateTermForVariable("term2", inputVariable2, new TrapezoidalFunction()); Term term3 = TermsFactory.Instance.CreateTermForVariable("term3", inputVariable3, new TrapezoidalFunction()); Term outputTerm = TermsFactory.Instance.CreateTermForVariable("outputTerm", outputVariable, new TrapezoidalFunction()); LinguisticVariableService.Instance.Add(inputVariable1, LinguisticVariableType.Input); LinguisticVariableService.Instance.Add(inputVariable2, LinguisticVariableType.Input); LinguisticVariableService.Instance.Add(inputVariable3, LinguisticVariableType.Input); LinguisticVariableService.Instance.Add(outputVariable, LinguisticVariableType.Input); // TODO : Variable input/output checking RuleBuilder builder = RuleBuilder.CreateBuilder(); Rule rule = builder .Conditions() .ConditionsOperation(OperationType.And) .Add(ConditionSign.Negation, term1) .Add(ConditionSign.Identity, term2) .Add(ConditionSign.Negation, term3) .And() .OutputTerm(outputTerm) .Build(); }
private void Initialize() { serviceProvider = new ServiceCollection() .AddLogging ( loggingBuilder => { loggingBuilder.ClearProviders(); loggingBuilder.Services.AddSingleton <ILoggerProvider> ( serviceProvider => new XUnitLoggerProvider(this.output) ); loggingBuilder.AddFilter <XUnitLoggerProvider>("*", LogLevel.None); loggingBuilder.AddFilter <XUnitLoggerProvider>("Contoso.Test.Flow", LogLevel.Trace); } ) .AddTransient <IFlowManager, FlowManager>() .AddTransient <FlowActivityFactory, FlowActivityFactory>() .AddTransient <DirectorFactory, DirectorFactory>() .AddTransient <ICustomActions, CustomActions>() .AddSingleton <FlowDataCache, FlowDataCache>() .AddSingleton <Progress, Progress>() .AddSingleton <IRulesCache>(sp => { return(RulesService.LoadRulesSync(new RulesLoader())); }) .BuildServiceProvider(); }
public void GetMatchingRules_StringStartsWithMatch_RuleReturned() { // Arrange var engine = new RulesService <TestModel>(new RulesCompiler(), new LazyCache.Mocks.MockCachingService()); // Act var matching = engine.GetMatchingRules( new TestModel { TextField = "SomePrefixBlahBlah" }, new[] { new RulesConfig { Id = Guid.NewGuid(), RulesOperator = Rule.InterRuleOperatorType.And, RulesGroups = new RulesGroup[] { new RulesGroup { RulesOperator = Rule.InterRuleOperatorType.And, Rules = new[] { new Rule { ComparisonOperator = Rule.ComparisonOperatorType.StringStartsWith, ComparisonValue = "someprefix", ComparisonPredicate = nameof(TestModel.TextField) } } } } } }); // Assert Assert.Single(matching.Data); }
public RulesProcessor( ILogger <RulesProcessor> logger, ChatService chatService, RulesService rulesService ) { _logger = logger; _chatService = chatService; _rulesService = rulesService; }
public Globals(IDiscordClient client, IGuild guild, IMongoDatabase database, SendingService sender, RulesService rulesService, ReputationService reputationService) { Client = client; Guild = guild; Database = database; Sender = sender; RulesService = rulesService; ReputationService = reputationService; }
public RulesCommand( RulesService rulesService, SettingsService settingsService, TelegramService telegramService, PrivilegeService privilegeService ) { _rulesService = rulesService; _settingsService = settingsService; _telegramService = telegramService; _privilegeService = privilegeService; }
public void TotalDiscount_should_be_0_for_employee_name_not_starting_with_the_letter_A() { var employee = _employees[2]; var quoteParam = _quoteParameters[2]; var mockRuleRepository = new Mock <IRuleRepository>(); mockRuleRepository.Setup(x => x.GetAll()).Returns(_discountRules); IRulesService rulesService = new RulesService(mockRuleRepository.Object); IRuleEvaluator service = new DiscountCalculator(ruleAccessor); var quote = service.GetQuote(employee, quoteParam.Dependents); Assert.True(quote.TotalDiscounts == 0); }
public void GetMatchingRules_MultiRulesAllMatch_AllRulesReturned() { // Arrange var engine = new RulesService <TestModel>(new RulesCompiler(), new LazyCache.Mocks.MockCachingService()); // Act var matching = engine.GetMatchingRules( new TestModel { NumericField = 10, TextField = "test1" }, new[] { new RulesConfig { Id = Guid.NewGuid(), RulesOperator = Rule.InterRuleOperatorType.And, RulesGroups = new RulesGroup[] { new RulesGroup { RulesOperator = Rule.InterRuleOperatorType.Or, Rules = new[] { new Rule { ComparisonOperator = Rule.ComparisonOperatorType.NotIn, ComparisonValue = "1|2|3|4|5", ComparisonPredicate = $"{nameof(TestModel.NumericField)}" } } } } }, new RulesConfig { Id = Guid.NewGuid(), RulesOperator = Rule.InterRuleOperatorType.And, RulesGroups = new RulesGroup[] { new RulesGroup { RulesOperator = Rule.InterRuleOperatorType.Or, Rules = new[] { new Rule { ComparisonOperator = Rule.ComparisonOperatorType.StringEndsWith, ComparisonValue = "1", ComparisonPredicate = $"{nameof(TestModel.TextField)}" } } } } } }); // Assert Assert.Equal(2, matching.Data.Count()); }
private static Tuple <bool, Rules> GetRulesTuple() { Rules rules = null; var ruleTuple = new Tuple <bool, Rules>(false, rules); var rulesFilePath = ApplicationSettingsUtility.GetRulesFilePath(); if (string.IsNullOrEmpty(rulesFilePath)) { rulesFilePath = @"Input\Rules.xml"; } if (!File.Exists(rulesFilePath)) { Console.WriteLine("Rules.xml is not present"); return(ruleTuple); } try { rules = XMLService.ReadRules(rulesFilePath); if (!ValidatorService.ValidateInputRulesForExclusivity(rules)) { Console.WriteLine("Given input rules are either not proper or Contradicting. Please check."); return(ruleTuple); } } catch (Exception ex) { Console.WriteLine("There's come problem with Rules.xml. \n" + ex.Message); return(ruleTuple); } try { RulesService.CheckContradictingInputValues(rules); } catch (Exception ex) { Console.WriteLine($"Error in given rules : {ex.Message}"); return(ruleTuple); } return(new Tuple <bool, Rules>(true, rules)); }
public void RuleTest() { RulesService.Clear(); LinguisticVariableService.Clear(); LinguisticVariable inputVariable1 = new LinguisticVariable("var1", 1, 10); LinguisticVariable inputVariable2 = new LinguisticVariable("var2", 1, 10); LinguisticVariable inputVariable3 = new LinguisticVariable("var3", 1, 10); LinguisticVariable outputVariable = new LinguisticVariable("outputVar", 1, 10); Term term1 = TermsFactory.Instance.CreateTermForVariable("term1", inputVariable1, new TrapezoidalFunction()); Term term2 = TermsFactory.Instance.CreateTermForVariable("term2", inputVariable2, new TrapezoidalFunction()); Term term3 = TermsFactory.Instance.CreateTermForVariable("term3", inputVariable3, new TrapezoidalFunction()); Term outputTerm = TermsFactory.Instance.CreateTermForVariable("outputTerm", outputVariable, new TrapezoidalFunction()); LinguisticVariableService.Instance.Add(inputVariable1, LinguisticVariableType.Input); LinguisticVariableService.Instance.Add(inputVariable2, LinguisticVariableType.Input); LinguisticVariableService.Instance.Add(inputVariable3, LinguisticVariableType.Input); LinguisticVariableService.Instance.Add(outputVariable, LinguisticVariableType.Output); // TODO : Variable input/output checking RuleBuilder builder = RuleBuilder.CreateBuilder(); Rule rule = builder .Conditions() .ConditionsOperation(OperationType.And) .Add(ConditionSign.Negation, term1) .Add(ConditionSign.Identity, term2) .Add(ConditionSign.Negation, term3) .And() .OutputTerm(outputTerm) .Build(); string ruleString = rule.Stringify; Assert.AreEqual(ruleString, "If (var1 is not term1) and (var2 is term2) and (var3 is not term3) then outputVar is outputTerm"); }
public void GetMatchingRules_NumericValueMatch_ShouldMatchByOperatorAndValue(int ruleVal, Rule.ComparisonOperatorType op, int objectVal, bool shouldMatch) { // Arrange var engine = new RulesService <TestModel>(new RulesCompiler(), new LazyCache.Mocks.MockCachingService()); // Act var numericValueTest = objectVal; var matching = engine.GetMatchingRules( new TestModel { NumericField = numericValueTest }, new[] { new RulesConfig { Id = Guid.NewGuid(), RulesOperator = Rule.InterRuleOperatorType.And, RulesGroups = new RulesGroup[] { new RulesGroup { RulesOperator = Rule.InterRuleOperatorType.And, Rules = new[] { new Rule { ComparisonOperator = op, ComparisonValue = ruleVal.ToString(), ComparisonPredicate = nameof(TestModel.NumericField) } } } } } }); // Assert if (shouldMatch) { Assert.Single(matching.Data); } else { Assert.Empty(matching.Data); } }
private static Tuple <bool, List <InputDataUnit> > GetViolationsTuple(string inputFilePath, Rules rules) { var violationTuple = new Tuple <bool, List <InputDataUnit> >(false, null); List <InputDataUnit> violatedOutputRules = new List <InputDataUnit>(); try { foreach (var inputData in JSONService.ReadInputData(inputFilePath)) { if (!RulesService.IsValidRule(inputData, rules)) { Console.WriteLine($"{inputData.Signal}\t :\t {inputData.ValueType}\t :\t {inputData.Value}"); violatedOutputRules.Add(inputData); } } } catch (Exception ex) { Console.WriteLine("Error in raw_data.json file. " + ex.Message); return(violationTuple); } return(new Tuple <bool, List <InputDataUnit> >(true, violatedOutputRules)); }
public void Arrange() { _rule = new Rule { Id = 123, ActiveFrom = DateTime.UtcNow, ActiveTo = DateTime.UtcNow.AddMonths(6), CourseId = "3", CreatedDate = DateTime.UtcNow.AddMonths(-1), Restriction = 0, Course = new Course { CourseId = "2-123-3", Level = 1, Title = "Test Course" } }; _ruleRepository = new Mock <IRuleRepository>(); _ruleRepository.Setup(x => x.GetReservationRules(It.IsAny <DateTime>())).ReturnsAsync(new List <Rule> { _rule }); _service = new RulesService(_ruleRepository.Object); }
public EntityUnitOfWork(DbContext context, RulesService rules) { _context = context; _rules = rules; }
public void TestInit() { _attributes = new InMemoryEntityRepository <Campaigns.Model.Attribute>(); _contributions = new InMemoryEntityRepository <Campaigns.Model.AttributeContribution>(); _contributions.AddForeignStore(_attributes); _characterSheets = new InMemoryEntityRepository <Campaigns.Model.CharacterSheet>(); _characters = new InMemoryEntityRepository <Campaigns.Model.Character>(); _characters.AddForeignStore(_characterSheets); _attributes.AddRange(new[] { new Campaigns.Model.Attribute { Name = "human", Category = "race", IsStandard = false }, new Campaigns.Model.Attribute { Name = "gnome", Category = "race", IsStandard = false }, new Campaigns.Model.Attribute { Name = "str", Category = "abilities", IsStandard = true }, new Campaigns.Model.Attribute { Name = "int", Category = "abilities", IsStandard = true }, new Campaigns.Model.Attribute { Name = "str", Category = "ability-mods", IsStandard = true }, new Campaigns.Model.Attribute { Name = "int", Category = "ability-mods", IsStandard = true }, }); _contributions.AddRange(new[] { Attrib("gnome", "race").ConstantContributionTo(Attrib("int", "abilities"), 2), Attrib("str", "abilities").ContributionTo(Attrib("str", "ability-mods"), n => n / 2 - 5), Attrib("int", "abilities").ContributionTo(Attrib("int", "ability-mods"), n => n / 2 - 5), }); _rules = new RulesService( _attributes, _contributions, _characterSheets, _characters); _gnomeAllocations = new [] { new AttributeAllocation { Attribute = Attrib("gnome", "race") }, new AttributeAllocation { Attribute = Attrib("str", "abilities"), Value = 8 }, new AttributeAllocation { Attribute = Attrib("int", "abilities"), Value = 8 }, }; _superStrongUpdate = new CharacterUpdate { AddedOrUpdatedAllocations = new [] { new AttributeAllocation { Attribute = Attrib("str", "abilities"), Value = 20 }, } }; }
public RulesController() { this.svc = new RulesService(); }
public Owner(RulesService rulesService, IMongoCollection <Guild> dbGuilds, IMongoCollection <Rule> dbRules) { _rulesService = rulesService; _dbGuilds = dbGuilds; _dbRules = dbRules; }
public void TestInit() { _attributes = new InMemoryEntityRepository<Campaigns.Model.Attribute>(); _contributions = new InMemoryEntityRepository<Campaigns.Model.AttributeContribution>(); _contributions.AddForeignStore(_attributes); _characterSheets = new InMemoryEntityRepository<Campaigns.Model.CharacterSheet>(); _characters = new InMemoryEntityRepository<Campaigns.Model.Character>(); _characters.AddForeignStore(_characterSheets); _attributes.AddRange(new[] { new Campaigns.Model.Attribute { Name = "human", Category = "race", IsStandard = false }, new Campaigns.Model.Attribute { Name = "gnome", Category = "race", IsStandard = false }, new Campaigns.Model.Attribute { Name = "str", Category = "abilities", IsStandard = true }, new Campaigns.Model.Attribute { Name = "int", Category = "abilities", IsStandard = true }, new Campaigns.Model.Attribute { Name = "str", Category = "ability-mods", IsStandard = true }, new Campaigns.Model.Attribute { Name = "int", Category = "ability-mods", IsStandard = true }, }); _contributions.AddRange(new[] { Attrib("gnome", "race").ConstantContributionTo(Attrib("int", "abilities"), 2), Attrib("str", "abilities").ContributionTo(Attrib("str", "ability-mods"), n => n / 2 - 5), Attrib("int", "abilities").ContributionTo(Attrib("int", "ability-mods"), n => n / 2 - 5), }); _rules = new RulesService( _attributes, _contributions, _characterSheets, _characters); _gnomeAllocations = new [] { new AttributeAllocation { Attribute = Attrib("gnome", "race") }, new AttributeAllocation { Attribute = Attrib("str", "abilities"), Value = 8 }, new AttributeAllocation { Attribute = Attrib("int", "abilities"), Value = 8 }, }; _superStrongUpdate = new CharacterUpdate { AddedOrUpdatedAllocations = new [] { new AttributeAllocation { Attribute = Attrib("str", "abilities"), Value = 20 }, } }; }