Business rules form an integral part of many applications. Rules are usually conditional and the evaluation of the rules is triggered by the user input.
Simple rule engine is an attempt to create a rule engine which externalizes both the rule and the trigger.
It can be explained better with an example:
Consider a simple application which accepts details of a person and determines if the person depending on his age can go out alone:
- If the age of the person is less than 15, he cannot go out alone else he can, however person "BAKTHA" is an exception and can go out alone irrespective of how old he is.
Different approaches to implement triggers and the rule itself which can come to our mind are:
- Coding in the view model
- Event handling
- Work flow Rules Engine
Each has some drawbacks:
- Coding in the view model: Code written in the view model tends to clutter the code file.
- Event handling: UI Code is the simplest way to implement this code but changing the code is difficult, also sometimes the necessary events are not available.
- Workflow rules engine: Needs to be triggered explicitly.
Additionally, except for Workflow Rules Engine the rules have to be coded and are not externalized (in Workflow Rule Engine though they are externalized, they are not meant to be edited by a simple XML editor and are verbose enough to prevent you from doing so).
First let's understand what we require for a simple rule engine:
- If the rule is conditional, like the one mentioned in the example, then the condition has to be evaluated before the rule has to be applied - Condition Part
- When the variables used in the condition change, the condition has to be evaluated and the rule has to be applied if the condition is satisfied - Trigger Part
- Action to be performed when the condition is satisfied - Actual rule to be applied
Another way triggers can be implemented is by using Data Binding Framework provided by WPF.
WPF provides a powerful feature, Data Binding which allows binding CLR objects to the UI controls. I would not delve much into Data Binding. This article assumes you have some knowledge about data binding and dependency properties.
Why do we need data binding for this?
Data binding will provide the trigger which triggers the evaluation of the condition or execution of the action if there's no condition.
- Data Binding
- Dependency Properties
- MVVM - Model View ViewModel
Rule Engine - Code
An XML rule entry for the sample application:
The rule is then read by the RulesLoader and converted to condition object, which is evaluated by the Trigger.
<TRIGGER TYPE="SETTER" ELSE_ACTION="SETTER" PARAMETER="false"
<CONDITION PROPERTY_GROUP="OBJECT" PARAMETER="" PROPERTY_TYPE="int"
PROPERTY_NAME="Age" VALUE="15" CONDITION_TYPE="IsGreaterThanOrEqualTo"/>
<CONDITION PROPERTY_GROUP="OBJECT" PARAMETER=""
PROPERTY_TYPE="string" PROPERTY_NAME="Name" VALUE="XYZ"
<CONDITION PROPERTY_GROUP="OBJECT" PARAMETER="" PROPERTY_TYPE="string"
PROPERTY_NAME="Name" VALUE="BAKTHA" CONDITION_TYPE="IsEqualTo"/>
Dependency Property which fires the trigger:
FireTrigger property is bound to the variable if the trigger is unconditional. When the variable changes, the binding framework invokes the
TriggerCallback() method which triggers the execution of the rule.
public object FireTrigger
public static readonly DependencyProperty FireTriggerProperty =
DependencyProperty.Register("FireTrigger", typeof(object), typeof(Trigger),
new UIPropertyMetadata(null, TriggerCallBack));
private static void TriggerCallBack(DependencyObject obj,
Execute method evaluates the condition and if the condition is satisfied then executes the true action or if the else action is specified then it is executed.
private void Execute()
bool blnIsConditionSatisfied = true;
if (null != triggerCondition)
blnIsConditionSatisfied = triggerCondition.IsConditionSatisfied();
else if (null != triggerElseAction)
The condition class evaluates the condition. Several conditions can be joined by using logical condition classes
Or which perform the logical operations of And'ing and Or'ing the conditions.
Again, Dependency property is used to trigger the evaluation of the condition. As conditions involve variables the condition has to be reevaluated when there is a change in the variable involved, the
RequeryTriggerCondition does exactly that, it reevaluates the entire trigger condition.
private object RequeryTriggerCondition
private static readonly DependencyProperty RequeryTriggerConditionProperty =
typeof(Condition), new UIPropertyMetadata(null, ConditionRequeryCallBack));
private static void ConditionRequeryCallBack(DependencyObject obj,
var objCondition = (obj as Condition);
if (objCondition != null && null != objCondition.Trigger)
Then the action part.
public abstract class TriggerAction
public abstract void Action();
TriggerAction class is an
abstract class and has to be inherited to specify custom rules. The trigger is run. It calls the
Action() method of the class.
It is easy to develop a simple rules engine using the powers of Binding Framework to provide the triggers. It reduces the coding effort and externalizes the rules and the triggers which can be maintained easily.
- 17th June, 2009: Initial post