Evaluation pipeline
This page explains what happens between AddWorkflow and the list of RuleResultTree objects that ExecuteAllRulesAsync returns. RulesEngine evaluates every enabled top-level rule of a workflow on each call. It does not stop at the first match and it has no priorities or agenda.
Registering a workflow
AddWorkflow and AddOrUpdateWorkflow validate the workflow and store it together with a timestamp. Nothing is compiled yet. Compilation happens the first time a workflow is executed with a particular set of input names and types.
Executing a workflow
sequenceDiagram
autonumber
actor App as Your application
participant RE as RulesEngine
participant RC as RulesCache
participant CP as RuleCompiler
participant AC as Actions
App->>RE: ExecuteAllRulesAsync(workflowName, inputs)
RE->>RE: Wrap inputs as RuleParameter and sort by name
RE->>RC: Are compiled rules up to date for this key?
alt compiled rules missing or older than the workflow
RE->>RC: GetWorkflow (injects WorkflowsToInject rules)
RE->>CP: Compile global params once
RE->>CP: Compile every enabled rule
RE->>RC: Store compiled rules and globals delegate
end
RE->>RE: Evaluate global params once
loop each compiled top-level rule
RE->>RE: Check cancellation token
RE->>RE: Run compiled rule with inputs and globals
end
RE->>RE: Format ErrorMessage templates for failed rules
opt AutoExecuteActions is true
RE->>AC: Run OnSuccess or OnFailure action, children first
AC-->>RE: ActionResult stored on each RuleResultTree
end
RE-->>App: List of RuleResultTree
Figure 1. One call to ExecuteAllRulesAsync.
- Name and sort the inputs. Plain objects become
input1,input2and so on;RuleParameterkeeps your names. Inputs are sorted by name so the compiled delegate always receives its arguments in the same order. - Find or compile the rules. The cache key combines the workflow name with every input's name and full type name. A new input shape therefore compiles the workflow once more; the same shape reuses the cache.
- Evaluate global parameters once for the whole call, not once per rule, and pass the values to each rule as extra inputs.
- Run each compiled rule. Cancellation is checked between top-level rules and before each action; a single expression is never interrupted.
- Format error messages for failed rules (see Error handling).
- Run actions when
AutoExecuteActionsistrue. Child results run their actions before their parent.
ExecuteActionWorkflowAsync follows the same compile step but runs one named top-level rule and always runs its action.
What compiling a workflow involves
flowchart TD
start(["Execute called"]) --> key["Build cache key from workflow name<br/>and input names and types"]
key --> fresh{"Compiled rules cached<br/>and newer than the workflow?"}
fresh -- yes --> done(["Use cached rules"])
fresh -- no --> wf{"Workflow registered?"}
wf -- no --> notfound(["Throw ArgumentException:<br/>rule config is not present"])
wf -- yes --> inject["Merge rules of WorkflowsToInject<br/>(cycles throw InvalidOperationException)"]
inject --> types{"AutoRegisterInputType?"}
types -- yes --> collect["Add input types to CustomTypes<br/>only when new types were found"]
types -- no --> globals
collect --> globals["Parse and compile GlobalParams<br/>into one delegate"]
globals --> gok{"Globals compiled?"}
gok -- no --> err["Every enabled rule becomes<br/>an error result"]
gok -- yes --> mode{"Parallel compilation enabled,<br/>FastExpressionCompiler off,<br/>32 or more rules?"}
mode -- yes --> par["Compile rules in parallel"]
mode -- no --> ser["Compile rules one by one"]
par --> store
ser --> store["Cache compiled rules and<br/>globals delegate with a new timestamp"]
err --> store
store --> done
Figure 2. Compile-on-demand. A rule that fails to compile does not fail the workflow; it produces an error result (see the error policy).
Nested rules
A rule with an Operator of And, AndAlso, Or or OrElse and child Rules is evaluated from its children. Operator names are not case-sensitive, and disabled children are skipped.
flowchart TD
parent["Parent rule<br/>Operator and child rules"] --> next{"Next enabled child"}
next -- none left --> final["Parent result = accumulated value"]
next -- one --> run["Evaluate child<br/>and record its result"]
run --> op{"Operator"}
op -- "And / AndAlso" --> andv["value = value and child succeeded"]
op -- "Or / OrElse" --> orv["value = value or child succeeded"]
andv --> shortand{"Performance mode<br/>and value is false?"}
orv --> shortor{"Performance mode<br/>and value is true?"}
shortand -- yes --> final
shortor -- yes --> final
shortand -- no --> next
shortor -- no --> next
Figure 3. Nested evaluation. The value starts as true for And and false for Or.
NestedRuleExecutionMode |
Behavior |
|---|---|
All (default) |
Every child is evaluated and appears in ChildResults. |
Performance |
Evaluation stops as soon as the result is decided, so ChildResults can be shorter than the list of children. |
A parent with no enabled children fails. Children can use the parent's LocalParams.
Global and local parameters
A scoped parameter is a named expression, similar to a var in C#. It makes long rules readable and avoids repeating expensive sub-expressions.
flowchart LR
inputs["Inputs<br/>input1, input2, ..."] --> g["Workflow GlobalParams<br/>evaluated once per call, in order"]
g --> merged1["Inputs + globals"]
merged1 --> l["Rule LocalParams<br/>evaluated for each rule, in order"]
l --> merged2["Inputs + globals + locals"]
merged2 --> expr["Rule expression"]
expr --> result["RuleResultTree.Inputs<br/>contains all of them"]
Figure 4. Scoped parameters. Each parameter can use the inputs and every parameter defined before it.
- Names must be unique within the workflow (globals) or within the rule (locals); duplicates are rejected when the workflow is added.
- A caller-supplied input must not have the same name as a global parameter. The collision is reported as a rule error that names the identifier.
- With
EnableScopedParams = false, both kinds are ignored and rules that use them fail to parse.