RulesEngine with global params

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using RulesEngine.Models;
using Newtonsoft.Json;
public class Program
{
	public static void Main()
	{
		var serializer = new JsonSerializer();
		serializer.Formatting = Formatting.Indented;
		
		
		var workflow = new WorkflowRules{
			WorkflowName = "workflowWithGlobalParam",
			GlobalParams = new []{
				new ScopedParam{
					Name = "myglobal1",
					Expression = "myInput.hello.ToLower()"
				}
			},
			Rules = new []{
				new Rule{
					RuleName = "checkGlobalEqualsHello",
					Expression = "myglobal1 == \"hello\""
				},
				new Rule{
					RuleName = "checkGlobalEqualsInputHello",
					Expression = "myInput.hello.ToLower() == myglobal1"
				}
			}
		};
		
		var workflowAsStr = JsonConvert.SerializeObject(workflow,new JsonSerializerSettings { Formatting = Formatting.Indented, NullValueHandling = NullValueHandling.Ignore });
		Console.WriteLine("Workflow");
		Console.WriteLine(workflowAsStr);
		
		var re = new RulesEngine.RulesEngine(new []{workflowAsStr});
		
		var input = new RuleParameter("myInput",new {
						hello = "HELLO"
					  });

		var resultList  = re.ExecuteAllRulesAsync("workflowWithGlobalParam",input).Result;
		Console.WriteLine("*********************************************");
		Console.WriteLine("results");
		Console.WriteLine(JsonConvert.SerializeObject(resultList,new JsonSerializerSettings { Formatting = Formatting.Indented, NullValueHandling = NullValueHandling.Ignore }));

	}
}
;