Execution Type Rules

The Code Effects engine is formerly known as Web Rule

A rule of execution type can either invoke one or more actions "assigned" to a flow section of a rule, set values of one or more source object properties, or both. The rule 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 With Condition (condition)
else Reject

In this example A, B, C and D are rule fields. Fields can be value type properties of a source object, in-rule methods, or other rules of evaluation type that are used as reusable rules. Accept, Accept With Condition, 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 and value setters.

Evaluation of a single executable type rule can be performed in a loop using the Loop mode of the Rule Editor.

For the Code Effects component 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 declare at least one public property or field of value type (except for System.Guid and nullable enum.) Or it must declare or reference at least one public method that returns a value type. Those members become rule fields and in-rule methods. Members decorated with ExcludeFromEvaluationAttribute are ignored.
  • If value setters are not used, the source object must declare or reference at least one public void method that would serve as rule action.
  • The Mode property of the RuleEditor class (either in Mvc or Asp namespace) must be set to RuleType.Execution or RuleType.While.

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 section of the rule evaluated to True, otherwise it will have a value of False.

Code Effects component also supports situations where the source objects are declared at run-time and their exact types are unknown at design-time. See topics for Evaluator and DynamicEvaluator classes for details.

In Code Effects, rules can be created using the Rule Editor or Rule XML schema, 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 the 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 UnexpectedScenarioAction

While the PositiveScenarioAction, StandardScenarioAction and NegativeScenarioAction take care of the main business, the UnexpectedScenarioAction 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: