Simplified the IRulesEngine interface to have a params array for Obje… (#14)

* Simplified the IRulesEngine interface to have a params array for Objects and Rule Parameters.  Also updated the code to use the ILogger from Microsoft.Extensions.Logging.

* Removed custom NullLogger and replaced with Microsoft NullLogger from Microsoft.Extensions.Logging.Abstractions package.
pull/21/head
Todd Meinershagen 2020-05-12 05:22:50 -05:00 committed by GitHub
parent dbb8cfa238
commit e920138edd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 96 additions and 146 deletions

View File

@ -1,13 +0,0 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
namespace RulesEngine
{
public interface ILogger
{
void LogTrace(string msg);
void LogError(Exception ex);
}
}

View File

@ -1,46 +0,0 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using RulesEngine.Models;
using System.Collections.Generic;
namespace RulesEngine.Interfaces
{
public interface IRulesEngine
{
/// <summary>
/// This will execute all the rules of the specified workflow
/// </summary>
/// <param name="workflowName"></param>
/// <param name="input"></param>
/// <param name="otherInputs"></param>
/// <returns>List of Result</returns>
List<RuleResultTree> ExecuteRule(string workflowName, IEnumerable<dynamic> input, object[] otherInputs);
/// <summary>
/// This will execute all the rules of the specified workflow
/// </summary>
/// <param name="workflowName"></param>
/// <param name="inputs"></param>
/// <returns>List of Result</returns>
List<RuleResultTree> ExecuteRule(string workflowName, object[] inputs);
/// <summary>
///
/// </summary>
/// <param name="workflowName"></param>
/// <param name="input"></param>
/// <returns></returns>
List<RuleResultTree> ExecuteRule(string workflowName, object input);
/// <summary>
/// This will execute all the rules of the specified workflow
/// </summary>
/// <param name="workflowName"></param>
/// <param name="ruleParams"></param>
/// <returns>List of Result</returns>
List<RuleResultTree> ExecuteRule(string workflowName, RuleParameter[] ruleParams);
}
}

View File

@ -0,0 +1,27 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using RulesEngine.Models;
using System.Collections.Generic;
namespace RulesEngine.Interfaces
{
public interface IRulesEngine
{
/// <summary>
/// This will execute all the rules of the specified workflow
/// </summary>
/// <param name="workflowName">The name of the workflow with rules to execute against the inputs</param>
/// <param name="inputs">A variable number of inputs</param>
/// <returns>List of rule results</returns>
List<RuleResultTree> ExecuteRule(string workflowName, params object[] inputs);
/// <summary>
/// This will execute all the rules of the specified workflow
/// </summary>
/// <param name="workflowName">The name of the workflow with rules to execute against the inputs</param>
/// <param name="ruleParams">A variable number of rule parameters</param>
/// <returns>List of rule results</returns>
List<RuleResultTree> ExecuteRule(string workflowName, params RuleParameter[] ruleParams);
}
}

View File

@ -1,18 +0,0 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
namespace RulesEngine
{
internal class NullLogger : ILogger
{
public void LogError(Exception ex)
{
}
public void LogTrace(string msg)
{
}
}
}

View File

@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Microsoft.Extensions.Logging;
using RulesEngine.HelperFunctions;
using RulesEngine.Models;
using System;
@ -32,7 +33,7 @@ namespace RulesEngine
/// </summary>
/// <param name="expressionBuilderFactory">The expression builder factory.</param>
/// <exception cref="ArgumentNullException">expressionBuilderFactory</exception>
internal RuleCompiler(RuleExpressionBuilderFactory expressionBuilderFactory,ILogger logger)
internal RuleCompiler(RuleExpressionBuilderFactory expressionBuilderFactory, ILogger logger)
{
if (expressionBuilderFactory == null)
{
@ -76,7 +77,7 @@ namespace RulesEngine
}
catch (Exception ex)
{
_logger.LogError(ex);
_logger.LogError(ex.Message);
throw;
}
}

View File

@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using RulesEngine.HelperFunctions;
using RulesEngine.Interfaces;
using RulesEngine.Models;
@ -36,7 +38,7 @@ namespace RulesEngine
public RulesEngine(ILogger logger, ReSettings reSettings = null)
{
_logger = logger ?? new NullLogger();
_logger = logger ?? new NullLogger<RulesEngine>();
_reSettings = reSettings ?? new ReSettings();
}
#endregion
@ -46,30 +48,13 @@ namespace RulesEngine
/// <summary>
/// This will execute all the rules of the specified workflow
/// </summary>
/// <typeparam name="T">type of input</typeparam>
/// <param name="input">input</param>
/// <param name="workflowName">Workflow Name</param>
/// <returns>List of Result</returns>
public List<RuleResultTree> ExecuteRule(string workflowName, IEnumerable<dynamic> input, object[] otherInputs)
/// <param name="workflowName">The name of the workflow with rules to execute against the inputs</param>
/// <param name="inputs">A variable number of inputs</param>
/// <returns>List of rule results</returns>
public List<RuleResultTree> ExecuteRule(string workflowName, params object[] inputs)
{
_logger.LogTrace($"Called ExecuteRule for workflow {workflowName} and count of input {input.Count()}");
_logger.LogTrace($"Called ExecuteRule for workflow {workflowName} and count of input {inputs.Count()}");
var result = new List<RuleResultTree>();
foreach (var item in input)
{
var ruleInputs = new List<object>();
ruleInputs.Add(item);
if (otherInputs != null)
ruleInputs.AddRange(otherInputs);
result.AddRange(ExecuteRule(workflowName, ruleInputs.ToArray()));
}
return result;
}
public List<RuleResultTree> ExecuteRule(string workflowName, object[] inputs)
{
var ruleParams = new List<RuleParameter>();
for (int i = 0; i < inputs.Length; i++)
@ -78,16 +63,17 @@ namespace RulesEngine
var obj = Utils.GetTypedObject(input);
ruleParams.Add(new RuleParameter($"input{i + 1}", obj));
}
return ExecuteRule(workflowName, ruleParams.ToArray());
}
public List<RuleResultTree> ExecuteRule(string workflowName, object input)
{
var inputs = new[] { input };
return ExecuteRule(workflowName, inputs);
}
public List<RuleResultTree> ExecuteRule(string workflowName, RuleParameter[] ruleParams)
/// <summary>
/// This will execute all the rules of the specified workflow
/// </summary>
/// <param name="workflowName">The name of the workflow with rules to execute against the inputs</param>
/// <param name="ruleParams">A variable number of rule parameters</param>
/// <returns>List of rule results</returns>
public List<RuleResultTree> ExecuteRule(string workflowName, params RuleParameter[] ruleParams)
{
return ValidateWorkflowAndExecuteRule(workflowName, ruleParams);
}

View File

@ -7,6 +7,7 @@
<ItemGroup>
<PackageReference Include="FluentValidation" Version="8.4.0" />
<PackageReference Include="Microsoft.CSharp" Version="4.5.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.3" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="System.Linq" Version="4.3.0" />
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.0.18" />

View File

@ -1,5 +1,5 @@
{
"sdk": {
"version": "3.0.101"
"version": "3.1.101"
}
}

View File

@ -1,9 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using RulesEngine;
using Microsoft.Extensions.Logging;
using RulesEngine.Exceptions;
using RulesEngine.HelperFunctions;
using RulesEngine.Models;
using Moq;
using Newtonsoft.Json;

View File

@ -1,30 +0,0 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using RulesEngine;
using System;
using System.Collections.Generic;
using System.Text;
using Xunit;
namespace RulesEngine.UnitTest
{
[Trait("Category","Unit")]
public class NullLoggerTest
{
[Fact]
public void NullLogger_LogTrace()
{
var logger = new NullLogger();
logger.LogTrace("hello");
}
[Fact]
public void NullLogger_LogError()
{
var logger = new NullLogger();
logger.LogError(new Exception("hello"));
}
}
}

View File

@ -0,0 +1,44 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using System;
using Xunit;
namespace RulesEngine.UnitTest
{
[Trait("Category","Unit")]
public class NullLoggerTest
{
[Fact]
public void NullLogger_BeginScope_DoesNotThrow()
{
var logger = new NullLogger<RulesEngine>();
using (logger.BeginScope("test-value"))
{ }
}
[Theory]
[InlineData(LogLevel.Critical)]
[InlineData(LogLevel.Debug)]
[InlineData(LogLevel.Error)]
[InlineData(LogLevel.Information)]
[InlineData(LogLevel.None)]
[InlineData(LogLevel.Trace)]
[InlineData(LogLevel.Warning)]
public void NullLogger_IsEnabled_ReturnsTrue(LogLevel logLevel)
{
var logger = new NullLogger<RulesEngine>();
logger.IsEnabled(logLevel).Equals(true);
}
[Fact]
public void NullLogger_Log_DoesNotThrow()
{
var logger = new NullLogger<RulesEngine>();
logger.Log(LogLevel.Critical, 1, "This is a critical message.");
}
}
}

View File

@ -1,11 +1,9 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using RulesEngine;
using Microsoft.Extensions.Logging.Abstractions;
using RulesEngine.Models;
using System;
using System.Collections.Generic;
using System.Text;
using Xunit;
namespace RulesEngine.UnitTest
@ -23,7 +21,7 @@ namespace RulesEngine.UnitTest
[Fact]
public void RuleCompiler_CompileRule_ThrowsException()
{
var compiler = new RuleCompiler(new RuleExpressionBuilderFactory(new ReSettings()), new NullLogger());
var compiler = new RuleCompiler(new RuleExpressionBuilderFactory(new ReSettings()), new NullLogger<RuleCompiler>());
Assert.Throws<ArgumentException>(() => compiler.CompileRule(null, null));
Assert.Throws<ArgumentException>(() => compiler.CompileRule(null, new RuleParameter[] { null}));
}

View File

@ -6,6 +6,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="Moq" Version="4.12.0" />
<PackageReference Include="xunit" Version="2.4.1" />