Execution Type Rules
A rule of execution type invokes an action "assigned" to a flow section of the rule if conditions in that flow section evaluate to True. Basically, the action can take over the further processing of the source object after the Evaluator class has finished evaluation of the rule.
Here is an example of the execution type rule:
If A has any value or ( B is not equal to C and D is greater than { C x (B + [2]) } ) then Accept
else Reject
In this example A, B, C and D are rule fields. Fields can be value type properties of the source object, or in-rule methods, or any evaluation type rule. The Accept and Reject are rule actions.; they can be declared as public void methods in any referenced .NET public class, including the source object.
Execution type rules support flow elements if, else if and else. The only required flow element is if.
Together with and and or clauses used in evaluation type rules, the execution type rules add then clause. It is used in conjunction with rule actions inside of if and else if flow elements.
For Web Rule control to be able to create rules of execution type, the following simple requirements must apply:
- Source object or any of its reference type members must have at least one public property or field of value type, except for System.Guid and nullable enum. Each value type member becomes a rule field. Members decorated with ExcludeFromEvaluationAttribute are ignored.
- Source object must either contain at least one public void method that would serve as rule action or have at least one ExternalActionAttribute.
- Property Mode of the RuleEditor class (either in Mvc or Asp namespace) used to create the rule must be set to RuleType.Execution.
The rule can be evaluated by calling the Evaluate method of Evaluator class:
using CodeEffects.Rule.Core;
...
bool success = new Evaluator<SourceType>(ruleXml).Evaluate(sourceInstance);
The success variable will have a value of True if any action declared in the rule was invoked. It'll be False otherwise.
Although vital for debugging or testing purposes, inspecting the return of the Execution method doesn't really make sense unless the rule is not designed to handle all possible outcomes and there is a chance that the value of success variable could end up being equal to False.
The real power of execution type rules is in their ability to take over the further processing of source objects by invoking particular actions once the rule evaluation is finished. Actions can be declared in any public class and can receive user-defined values, rule fields or the source object itself as parameters. Therefore, once invoked, actions could do pretty much anything, including delegating processing to new threads or passing source objects to remote services. This amazing capability could be expressed in the following form:
If ImportantValue is AboveExpectations then PositiveScenarioAction
else if ImportantValue is Average then StandardScenarioAction
else if ImportantValue is BelowExpectations then NegativeScenarioAction
else EverythingElseAction
While the PositiveScenarioAction, StandardScenarioAction and NegativeScenarioAction take care of the main business, the EverythingElseAction handles those instances of the source object that contain unexpected values, errors, and anything else that do not belong to the normal flow.
|