Abbasc52/combined fixes (#341)

* Fixed actions not working with complex objects

* removed Microsoft.Extensions.Caching dependency

* * updated nuget packages
* update dotnet version to 6 for demo/test proj
* improved integration with DynamicLinq

* added test case for jsonElement getProperty method

* updated test cases to cover case insensitivity

* updated workflow schema

* added schema for list of workflows

* fixed schema name and added to solution

* Update dotnetcore-build.yml

* Update dotnetcore-build.yml

* updated unit test proj to point to dotnet 6

* removed ilogger
pull/342/head
Abbas Cyclewala 2022-04-12 19:24:16 +05:30 committed by GitHub
parent 99bad9ffff
commit 571490455c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
30 changed files with 1603 additions and 1403 deletions

View File

@ -3,7 +3,7 @@ updates:
- package-ecosystem: "github-actions"
# default location of `.github/workflows`
directory: "/"
open-pull-requests-limit: 10
open-pull-requests-limit: 3
schedule:
interval: "weekly"
# assignees:
@ -14,9 +14,9 @@ updates:
- package-ecosystem: "nuget"
# location of package manifests
directory: "/"
open-pull-requests-limit: 10
open-pull-requests-limit: 3
schedule:
interval: "daily"
interval: "weekly"
ignore:
- dependency-name: "*"
update-types: ["version-update:semver-minor"]

View File

@ -31,12 +31,12 @@ jobs:
- uses: actions/checkout@v2
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v2
with:
dotnet-version: 3.1
dotnet-version: 6.0.x
- name: Install minicover
run: dotnet tool install --global minicover --version 3.0.6
run: dotnet tool install --global minicover --version 3.4.4
- name: Install dependencies
run: dotnet restore RulesEngine.sln

View File

@ -18,6 +18,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
CHANGELOG.md = CHANGELOG.md
global.json = global.json
README.md = README.md
schema\workflow-list-schema.json = schema\workflow-list-schema.json
schema\workflow-schema.json = schema\workflow-schema.json
EndProjectSection
EndProject

View File

@ -36,7 +36,7 @@ namespace RulesEngineBenchmark
var fileData = File.ReadAllText(files[0]);
workflow = JsonConvert.DeserializeObject<List<Workflow>>(fileData);
rulesEngine = new RulesEngine.RulesEngine(workflow.ToArray(), null, new ReSettings {
rulesEngine = new RulesEngine.RulesEngine(workflow.ToArray(), new ReSettings {
EnableFormattedErrorMessage = false,
EnableScopedParams = false
});

View File

@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>

View File

@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>DemoApp.EFDataExample</RootNamespace>
<AssemblyName>DemoApp.EFDataExample</AssemblyName>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.3" />
</ItemGroup>
<ItemGroup>

View File

@ -24,18 +24,20 @@ namespace RulesEngine.Data
entity.Ignore(b => b.WorkflowsToInject);
});
var serializationOptions = new JsonSerializerOptions(JsonSerializerDefaults.General);
modelBuilder.Entity<Rule>(entity => {
entity.HasKey(k => k.RuleName);
entity.Property(b => b.Properties)
.HasConversion(
v => JsonSerializer.Serialize(v, null),
v => JsonSerializer.Deserialize<Dictionary<string, object>>(v, null));
v => JsonSerializer.Serialize(v, serializationOptions),
v => JsonSerializer.Deserialize<Dictionary<string, object>>(v, serializationOptions)); ;
entity.Property(p => p.Actions)
.HasConversion(
v => JsonSerializer.Serialize(v, null),
v => JsonSerializer.Deserialize<RuleActions>(v, null));
v => JsonSerializer.Serialize(v, serializationOptions),
v => JsonSerializer.Deserialize<RuleActions>(v, serializationOptions));
entity.Ignore(b => b.WorkflowsToInject);
});

View File

@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<StartupObject>DemoApp.Program</StartupObject>
</PropertyGroup>

View File

@ -1,6 +1,6 @@
{
"sdk": {
"version": "3.1",
"version": "6.0",
"rollForward": "latestFeature",
"allowPrerelease": false
}

View File

@ -0,0 +1,7 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"items": {
"$ref": "https://raw.githubusercontent.com/microsoft/RulesEngine/main/schema/workflow-schema.json"
}
}

View File

@ -1,12 +1,24 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"ScopedParam": {
"type": "object",
"properties": {
"Name": { "type": "string" },
"Expression": { "type": "string" }
},
"required": [ "Name", "Expression" ]
},
"Rule": {
"title": "Rule",
"properties": {
"RuleName": {
"type": "string"
},
"LocalParams": {
"type": "array",
"items": { "$ref": "#/definitions/ScopedParam" }
},
"Operator": {
"enum": [
"And",
@ -18,12 +30,6 @@
"ErrorMessage": {
"type": "string"
},
"ErrorType": {
"enum": [
"Warning",
"Error"
]
},
"SuccessEvent": {
"type": "string"
},
@ -45,6 +51,10 @@
},
"Actions": {
"$ref": "#/definitions/RuleActions"
},
"Enabled": {
"type": "boolean",
"default": true
}
},
"required": [
@ -65,6 +75,10 @@
"RuleName": {
"type": "string"
},
"LocalParams": {
"type": "array",
"items": { "$ref": "#/definitions/ScopedParam" }
},
"Expression": {
"type": "string"
},
@ -76,12 +90,6 @@
"ErrorMessage": {
"type": "string"
},
"ErrorType": {
"enum": [
"Warning",
"Error"
]
},
"SuccessEvent": {
"type": "string"
},
@ -90,6 +98,10 @@
},
"Actions": {
"$ref": "#/definitions/RuleActions"
},
"Enabled": {
"type": "boolean",
"default": true
}
}
},
@ -116,11 +128,20 @@
}
}
}
},
"properties": {
"WorkFlowName": {
"WorkflowName": {
"type": "string"
},
"WorkflowsToInject": {
"type": "array",
"items": { "type": "string" }
},
"GlobalParams": {
"type": "array",
"items": { "$ref": "#/definitions/ScopedParam" }
},
"Rules": {
"type": "array",
"items": {
@ -136,7 +157,7 @@
}
},
"required": [
"WorkFlowName",
"WorkflowName",
"Rules"
],
"type": "object"

View File

@ -43,7 +43,7 @@ namespace RulesEngine.ExpressionBuilders
}
}
internal override LambdaExpression Parse(string expression, ParameterExpression[] parameters, Type returnType)
internal override Expression Parse(string expression, ParameterExpression[] parameters, Type returnType)
{
try
{

View File

@ -22,7 +22,7 @@ namespace RulesEngine.ExpressionBuilders
/// <returns>Expression type</returns>
internal abstract RuleFunc<RuleResultTree> BuildDelegateForRule(Rule rule, RuleParameter[] ruleParams);
internal abstract LambdaExpression Parse(string expression, ParameterExpression[] parameters, Type returnType);
internal abstract Expression Parse(string expression, ParameterExpression[] parameters, Type returnType);
internal abstract Func<object[], Dictionary<string, object>> CompileScopedParams(RuleParameter[] ruleParameters, RuleExpressionParameter[] scopedParameters);
}

View File

@ -2,12 +2,13 @@
// Licensed under the MIT License.
using FastExpressionCompiler;
using Microsoft.Extensions.Caching.Memory;
using RulesEngine.HelperFunctions;
using RulesEngine.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Dynamic.Core.Parser;
using System.Linq.Expressions;
using System.Reflection;
@ -16,13 +17,13 @@ namespace RulesEngine.ExpressionBuilders
public class RuleExpressionParser
{
private readonly ReSettings _reSettings;
private static IMemoryCache _memoryCache;
private static MemCache _memoryCache;
private readonly IDictionary<string, MethodInfo> _methodInfo;
public RuleExpressionParser(ReSettings reSettings)
{
_reSettings = reSettings;
_memoryCache = _memoryCache ?? new MemoryCache(new MemoryCacheOptions {
_memoryCache = _memoryCache ?? new MemCache(new MemCacheConfig {
SizeLimit = 1000
});
_methodInfo = new Dictionary<string, MethodInfo>();
@ -34,22 +35,30 @@ namespace RulesEngine.ExpressionBuilders
var dict_add = typeof(Dictionary<string, object>).GetMethod("Add", BindingFlags.Public | BindingFlags.Instance, null, new[] { typeof(string), typeof(object) }, null);
_methodInfo.Add("dict_add", dict_add);
}
public LambdaExpression Parse(string expression, ParameterExpression[] parameters, Type returnType)
public Expression Parse(string expression, ParameterExpression[] parameters, Type returnType)
{
var config = new ParsingConfig { CustomTypeProvider = new CustomTypeProvider(_reSettings.CustomTypes) };
return new ExpressionParser(parameters, expression, new object[] { }, config).Parse(returnType);
return DynamicExpressionParser.ParseLambda(config, false, parameters, returnType, expression);
}
public Func<object[], T> Compile<T>(string expression, RuleParameter[] ruleParams)
{
var rtype = typeof(T);
if(rtype == typeof(object))
{
rtype = null;
}
var cacheKey = GetCacheKey(expression, ruleParams, typeof(T));
return _memoryCache.GetOrCreate(cacheKey, (entry) => {
entry.SetSize(1);
return _memoryCache.GetOrCreate(cacheKey, () => {
var parameterExpressions = GetParameterExpression(ruleParams).ToArray();
var e = Parse(expression, parameterExpressions, typeof(T));
var expressionBody = new List<Expression>() { e.Body };
var e = Parse(expression, parameterExpressions, rtype);
if(rtype == null)
{
e = Expression.Convert(e, typeof(T));
}
var expressionBody = new List<Expression>() { e };
var wrappedExpression = WrapExpression<T>(expressionBody, parameterExpressions, new ParameterExpression[] { });
return wrappedExpression.CompileFast();
});

View File

@ -10,7 +10,7 @@ namespace RulesEngine.HelperFunctions
{
public static bool CheckContains(string check, string valList)
{
if (String.IsNullOrEmpty(check) || String.IsNullOrEmpty(valList))
if (string.IsNullOrEmpty(check) || string.IsNullOrEmpty(valList))
return false;
var list = valList.Split(',').ToList();

View File

@ -0,0 +1,115 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
namespace RulesEngine.HelperFunctions
{
public class MemCacheConfig {
public int SizeLimit { get; set; } = 1000;
}
internal class MemCache
{
private readonly MemCacheConfig _config;
private ConcurrentDictionary<string, (object value, DateTimeOffset expiry)> _cacheDictionary;
private ConcurrentQueue<(string key, DateTimeOffset expiry)> _cacheEvictionQueue;
public MemCache(MemCacheConfig config)
{
if(config == null)
{
config = new MemCacheConfig();
}
_config = config;
_cacheDictionary = new ConcurrentDictionary<string, (object value, DateTimeOffset expiry)>();
_cacheEvictionQueue = new ConcurrentQueue<(string key, DateTimeOffset expiry)>();
}
public bool TryGetValue<T>(string key,out T value)
{
value = default;
if (_cacheDictionary.TryGetValue(key, out var cacheItem))
{
if(cacheItem.expiry < DateTimeOffset.UtcNow)
{
_cacheDictionary.TryRemove(key, out _);
return false;
}
else
{
value = (T)cacheItem.value;
return true;
}
}
return false;
}
public T Get<T>(string key)
{
TryGetValue<T>(key, out var value);
return value;
}
/// <summary>
/// Returns all known keys. May return keys for expired data as well
/// </summary>
/// <returns></returns>
public IEnumerable<string> GetKeys()
{
return _cacheDictionary.Keys;
}
public T GetOrCreate<T>(string key, Func<T> createFn, DateTimeOffset? expiry = null)
{
if(!TryGetValue<T>(key,out var value))
{
value = createFn();
return Set<T>(key,value,expiry);
}
return value;
}
public T Set<T>(string key, T value, DateTimeOffset? expiry = null)
{
var fixedExpiry = expiry ?? DateTimeOffset.MaxValue;
while (_cacheDictionary.Count > _config.SizeLimit)
{
if (_cacheEvictionQueue.IsEmpty)
{
_cacheDictionary.Clear();
}
if(_cacheEvictionQueue.TryDequeue(out var result)
&& _cacheDictionary.TryGetValue(result.key,out var dictionaryValue)
&& dictionaryValue.expiry == result.expiry)
{
_cacheDictionary.TryRemove(result.key, out _);
}
}
_cacheDictionary.AddOrUpdate(key, (value, fixedExpiry), (k, v) => (value, fixedExpiry));
_cacheEvictionQueue.Enqueue((key, fixedExpiry));
return value;
}
public void Remove(string key)
{
_cacheDictionary.TryRemove(key, out _);
}
public void Clear()
{
_cacheDictionary.Clear();
_cacheEvictionQueue = new ConcurrentQueue<(string key, DateTimeOffset expiry)>();
}
}
}

View File

@ -2,6 +2,7 @@
// Licensed under the MIT License.
using RulesEngine.Actions;
using RulesEngine.HelperFunctions;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
@ -57,6 +58,8 @@ namespace RulesEngine.Models
get { return EnableScopedParams; }
set { EnableScopedParams = value; }
}
public MemCacheConfig CacheConfig { get; set; }
}
public enum NestedRuleExecutionMode

View File

@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Microsoft.Extensions.Logging;
using RulesEngine.Exceptions;
using RulesEngine.ExpressionBuilders;
using RulesEngine.HelperFunctions;
@ -10,7 +9,6 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.InteropServices;
namespace RulesEngine
{
@ -30,20 +28,13 @@ namespace RulesEngine
private readonly RuleExpressionBuilderFactory _expressionBuilderFactory;
private readonly ReSettings _reSettings;
/// <summary>
/// The logger
/// </summary>
private readonly ILogger _logger;
/// <summary>
/// Initializes a new instance of the <see cref="RuleCompiler"/> class.
/// </summary>
/// <param name="expressionBuilderFactory">The expression builder factory.</param>
/// <exception cref="ArgumentNullException">expressionBuilderFactory</exception>
internal RuleCompiler(RuleExpressionBuilderFactory expressionBuilderFactory, ReSettings reSettings, ILogger logger)
internal RuleCompiler(RuleExpressionBuilderFactory expressionBuilderFactory, ReSettings reSettings)
{
_logger = logger ?? throw new ArgumentNullException($"{nameof(logger)} can't be null.");
_expressionBuilderFactory = expressionBuilderFactory ?? throw new ArgumentNullException($"{nameof(expressionBuilderFactory)} can't be null.");
_reSettings = reSettings;
}
@ -61,7 +52,6 @@ namespace RulesEngine
if (rule == null)
{
var ex = new ArgumentNullException(nameof(rule));
_logger.LogError(ex.Message);
throw ex;
}
try
@ -75,7 +65,6 @@ namespace RulesEngine
catch (Exception ex)
{
var message = $"Error while compiling rule `{rule.RuleName}`: {ex.Message}";
_logger.LogError(message);
return Helpers.ToRuleExceptionResult(_reSettings, rule, new RuleException(message, ex));
}
}
@ -131,7 +120,7 @@ namespace RulesEngine
{
try
{
var lpExpression = expressionBuilder.Parse(lp.Expression, parameters.ToArray(), null).Body;
var lpExpression = expressionBuilder.Parse(lp.Expression, parameters.ToArray(), null);
var ruleExpParam = new RuleExpressionParameter() {
ParameterExpression = Expression.Parameter(lpExpression.Type, lp.Name),
ValueExpression = lpExpression
@ -187,7 +176,7 @@ namespace RulesEngine
return (paramArray) => {
var (isSuccess, resultList) = ApplyOperation(paramArray, ruleFuncList, operation);
Func<object[], bool> isSuccessFn = (p) => isSuccess;
bool isSuccessFn(object[] p) => isSuccess;
var result = Helpers.ToResultTree(_reSettings, parentRule, resultList, isSuccessFn);
return result(paramArray);
};

View File

@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using RulesEngine.HelperFunctions;
using RulesEngine.Models;
using System;
using System.Collections.Concurrent;
@ -13,10 +14,17 @@ namespace RulesEngine
internal class RulesCache
{
/// <summary>The compile rules</summary>
private ConcurrentDictionary<string, (IDictionary<string, RuleFunc<RuleResultTree>>, Int64)> _compileRules = new ConcurrentDictionary<string, (IDictionary<string, RuleFunc<RuleResultTree>>, Int64)>();
private readonly MemCache _compileRules;
/// <summary>The workflow rules</summary>
private ConcurrentDictionary<string, (Workflow, Int64)> _workflow = new ConcurrentDictionary<string, (Workflow, Int64)>();
private readonly ConcurrentDictionary<string, (Workflow, long)> _workflow = new ConcurrentDictionary<string, (Workflow, long)>();
public RulesCache(ReSettings reSettings)
{
_compileRules = new MemCache(reSettings.CacheConfig);
}
/// <summary>Determines whether [contains workflow rules] [the specified workflow name].</summary>
/// <param name="workflowName">Name of the workflow.</param>
@ -32,21 +40,12 @@ namespace RulesEngine
return _workflow.Keys.ToList();
}
/// <summary>Determines whether [contains compiled rules] [the specified workflow name].</summary>
/// <param name="workflowName">Name of the workflow.</param>
/// <returns>
/// <c>true</c> if [contains compiled rules] [the specified workflow name]; otherwise, <c>false</c>.</returns>
public bool ContainsCompiledRules(string workflowName)
{
return _compileRules.ContainsKey(workflowName);
}
/// <summary>Adds the or update workflow rules.</summary>
/// <param name="workflowName">Name of the workflow.</param>
/// <param name="rules">The rules.</param>
public void AddOrUpdateWorkflows(string workflowName, Workflow rules)
{
Int64 ticks = DateTime.UtcNow.Ticks;
long ticks = DateTime.UtcNow.Ticks;
_workflow.AddOrUpdate(workflowName, (rules, ticks), (k, v) => (rules, ticks));
}
@ -55,8 +54,8 @@ namespace RulesEngine
/// <param name="compiledRule">The compiled rule.</param>
public void AddOrUpdateCompiledRule(string compiledRuleKey, IDictionary<string, RuleFunc<RuleResultTree>> compiledRule)
{
Int64 ticks = DateTime.UtcNow.Ticks;
_compileRules.AddOrUpdate(compiledRuleKey, (compiledRule, ticks), (k, v) => (compiledRule, ticks));
long ticks = DateTime.UtcNow.Ticks;
_compileRules.Set(compiledRuleKey,(compiledRule, ticks));
}
/// <summary>Checks if the compiled rules are up-to-date.</summary>
@ -66,9 +65,9 @@ namespace RulesEngine
/// <c>true</c> if [compiled rules] is newer than the [workflow rules]; otherwise, <c>false</c>.</returns>
public bool AreCompiledRulesUpToDate(string compiledRuleKey, string workflowName)
{
if (_compileRules.TryGetValue(compiledRuleKey, out (IDictionary<string, RuleFunc<RuleResultTree>> rules, Int64 tick) compiledRulesObj))
if (_compileRules.TryGetValue(compiledRuleKey, out (IDictionary<string, RuleFunc<RuleResultTree>> rules, long tick) compiledRulesObj))
{
if (_workflow.TryGetValue(workflowName, out (Workflow rules, Int64 tick) WorkflowsObj))
if (_workflow.TryGetValue(workflowName, out (Workflow rules, long tick) WorkflowsObj))
{
return compiledRulesObj.tick >= WorkflowsObj.tick;
}
@ -90,7 +89,7 @@ namespace RulesEngine
/// <exception cref="Exception">Could not find injected Workflow: {wfname}</exception>
public Workflow GetWorkflow(string workflowName)
{
if (_workflow.TryGetValue(workflowName, out (Workflow rules, Int64 tick) WorkflowsObj))
if (_workflow.TryGetValue(workflowName, out (Workflow rules, long tick) WorkflowsObj))
{
var workflow = WorkflowsObj.rules;
if (workflow.WorkflowsToInject?.Any() == true)
@ -125,19 +124,19 @@ namespace RulesEngine
/// <returns>CompiledRule.</returns>
public IDictionary<string, RuleFunc<RuleResultTree>> GetCompiledRules(string compiledRulesKey)
{
return _compileRules[compiledRulesKey].Item1;
return _compileRules.Get<(IDictionary<string, RuleFunc<RuleResultTree>> rules, long tick)>(compiledRulesKey).rules;
}
/// <summary>Removes the specified workflow name.</summary>
/// <param name="workflowName">Name of the workflow.</param>
public void Remove(string workflowName)
{
if (_workflow.TryRemove(workflowName, out (Workflow, Int64) workflowObj))
if (_workflow.TryRemove(workflowName, out var workflowObj))
{
var compiledKeysToRemove = _compileRules.Keys.Where(key => key.StartsWith(workflowName));
var compiledKeysToRemove = _compileRules.GetKeys().Where(key => key.StartsWith(workflowName));
foreach (var key in compiledKeysToRemove)
{
_compileRules.TryRemove(key, out (IDictionary<string, RuleFunc<RuleResultTree>>, Int64) val);
_compileRules.Remove(key);
}
}
}

View File

@ -2,13 +2,12 @@
// Licensed under the MIT License.
using FluentValidation;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RulesEngine.Actions;
using RulesEngine.Exceptions;
using RulesEngine.ExpressionBuilders;
using RulesEngine.HelperFunctions;
using RulesEngine.Interfaces;
using RulesEngine.Models;
using RulesEngine.Validators;
@ -27,9 +26,8 @@ namespace RulesEngine
public class RulesEngine : IRulesEngine
{
#region Variables
private readonly ILogger _logger;
private readonly ReSettings _reSettings;
private readonly RulesCache _rulesCache = new RulesCache();
private readonly RulesCache _rulesCache;
private readonly RuleExpressionParser _ruleExpressionParser;
private readonly RuleCompiler _ruleCompiler;
private readonly ActionFactory _actionFactory;
@ -37,23 +35,27 @@ namespace RulesEngine
#endregion
#region Constructor
public RulesEngine(string[] jsonConfig, ILogger logger = null, ReSettings reSettings = null) : this(logger, reSettings)
public RulesEngine(string[] jsonConfig, ReSettings reSettings = null) : this(reSettings)
{
var workflow = jsonConfig.Select(item => JsonConvert.DeserializeObject<Workflow>(item)).ToArray();
AddWorkflow(workflow);
}
public RulesEngine(Workflow[] Workflows, ILogger logger = null, ReSettings reSettings = null) : this(logger, reSettings)
public RulesEngine(Workflow[] Workflows, ReSettings reSettings = null) : this(reSettings)
{
AddWorkflow(Workflows);
}
public RulesEngine(ILogger logger = null, ReSettings reSettings = null)
public RulesEngine(ReSettings reSettings = null)
{
_logger = logger ?? new NullLogger<RulesEngine>();
_reSettings = reSettings ?? new ReSettings();
if(_reSettings.CacheConfig == null)
{
_reSettings.CacheConfig = new MemCacheConfig();
}
_rulesCache = new RulesCache(_reSettings);
_ruleExpressionParser = new RuleExpressionParser(_reSettings);
_ruleCompiler = new RuleCompiler(new RuleExpressionBuilderFactory(_reSettings, _ruleExpressionParser),_reSettings, _logger);
_ruleCompiler = new RuleCompiler(new RuleExpressionBuilderFactory(_reSettings, _ruleExpressionParser),_reSettings);
_actionFactory = new ActionFactory(GetActionRegistry(_reSettings));
}
@ -80,8 +82,6 @@ namespace RulesEngine
/// <returns>List of rule results</returns>
public async ValueTask<List<RuleResultTree>> ExecuteAllRulesAsync(string workflowName, params object[] inputs)
{
_logger.LogTrace($"Called {nameof(ExecuteAllRulesAsync)} for workflow {workflowName} and count of input {inputs.Count()}");
var ruleParams = new List<RuleParameter>();
for (var i = 0; i < inputs.Length; i++)
@ -258,7 +258,6 @@ namespace RulesEngine
}
else
{
_logger.LogTrace($"Rule config file is not present for the {workflowName} workflow");
// if rules are not registered with Rules Engine
throw new ArgumentException($"Rule config file is not present for the {workflowName} workflow");
}
@ -291,7 +290,6 @@ namespace RulesEngine
}
_rulesCache.AddOrUpdateCompiledRule(compileRulesKey, dictFunc);
_logger.LogTrace($"Rules has been compiled for the {workflowName} workflow and added to dictionary");
return true;
}
else
@ -331,8 +329,6 @@ namespace RulesEngine
/// <returns>list of rule result set</returns>
private List<RuleResultTree> ExecuteAllRuleByWorkflow(string workflowName, RuleParameter[] ruleParameters)
{
_logger.LogTrace($"Compiled rules found for {workflowName} workflow and executed");
var result = new List<RuleResultTree>();
var compiledRulesCacheKey = GetCompiledRulesKey(workflowName, ruleParameters);
foreach (var compiledRule in _rulesCache.GetCompiledRules(compiledRulesCacheKey)?.Values)

View File

@ -31,15 +31,13 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="FastExpressionCompiler" Version="3.2.1" />
<PackageReference Include="FluentValidation" Version="10.3.0" />
<PackageReference Include="FastExpressionCompiler" Version="3.2.2" />
<PackageReference Include="FluentValidation" Version="10.4.0" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1.6" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.Linq" Version="4.3.0" />
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.2.14" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="3.1.6" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.2.18" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
</ItemGroup>

View File

@ -19,7 +19,7 @@ namespace RulesEngine.UnitTest.ActionTests
public async Task CustomActionOnRuleMustHaveContextValues()
{
var workflow = GetWorkflow();
var re = new RulesEngine(workflow, null, reSettings: new ReSettings {
var re = new RulesEngine(workflow, reSettings: new ReSettings {
CustomActions = new Dictionary<string, System.Func<Actions.ActionBase>> {
{ "ReturnContext", () => new ReturnContextAction() }
@ -39,7 +39,7 @@ namespace RulesEngine.UnitTest.ActionTests
var workflowViaTextJson = System.Text.Json.JsonSerializer.Deserialize<Workflow[]>(workflowStr,serializationOptions);
var re = new RulesEngine(workflow, null, reSettings: new ReSettings {
var re = new RulesEngine(workflow, reSettings: new ReSettings {
CustomActions = new Dictionary<string, System.Func<Actions.ActionBase>> {
{ "ReturnContext", () => new ReturnContextAction() }

View File

@ -23,6 +23,16 @@ namespace RulesEngine.UnitTest
Assert.Equal(2 * 2, result.Output);
}
[Fact]
public async Task WhenExpressionIsSuccess_ComplexOutputExpressionAction_ReturnsExpressionEvaluation()
{
var engine = new RulesEngine(GetWorkflowWithActions());
var result = await engine.ExecuteActionWorkflowAsync("ActionWorkflow", "ComplexOutputRuleTest", new RuleParameter[0]);
Assert.NotNull(result);
dynamic output = result.Output;
Assert.Equal(2, output.test);
}
[Fact]
public async Task WhenExpressionIsSuccess_EvaluateRuleAction_ReturnsExpressionEvaluation()
{
@ -108,6 +118,19 @@ namespace RulesEngine.UnitTest
}
}
},
new Rule{
RuleName = "ComplexOutputRuleTest",
RuleExpressionType = RuleExpressionType.LambdaExpression,
Expression = "1 == 1",
Actions = new RuleActions{
OnSuccess = new ActionInfo{
Name = "OutputExpression",
Context = new Dictionary<string, object>{
{"expression", "new (2 as test)"}
}
}
}
},
new Rule{
RuleName = "EvaluateRuleTest",
RuleExpressionType = RuleExpressionType.LambdaExpression,

View File

@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Microsoft.Extensions.Logging;
using Moq;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
@ -16,6 +15,7 @@ using System.Dynamic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using System.Threading.Tasks;
using Xunit;
@ -171,7 +171,9 @@ namespace RulesEngine.UnitTest
dynamic input17 = GetInput2();
dynamic input18 = GetInput3();
List<RuleResultTree> result = await re.ExecuteAllRulesAsync("inputWorkflow", input1, input2, input3, input4, input5, input6, input7, input8, input9, input10, input11, input12, input13, input14, input15, input16, input17, input18);
List<RuleResultTree> result = await re.ExecuteAllRulesAsync("inputWorkflow",
input1, input2, input3, input4, input5, input6, input7, input8, input9, input10, input11, input12, input13, input14, input15, input16, input17, input18);
//, input9, input10, input11, input12, input13, input14, input15, input16, input17, input18);
Assert.NotNull(result);
Assert.IsType<List<RuleResultTree>>(result);
Assert.Contains(result, c => c.IsSuccess);
@ -386,15 +388,9 @@ namespace RulesEngine.UnitTest
var re = GetRulesEngine(ruleFileName);
dynamic input1 = new ExpandoObject();
if (propValue != null)
{
input1.Property1 = propValue;
}
if (propValue == null)
{
input1.Property1 = null;
}
input1.Property1 = propValue;
var utils = new TestInstanceUtils();
@ -511,6 +507,29 @@ namespace RulesEngine.UnitTest
}
[Theory]
[InlineData("rules10.json")]
public async Task ExecuteRuleWithJsonElement(string ruleFileName)
{
var re = GetRulesEngine(ruleFileName, new ReSettings() {
EnableExceptionAsErrorMessage = true,
CustomTypes = new Type[] { typeof(System.Text.Json.JsonElement) }
});
var input1 = new {
Data = System.Text.Json.JsonSerializer.SerializeToElement(new {
category= "abc"
})
};
var result = await re.ExecuteAllRulesAsync("inputWorkflow", new RuleParameter("input1", input1));
Assert.NotNull(result);
Assert.All(result, c => Assert.True(c.IsSuccess));
}
[Fact]
public async Task ExecuteRule_RuntimeError_ShouldReturnAsErrorMessage()
{
@ -525,7 +544,7 @@ namespace RulesEngine.UnitTest
}
};
var re = new RulesEngine(new[] { workflow }, null, null);
var re = new RulesEngine(new[] { workflow }, null);
var input = new RuleTestClass {
Country = null
};
@ -552,7 +571,7 @@ namespace RulesEngine.UnitTest
}
};
var re = new RulesEngine(new[] { workflow }, null, new ReSettings {
var re = new RulesEngine(new[] { workflow }, new ReSettings {
EnableExceptionAsErrorMessage = false
});
var input = new RuleTestClass {
@ -577,7 +596,7 @@ namespace RulesEngine.UnitTest
}
};
var re = new RulesEngine(new[] { workflow }, null, new ReSettings {
var re = new RulesEngine(new[] { workflow }, new ReSettings {
IgnoreException = true
});
var input = new RuleTestClass {
@ -711,7 +730,7 @@ namespace RulesEngine.UnitTest
var workflowStr = "{\"WorkflowName\":\"Exámple\",\"WorkflowsToInject\":null,\"GlobalParams\":null,\"Rules\":[{\"RuleName\":\"RuleWithLocalParam\",\"Properties\":null,\"Operator\":null,\"ErrorMessage\":null,\"Enabled\":true,\"ErrorType\":\"Warning\",\"RuleExpressionType\":\"LambdaExpression\",\"WorkflowsToInject\":null,\"Rules\":null,\"LocalParams\":null,\"Expression\":\"input1 == null || input1.hello.world = \\\"wow\\\"\",\"Actions\":null,\"SuccessEvent\":null}]}";
var re = new RulesEngine(new string[] { workflowStr },null,null);
var re = new RulesEngine(new string[] { workflowStr }, null);
dynamic input1 = new ExpandoObject();
input1.hello = new ExpandoObject();
@ -779,8 +798,7 @@ namespace RulesEngine.UnitTest
};
var injectWorkflowStr = JsonConvert.SerializeObject(injectWorkflow);
var mockLogger = new Mock<ILogger>();
return new RulesEngine(new string[] { data, injectWorkflowStr }, mockLogger.Object, reSettings);
return new RulesEngine(new string[] { data, injectWorkflowStr }, reSettings);
}
private string GetFileContent(string filename)

View File

@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Microsoft.Extensions.Logging.Abstractions;
using RulesEngine.ExpressionBuilders;
using RulesEngine.Models;
using System;
@ -17,10 +16,10 @@ namespace RulesEngine.UnitTest
[Fact]
public void RuleCompiler_NullCheck()
{
Assert.Throws<ArgumentNullException>(() => new RuleCompiler(null, null,null));
Assert.Throws<ArgumentNullException>(() => new RuleCompiler(null, null));
var reSettings = new ReSettings();
var parser = new RuleExpressionParser(reSettings);
Assert.Throws<ArgumentNullException>(() => new RuleCompiler(new RuleExpressionBuilderFactory(reSettings, parser), null,null));
Assert.Throws<ArgumentNullException>(() => new RuleCompiler(null, null));
}
[Fact]
@ -28,11 +27,9 @@ namespace RulesEngine.UnitTest
{
var reSettings = new ReSettings();
var parser = new RuleExpressionParser(reSettings);
var compiler = new RuleCompiler(new RuleExpressionBuilderFactory(reSettings, parser),null, new NullLogger<RuleCompiler>());
var compiler = new RuleCompiler(new RuleExpressionBuilderFactory(reSettings, parser),null);
Assert.Throws<ArgumentNullException>(() => compiler.CompileRule(null, null,null));
Assert.Throws<ArgumentNullException>(() => compiler.CompileRule(null, new RuleParameter[] { null },null));
}
}
}

View File

@ -1,21 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\..\signing\RulesEngine-publicKey.snk</AssemblyOriginatorKeyFile>
<DelaySign>True</DelaySign>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoFixture" Version="4.17.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="System.Text.Json" Version="5.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="Moq" Version="4.17.2" />
<PackageReference Include="System.Text.Json" Version="6.0.2" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.0">
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
@ -48,6 +48,9 @@
<None Update="TestData\rules8.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestData\rules10.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestData\rules9.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>

View File

@ -26,7 +26,7 @@ namespace RulesEngine.UnitTest
{
var workflow = GetWorkflowList();
var engine = new RulesEngine(null, null);
var engine = new RulesEngine();
engine.AddWorkflow(workflow);
var input1 = new {
@ -47,7 +47,7 @@ namespace RulesEngine.UnitTest
{
var workflow = GetWorkflowList();
var engine = new RulesEngine(null, null);
var engine = new RulesEngine();
engine.AddWorkflow(workflow);
var input1 = new {
@ -77,7 +77,7 @@ namespace RulesEngine.UnitTest
{
var workflow = GetWorkflowList();
var engine = new RulesEngine(new string[] { }, null, new ReSettings {
var engine = new RulesEngine(new string[] { }, new ReSettings {
EnableScopedParams = false
});
engine.AddWorkflow(workflow);

View File

@ -0,0 +1,11 @@
{
"WorkflowName": "inputWorkflow",
"Rules": [
{
"RuleName": "GiveDiscount10",
"SuccessEvent": "10",
"RuleExpressionType": "LambdaExpression",
"Expression": "input1.Data.GetProperty(\"category\").GetString() == \"abc\""
}
]
}

View File

@ -2,12 +2,20 @@
"WorkflowName": "inputWorkflow",
"Rules": [
{
"RuleName": "GiveDiscount10",
"RuleName": "upperCaseAccess",
"SuccessEvent": "10",
"ErrorMessage": "One or more adjust rules failed.",
"ErrorType": "Error",
"RuleExpressionType": "LambdaExpression",
"Expression": "utils.CheckExists(String(input1.Property1)) == true"
},
{
"RuleName": "lowerCaseAccess",
"SuccessEvent": "10",
"ErrorMessage": "One or more adjust rules failed.",
"ErrorType": "Error",
"RuleExpressionType": "LambdaExpression",
"Expression": "utils.CheckExists(String(input1.property1)) == true"
}
]
}