Actions
An action is code that runs after a rule has been evaluated. Each rule can define an OnSuccess and an OnFailure action; the one that matches the rule's result runs.
flowchart TD
result["Rule result"] --> which{"IsSuccess?"}
which -- true --> onsuccess["Rule.Actions.OnSuccess"]
which -- false --> onfailure["Rule.Actions.OnFailure"]
onsuccess --> has{"Action defined?"}
onfailure --> has
has -- no --> none["Output is null"]
has -- yes --> factory["ActionFactory creates a new action instance<br/>by name (case-insensitive)"]
factory --> which2{"Which action?"}
which2 -- "OutputExpression" --> out["Evaluate Context.expression<br/>against the rule's inputs"]
which2 -- "EvaluateRule" --> eval["Run another rule<br/>with filtered and extra inputs"]
which2 -- "custom" --> custom["Your ActionBase.Run"]
eval --> chain["ExecuteActionWorkflowAsync<br/>target workflow and rule"]
chain --> result2["Target rule evaluated,<br/>its own action runs"]
out --> store["ActionResult: Output or Exception"]
custom --> store
result2 --> store
none --> store
Figure 1. How an action is selected and run.
Built-in actions
OutputExpression
Evaluates Context.expression and returns the value as Output. The expression can use the rule's inputs, including global and local parameters.
{
"RuleName": "GiveDiscount",
"Expression": "input1.TotalBilled > 1000",
"Actions": {
"OnSuccess": { "Name": "OutputExpression", "Context": { "expression": "input1.TotalBilled * 0.9" } }
}
}
To return an object use the System.Linq.Dynamic.Core syntax new (value as Name). The C# form new { Name = value } is not supported, and the engine says so in the error message.
EvaluateRule
Runs another rule, together with that rule's action, and returns its output. The context accepts:
| Key | Meaning |
|---|---|
workflowName, ruleName |
The rule to run. Must be a top-level rule of a registered workflow. |
inputFilter |
Names of the inputs to forward. When omitted, all inputs (including global and local parameters of the current rule) are forwarded. |
additionalInputs |
A list of { "Name", "Expression" } evaluated against the current inputs and added as inputs of the target rule. |
Forwarded values that have the same name as a global parameter of the target workflow are dropped, because the target workflow evaluates its own globals. Names are compared with the same case-sensitivity as expressions.
Chained rules have no recursion limit: avoid rules that evaluate themselves unconditionally.
Custom actions
public class AuditAction : ActionBase
{
public override ValueTask<object> Run(ActionContext context, RuleParameter[] ruleParameters)
{
var category = context.GetContext<string>("category");
// ... your code ...
return new ValueTask<object>("audited");
}
}
var settings = new ReSettings
{
CustomActions = new Dictionary<string, Func<ActionBase>> { { "Audit", () => new AuditAction() } }
};
ActionContext offers GetContext<T>(name) (throws ArgumentException when missing or not convertible), TryGetContext<T>(name, out value) and GetParentRuleResult(). Context names are case-insensitive, and a rule action's Context may be omitted.
When actions run
| Call | Actions |
|---|---|
ExecuteAllRulesAsync with AutoExecuteActions = true (default) |
Run for every result, children before their parent. |
ExecuteAllRulesAsync with AutoExecuteActions = false |
Not run. Run them yourself with ExecuteActionWorkflowAsync. |
ExecuteActionWorkflowAsync |
Always runs the named rule's action. |
For how action exceptions are reported, see Error handling.