Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make consistent singlar/plural names of instances to avoid downstream… #193

Merged
merged 21 commits into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions benchmark/RulesEngineBenchmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class REBenchmark
{
private readonly RulesEngine.RulesEngine rulesEngine;
private readonly object ruleInput;
private readonly List<WorkflowRules> workflows;
private readonly List<Workflow> workflows;

private class ListItem
{
Expand All @@ -34,7 +34,7 @@ public REBenchmark()
}

var fileData = File.ReadAllText(files[0]);
workflows = JsonConvert.DeserializeObject<List<WorkflowRules>>(fileData);
workflows = JsonConvert.DeserializeObject<List<Workflow>>(fileData);

rulesEngine = new RulesEngine.RulesEngine(workflows.ToArray(), null, new ReSettings {
EnableFormattedErrorMessage = false,
Expand Down
52 changes: 52 additions & 0 deletions demo/DemoApp.EFDataExample/RulesEngineContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using RulesEngine.Models;

namespace RulesEngine.Data
{
public class RulesEngineContext : DbContext
{
public DbSet<Workflow> Workflows { get; set; }
public DbSet<ActionInfo> ActionInfos { get; set; }

public DbSet<RuleActions> RuleActions { get; set; }
public DbSet<Rule> Rules { get; set; }
public DbSet<ScopedParam> ScopedParams { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Ignore<ActionInfo>();
modelBuilder.Ignore<ScopedParam>();
modelBuilder.Ignore<RuleActions>();

modelBuilder.Entity<Workflow>(entity => {
entity.HasKey(k => k.Id);
entity.Property(p => p.Id).ValueGeneratedOnAdd();

entity.Ignore(b => b.WorkflowsToInject);
});

modelBuilder.Entity<Rule>(entity => {
entity.HasKey(k => k.Id);
entity.Property(p => p.Id).ValueGeneratedOnAdd();

entity.Property(b => b.Properties)
.HasConversion(
v => JsonSerializer.Serialize(v, null),
v => JsonSerializer.Deserialize<Dictionary<string, object>>(v, null));

entity.Property(p => p.Actions)
.HasConversion(
v => JsonSerializer.Serialize(v, null),
v => JsonSerializer.Deserialize<RuleActions>(v, null));

entity.Ignore(b => b.WorkflowsToInject);
});
}
}

}
52 changes: 2 additions & 50 deletions demo/DemoApp.EFDataExample/RulesEngineDemoContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@
using System.Collections.Generic;
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using RulesEngine.Data;
using RulesEngine.Models;

namespace DemoApp.EFDataExample
{
public class RulesEngineDemoContext : DbContext
public class RulesEngineDemoContext : RulesEngineContext
{
public DbSet<WorkflowRules> WorkflowRules { get; set; }
public DbSet<ActionInfo> ActionInfos { get; set; }

public DbSet<RuleActions> RuleActions { get; set; }
public DbSet<Rule> Rules { get; set; }
public DbSet<ScopedParam> ScopedParams { get; set; }

public string DbPath { get; private set; }

public RulesEngineDemoContext()
Expand All @@ -26,48 +20,6 @@ public RulesEngineDemoContext()
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlite($"Data Source={DbPath}");

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Entity<ActionInfo>()
.Property(b => b.Context)
.HasConversion(
v => JsonSerializer.Serialize(v, null),
v => JsonSerializer.Deserialize<Dictionary<string, object>>(v, null));

modelBuilder.Entity<ActionInfo>()
.HasKey(k => k.Name);

modelBuilder.Entity<ScopedParam>()
.HasKey(k => k.Name);

modelBuilder.Entity<WorkflowRules>(entity => {
entity.HasKey(k => k.WorkflowName);
});

modelBuilder.Entity<RuleActions>(entity => {
entity.HasNoKey();
entity.HasOne(o => o.OnSuccess).WithMany();
entity.HasOne(o => o.OnFailure).WithMany();
});

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));
entity.Ignore(e => e.Actions);
});

modelBuilder.Entity<WorkflowRules>()
.Ignore(b => b.WorkflowRulesToInject);

modelBuilder.Entity<Rule>()
.Ignore(b => b.WorkflowRulesToInject);
}
}

}
15 changes: 6 additions & 9 deletions demo/DemoApp/BasicDemo.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using RulesEngine.Models;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using static RulesEngine.Extensions.ListofRuleResultTreeExtension;

namespace DemoApp
Expand All @@ -17,9 +14,9 @@ public class BasicDemo
public void Run()
{
Console.WriteLine($"Running {nameof(BasicDemo)}....");
List<WorkflowRules> workFlowRules = new List<WorkflowRules>();
WorkflowRules workflowRule = new WorkflowRules();
workflowRule.WorkflowName = "Test Workflow Rule 1";
List<Workflow> workflows = new List<Workflow>();
Workflow workflow = new Workflow();
workflow.WorkflowName = "Test Workflow Rule 1";

List<Rule> rules = new List<Rule>();

Expand All @@ -32,11 +29,11 @@ public void Run()

rules.Add(rule);

workflowRule.Rules = rules;
workflow.Rules = rules;

workFlowRules.Add(workflowRule);
workflows.Add(workflow);

var bre = new RulesEngine.RulesEngine(workFlowRules.ToArray(), null);
var bre = new RulesEngine.RulesEngine(workflows.ToArray(), null);

dynamic datas = new ExpandoObject();
datas.count = 1;
Expand Down
1 change: 0 additions & 1 deletion demo/DemoApp/DemoApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<ProjectReference Include="../../src/RulesEngine/RulesEngine.csproj" />
<ProjectReference Include="..\DemoApp.EFDataExample\DemoApp.EFDataExample.csproj" />
</ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions demo/DemoApp/EFDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ public void Run()
throw new Exception("Rules not found.");

var fileData = File.ReadAllText(files[0]);
var workflowRules = JsonConvert.DeserializeObject<List<WorkflowRules>>(fileData);
var Workflows = JsonConvert.DeserializeObject<List<Workflow>>(fileData);

RulesEngineDemoContext db = new RulesEngineDemoContext();
if (db.Database.EnsureCreated())
{
db.WorkflowRules.AddRange(workflowRules);
db.Workflows.AddRange(Workflows);
db.SaveChanges();
}

var wfr = db.WorkflowRules.Include(i => i.Rules).ThenInclude(i => i.Rules).ToArray();
var wfr = db.Workflows.Include(i => i.Rules).ThenInclude(i => i.Rules).ToArray();

var bre = new RulesEngine.RulesEngine(wfr, null);

Expand Down
4 changes: 2 additions & 2 deletions demo/DemoApp/JSONDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public void Run()
throw new Exception("Rules not found.");

var fileData = File.ReadAllText(files[0]);
var workflowRules = JsonConvert.DeserializeObject<List<WorkflowRules>>(fileData);
var workflows = JsonConvert.DeserializeObject<List<Workflow>>(fileData);

var bre = new RulesEngine.RulesEngine(workflowRules.ToArray(), null);
var bre = new RulesEngine.RulesEngine(workflows.ToArray(), null);

string discountOffered = "No discount offered.";

Expand Down
6 changes: 3 additions & 3 deletions demo/DemoApp/NestedInputDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public void Run()
}

var fileData = File.ReadAllText(files[0]);
var workflowRules = JsonConvert.DeserializeObject<List<WorkflowRules>>(fileData);
var Workflows = JsonConvert.DeserializeObject<List<Workflow>>(fileData);

var bre = new RulesEngine.RulesEngine(workflowRules.ToArray(), null);
foreach (var workflow in workflowRules)
var bre = new RulesEngine.RulesEngine(Workflows.ToArray(), null);
foreach (var workflow in Workflows)
{
var resultList = bre.ExecuteAllRulesAsync(workflow.WorkflowName, nestedInput).Result;

Expand Down
2 changes: 1 addition & 1 deletion src/RulesEngine/HelperFunctions/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace RulesEngine.HelperFunctions
public static class Constants
{
public const string WORKFLOW_NAME_NULL_ERRMSG = "Workflow name can not be null or empty";
public const string INJECT_WORKFLOW_RULES_ERRMSG = "Atleast one of Rules or WorkflowRulesToInject must be not empty";
public const string INJECT_WORKFLOW_RULES_ERRMSG = "Atleast one of Rules or WorkflowsToInject must be not empty";
public const string RULE_CATEGORY_CONFIGURED_ERRMSG = "Rule Category should be configured";
public const string RULE_NULL_ERRMSG = "Rules can not be null or zero";
public const string NESTED_RULE_NULL_ERRMSG = "Nested rules can not be null";
Expand Down
6 changes: 3 additions & 3 deletions src/RulesEngine/Interfaces/IRulesEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public interface IRulesEngine
/// <summary>
/// Adds new workflows to RulesEngine
/// </summary>
/// <param name="workflowRules"></param>
void AddWorkflow(params WorkflowRules[] workflowRules);
/// <param name="Workflows"></param>
void AddWorkflow(params Workflow[] Workflows);

/// <summary>
/// Removes all registered workflows from RulesEngine
Expand All @@ -48,6 +48,6 @@ public interface IRulesEngine
/// </summary>
/// <returns></returns>
List<string> GetAllRegisteredWorkflowNames();
void AddOrUpdateWorkflow(params WorkflowRules[] workflowRules);
void AddOrUpdateWorkflow(params Workflow[] Workflows);
}
}
7 changes: 6 additions & 1 deletion src/RulesEngine/Models/Rule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ namespace RulesEngine.Models
[ExcludeFromCodeCoverage]
public class Rule
{
/// <summary>
/// Gets the Rule Id.
/// </summary>
public int Id { get; set; }

/// <summary>
/// Rule name for the Rule
/// </summary>
Expand All @@ -40,7 +45,7 @@ public class Rule

[JsonConverter(typeof(StringEnumConverter))]
public RuleExpressionType RuleExpressionType { get; set; } = RuleExpressionType.LambdaExpression;
public IEnumerable<string> WorkflowRulesToInject { get; set; }
public IEnumerable<string> WorkflowsToInject { get; set; }
public IEnumerable<Rule> Rules { get; set; }
public IEnumerable<ScopedParam> LocalParams { get; set; }
public string Expression { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace RulesEngine.Models
{
[Obsolete("Workflow is now Workflow - Workflow will be removed in next major version")]
alexreich marked this conversation as resolved.
Show resolved Hide resolved
[ExcludeFromCodeCoverage]
public class WorkflowRules : Workflow {
}

/// <summary>
/// Workflow rules class for deserialization the json config file
/// </summary>
[ExcludeFromCodeCoverage]
public class WorkflowRules
public class Workflow
{
/// <summary>
/// Gets the workflow Id.
/// </summary>
public int Id { get; set; }

alexreich marked this conversation as resolved.
Show resolved Hide resolved
/// <summary>
/// Gets the workflow name.
/// </summary>
public string WorkflowName { get; set; }

/// <summary>Gets or sets the workflow rules to inject.</summary>
/// <value>The workflow rules to inject.</value>
public IEnumerable<string> WorkflowRulesToInject { get; set; }
alexreich marked this conversation as resolved.
Show resolved Hide resolved
public IEnumerable<string> WorkflowsToInject { get; set; }

/// <summary>
/// Gets or Sets the global params which will be applicable to all rules
Expand Down
Loading