Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ namespace Rules.Framework.InMemory.Sample.Engine
using System.Collections.Generic;
using global::Rules.Framework.InMemory.Sample.Enums;

internal interface IContentTypes
internal interface IRuleSpecificationsProvider
{
ContentTypes ContentType { get; }
RulesetNames[] Rulesets { get; }

IEnumerable<RuleSpecification> GetRulesSpecifications();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ internal class RuleSpecification
{
public RuleAddPriorityOption RuleAddPriorityOption { get; set; }

public RuleBuilderResult<ContentTypes, ConditionTypes> RuleBuilderResult { get; set; }
public RuleBuilderResult<RulesetNames, ConditionNames> RuleBuilderResult { get; set; }
}
}
13 changes: 8 additions & 5 deletions samples/Rules.Framework.InMemory.Sample/Engine/RulesBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ namespace Rules.Framework.InMemory.Sample.Engine

internal class RulesBuilder
{
private readonly IEnumerable<IContentTypes> contentTypes;
private readonly IEnumerable<IRuleSpecificationsProvider> ruleSpecificationsRegistrars;

public RulesBuilder(IEnumerable<IContentTypes> contentTypes) => this.contentTypes = contentTypes;
public RulesBuilder(IEnumerable<IRuleSpecificationsProvider> ruleSpecificationsProviders) => this.ruleSpecificationsRegistrars = ruleSpecificationsProviders;

public async Task BuildAsync(IRulesEngine rulesEngine)
{
foreach (var contentType in contentTypes)
foreach (var ruleSpecificationsProvider in ruleSpecificationsRegistrars)
{
await rulesEngine.CreateContentTypeAsync(contentType.ContentType.ToString());
foreach (var ruleset in ruleSpecificationsProvider.Rulesets)
{
await rulesEngine.CreateRulesetAsync(ruleset.ToString());
}

var rulesSpecifications = contentType.GetRulesSpecifications();
var rulesSpecifications = ruleSpecificationsProvider.GetRulesSpecifications();

foreach (var ruleSpecification in rulesSpecifications)
{
Expand Down
18 changes: 9 additions & 9 deletions samples/Rules.Framework.InMemory.Sample/Engine/RulesService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ internal class RulesService
{
private readonly RulesEngineProvider rulesEngineProvider;

public RulesService(IEnumerable<IContentTypes> contentTypes)
public RulesService(IEnumerable<IRuleSpecificationsProvider> ruleSpecificationsProviders)
{
this.rulesEngineProvider = new RulesEngineProvider(new RulesBuilder(contentTypes));
this.rulesEngineProvider = new RulesEngineProvider(new RulesBuilder(ruleSpecificationsProviders));
}

public async Task<T> MatchOneAsync<T>(
ContentTypes contentType,
RulesetNames ruleset,
DateTime dateTime,
IDictionary<ConditionTypes, object> conditions)
IDictionary<ConditionNames, object> conditions)
{
var rulesConditions = (conditions is null) ? new Condition<ConditionTypes>[] { } :
conditions.Select(x => new Condition<ConditionTypes>(x.Key, x.Value))
var rulesConditions = (conditions is null) ? new Condition<ConditionNames>[] { } :
conditions.Select(x => new Condition<ConditionNames>(x.Key, x.Value))
.ToArray();

var rulesEngine = await
Expand All @@ -31,13 +31,13 @@ public async Task<T> MatchOneAsync<T>(
.ConfigureAwait(false);

var match = await rulesEngine
.MakeGeneric<ContentTypes, ConditionTypes>()
.MatchOneAsync(contentType, dateTime, rulesConditions)
.MakeGeneric<RulesetNames, ConditionNames>()
.MatchOneAsync(ruleset, dateTime, rulesConditions)
.ConfigureAwait(false);

if (match is null)
{
var message = $"Error trying to match one rule. No rule was found {contentType} {dateTime} {string.Join(", ", conditions)}.";
var message = $"Error trying to match one rule. No rule was found {ruleset} {dateTime} {string.Join(", ", conditions)}.";

throw new RulesNotFoundException(message);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
namespace Rules.Framework.InMemory.Sample.Enums
{
internal enum ConditionTypes
internal enum ConditionNames
{
/// <summary>
/// no condition type defined
/// no condition defined
/// </summary>
None = 0,

/// <summary>
/// condition type to filter by royal numbers
/// condition to filter by royal numbers
/// </summary>
RoyalNumber = 1,

/// <summary>
/// condition type to filter if the number can be divided by 3
/// condition to filter if the number can be divided by 3
/// </summary>
CanNumberBeDividedBy3 = 2,

/// <summary>
/// condition type to filter if the number is prime
/// condition to filter if the number is prime
/// </summary>
IsPrimeNumber = 3,

/// <summary>
/// condition type to filter number sums
/// condition to filter number sums
/// </summary>
SumAll = 4
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
namespace Rules.Framework.InMemory.Sample.Enums
{
internal enum ContentTypes
internal enum RulesetNames
{
None = 0,

/// <summary>
/// this content verifies if number is cool or not
/// this ruleset contains rules that verify if a number is cool or not
/// </summary>
TestNumber = 1,
}
Expand Down
22 changes: 10 additions & 12 deletions samples/Rules.Framework.InMemory.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ namespace Rules.Framework.InMemory.Sample
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading.Tasks;
using global::Rules.Framework.InMemory.Sample.Engine;
using global::Rules.Framework.InMemory.Sample.Enums;
using global::Rules.Framework.InMemory.Sample.Helper;
using global::Rules.Framework.InMemory.Sample.Rules;

internal class Program
{
private static void Main(string[] args)
private static async Task Main(string[] args)
{
var rulesService = new RulesService(new List<IContentTypes>()
var rulesService = new RulesService(new List<IRuleSpecificationsProvider>()
{
new TestNumberRules()
});
Expand All @@ -31,18 +32,15 @@ private static void Main(string[] args)
break;
}

var conditions = new Dictionary<ConditionTypes, object> {
{ ConditionTypes.IsPrimeNumber, value.IsPrime() },
{ ConditionTypes.CanNumberBeDividedBy3, value.CanNumberBeDividedBy3() },
{ ConditionTypes.SumAll, value.SumAll() },
{ ConditionTypes.RoyalNumber, value }
var conditions = new Dictionary<ConditionNames, object> {
{ ConditionNames.IsPrimeNumber, value.IsPrime() },
{ ConditionNames.CanNumberBeDividedBy3, value.CanNumberBeDividedBy3() },
{ ConditionNames.SumAll, value.SumAll() },
{ ConditionNames.RoyalNumber, value }
};

var result = rulesService
.MatchOneAsync<string>(ContentTypes.TestNumber, targetDate, conditions)
.ConfigureAwait(false)
.GetAwaiter()
.GetResult();
var result = await rulesService
.MatchOneAsync<string>(RulesetNames.TestNumber, targetDate, conditions);

Console.WriteLine($"The result is: {result}");
}
Expand Down
50 changes: 25 additions & 25 deletions samples/Rules.Framework.InMemory.Sample/Rules/TestNumberRules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Rules.Framework.InMemory.Sample.Rules
using global::Rules.Framework.InMemory.Sample.Engine;
using global::Rules.Framework.InMemory.Sample.Enums;

internal class TestNumberRules : IContentTypes
internal class TestNumberRules : IRuleSpecificationsProvider
{
public readonly List<RuleSpecification> rulesSpecifications;

Expand All @@ -15,7 +15,7 @@ public TestNumberRules()
this.rulesSpecifications = new List<RuleSpecification>();
}

public ContentTypes ContentType => ContentTypes.TestNumber;
public RulesetNames[] Rulesets => new[] { RulesetNames.TestNumber, };

public IEnumerable<RuleSpecification> GetRulesSpecifications()
{
Expand All @@ -27,41 +27,41 @@ public IEnumerable<RuleSpecification> GetRulesSpecifications()
}

private void Add(
RuleBuilderResult<ContentTypes, ConditionTypes> rule,
RuleBuilderResult<RulesetNames, ConditionNames> rule,
RuleAddPriorityOption ruleAddPriorityOption) => rulesSpecifications.Add(
new RuleSpecification
{
RuleBuilderResult = rule,
RuleAddPriorityOption = ruleAddPriorityOption,
});

private RuleBuilderResult<ContentTypes, ConditionTypes> CreateDefaultRule() => Rule.New<ContentTypes, ConditionTypes>()
.WithName("Default rule for test number")
.WithContent(ContentTypes.TestNumber, ":| default nothing special about this number")
.WithDateBegin(new DateTime(2019, 01, 01))
private RuleBuilderResult<RulesetNames, ConditionNames> CreateDefaultRule() => Rule.Create<RulesetNames, ConditionNames>("Default rule for test number")
.InRuleset(RulesetNames.TestNumber)
.SetContent(":| default nothing special about this number")
.Since(new DateTime(2019, 01, 01))
.Build();

private RuleBuilderResult<ContentTypes, ConditionTypes> CreateRuleForCoolNumbers() => Rule.New<ContentTypes, ConditionTypes>()
.WithName("Rule for cool numbers")
.WithContent(ContentTypes.TestNumber, ":D this number is so COOL!")
.WithDateBegin(new DateTime(2019, 01, 01))
.WithCondition(c => c
.Or(o => o
.Value(ConditionTypes.RoyalNumber, Operators.Equal, 7)
.And(a => a
.Value(ConditionTypes.IsPrimeNumber, Operators.Equal, 7)
.Value(ConditionTypes.SumAll, Operators.StartsWith, "5"))))
private RuleBuilderResult<RulesetNames, ConditionNames> CreateRuleForCoolNumbers() => Rule.Create<RulesetNames, ConditionNames>("Rule for cool numbers")
.InRuleset(RulesetNames.TestNumber)
.SetContent(":D this number is so COOL!")
.Since(new DateTime(2019, 01, 01))
Comment thread
luispfgarces marked this conversation as resolved.
.ApplyWhen(c => c
.Or(o => o
.Value(ConditionNames.RoyalNumber, Operators.Equal, 7)
.And(a => a
.Value(ConditionNames.IsPrimeNumber, Operators.Equal, 7)
.Value(ConditionNames.SumAll, Operators.StartsWith, "5"))))
.Build();

private RuleBuilderResult<ContentTypes, ConditionTypes> CreateRuleForSosoNumbers() => Rule.New<ContentTypes, ConditionTypes>()
.WithName("Rule for so so numbers")
.WithContent(ContentTypes.TestNumber, ":) this number is so so")
.WithDateBegin(new DateTime(2019, 01, 01))
.WithCondition(c => c
private RuleBuilderResult<RulesetNames, ConditionNames> CreateRuleForSosoNumbers() => Rule.Create<RulesetNames, ConditionNames>("Rule for so so numbers")
.InRuleset(RulesetNames.TestNumber)
.SetContent(":) this number is so so")
.Since(new DateTime(2019, 01, 01))
.ApplyWhen(c => c
.Or(o => o
.Value(ConditionTypes.CanNumberBeDividedBy3, Operators.Equal, true)
.Value(ConditionTypes.IsPrimeNumber, Operators.Equal, false)
.Value(ConditionTypes.SumAll, Operators.StartsWith, "9")))
.Value(ConditionNames.CanNumberBeDividedBy3, Operators.Equal, true)
.Value(ConditionNames.IsPrimeNumber, Operators.Equal, false)
.Value(ConditionNames.SumAll, Operators.StartsWith, "9")))
.Build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ namespace Rules.Framework.WebUI.Sample.Engine
using System.Collections.Generic;
using global::Rules.Framework.WebUI.Sample.Enums;

internal interface IContentTypes
internal interface IRuleSpecificationsProvider
{
ContentTypes[] ContentTypes { get; }
RulesetNames[] Rulesets { get; }

IEnumerable<RuleSpecification> GetRulesSpecifications();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ namespace Rules.Framework.WebUI.Sample.Engine
using global::Rules.Framework.Builder.Generic;
using global::Rules.Framework.WebUI.Sample.Enums;

internal sealed class RuleSpecification : RuleSpecificationBase<ContentTypes, ConditionTypes>
internal sealed class RuleSpecification : RuleSpecificationBase<RulesetNames, ConditionNames>
{
public RuleSpecification(
RuleBuilderResult<ContentTypes, ConditionTypes> ruleBuilderResult,
RuleBuilderResult<RulesetNames, ConditionNames> ruleBuilderResult,
RuleAddPriorityOption ruleAddPriorityOption)
: base(ruleBuilderResult, ruleAddPriorityOption)
{
Expand Down
12 changes: 6 additions & 6 deletions samples/Rules.Framework.WebUI.Sample/Engine/RulesBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ namespace Rules.Framework.WebUI.Sample.Engine

internal class RulesBuilder
{
private readonly IEnumerable<IContentTypes> contentTypes;
private readonly IEnumerable<IRuleSpecificationsProvider> ruleSpecificationsProviders;

public RulesBuilder(IEnumerable<IContentTypes> contentTypes) => this.contentTypes = contentTypes;
public RulesBuilder(IEnumerable<IRuleSpecificationsProvider> ruleSpecificationsProviders) => this.ruleSpecificationsProviders = ruleSpecificationsProviders;

public async Task BuildAsync(IRulesEngine rulesEngine)
{
foreach (var contentType in contentTypes)
foreach (var ruleSpecificationsProvider in ruleSpecificationsProviders)
{
foreach (var contentTypeValue in contentType.ContentTypes)
foreach (var ruleset in ruleSpecificationsProvider.Rulesets)
{
await rulesEngine.CreateContentTypeAsync(contentTypeValue.ToString());
await rulesEngine.CreateRulesetAsync(ruleset.ToString());
}

var rulesSpecifications = contentType.GetRulesSpecifications();
var rulesSpecifications = ruleSpecificationsProvider.GetRulesSpecifications();

foreach (var ruleSpecification in rulesSpecifications)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
namespace Rules.Framework.WebUI.Sample.Enums
{
public enum ConditionTypes
public enum ConditionNames
{
/// <summary>
/// no condition type defined
/// no condition defined
/// </summary>
None = 0,

/// <summary>
/// condition type to filter by royal numbers
/// condition to filter by royal numbers
/// </summary>
RoyalNumber = 1,

/// <summary>
/// condition type to filter if the number can be divided by 3
/// condition to filter if the number can be divided by 3
/// </summary>
CanNumberBeDividedBy3 = 2,

/// <summary>
/// condition type to filter if the number is prime
/// condition to filter if the number is prime
/// </summary>
IsPrimeNumber = 3,

/// <summary>
/// condition type to filter number sums
/// condition to filter number sums
/// </summary>
SumAll = 4
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Rules.Framework.WebUI.Sample.Enums
{
public enum ContentTypes
public enum RulesetNames
{
TestNumber = 0,

Expand All @@ -18,4 +18,4 @@ public enum ContentTypes

TestBlob = 7
}
}
}
3 changes: 1 addition & 2 deletions samples/Rules.Framework.WebUI.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,13 @@ private static void AddRulesFrameworkUI(IApplicationBuilder app, bool useReadmeE
return;
}

var rulesProvider = new RulesEngineProvider(new RulesBuilder(new List<IContentTypes>()
var rulesProvider = new RulesEngineProvider(new RulesBuilder(new List<IRuleSpecificationsProvider>()
{
new RulesRandomFactory()
}));

var rulesEngine = rulesProvider
.GetRulesEngineAsync()
.ConfigureAwait(false)
.GetAwaiter()
.GetResult();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Rules.Framework.WebUI.Sample.ReadmeExample
{
public enum BasicConditionType
public enum BasicConditionNames
{
ClientType = 1,
Country = 2
Expand Down
Loading