Table of Contents

API overview

A short map of the public API. A generated API reference with every member will replace this page when the XML documentation is published.

Creating an engine

new RulesEngine.RulesEngine(ReSettings reSettings = null);
new RulesEngine.RulesEngine(Workflow[] workflows, ReSettings reSettings = null);
new RulesEngine.RulesEngine(string[] jsonConfig, ReSettings reSettings = null);   // one workflow JSON object per string

The engine copies the settings you pass. See the ReSettings reference.

IRulesEngine

RulesEngine.RulesEngine implements RulesEngine.Interfaces.IRulesEngine, which you can use to mock the engine in your own tests.

Member Purpose
ValueTask<List<RuleResultTree>> ExecuteAllRulesAsync(string workflowName, params object[] inputs) Evaluate every enabled rule. Inputs are named input1, input2, ...
ValueTask<List<RuleResultTree>> ExecuteAllRulesAsync(string workflowName, params RuleParameter[] ruleParams) Same, with your own input names.
ValueTask<List<RuleResultTree>> ExecuteAllRulesAsync(string workflowName, RuleParameter[] ruleParams, CancellationToken cancellationToken) Same, with cooperative cancellation between rules and before each action.
ValueTask<ActionRuleResult> ExecuteActionWorkflowAsync(string workflowName, string ruleName, RuleParameter[] ruleParameters) Evaluate one top-level rule and always run its action.
void AddWorkflow(params Workflow[] workflows) Validate and add. Throws RuleValidationException if invalid or if the name exists.
void AddOrUpdateWorkflow(params Workflow[] workflows) Validate and add or replace.
void RemoveWorkflow(params string[] workflowNames) Remove workflows and their compiled rules.
void ClearWorkflows() Remove everything.
bool ContainsWorkflow(string workflowName) Whether a workflow is registered.
List<string> GetAllRegisteredWorkflowNames() Names of the registered workflows.

A unit test requires every public instance method declared on RulesEngine to be on IRulesEngine.

Inputs: RuleParameter

new RuleParameter("customer", value);          // name and value; ExpandoObject/dictionaries become typed objects
RuleParameter.Create<T>("name", value);        // keeps the declared type T when value is null

See How inputs are typed.

Results: RuleResultTree

Property Meaning
Rule The rule that was evaluated (the Rule object you registered).
IsSuccess Whether the rule passed.
ExceptionMessage Empty on success. For a failed rule: the engine's error text, or the formatted ErrorMessage.
ChildResults Results of child rules of a nested rule, otherwise null.
Inputs Names and values of the inputs, the global parameters and the local parameters that were in scope.
ActionResult Output or exception of the rule's action, when one ran.

ExecuteActionWorkflowAsync returns an ActionRuleResult (Output, Exception and Results, the evaluated rule results including chained rules).

Result extension methods

In the RulesEngine.Extensions namespace, on List<RuleResultTree>:

Method Behavior
OnSuccess(OnSuccessFunc callback) OnSuccessFunc is void (string eventName). Called once, for the first rule that passed, with its SuccessEvent or, when none, its RuleName.
OnFail(OnFailureFunc callback) OnFailureFunc is void (). Called only when all rules failed (also for an empty list).

Both return the list so they can be chained.

Extending the engine

To... Use
Call your own methods in expressions ReSettings.CustomTypes (how to)
Run code after a rule derive from ActionBase, register in ReSettings.CustomActions (how to)
Evaluate an expression without workflows RuleExpressionParser.Evaluate<T>

Exceptions

RuleValidationException, RuleException, ExpressionParserException, plus framework exceptions listed in Error handling.