├── .gitattributes
├── .github
├── ISSUE_TEMPLATE
│ ├── bug_report.md
│ └── feature_request.md
├── dependabot.yml
└── workflows
│ ├── ci.yml
│ └── release.yml
├── .gitignore
├── GSoulavy.RuleEngine.Tests
├── GSoulavy.RuleEngine.Tests.csproj
├── Kernel
│ ├── EvaluateCollectionTests.cs
│ ├── EvaluateTests.cs
│ ├── ValidateAllTests.cs
│ ├── ValidateAnyTests.cs
│ ├── ValidateCollectionTests.cs
│ └── ValidateTests.cs
└── Models
│ ├── Gender.cs
│ └── Person.cs
├── GSoulavy.RuleEngine.sln
├── GSoulavy.RuleEngine
├── GSoulavy.RuleEngine.csproj
├── IKernel.cs
├── IRule.cs
├── Kernel.cs
├── Properties
│ └── PublishProfiles
│ │ └── FolderProfile.pubxml
└── Rule.cs
├── LICENSE
└── README.md
/.gitattributes:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # Set default behavior to automatically normalize line endings.
3 | ###############################################################################
4 | * text=auto
5 |
6 | ###############################################################################
7 | # Set default behavior for command prompt diff.
8 | #
9 | # This is need for earlier builds of msysgit that does not have it on by
10 | # default for csharp files.
11 | # Note: This is only used by command line
12 | ###############################################################################
13 | #*.cs diff=csharp
14 |
15 | ###############################################################################
16 | # Set the merge driver for project and solution files
17 | #
18 | # Merging from the command prompt will add diff markers to the files if there
19 | # are conflicts (Merging from VS is not affected by the settings below, in VS
20 | # the diff markers are never inserted). Diff markers may cause the following
21 | # file extensions to fail to load in VS. An alternative would be to treat
22 | # these files as binary and thus will always conflict and require user
23 | # intervention with every merge. To do so, just uncomment the entries below
24 | ###############################################################################
25 | #*.sln merge=binary
26 | #*.csproj merge=binary
27 | #*.vbproj merge=binary
28 | #*.vcxproj merge=binary
29 | #*.vcproj merge=binary
30 | #*.dbproj merge=binary
31 | #*.fsproj merge=binary
32 | #*.lsproj merge=binary
33 | #*.wixproj merge=binary
34 | #*.modelproj merge=binary
35 | #*.sqlproj merge=binary
36 | #*.wwaproj merge=binary
37 |
38 | ###############################################################################
39 | # behavior for image files
40 | #
41 | # image files are treated as binary by default.
42 | ###############################################################################
43 | #*.jpg binary
44 | #*.png binary
45 | #*.gif binary
46 |
47 | ###############################################################################
48 | # diff behavior for common document formats
49 | #
50 | # Convert binary document formats to text before diffing them. This feature
51 | # is only available from the command line. Turn it on by uncommenting the
52 | # entries below.
53 | ###############################################################################
54 | #*.doc diff=astextplain
55 | #*.DOC diff=astextplain
56 | #*.docx diff=astextplain
57 | #*.DOCX diff=astextplain
58 | #*.dot diff=astextplain
59 | #*.DOT diff=astextplain
60 | #*.pdf diff=astextplain
61 | #*.PDF diff=astextplain
62 | #*.rtf diff=astextplain
63 | #*.RTF diff=astextplain
64 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/bug_report.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Bug report
3 | about: Create a report to help us improve
4 | title: ''
5 | labels: ''
6 | assignees: ''
7 |
8 | ---
9 |
10 | **Describe the bug**
11 | A clear and concise description of what the bug is.
12 |
13 | **To Reproduce**
14 | Steps to reproduce the behavior:
15 | 1. Go to '...'
16 | 2. Click on '....'
17 | 3. Scroll down to '....'
18 | 4. See error
19 |
20 | **Expected behavior**
21 | A clear and concise description of what you expected to happen.
22 |
23 | **Screenshots**
24 | If applicable, add screenshots to help explain your problem.
25 |
26 | **Desktop (please complete the following information):**
27 | - OS: [e.g. iOS]
28 | - Browser [e.g. chrome, safari]
29 | - Version [e.g. 22]
30 |
31 | **Smartphone (please complete the following information):**
32 | - Device: [e.g. iPhone6]
33 | - OS: [e.g. iOS8.1]
34 | - Browser [e.g. stock browser, safari]
35 | - Version [e.g. 22]
36 |
37 | **Additional context**
38 | Add any other context about the problem here.
39 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/feature_request.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Feature request
3 | about: Suggest an idea for this project
4 | title: ''
5 | labels: ''
6 | assignees: ''
7 |
8 | ---
9 |
10 | **Is your feature request related to a problem? Please describe.**
11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12 |
13 | **Describe the solution you'd like**
14 | A clear and concise description of what you want to happen.
15 |
16 | **Describe alternatives you've considered**
17 | A clear and concise description of any alternative solutions or features you've considered.
18 |
19 | **Additional context**
20 | Add any other context or screenshots about the feature request here.
21 |
--------------------------------------------------------------------------------
/.github/dependabot.yml:
--------------------------------------------------------------------------------
1 | version: 2
2 | updates:
3 | - package-ecosystem: nuget
4 | directory: "/"
5 | schedule:
6 | interval: daily
7 | open-pull-requests-limit: 10
8 | ignore:
9 | - dependency-name: System.Linq.Dynamic.Core
10 | versions:
11 | - 1.2.7
12 | - dependency-name: Microsoft.NET.Test.Sdk
13 | versions:
14 | - 16.8.3
15 |
--------------------------------------------------------------------------------
/.github/workflows/ci.yml:
--------------------------------------------------------------------------------
1 | name: .NET
2 |
3 | on:
4 | push:
5 | branches: [ master ]
6 | pull_request:
7 | branches: [ master ]
8 |
9 | jobs:
10 | build:
11 |
12 | runs-on: ubuntu-latest
13 |
14 | steps:
15 | - uses: actions/checkout@v2
16 | - name: Setup .NET
17 | uses: actions/setup-dotnet@v1
18 | with:
19 | dotnet-version: 6.0.x
20 | - name: Restore dependencies
21 | run: dotnet restore
22 | - name: Build
23 | run: dotnet build --no-restore
24 | - name: Test
25 | run: dotnet test --no-build --verbosity normal
26 |
--------------------------------------------------------------------------------
/.github/workflows/release.yml:
--------------------------------------------------------------------------------
1 | name: Release to Nuget
2 |
3 | on:
4 | release:
5 | types: [published]
6 |
7 | jobs:
8 | build:
9 |
10 | runs-on: ubuntu-latest
11 |
12 | steps:
13 | - uses: actions/checkout@v2
14 | - name: Setup .NET
15 | uses: actions/setup-dotnet@v1
16 | with:
17 | dotnet-version: 6.0.x
18 | - name: Restore dependencies
19 | run: dotnet restore
20 | - name: Create NuGet Package
21 | run: dotnet pack -c Release /p:Version=${{ github.event.release.tag_name }} /p:PackageReleaseNotes="See https://github.com/gsoulavy/RuleEngine/releases/tag/${{ github.event.release.tag_name }}"
22 | - name: Archive NuGet Package
23 | uses: actions/upload-artifact@v1
24 | with:
25 | name: RuleEngine
26 | path: ./GSoulavy.RuleEngine/bin/Release/GSoulavy.RuleEngine.${{ github.event.release.tag_name }}.nupkg
27 |
28 | - name: Publish NuGet Package
29 | run: dotnet nuget push **/*.nupkg --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json
30 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 |
4 | # User-specific files
5 | *.suo
6 | *.user
7 | *.userosscache
8 | *.sln.docstates
9 |
10 | # User-specific files (MonoDevelop/Xamarin Studio)
11 | *.userprefs
12 |
13 | # Build results
14 | [Dd]ebug/
15 | [Dd]ebugPublic/
16 | [Rr]elease/
17 | [Rr]eleases/
18 | x64/
19 | x86/
20 | bld/
21 | [Bb]in/
22 | [Oo]bj/
23 | [Ll]og/
24 |
25 | # Visual Studio 2015 cache/options directory
26 | .vs/
27 | # Uncomment if you have tasks that create the project's static files in wwwroot
28 | #wwwroot/
29 |
30 | # MSTest test Results
31 | [Tt]est[Rr]esult*/
32 | [Bb]uild[Ll]og.*
33 |
34 | # NUNIT
35 | *.VisualState.xml
36 | TestResult.xml
37 |
38 | # Build Results of an ATL Project
39 | [Dd]ebugPS/
40 | [Rr]eleasePS/
41 | dlldata.c
42 |
43 | # DNX
44 | project.lock.json
45 | project.fragment.lock.json
46 | artifacts/
47 |
48 | *_i.c
49 | *_p.c
50 | *_i.h
51 | *.ilk
52 | *.meta
53 | *.obj
54 | *.pch
55 | *.pdb
56 | *.pgc
57 | *.pgd
58 | *.rsp
59 | *.sbr
60 | *.tlb
61 | *.tli
62 | *.tlh
63 | *.tmp
64 | *.tmp_proj
65 | *.log
66 | *.vspscc
67 | *.vssscc
68 | .builds
69 | *.pidb
70 | *.svclog
71 | *.scc
72 |
73 | # Chutzpah Test files
74 | _Chutzpah*
75 |
76 | # Visual C++ cache files
77 | ipch/
78 | *.aps
79 | *.ncb
80 | *.opendb
81 | *.opensdf
82 | *.sdf
83 | *.cachefile
84 | *.VC.db
85 | *.VC.VC.opendb
86 |
87 | # Visual Studio profiler
88 | *.psess
89 | *.vsp
90 | *.vspx
91 | *.sap
92 |
93 | # TFS 2012 Local Workspace
94 | $tf/
95 |
96 | # Guidance Automation Toolkit
97 | *.gpState
98 |
99 | # ReSharper is a .NET coding add-in
100 | _ReSharper*/
101 | *.[Rr]e[Ss]harper
102 | *.DotSettings.user
103 |
104 | # JustCode is a .NET coding add-in
105 | .JustCode
106 |
107 | # TeamCity is a build add-in
108 | _TeamCity*
109 |
110 | # DotCover is a Code Coverage Tool
111 | *.dotCover
112 |
113 | # NCrunch
114 | _NCrunch_*
115 | .*crunch*.local.xml
116 | nCrunchTemp_*
117 |
118 | # MightyMoose
119 | *.mm.*
120 | AutoTest.Net/
121 |
122 | # Web workbench (sass)
123 | .sass-cache/
124 |
125 | # Installshield output folder
126 | [Ee]xpress/
127 |
128 | # DocProject is a documentation generator add-in
129 | DocProject/buildhelp/
130 | DocProject/Help/*.HxT
131 | DocProject/Help/*.HxC
132 | DocProject/Help/*.hhc
133 | DocProject/Help/*.hhk
134 | DocProject/Help/*.hhp
135 | DocProject/Help/Html2
136 | DocProject/Help/html
137 |
138 | # Click-Once directory
139 | publish/
140 |
141 | # Publish Web Output
142 | *.[Pp]ublish.xml
143 | *.azurePubxml
144 | # TODO: Comment the next line if you want to checkin your web deploy settings
145 | # but database connection strings (with potential passwords) will be unencrypted
146 | #*.pubxml
147 | *.publishproj
148 |
149 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
150 | # checkin your Azure Web App publish settings, but sensitive information contained
151 | # in these scripts will be unencrypted
152 | PublishScripts/
153 |
154 | # NuGet Packages
155 | *.nupkg
156 | # The packages folder can be ignored because of Package Restore
157 | **/packages/*
158 | # except build/, which is used as an MSBuild target.
159 | !**/packages/build/
160 | # Uncomment if necessary however generally it will be regenerated when needed
161 | #!**/packages/repositories.config
162 | # NuGet v3's project.json files produces more ignoreable files
163 | *.nuget.props
164 | *.nuget.targets
165 |
166 | # Microsoft Azure Build Output
167 | csx/
168 | *.build.csdef
169 |
170 | # Microsoft Azure Emulator
171 | ecf/
172 | rcf/
173 |
174 | # Windows Store app package directories and files
175 | AppPackages/
176 | BundleArtifacts/
177 | Package.StoreAssociation.xml
178 | _pkginfo.txt
179 |
180 | # Visual Studio cache files
181 | # files ending in .cache can be ignored
182 | *.[Cc]ache
183 | # but keep track of directories ending in .cache
184 | !*.[Cc]ache/
185 |
186 | # Others
187 | ClientBin/
188 | ~$*
189 | *~
190 | *.dbmdl
191 | *.dbproj.schemaview
192 | *.jfm
193 | *.pfx
194 | *.publishsettings
195 | node_modules/
196 | orleans.codegen.cs
197 |
198 | # Since there are multiple workflows, uncomment next line to ignore bower_components
199 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
200 | #bower_components/
201 |
202 | # RIA/Silverlight projects
203 | Generated_Code/
204 |
205 | # Backup & report files from converting an old project file
206 | # to a newer Visual Studio version. Backup files are not needed,
207 | # because we have git ;-)
208 | _UpgradeReport_Files/
209 | Backup*/
210 | UpgradeLog*.XML
211 | UpgradeLog*.htm
212 |
213 | # SQL Server files
214 | *.mdf
215 | *.ldf
216 |
217 | # Business Intelligence projects
218 | *.rdl.data
219 | *.bim.layout
220 | *.bim_*.settings
221 |
222 | # Microsoft Fakes
223 | FakesAssemblies/
224 |
225 | # GhostDoc plugin setting file
226 | *.GhostDoc.xml
227 |
228 | # Node.js Tools for Visual Studio
229 | .ntvs_analysis.dat
230 |
231 | # Visual Studio 6 build log
232 | *.plg
233 |
234 | # Visual Studio 6 workspace options file
235 | *.opt
236 |
237 | # Visual Studio LightSwitch build output
238 | **/*.HTMLClient/GeneratedArtifacts
239 | **/*.DesktopClient/GeneratedArtifacts
240 | **/*.DesktopClient/ModelManifest.xml
241 | **/*.Server/GeneratedArtifacts
242 | **/*.Server/ModelManifest.xml
243 | _Pvt_Extensions
244 |
245 | # Paket dependency manager
246 | .paket/paket.exe
247 | paket-files/
248 |
249 | # FAKE - F# Make
250 | .fake/
251 |
252 | # JetBrains Rider
253 | .idea/
254 | *.sln.iml
255 |
256 | # CodeRush
257 | .cr/
258 |
259 | # Python Tools for Visual Studio (PTVS)
260 | __pycache__/
261 | *.pyc
--------------------------------------------------------------------------------
/GSoulavy.RuleEngine.Tests/GSoulavy.RuleEngine.Tests.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net6.0
5 | false
6 |
7 |
8 |
9 |
10 |
11 |
12 | all
13 | runtime; build; native; contentfiles; analyzers
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/GSoulavy.RuleEngine.Tests/Kernel/EvaluateCollectionTests.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using GSoulavy.RuleEngine.Tests.Models;
3 | using Xunit;
4 |
5 | namespace GSoulavy.RuleEngine.Tests.Kernel
6 | {
7 | using RuleEngine;
8 | public class EvaluateCollectionTests
9 | {
10 | [Fact(DisplayName = "EvaluateCollection: true")]
11 | public void EvaluateCollection_True()
12 | {
13 | // Arrange
14 | var people = new List
15 | {
16 | new Person {Name = "John", Gender = Gender.Male, Age = 23, Income = 24000, NumberOfChildren = 0},
17 | new Person {Name = "Mary", Gender = Gender.Female, Age = 22, Income = 23000, NumberOfChildren = 1}
18 | };
19 |
20 | const string expression = "f.Any(p => p.Name.Equals(\"John\") && p.Age < 24)";
21 | var ruleEngine = new Kernel();
22 | // Act
23 | var result = ruleEngine.Evaluate, bool>(people, expression);
24 | // Assert
25 | Assert.True(result);
26 | }
27 | }
28 | }
--------------------------------------------------------------------------------
/GSoulavy.RuleEngine.Tests/Kernel/EvaluateTests.cs:
--------------------------------------------------------------------------------
1 | using GSoulavy.RuleEngine.Tests.Models;
2 | using Xunit;
3 |
4 | namespace GSoulavy.RuleEngine.Tests.Kernel
5 | {
6 | public class EvaluateTests
7 | {
8 | [Fact(DisplayName = "Evaluate: false")]
9 | public void Evaluate_False()
10 | {
11 | // Arrange
12 | const string expression = @"(f.Age > 3 && f.Income > 100000) || f.NumberOfChildren > 5";
13 | var p = new Person {Age = 37, Income = 45000, NumberOfChildren = 3};
14 | var ruleEngine = new RuleEngine.Kernel();
15 | // Act
16 | var result = ruleEngine.Evaluate(p, expression);
17 | // Assert
18 | Assert.False(result);
19 | }
20 |
21 | [Fact(DisplayName = "Evaluate: true")]
22 | public void Evaluate_True()
23 | {
24 | // Arrange
25 | const string expression = @"(f.Age > 3 && f.Income < 50000) || f.NumberOfChildren > 2";
26 | var p = new Person {Age = 37, Income = 45000, NumberOfChildren = 3};
27 | var ruleEngine = new RuleEngine.Kernel();
28 | // Act
29 | var result = ruleEngine.Evaluate(p, expression);
30 | // Assert
31 | Assert.True(result);
32 | }
33 | }
34 | }
--------------------------------------------------------------------------------
/GSoulavy.RuleEngine.Tests/Kernel/ValidateAllTests.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using GSoulavy.RuleEngine.Tests.Models;
3 | using Xunit;
4 |
5 | namespace GSoulavy.RuleEngine.Tests.Kernel
6 | {
7 | public class ValidateAllTests
8 | {
9 | [Fact(DisplayName = "ValidateAll[with param]: true")]
10 | public void ValidateAllW_True()
11 | {
12 | // Arrange
13 | var rules = new List
14 | {
15 | new Rule {Key = "1", Expression = "(f.Age > 3 && f.Income < 50000) || f.NumberOfChildren > 2"},
16 | new Rule {Key = "2", Expression = "(f.Age > 3 && f.Income > 100000) || f.NumberOfChildren > 5" }
17 | };
18 |
19 | var p = new Person {Age = 37, Income = 45000, NumberOfChildren = 3};
20 | var ruleEngine = new RuleEngine.Kernel();
21 | ruleEngine.AddRules(rules);
22 | // Act
23 | var result = ruleEngine.ValidateAll(p, "1");
24 | // Assert
25 | Assert.True(result);
26 | }
27 |
28 | [Fact(DisplayName = "ValidateAll[without param]: true")]
29 | public void ValidateAllWp_True()
30 | {
31 | // Arrange
32 | var rule = new Rule {Key = "1", Expression = "(f.Age > 3 && f.Income < 50000) || f.NumberOfChildren > 2"};
33 | var p = new Person {Age = 37, Income = 45000, NumberOfChildren = 3};
34 | var ruleEngine = new RuleEngine.Kernel();
35 | ruleEngine.AddRule(rule);
36 | // Act
37 | var result = ruleEngine.ValidateAll(p);
38 | // Assert
39 | Assert.True(result);
40 | }
41 |
42 | [Fact(DisplayName = "ValidateAll[with param]: false")]
43 | public void ValidateW_False()
44 | {
45 | // Arrange
46 | var rules = new List
47 | {
48 | new Rule {Key = "1", Expression = "(f.Age > 3 && f.Income < 50000) || f.NumberOfChildren > 2"},
49 | new Rule {Key = "2", Expression = "(f.Age > 3 && f.Income > 100000) || f.NumberOfChildren > 5"}
50 | };
51 |
52 | var p = new Person {Age = 37, Income = 45000, NumberOfChildren = 3};
53 | var ruleEngine = new RuleEngine.Kernel();
54 | ruleEngine.AddRules(rules);
55 |
56 | // Act
57 | var result = ruleEngine.ValidateAll(p, "2");
58 | // Assert
59 | Assert.False(result);
60 | }
61 |
62 | [Fact(DisplayName = "ValidateAll[without param]: false")]
63 | public void ValidateWp_False()
64 | {
65 | // Arrange
66 | var rules = new List
67 | {
68 | new Rule {Key = "1", Expression = "(f.Age > 3 && f.Income < 50000) || f.NumberOfChildren > 2"},
69 | new Rule {Key = "2", Expression = "(f.Age > 3 && f.Income > 100000) || f.NumberOfChildren > 5"}
70 | };
71 | var p = new Person {Age = 37, Income = 45000, NumberOfChildren = 3};
72 | var ruleEngine = new RuleEngine.Kernel();
73 | ruleEngine.AddRules(rules);
74 | // Act
75 | var result = ruleEngine.ValidateAll(p);
76 | // Assert
77 | Assert.False(result);
78 | }
79 | }
80 | }
--------------------------------------------------------------------------------
/GSoulavy.RuleEngine.Tests/Kernel/ValidateAnyTests.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using GSoulavy.RuleEngine.Tests.Models;
3 | using Xunit;
4 |
5 | namespace GSoulavy.RuleEngine.Tests.Kernel
6 | {
7 | public class ValidateAnyTests
8 | {
9 | [Fact(DisplayName = "ValidateAll[with param]: false")]
10 | public void ValidateAnyW_False()
11 | {
12 | // Arrange
13 | var rules = new List
14 | {
15 | new Rule {Key = "1", Expression = "(f.Age > 3 && f.Income < 50000) || f.NumberOfChildren > 2"},
16 | new Rule {Key = "2", Expression = "(f.Age > 3 && f.Income > 100000) || f.NumberOfChildren > 5"}
17 | };
18 | var p = new Person {Age = 37, Income = 45000, NumberOfChildren = 3};
19 | var ruleEngine = new RuleEngine.Kernel();
20 | ruleEngine.AddRules(rules);
21 | // Act
22 | var result = ruleEngine.ValidateAny(p, "2");
23 | // Assert
24 | Assert.False(result);
25 | }
26 |
27 | [Fact(DisplayName = "ValidateAll[with param]: true")]
28 | public void ValidateAnyW_True()
29 | {
30 | // Arrange
31 | var rules = new List
32 | {
33 | new Rule {Key = "1", Expression = "(f.Age > 3 && f.Income < 50000) || f.NumberOfChildren > 2"},
34 | new Rule {Key = "2", Expression = "(f.Age > 3 && f.Income > 100000) || f.NumberOfChildren > 5"}
35 | };
36 | var p = new Person {Age = 37, Income = 45000, NumberOfChildren = 3};
37 | var ruleEngine = new RuleEngine.Kernel();
38 | ruleEngine.AddRules(rules);
39 | // Act
40 | var result = ruleEngine.ValidateAny(p, "1");
41 | // Assert
42 | Assert.True(result);
43 | }
44 |
45 | [Fact(DisplayName = "ValidateAll[without param]: true")]
46 | public void ValidateAnyWp_False()
47 | {
48 | // Arrange
49 | var rules = new List
50 | {
51 | new Rule {Key = "1", Expression = "(f.Age > 3 && f.Income < 50000) || f.NumberOfChildren > 2"},
52 | new Rule {Key = "2", Expression = "(f.Age > 3 && f.Income > 100000) || f.NumberOfChildren > 5"}
53 | };
54 | var p = new Person {Age = 37, Income = 45000, NumberOfChildren = 3};
55 | var ruleEngine = new RuleEngine.Kernel();
56 | ruleEngine.AddRules(rules);
57 | // Act
58 | var result = ruleEngine.ValidateAny(p);
59 | // Assert
60 | Assert.True(result);
61 | }
62 | }
63 | }
--------------------------------------------------------------------------------
/GSoulavy.RuleEngine.Tests/Kernel/ValidateCollectionTests.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using GSoulavy.RuleEngine.Tests.Models;
3 | using Xunit;
4 |
5 | namespace GSoulavy.RuleEngine.Tests.Kernel
6 | {
7 | using RuleEngine;
8 | public class ValidateCollectionTests
9 | {
10 | [Fact(DisplayName = "ValidateCollection: true")]
11 | public void ValidateCollection_True()
12 | {
13 | // Arrange
14 | var people = new List
15 | {
16 | new Person {Name = "John", Gender = Gender.Male, Age = 23, Income = 24000, NumberOfChildren = 0},
17 | new Person {Name = "Mary", Gender = Gender.Female, Age = 22, Income = 23000, NumberOfChildren = 1}
18 | };
19 |
20 | const string expression = "f.Any(p => p.Name.Equals(\"John\") && p.Age < 24)";
21 | var ruleEngine = new Kernel();
22 | // Act
23 | var result = ruleEngine.Validate>(people, expression);
24 |
25 | // Assert
26 | Assert.True(result);
27 | }
28 | }
29 | }
--------------------------------------------------------------------------------
/GSoulavy.RuleEngine.Tests/Kernel/ValidateTests.cs:
--------------------------------------------------------------------------------
1 | using GSoulavy.RuleEngine.Tests.Models;
2 | using Xunit;
3 |
4 | namespace GSoulavy.RuleEngine.Tests.Kernel
5 | {
6 | public class ValidateTests
7 | {
8 | [Fact(DisplayName = "Validate: false")]
9 | public void Validate_False()
10 | {
11 | // Arrange
12 | const string expression = @"(f.Age > 3 && f.Income > 100000) || f.NumberOfChildren > 5";
13 | var p = new Person {Age = 37, Income = 45000, NumberOfChildren = 3};
14 | var ruleEngine = new RuleEngine.Kernel();
15 | // Act
16 | var result = ruleEngine.Validate(p, expression);
17 | // Assert
18 | Assert.False(result);
19 | }
20 |
21 | [Fact(DisplayName = "Validate: true")]
22 | public void Validate_True()
23 | {
24 | // Arrange
25 | const string expression = @"(f.Age > 3 && f.Income < 50000) || f.NumberOfChildren > 2";
26 | var p = new Person {Age = 37, Income = 45000, NumberOfChildren = 3};
27 | var ruleEngine = new RuleEngine.Kernel();
28 | // Act
29 | var result = ruleEngine.Validate(p, expression);
30 | // Assert
31 | Assert.True(result);
32 | }
33 | }
34 | }
--------------------------------------------------------------------------------
/GSoulavy.RuleEngine.Tests/Models/Gender.cs:
--------------------------------------------------------------------------------
1 | namespace GSoulavy.RuleEngine.Tests.Models
2 | {
3 | public enum Gender
4 | {
5 | Male,
6 | Female
7 | }
8 | }
--------------------------------------------------------------------------------
/GSoulavy.RuleEngine.Tests/Models/Person.cs:
--------------------------------------------------------------------------------
1 | namespace GSoulavy.RuleEngine.Tests.Models
2 | {
3 | public class Person
4 | {
5 | public string Name { get; set; }
6 | public Gender Gender { get; set; }
7 | public int Age { get; set; }
8 | public double Income { get; set; }
9 |
10 | public int NumberOfChildren { get; set; }
11 | }
12 | }
--------------------------------------------------------------------------------
/GSoulavy.RuleEngine.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.26430.16
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GSoulavy.RuleEngine", "GSoulavy.RuleEngine\GSoulavy.RuleEngine.csproj", "{F2099138-9D97-46F4-8121-A13BDFC858B8}"
7 | EndProject
8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GSoulavy.RuleEngine.Tests", "GSoulavy.RuleEngine.Tests\GSoulavy.RuleEngine.Tests.csproj", "{C17860A9-5E73-431F-9C1B-0AF2FE922891}"
9 | EndProject
10 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{5951A6C1-6995-4F32-935C-E6AA1F6D8252}"
11 | ProjectSection(SolutionItems) = preProject
12 | README.md = README.md
13 | EndProjectSection
14 | EndProject
15 | Global
16 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
17 | Debug|Any CPU = Debug|Any CPU
18 | Release|Any CPU = Release|Any CPU
19 | EndGlobalSection
20 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
21 | {F2099138-9D97-46F4-8121-A13BDFC858B8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
22 | {F2099138-9D97-46F4-8121-A13BDFC858B8}.Debug|Any CPU.Build.0 = Debug|Any CPU
23 | {F2099138-9D97-46F4-8121-A13BDFC858B8}.Release|Any CPU.ActiveCfg = Release|Any CPU
24 | {F2099138-9D97-46F4-8121-A13BDFC858B8}.Release|Any CPU.Build.0 = Release|Any CPU
25 | {C17860A9-5E73-431F-9C1B-0AF2FE922891}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
26 | {C17860A9-5E73-431F-9C1B-0AF2FE922891}.Debug|Any CPU.Build.0 = Debug|Any CPU
27 | {C17860A9-5E73-431F-9C1B-0AF2FE922891}.Release|Any CPU.ActiveCfg = Release|Any CPU
28 | {C17860A9-5E73-431F-9C1B-0AF2FE922891}.Release|Any CPU.Build.0 = Release|Any CPU
29 | EndGlobalSection
30 | GlobalSection(SolutionProperties) = preSolution
31 | HideSolutionNode = FALSE
32 | EndGlobalSection
33 | EndGlobal
34 |
--------------------------------------------------------------------------------
/GSoulavy.RuleEngine/GSoulavy.RuleEngine.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.0
5 | Gab Soulavy
6 | Gab Soulavy
7 | This simple rule engine is a .NET Standard library 2.0, which uses Microsoft's DLR DynamicExpressionParser in the background. The goal was to make a simple engine which is easy to use and compatible with many projects.
8 | Gab Soulavy
9 | https://opensource.org/licenses/MIT
10 | https://github.com/gsoulavy/RuleEngine
11 | https://github.com/gsoulavy/RuleEngine
12 | https://github.com/gsoulavy/RuleEngine.git
13 | Rules Engine dotnet standard dynamic linq expression
14 | Add MIT License
15 | README.md
16 | 1.0.1.0
17 | 1.0.1.0
18 | 1.0.1
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/GSoulavy.RuleEngine/IKernel.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Runtime.InteropServices.ComTypes;
3 |
4 | namespace GSoulavy.RuleEngine
5 | {
6 | public interface IKernel
7 | {
8 | void AddRule(IRule rule);
9 | void AddRules(IEnumerable rules);
10 | void RemoveRule(IRule rule);
11 | void RemoveRules(IEnumerable rules);
12 | bool Validate(T fact, string rule);
13 | bool ValidateAll(T fact, string key = null);
14 | bool ValidateAny(T fact, string key = null);
15 | }
16 | }
--------------------------------------------------------------------------------
/GSoulavy.RuleEngine/IRule.cs:
--------------------------------------------------------------------------------
1 | namespace GSoulavy.RuleEngine
2 | {
3 | public interface IRule
4 | {
5 | string Key { get; set; }
6 | string Expression { get; set; }
7 | }
8 | }
--------------------------------------------------------------------------------
/GSoulavy.RuleEngine/Kernel.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Linq;
3 | using System.Linq.Dynamic.Core;
4 | using System.Linq.Expressions;
5 |
6 | namespace GSoulavy.RuleEngine
7 | {
8 | public class Kernel : IKernel
9 | {
10 | private readonly List<(string Key, string Value)> _rules;
11 |
12 | public Kernel()
13 | {
14 | _rules = new List<(string Key, string Value)>();
15 | }
16 |
17 | public void AddRule(IRule rule)
18 | {
19 | _rules.Add((rule.Key, rule.Expression));
20 | }
21 |
22 | public void AddRules(IEnumerable rules)
23 | {
24 | _rules.AddRange(rules.Select(r => (r.Key, r.Expression)));
25 | }
26 |
27 | public void RemoveRule(IRule rule)
28 | {
29 | _rules.Remove((rule.Key, rule.Expression));
30 | }
31 |
32 | public void RemoveRules(IEnumerable rules)
33 | {
34 | foreach (var rule in rules)
35 | _rules.Remove((rule.Key, rule.Expression));
36 | }
37 |
38 | public bool Validate(T fact, string rule)
39 | {
40 | return Evaluate(fact, rule);
41 | }
42 |
43 | public bool ValidateAll(T fact, string key = null)
44 | {
45 | return key != null
46 | ? _rules.Where(r => r.Key.Equals(key)).Select(r => r.Value).All(rule => Validate(fact, rule))
47 | : _rules.Select(r => r.Value).All(rule => Validate(fact, rule));
48 | }
49 |
50 | public bool ValidateAny(T fact, string key = null)
51 | {
52 | return key != null
53 | ? _rules.Where(r => r.Key.Equals(key)).Select(r => r.Value).Any(rule => Validate(fact, rule))
54 | : _rules.Select(r => r.Value).Any(rule => Validate(fact, rule));
55 | }
56 |
57 | public TR Evaluate(T fact, string rule)
58 | {
59 | var parameter = Expression.Parameter(typeof(T), "f");
60 | var lambdaExpression = DynamicExpressionParser.ParseLambda(new[] {parameter}, null, rule);
61 | return (TR) lambdaExpression.Compile().DynamicInvoke(fact);
62 | }
63 | }
64 | }
--------------------------------------------------------------------------------
/GSoulavy.RuleEngine/Properties/PublishProfiles/FolderProfile.pubxml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 | FileSystem
9 | Release
10 | netstandard1.4
11 | bin\Release\PublishOutput
12 |
13 |
--------------------------------------------------------------------------------
/GSoulavy.RuleEngine/Rule.cs:
--------------------------------------------------------------------------------
1 | namespace GSoulavy.RuleEngine
2 | {
3 | public class Rule : IRule
4 | {
5 | public string Key { get; set; }
6 | public string Expression { get; set; }
7 | }
8 | }
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 Gab Soulavy
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # RuleEngine
2 | [](https://github.com/gsoulavy/RuleEngine/actions/workflows/ci.yml) [](https://github.com/gsoulavy/RuleEngine/actions/workflows/release.yml) 
3 |
4 | ### Purpose
5 |
6 | This simple rule engine is a .NET Standard library 2.0, which uses Microsoft's DLR DynamicExpressionParser in the background. The goal was to make a simple engine which is easy to use and compatible with many projects.
7 |
8 | ### Use
9 |
10 | #### Instantiating the kernel
11 |
12 | The IKernel interface is implemented with Kerner in order to support Inverson Of Control.
13 |
14 | ```cs
15 | IKernel ruleEngine = new Kernel();
16 | ```
17 |
18 | ##### Role of the IRule
19 | The engine is designed to use any object as a rule which implements IRule interface in order to make it easy to use with an ORM.
20 |
21 | ```cs
22 | public interface IRule
23 | {
24 | string Key { get; set; }
25 | string Expression { get; set; }
26 | }
27 | ```
28 | ```cs
29 | //...
30 | void AddRule(IRule rule);
31 | //...
32 | ```
33 |
34 |
35 | ##### Simple Validation
36 | String expressions can be simply against the object passed to the engine.
37 | Creating a fact:
38 | ```cs
39 | var fact = new Person {Age = 37, Income = 45000, NumberOfChildren = 3};
40 | ```
41 |
42 | Validating the fact:
43 | ```cs
44 | var result = ruleEngine.Validate(fact, "(f.Age > 3 && f.Income > 100000) || f.NumberOfChildren > 5");
45 | // result = false
46 | ```
47 |
48 | ##### Validate All
49 | More than one expreession can be added to the engine
50 | ```cs
51 | var rules = new List
52 | {
53 | new Rule {Key = "1", Expression = "(f.Age > 3 && f.Income < 50000) || f.NumberOfChildren > 2"},
54 | new Rule {Key = "2", Expression = "(f.Age > 3 && f.Income > 100000) || f.NumberOfChildren > 5"}
55 | };
56 |
57 | ruleEngine.AddRules(rules);
58 |
59 | // Validate against all rules when no key passed
60 | var result = ruleEngine.ValidateAll(f);
61 | // result = false
62 |
63 | // Only validate against rules with the matching key
64 | var result = ruleEngine.ValidateAll(f, "1");
65 | // result = true
66 | ```
67 |
68 | ##### Validate Any
69 | The calls are the same as the case of validate all, however it returns true if any case is true.
70 | ```cs
71 | var rules = new List
72 | {
73 | new Rule {Key = "1", Expression = "(f.Age > 3 && f.Income < 50000) || f.NumberOfChildren > 2"},
74 | new Rule {Key = "2", Expression = "(f.Age > 3 && f.Income > 100000) || f.NumberOfChildren > 5"}
75 | };
76 |
77 | ruleEngine.AddRules(rules);
78 |
79 | // Validate against all rules when no key passed
80 | var result = ruleEngine.ValidateAny(f);
81 | // result = true
82 |
83 | // Only validate against rules with the matching key
84 | var result = ruleEngine.ValidateAny(f, "1");
85 | // result = true
86 | ```
87 |
--------------------------------------------------------------------------------