public virtual void Visit(CompositeRule visitee) { foreach (Rule child in visitee.Rules) { child.Accept(this); } }
public override string Part2() { rules[8] = new CompositeRule { Self = 8, Subrules = new List <List <int> > { new List <int> { 42 }, new List <int> { 42, 8 }, }, }; rules[11] = new CompositeRule { Self = 11, Subrules = new List <List <int> > { new List <int> { 42, 31 }, new List <int> { 42, 11, 31 }, }, }; return(messages.Where(m => match(m, new[] { 0 })).Count().ToString()); }
public void Test_ShouldNotUpdateSlip() { var rule = new CompositeRule( new ActivateRule()); var book = new Book("book01", "CLRS"); IReadOnlyCollection <ICommand> commands = rule.Handle(book); Assert.AreEqual(commands.Count, 0); }
public void TestForBookProducts() { var rule = new CompositeRule(new SlipForShippingRule(), new SlipForRoyaltyRule()); var book = new Book("book01", "CLRS"); IReadOnlyCollection <ICommand> commands = rule.Handle(book); // Shall return 2 objects, for shipping and royality department Assert.AreEqual(commands.Count, 2); }
public CompositeRuleViewModel(CompositeRule rule, RuleViewModelFactory rulesFactory, IEventAggregator eventAggregator) : base(rule, eventAggregator) { _model = rule; _rulesFactory = rulesFactory; _eventAggregator = eventAggregator; _rules = new BindableCollection <RuleViewModelBase>(rule.Rules.Select(r => rulesFactory.Create(r, this))); Rules = _rules; }
public void AddCompositeRule() { var compositeRule = new CompositeRule() { Name = nameof(CompositeRule) }; _model.Rules.Add(compositeRule); _rules.Add(_rulesFactory.Create(compositeRule, this)); }
public void TestCommisionPayment() { var rule = new CompositeRule( new PaymentCommissionRule()); var physicalProduct = new PhysicalProduct("pp01", "computer"); Assert.AreEqual(false, physicalProduct.isCommissionGenerated); IReadOnlyCollection <ICommand> commands = rule.Handle(physicalProduct); Assert.AreEqual(true, physicalProduct.isCommissionGenerated); }
public void TestUpgradeMembership() { var rule = new CompositeRule( new UpgradeRule()); var membership = new Membership("regular", "regular membership"); Assert.AreEqual("Regular", membership.membershipType.ToString()); IReadOnlyCollection <ICommand> commands = rule.Handle(membership); Assert.AreEqual("Premium", membership.membershipType.ToString()); }
public void TestActiveMembership() { var rule = new CompositeRule( new ActivateRule()); var membership = new Membership("regular", "regular membership"); Assert.AreEqual(false, membership.IsActive); IReadOnlyCollection <ICommand> commands = rule.Handle(membership); Assert.AreEqual(true, membership.IsActive); }
public void MembershipTests() { var rule = new CompositeRule( new SlipForShippingRule(), new ActivateRule()); var membership = new MembershipGood("John Smith"); var commands = rule.Handle(membership); var command = commands.First() as ActivateCommand; Assert.Equal("John Smith", command.Membership); }
public void BooksTest() { var rule = new CompositeRule( new SlipForShippingRule(), new ActivateRule()); var book = new Book("The Annotated Turing"); var commands = rule.Handle(book); var command = commands.First() as SlipCommand; Assert.Equal(book, command.Good); Assert.Equal("Shipping", command.Action); }
public void EvaluateShouldInvokeAllComposedRules() { // arrange var target = new CompositeRule<int>() { new Rule<int, int>( i => ++i ), new Rule<int, int>( i => ++i ), new Rule<int, int>( i => ++i ) }; var seed = 0; // act var actual = target.Evaluate( seed ); // assert Assert.Equal( 3, actual ); }
private void Initialize() { var mapper = BsonMapper.Global; mapper.Entity <RuleInfo>() .Id(x => x.Id); mapper.RegisterType <CompositeRule>(rule => { var bson = new BsonDocument { ["Type"] = new BsonValue(rule.GetType().Name), [nameof(rule.SelectedType)] = new BsonValue(rule.SelectedType.ToString()), [nameof(rule.IsEnabled)] = new BsonValue(rule.IsEnabled), [nameof(rule.Name)] = new BsonValue(rule.Name) }; bson.Add(nameof(rule.Rules), new BsonArray(rule.Rules.Select(r => mapper.ToDocument(r)))); return(bson); }, value => { var rule = new CompositeRule(); rule.IsEnabled = value.AsDocument[nameof(rule.IsEnabled)].AsBoolean; rule.Name = value.AsDocument[nameof(rule.Name)].AsString; rule.SelectedType = (RuleGroupType) Enum.Parse(typeof(RuleGroupType), value.AsDocument[nameof(rule.SelectedType)].AsString); rule.Rules = value.AsDocument[nameof(rule.Rules)].AsArray.Select(v => { var type = typeof(RuleBase).Assembly.GetType( $"{typeof(RuleBase).Namespace}.{v.AsDocument["Type"].AsString}", true); return(mapper.ToObject(type, v.AsDocument) as IRule); }).ToList(); return(rule); }); mapper.RegisterType <TextRule>( serialize: rule => { var bson = new BsonDocument { ["Type"] = new BsonValue(rule.GetType().Name), [nameof(rule.Name)] = new BsonValue(rule.Name), [nameof(rule.IsEnabled)] = new BsonValue(rule.IsEnabled), [nameof(rule.SelectedAction)] = new BsonValue(rule.SelectedAction.ToString()), [nameof(rule.Text)] = new BsonValue(rule.Text), [nameof(rule.IsCaseSensitive)] = new BsonValue(rule.IsCaseSensitive) }; return(bson); }, deserialize: bson => { var rule = new TextRule(); rule.Name = bson.AsDocument[nameof(rule.Name)].AsString; rule.IsEnabled = bson.AsDocument[nameof(rule.IsEnabled)].AsBoolean; rule.IsCaseSensitive = bson.AsDocument[nameof(rule.IsCaseSensitive)].AsBoolean; rule.SelectedAction = (TextRuleAction) Enum.Parse(typeof(TextRuleAction), bson.AsDocument[nameof(rule.SelectedAction)].AsString); rule.Text = bson.AsDocument[nameof(rule.Text)].AsString; return(rule); }); mapper.RegisterType <DateRule>( serialize: rule => { var bson = new BsonDocument { ["Type"] = new BsonValue(rule.GetType().Name), [nameof(rule.Name)] = new BsonValue(rule.Name), [nameof(rule.IsEnabled)] = new BsonValue(rule.IsEnabled), [nameof(rule.SelectedAction)] = new BsonValue(rule.SelectedAction.ToString()), [nameof(rule.Date)] = !rule.Date.HasValue ? BsonValue.Null : new BsonValue(rule.Date) }; return(bson); }, deserialize: bson => { var rule = new DateRule(); rule.Name = bson.AsDocument[nameof(rule.Name)].AsString; rule.IsEnabled = bson.AsDocument[nameof(rule.IsEnabled)].AsBoolean; rule.SelectedAction = (DateRuleAction)Enum.Parse(typeof(DateRuleAction), bson.AsDocument[nameof(rule.SelectedAction)].AsString); rule.Date = bson.AsDocument[nameof(rule.Date)].IsNull ? new DateTime?() : bson.AsDocument[nameof(rule.Date)].AsDateTime; return(rule); }); mapper.RegisterType <RegexRule>( serialize: rule => { var bson = new BsonDocument { ["Type"] = new BsonValue(rule.GetType().Name), [nameof(rule.Name)] = new BsonValue(rule.Name), [nameof(rule.IsEnabled)] = new BsonValue(rule.IsEnabled), [nameof(rule.SelectedAction)] = new BsonValue(rule.SelectedAction.ToString()), [nameof(rule.Pattern)] = new BsonValue(rule.Pattern) }; return(bson); }, deserialize: bson => { var rule = new RegexRule(); rule.Name = bson.AsDocument[nameof(rule.Name)].AsString; rule.IsEnabled = bson.AsDocument[nameof(rule.IsEnabled)].AsBoolean; rule.SelectedAction = (RegexRuleAction) Enum.Parse(typeof(RegexRuleAction), bson.AsDocument[nameof(rule.SelectedAction)].AsString); rule.Pattern = bson.AsDocument[nameof(rule.Pattern)].AsString; return(rule); }); mapper.RegisterType <IRule>( serialize: rule => mapper.ToDocument(rule.GetType(), rule), deserialize: value => { var type = typeof(RuleBase).Assembly.GetType( $"{typeof(RuleBase).Namespace}.{value.AsDocument["Type"].AsString}", true); return(mapper.ToObject(type, value.AsDocument) as IRule); } ); }
public override void Visit(CompositeRule visitee) { //root = false; visitee.Rules.Aggregate(this, (visitor, r) => r.Accept(visitor)); }