Show Navigation
Next Topic »
Execution Type Rules
A rule of execution type can either invoke one or more actions (.NET methods) "assigned" to a flow section of a rule, or 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 set E to Failed and Reject
In this example A, B, C, D and E are rule fields. Fields can be public value type properties or fields 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 Rule Editor 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 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>(ruleXmlString).Evaluate(sourceInstance);
The success variable will have a value of True if any section of the rule evaluated to True (even if the entire rule evaluates to False). It will have a value of False if ALL rule sections evaluate to False. DO NOT rely on the return value of the Evaluate method as indication of either success or failure of the entire rule of execution type.
Code Effects business rules engine also supports situations where the source objects is unknown or simply unavailable at design-time. See topics for source object types and Evaluator 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.
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.
Post your questions on
Stackoverflow and become a part of our growing community
Comments:
0
|
|