Skip to content

Commit d50b1fa

Browse files
authored
feat(RulesEngine): Add RulesEngine (#308)
* feat(RulesEngine): Add RulesEngine * feat(RulesEngine): Add RulesEngine * chore: remove redundant references * chore: fix bad code smell
1 parent 5923b08 commit d50b1fa

31 files changed

+901
-0
lines changed

Masa.Framework.sln

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,18 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Service.Caller
577577
EndProject
578578
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.Authentication.OpenIdConnect.EFCore.Oracle", "src\Contrib\Authentication\OpenIdConnect\Masa.Contrib.Authentication.OpenIdConnect.EFCore.Oracle\Masa.Contrib.Authentication.OpenIdConnect.EFCore.Oracle.csproj", "{03F476EF-022A-4370-B1C3-FEEDE3BB4E7F}"
579579
EndProject
580+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "RulesEngine", "RulesEngine", "{A7081ADB-DB21-4678-8510-A5367D470743}"
581+
EndProject
582+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "RulesEngine", "RulesEngine", "{8E74F3D4-9387-4F69-9A17-C793CE2CBAA9}"
583+
EndProject
584+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.BuildingBlocks.RulesEngine", "src\BuildingBlocks\RulesEngine\Masa.BuildingBlocks.RulesEngine\Masa.BuildingBlocks.RulesEngine.csproj", "{B03C329C-70F4-442A-B420-90DDF7E31847}"
585+
EndProject
586+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.RulesEngine.MicrosoftRulesEngine", "src\Contrib\RulesEngine\Masa.Contrib.RulesEngine.MicrosoftRulesEngine\Masa.Contrib.RulesEngine.MicrosoftRulesEngine.csproj", "{4E217EC9-0616-414B-82D9-9107F9826D6E}"
587+
EndProject
588+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{3FADF704-2581-47AC-A1F7-07091B6328A1}"
589+
EndProject
590+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.RulesEngine.MicrosoftRulesEngine.Tests", "src\Contrib\RulesEngine\Tests\Masa.Contrib.RulesEngine.MicrosoftRulesEngine.Tests\Masa.Contrib.RulesEngine.MicrosoftRulesEngine.Tests.csproj", "{EEB2D542-5A2C-4E18-A0E6-72844C359DAD}"
591+
EndProject
580592
Global
581593
GlobalSection(SolutionConfigurationPlatforms) = preSolution
582594
Debug|Any CPU = Debug|Any CPU
@@ -2041,6 +2053,14 @@ Global
20412053
{BEA8E5A5-E7BD-4165-80CD-D1F53ED82D02}.Release|Any CPU.Build.0 = Release|Any CPU
20422054
{BEA8E5A5-E7BD-4165-80CD-D1F53ED82D02}.Release|x64.ActiveCfg = Release|Any CPU
20432055
{BEA8E5A5-E7BD-4165-80CD-D1F53ED82D02}.Release|x64.Build.0 = Release|Any CPU
2056+
{B03C329C-70F4-442A-B420-90DDF7E31847}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2057+
{B03C329C-70F4-442A-B420-90DDF7E31847}.Debug|Any CPU.Build.0 = Debug|Any CPU
2058+
{B03C329C-70F4-442A-B420-90DDF7E31847}.Debug|x64.ActiveCfg = Debug|Any CPU
2059+
{B03C329C-70F4-442A-B420-90DDF7E31847}.Debug|x64.Build.0 = Debug|Any CPU
2060+
{B03C329C-70F4-442A-B420-90DDF7E31847}.Release|Any CPU.ActiveCfg = Release|Any CPU
2061+
{B03C329C-70F4-442A-B420-90DDF7E31847}.Release|Any CPU.Build.0 = Release|Any CPU
2062+
{B03C329C-70F4-442A-B420-90DDF7E31847}.Release|x64.ActiveCfg = Release|Any CPU
2063+
{B03C329C-70F4-442A-B420-90DDF7E31847}.Release|x64.Build.0 = Release|Any CPU
20442064
{03F476EF-022A-4370-B1C3-FEEDE3BB4E7F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
20452065
{03F476EF-022A-4370-B1C3-FEEDE3BB4E7F}.Debug|Any CPU.Build.0 = Debug|Any CPU
20462066
{03F476EF-022A-4370-B1C3-FEEDE3BB4E7F}.Debug|x64.ActiveCfg = Debug|Any CPU
@@ -2049,6 +2069,22 @@ Global
20492069
{03F476EF-022A-4370-B1C3-FEEDE3BB4E7F}.Release|Any CPU.Build.0 = Release|Any CPU
20502070
{03F476EF-022A-4370-B1C3-FEEDE3BB4E7F}.Release|x64.ActiveCfg = Release|Any CPU
20512071
{03F476EF-022A-4370-B1C3-FEEDE3BB4E7F}.Release|x64.Build.0 = Release|Any CPU
2072+
{4E217EC9-0616-414B-82D9-9107F9826D6E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2073+
{4E217EC9-0616-414B-82D9-9107F9826D6E}.Debug|Any CPU.Build.0 = Debug|Any CPU
2074+
{4E217EC9-0616-414B-82D9-9107F9826D6E}.Debug|x64.ActiveCfg = Debug|Any CPU
2075+
{4E217EC9-0616-414B-82D9-9107F9826D6E}.Debug|x64.Build.0 = Debug|Any CPU
2076+
{4E217EC9-0616-414B-82D9-9107F9826D6E}.Release|Any CPU.ActiveCfg = Release|Any CPU
2077+
{4E217EC9-0616-414B-82D9-9107F9826D6E}.Release|Any CPU.Build.0 = Release|Any CPU
2078+
{4E217EC9-0616-414B-82D9-9107F9826D6E}.Release|x64.ActiveCfg = Release|Any CPU
2079+
{4E217EC9-0616-414B-82D9-9107F9826D6E}.Release|x64.Build.0 = Release|Any CPU
2080+
{EEB2D542-5A2C-4E18-A0E6-72844C359DAD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2081+
{EEB2D542-5A2C-4E18-A0E6-72844C359DAD}.Debug|Any CPU.Build.0 = Debug|Any CPU
2082+
{EEB2D542-5A2C-4E18-A0E6-72844C359DAD}.Debug|x64.ActiveCfg = Debug|Any CPU
2083+
{EEB2D542-5A2C-4E18-A0E6-72844C359DAD}.Debug|x64.Build.0 = Debug|Any CPU
2084+
{EEB2D542-5A2C-4E18-A0E6-72844C359DAD}.Release|Any CPU.ActiveCfg = Release|Any CPU
2085+
{EEB2D542-5A2C-4E18-A0E6-72844C359DAD}.Release|Any CPU.Build.0 = Release|Any CPU
2086+
{EEB2D542-5A2C-4E18-A0E6-72844C359DAD}.Release|x64.ActiveCfg = Release|Any CPU
2087+
{EEB2D542-5A2C-4E18-A0E6-72844C359DAD}.Release|x64.Build.0 = Release|Any CPU
20522088
EndGlobalSection
20532089
GlobalSection(SolutionProperties) = preSolution
20542090
HideSolutionNode = FALSE
@@ -2332,7 +2368,13 @@ Global
23322368
{9BB5CC86-C2E8-4859-9610-50DB263605A3} = {FC4A0C94-47F7-4C9A-A7EC-138DE8EB842D}
23332369
{8D7D3D21-86DB-4FCC-8FF0-6E1587ABD22A} = {FC4A0C94-47F7-4C9A-A7EC-138DE8EB842D}
23342370
{BEA8E5A5-E7BD-4165-80CD-D1F53ED82D02} = {8D7D3D21-86DB-4FCC-8FF0-6E1587ABD22A}
2371+
{A7081ADB-DB21-4678-8510-A5367D470743} = {950DA7D0-48C1-42BA-8E8F-F72C0DCE41C4}
23352372
{03F476EF-022A-4370-B1C3-FEEDE3BB4E7F} = {41769FBF-91A8-48D1-B3BB-CAE4C814E7CD}
2373+
{8E74F3D4-9387-4F69-9A17-C793CE2CBAA9} = {DC578D74-98F0-4F19-A230-CFA8DAEE0AF1}
2374+
{B03C329C-70F4-442A-B420-90DDF7E31847} = {8E74F3D4-9387-4F69-9A17-C793CE2CBAA9}
2375+
{4E217EC9-0616-414B-82D9-9107F9826D6E} = {A7081ADB-DB21-4678-8510-A5367D470743}
2376+
{3FADF704-2581-47AC-A1F7-07091B6328A1} = {A7081ADB-DB21-4678-8510-A5367D470743}
2377+
{EEB2D542-5A2C-4E18-A0E6-72844C359DAD} = {3FADF704-2581-47AC-A1F7-07091B6328A1}
23362378
EndGlobalSection
23372379
GlobalSection(ExtensibilityGlobals) = postSolution
23382380
SolutionGuid = {40383055-CC50-4600-AD9A-53C14F620D03}

src/BuildingBlocks/Data/Masa.BuildingBlocks.Data.Contracts/Options/MasaRelationOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) MASA Stack All rights reserved.
22
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
33

4+
// ReSharper disable once CheckNamespace
5+
46
namespace Masa.BuildingBlocks.Data;
57

68
public class MasaRelationOptions
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.BuildingBlocks.Data.Contracts.Response;
5+
6+
public class ResponseBase
7+
{
8+
public bool IsValid { get; }
9+
10+
public string Message { get; }
11+
12+
public ResponseBase(bool isValid, string message = "")
13+
{
14+
IsValid = isValid;
15+
Message = message;
16+
}
17+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.BuildingBlocks.RulesEngine;
5+
6+
public class DefaultRulesEngineFactory : MasaFactoryBase<IRulesEngineClient, RulesEngineRelationOptions>,
7+
IRulesEngineFactory
8+
{
9+
protected override string DefaultServiceNotFoundMessage
10+
=> $"No default {nameof(IRulesEngineClient)} found, you may need services.AddRulesEngine(options => options.UseRulesEngine())";
11+
12+
protected override string SpecifyServiceNotFoundMessage
13+
=> $"Please make sure you have used [{0}] {nameof(IRulesEngineClient)}, it was not found";
14+
15+
protected override MasaFactoryOptions<RulesEngineRelationOptions> FactoryOptions => _options.Value;
16+
17+
private readonly IOptions<RulesEngineFactoryOptions> _options;
18+
19+
public DefaultRulesEngineFactory(IServiceProvider serviceProvider) : base(serviceProvider)
20+
{
21+
_options = serviceProvider.GetRequiredService<IOptions<RulesEngineFactoryOptions>>();
22+
}
23+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.BuildingBlocks.RulesEngine;
5+
6+
public interface IRulesEngineClient
7+
{
8+
/// <summary>
9+
/// Check if the rule format is correct
10+
/// </summary>
11+
/// <param name="ruleRaw"></param>
12+
/// <returns></returns>
13+
VerifyResponse Verify(string ruleRaw);
14+
15+
/// <summary>
16+
/// Obtain whether the match is successful according to the rules and input parameters
17+
/// </summary>
18+
/// <param name="ruleRaw"></param>
19+
/// <param name="data">Input data</param>
20+
/// <typeparam name="TRequest"></typeparam>
21+
/// <returns>Whether the match was successful</returns>
22+
List<ExecuteResponse> Execute<TRequest>(string ruleRaw, TRequest data);
23+
24+
/// <summary>
25+
/// Obtain whether the match is successful according to the rules and input parameters
26+
/// </summary>
27+
/// <param name="ruleRaw">Rule Json</param>
28+
/// <param name="data">Input data</param>
29+
/// <typeparam name="TRequest"></typeparam>
30+
/// <returns>Whether the match was successful</returns>
31+
Task<List<ExecuteResponse>> ExecuteAsync<TRequest>(string ruleRaw, TRequest data);
32+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.BuildingBlocks.RulesEngine;
5+
6+
public interface IRulesEngineFactory : IMasaFactory<IRulesEngineClient>
7+
{
8+
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\..\Data\Masa.BuildingBlocks.Data\Masa.BuildingBlocks.Data.csproj" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
// ReSharper disable once CheckNamespace
5+
6+
namespace Masa.BuildingBlocks.RulesEngine;
7+
8+
public class RulesEngineFactoryOptions : MasaFactoryOptions<RulesEngineRelationOptions>
9+
{
10+
public void TryAddRulesEngine(string name, Func<IServiceProvider, IRulesEngineClient> func)
11+
{
12+
if (Options.Any(opt => opt.Name.Equals(name, StringComparison.OrdinalIgnoreCase)))
13+
return;
14+
15+
Options.Add(new RulesEngineRelationOptions(name, func));
16+
}
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
// ReSharper disable once CheckNamespace
5+
namespace Masa.BuildingBlocks.RulesEngine;
6+
7+
public class RulesEngineOptions
8+
{
9+
public IServiceCollection Services { get; }
10+
11+
public RulesEngineOptions(IServiceCollection services)
12+
{
13+
Services = services;
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
// ReSharper disable once CheckNamespace
5+
6+
namespace Masa.BuildingBlocks.RulesEngine;
7+
8+
public class RulesEngineRelationOptions : MasaRelationOptions<IRulesEngineClient>
9+
{
10+
public RulesEngineRelationOptions(string name, Func<IServiceProvider, IRulesEngineClient> func)
11+
: base(name)
12+
{
13+
Func = func;
14+
}
15+
}

0 commit comments

Comments
 (0)