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 further processing of the source object after one of the Evaluator classes 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 if C has no value and A is equal to [123]  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. Accept and Reject are rule actions; they can be declared as public void methods in any .NET public class, including the source object itself.

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 also use a then clause. It is used in conjunction with rule actions inside of if and else if flow sections.

For Web Rule control to be able to create rules of execution type, the following simple requirements must apply:

  • A 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.
  • A source object must either contain at least one public void method that would serve as rule action or have at least one ExternalActionAttribute.
  • The property Mode of the RuleEditor class (either in Mvc or Asp namespace) used to create the rule must be set to RuleType.Execution.

If the type of your source object is known at run-time, the rule can be evaluated by calling the Evaluate method of Evaluator<T> 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. Otherwise it will be False otherwise.

Web Rule also supports situations where the source objects are declared at run-time and their exact types are not known at design-time. See topics for Evaluator and DynamicEvaluator classes for details.

In Web Rule, rules can be created using the Rule Editor or Rule XML format. But the evaluation of rules does not need to be part of the same web application that created them. You can evaluate rules in any .NET code, as long as that code references CodeEffects.Rule.dll assembly.

The real power of execution type rules is in their ability to take over any 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 can do pretty much anything, including delegating processing to new threads or passing source objects to remote services. This amazing capability can 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 does not fit in the normal flow.

Comments: 0
Name (optional):
Comment (URLs are allowed and must start with http:// or https://; all tags will be encoded):
Remaining character count: