Skip to content

Commit 519ae6f

Browse files
authored
Add streamlined way for building conditions (#143)
[FEAT] Added a streamlined way for building conditions
1 parent 2bd7246 commit 519ae6f

51 files changed

Lines changed: 1201 additions & 2991 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,7 @@ var ruleForPremiumFreeSample = RuleBuilder
5252
.NewRule<ContentType, ConditionType>()
5353
.WithName("Rule for perfume sample for premium clients.")
5454
.WithContent(ContentType.FreeSample, "SmallPerfumeSample")
55-
.WithCondition(c => c
56-
.AsValued(ConditionType.ClientType)
57-
.OfDataType<string>()
58-
.WithComparisonOperator(Operators.Equal)
59-
.SetOperand("Premium")
60-
.Build())
55+
.WithCondition(ConditionType.ClientType, Operators.Equal, "Premium")
6156
.WithDateBegin(new DateTime(2020, 01, 01))
6257
.Build();
6358
```
@@ -76,7 +71,7 @@ var matchingRule = rulesEngine.MatchOneAsync(
7671
new DateTime(2021, 12, 25),
7772
new[]
7873
{
79-
new Condition<ConditionType>() { Type = ConditionType.ClientType, Value = "Premium" }
74+
new Condition<ConditionType>(ConditionType.ClientType, "Premium")
8075
});
8176
```
8277

samples/Rules.Framework.InMemory.Sample/Rules/TestNumberRules.cs

Lines changed: 26 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -36,65 +36,37 @@ private void Add(
3636

3737
private RuleBuilderResult<ContentTypes, ConditionTypes> CreateDefaultRule() =>
3838
RuleBuilder
39-
.NewRule<ContentTypes, ConditionTypes>()
40-
.WithName("Default rule for test number")
41-
.WithContent(ContentTypes.TestNumber, ":| default nothing special about this number")
42-
.WithDateBegin(new DateTime(2019, 01, 01))
43-
.Build();
39+
.NewRule<ContentTypes, ConditionTypes>()
40+
.WithName("Default rule for test number")
41+
.WithContent(ContentTypes.TestNumber, ":| default nothing special about this number")
42+
.WithDateBegin(new DateTime(2019, 01, 01))
43+
.Build();
4444

4545
private RuleBuilderResult<ContentTypes, ConditionTypes> CreateRuleForCoolNumbers() =>
4646
RuleBuilder
47-
.NewRule<ContentTypes, ConditionTypes>()
48-
.WithName("Rule for cool numbers")
49-
.WithContent(ContentTypes.TestNumber, ":D this number is so COOL!")
50-
.WithDateBegin(new DateTime(2019, 01, 01))
51-
.WithCondition(cnb => cnb.AsComposed()
52-
.WithLogicalOperator(LogicalOperators.Or)
53-
.AddCondition(condition => condition
54-
.AsValued(ConditionTypes.RoyalNumber).OfDataType<int>()
55-
.WithComparisonOperator(Operators.Equal)
56-
.SetOperand(7)
57-
.Build())
58-
.AddCondition(condition => condition.AsComposed()
59-
.WithLogicalOperator(LogicalOperators.And)
60-
.AddCondition(sub => sub
61-
.AsValued(ConditionTypes.IsPrimeNumber).OfDataType<bool>()
62-
.WithComparisonOperator(Operators.Equal)
63-
.SetOperand(true)
64-
.Build())
65-
.AddCondition(sub => sub
66-
.AsValued(ConditionTypes.SumAll).OfDataType<string>()
67-
.WithComparisonOperator(Operators.StartsWith)
68-
.SetOperand("5")
69-
.Build())
70-
.Build())
71-
.Build())
72-
.Build();
47+
.NewRule<ContentTypes, ConditionTypes>()
48+
.WithName("Rule for cool numbers")
49+
.WithContent(ContentTypes.TestNumber, ":D this number is so COOL!")
50+
.WithDateBegin(new DateTime(2019, 01, 01))
51+
.WithCondition(c => c
52+
.Or(o => o
53+
.Value(ConditionTypes.RoyalNumber, Operators.Equal, 7)
54+
.And(a => a
55+
.Value(ConditionTypes.IsPrimeNumber, Operators.Equal, 7)
56+
.Value(ConditionTypes.SumAll, Operators.StartsWith, "5"))))
57+
.Build();
7358

7459
private RuleBuilderResult<ContentTypes, ConditionTypes> CreateRuleForSosoNumbers() =>
7560
RuleBuilder
76-
.NewRule<ContentTypes, ConditionTypes>()
77-
.WithName("Rule for so so numbers")
78-
.WithContent(ContentTypes.TestNumber, ":) this number is so so")
79-
.WithDateBegin(new DateTime(2019, 01, 01))
80-
.WithCondition(cnb => cnb.AsComposed()
81-
.WithLogicalOperator(LogicalOperators.Or)
82-
.AddCondition(condition => condition
83-
.AsValued(ConditionTypes.CanNumberBeDividedBy3).OfDataType<bool>()
84-
.WithComparisonOperator(Operators.Equal)
85-
.SetOperand(true)
86-
.Build())
87-
.AddCondition(condition => condition
88-
.AsValued(ConditionTypes.IsPrimeNumber).OfDataType<bool>()
89-
.WithComparisonOperator(Operators.Equal)
90-
.SetOperand(false)
91-
.Build())
92-
.AddCondition(sub => sub
93-
.AsValued(ConditionTypes.SumAll).OfDataType<string>()
94-
.WithComparisonOperator(Operators.EndsWith)
95-
.SetOperand("9")
96-
.Build())
97-
.Build())
98-
.Build();
61+
.NewRule<ContentTypes, ConditionTypes>()
62+
.WithName("Rule for so so numbers")
63+
.WithContent(ContentTypes.TestNumber, ":) this number is so so")
64+
.WithDateBegin(new DateTime(2019, 01, 01))
65+
.WithCondition(c => c
66+
.Or(o => o
67+
.Value(ConditionTypes.CanNumberBeDividedBy3, Operators.Equal, true)
68+
.Value(ConditionTypes.IsPrimeNumber, Operators.Equal, false)
69+
.Value(ConditionTypes.SumAll, Operators.StartsWith, "9")))
70+
.Build();
9971
}
10072
}

samples/Rules.Framework.WebUI.Sample/Program.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace Rules.Framework.WebUI.Sample
22
{
33
using global::Rules.Framework.Extension;
44
using global::Rules.Framework.WebUI.Sample.Engine;
5+
using global::Rules.Framework.WebUI.Sample.ReadmeExample;
56
using global::Rules.Framework.WebUI.Sample.Rules;
67

78
public static class Program
@@ -32,7 +33,7 @@ public static void Main(string[] args)
3233

3334
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
3435

35-
AddRulesFrameworkUI(app);
36+
AddRulesFrameworkUI(app, useReadmeExample: false);
3637

3738
app.MapControllerRoute(
3839
name: "default",
@@ -41,18 +42,25 @@ public static void Main(string[] args)
4142
app.Run();
4243
}
4344

44-
private static void AddRulesFrameworkUI(IApplicationBuilder app)
45+
private static void AddRulesFrameworkUI(IApplicationBuilder app, bool useReadmeExample = false)
4546
{
47+
if (useReadmeExample)
48+
{
49+
app.UseRulesFrameworkWebUI(new BasicRulesEngineExample().RulesEngine.CreateGenericEngine());
50+
51+
return;
52+
}
53+
4654
var rulesProvider = new RulesEngineProvider(new RulesBuilder(new List<IContentTypes>()
4755
{
4856
new RulesRandomFactory()
4957
}));
5058

5159
var rulesEngine = rulesProvider
52-
.GetRulesEngineAsync()
53-
.ConfigureAwait(false)
54-
.GetAwaiter()
55-
.GetResult();
60+
.GetRulesEngineAsync()
61+
.ConfigureAwait(false)
62+
.GetAwaiter()
63+
.GetResult();
5664

5765
app.UseRulesFrameworkWebUI(rulesEngine.CreateGenericEngine());
5866
}
Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
{
2-
"iisSettings": {
3-
"windowsAuthentication": false,
4-
"anonymousAuthentication": true,
5-
"iisExpress": {
6-
"applicationUrl": "http://localhost:2604",
7-
"sslPort": 44302
8-
}
9-
},
10-
"profiles": {
11-
"Rules.Framework.UI.Sample": {
12-
"commandName": "Project",
13-
"dotnetRunMessages": true,
14-
"launchBrowser": true,
15-
"launchUrl": "rules/index.html",
16-
"applicationUrl": "http://localhost:5000",
17-
"environmentVariables": {
18-
"ASPNETCORE_ENVIRONMENT": "Development"
19-
}
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:2604",
7+
"sslPort": 44302
8+
}
209
},
21-
"IIS Express": {
22-
"commandName": "IISExpress",
23-
"launchBrowser": true,
24-
"environmentVariables": {
25-
"ASPNETCORE_ENVIRONMENT": "Development"
26-
}
10+
"profiles": {
11+
"Rules.Framework.WebUI.Sample": {
12+
"commandName": "Project",
13+
"dotnetRunMessages": true,
14+
"launchBrowser": true,
15+
"launchUrl": "rules/index.html",
16+
"applicationUrl": "http://localhost:5000",
17+
"environmentVariables": {
18+
"ASPNETCORE_ENVIRONMENT": "Development"
19+
}
20+
},
21+
"IIS Express": {
22+
"commandName": "IISExpress",
23+
"launchBrowser": true,
24+
"environmentVariables": {
25+
"ASPNETCORE_ENVIRONMENT": "Development"
26+
}
27+
}
2728
}
28-
}
2929
}

samples/Rules.Framework.WebUI.Sample/ReadmeExample/BasicRulesEngineExample.cs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
namespace Rules.Framework.WebUI.Sample.ReadmeExample
22
{
33
using System;
4-
using global::Rules.Framework.Builder;
54
using global::Rules.Framework.Core;
65
using global::Rules.Framework.Providers.InMemory;
76
using global::Rules.Framework.WebUI.Sample.Engine;
@@ -42,38 +41,23 @@ private IEnumerable<RuleSpecificationBase<BasicContentType, BasicConditionType>>
4241
.NewRule<BasicContentType, BasicConditionType>()
4342
.WithName("Rule for January sample for premium clients.")
4443
.WithContent(BasicContentType.FreeSample, "SmallPerfumeSample")
45-
.WithCondition(c => c
46-
.AsValued(BasicConditionType.ClientType)
47-
.OfDataType<string>()
48-
.WithComparisonOperator(Operators.Equal)
49-
.SetOperand("Premium")
50-
.Build())
44+
.WithCondition(BasicConditionType.ClientType, Operators.Equal, "Premium")
5145
.WithDatesInterval(new DateTime(2023, 01, 01), new DateTime(2023, 02, 01))
5246
.Build();
5347

5448
var ruleForPremiumFreeSampleApril = RuleBuilder
5549
.NewRule<BasicContentType, BasicConditionType>()
5650
.WithName("Rule for April sample for premium clients.")
5751
.WithContent(BasicContentType.FreeSample, "ShampooSample")
58-
.WithCondition(c => c
59-
.AsValued(BasicConditionType.ClientType)
60-
.OfDataType<string>()
61-
.WithComparisonOperator(Operators.Equal)
62-
.SetOperand("Premium")
63-
.Build())
52+
.WithCondition(BasicConditionType.ClientType, Operators.Equal, "Premium")
6453
.WithDatesInterval(new DateTime(2023, 04, 01), new DateTime(2023, 05, 01))
6554
.Build();
6655

6756
var ruleForPremiumFreeSampleSeptember = RuleBuilder
6857
.NewRule<BasicContentType, BasicConditionType>()
6958
.WithName("Rule for September sample for premium clients.")
7059
.WithContent(BasicContentType.FreeSample, "ConditionerSample")
71-
.WithCondition(c => c
72-
.AsValued(BasicConditionType.ClientType)
73-
.OfDataType<string>()
74-
.WithComparisonOperator(Operators.Equal)
75-
.SetOperand("Premium")
76-
.Build())
60+
.WithCondition(BasicConditionType.ClientType, Operators.Equal, "Premium")
7761
.WithDatesInterval(new DateTime(2023, 09, 01), new DateTime(2023, 10, 01))
7862
.Build();
7963

samples/Rules.Framework.WebUI.Sample/Rules/RulesRandomFactory.cs

Lines changed: 30 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -44,72 +44,35 @@ public IEnumerable<RuleSpecification> GetRulesSpecifications()
4444
return rulesSpecifications;
4545
}
4646

47-
private static RuleBuilderResult<ContentTypes, ConditionTypes> CreateMultipleRule(ContentTypes contentTypes, int value, DateTime dateBegin,
48-
DateTime? dateEnd, bool isActive = true) =>
49-
RuleBuilder
50-
.NewRule<ContentTypes, ConditionTypes>()
51-
.WithName($"Multi rule for test {contentTypes} {value}")
52-
.WithContent(contentTypes, new { Value = value })
53-
.WithDatesInterval(dateBegin, dateEnd)
54-
.WithActive(isActive)
55-
.WithCondition(cnb => cnb.AsComposed()
56-
.WithLogicalOperator(LogicalOperators.Or)
57-
.AddCondition(condition => condition
58-
.AsValued(ConditionTypes.RoyalNumber).OfDataType<int>()
59-
.WithComparisonOperator(Operators.Equal)
60-
.SetOperand(7)
61-
.Build())
62-
.AddCondition(condition => condition
63-
.AsValued(ConditionTypes.SumAll).OfDataType<IEnumerable<int>>()
64-
.WithComparisonOperator(Operators.In)
65-
.SetOperand(new int[] { 9, 8, 6 })
66-
.Build())
67-
.AddCondition(condition => condition.AsComposed()
68-
.WithLogicalOperator(LogicalOperators.And)
69-
.AddCondition(sub => sub
70-
.AsValued(ConditionTypes.IsPrimeNumber).OfDataType<bool>()
71-
.WithComparisonOperator(Operators.Equal)
72-
.SetOperand(false)
73-
.Build())
74-
.AddCondition(sub => sub
75-
.AsValued(ConditionTypes.SumAll).OfDataType<string>()
76-
.WithComparisonOperator(Operators.StartsWith)
77-
.SetOperand("15")
78-
.Build())
79-
.AddCondition(condition => condition.AsComposed()
80-
.WithLogicalOperator(LogicalOperators.And)
81-
.AddCondition(sub => sub
82-
.AsValued(ConditionTypes.CanNumberBeDividedBy3).OfDataType<bool>()
83-
.WithComparisonOperator(Operators.Equal)
84-
.SetOperand(false)
85-
.Build())
86-
.AddCondition(sub => sub
87-
.AsValued(ConditionTypes.SumAll).OfDataType<string>()
88-
.WithComparisonOperator(Operators.NotEqual)
89-
.SetOperand(string.Empty)
90-
.Build())
91-
.Build())
92-
.Build())
93-
.AddCondition(condition => condition.AsComposed()
94-
.WithLogicalOperator(LogicalOperators.And)
95-
.AddCondition(sub => sub
96-
.AsValued(ConditionTypes.IsPrimeNumber).OfDataType<bool>()
97-
.WithComparisonOperator(Operators.Equal)
98-
.SetOperand(true)
99-
.Build())
100-
.AddCondition(sub => sub
101-
.AsValued(ConditionTypes.SumAll).OfDataType<string>()
102-
.WithComparisonOperator(Operators.StartsWith)
103-
.SetOperand("5")
104-
.Build())
105-
.AddCondition(sub => sub
106-
.AsValued(ConditionTypes.CanNumberBeDividedBy3).OfDataType<bool>()
107-
.WithComparisonOperator(Operators.Equal)
108-
.SetOperand(false)
109-
.Build())
110-
.Build())
111-
.Build())
112-
.Build();
47+
private static RuleBuilderResult<ContentTypes, ConditionTypes> CreateMultipleRule(
48+
ContentTypes contentTypes,
49+
int value,
50+
DateTime dateBegin,
51+
DateTime? dateEnd,
52+
bool isActive = true) => RuleBuilder
53+
.NewRule<ContentTypes, ConditionTypes>()
54+
.WithName($"Multi rule for test {contentTypes} {value}")
55+
.WithContent(contentTypes, new { Value = value })
56+
.WithDatesInterval(dateBegin, dateEnd)
57+
.WithActive(isActive)
58+
.WithCondition(rootCond => rootCond
59+
.Or(o => o
60+
.Value(ConditionTypes.RoyalNumber, Operators.Equal, 7)
61+
.Value(ConditionTypes.SumAll, Operators.In, new int[] { 9, 8, 6 })
62+
.And(a => a
63+
.Value(ConditionTypes.IsPrimeNumber, Operators.Equal, false)
64+
.Value(ConditionTypes.SumAll, Operators.StartsWith, "15")
65+
)
66+
.And(a => a
67+
.Value(ConditionTypes.CanNumberBeDividedBy3, Operators.Equal, false)
68+
.Value(ConditionTypes.SumAll, Operators.NotEqual, string.Empty)
69+
)
70+
.And(a => a
71+
.Value(ConditionTypes.IsPrimeNumber, Operators.Equal, true)
72+
.Value(ConditionTypes.SumAll, Operators.StartsWith, "5")
73+
.Value(ConditionTypes.CanNumberBeDividedBy3, Operators.Equal, false)
74+
)))
75+
.Build();
11376

11477
private void Add(
11578
RuleBuilderResult<ContentTypes, ConditionTypes> rule,
@@ -134,4 +97,4 @@ private DateTime CreateRandomDateBegin(int year)
13497
return dateBegin.AddMonths(months).AddDays(1);
13598
}
13699
}
137-
}
100+
}

0 commit comments

Comments
 (0)