![]() |
Platforms, Frameworks & Libraries »
.NET Framework »
Applications
Intermediate
License: The Code Project Open License (CPOL)
Trigger Based Rule EngineBy Deepak-VSSimple Rule Engine with Triggers to apply the rule |
C# (C# 1.0, C# 2.0, C# 3.0), WPF, WinForms, Dev, Design
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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:
Rule:
Different approaches to implement triggers and the rule itself which can come to our mind are:
Each has some drawbacks:
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:
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.
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.
<RULES>
<TRIGGERS>
<TRIGGER TYPE="SETTER" ELSE_ACTION="SETTER" PARAMETER="false"
PROPERTY_NAME="CanGoOutAlone" VALUE="true"
BINDPROPERTY_NAME="" PROPERTY_GROUP="">
<CONDITIONS>
<CONDITION TYPE="OR">
<CONDITION TYPE="AND">
<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_TYPE="IsNotEqualTo"/>
</CONDITION>
<CONDITION PROPERTY_GROUP="OBJECT" PARAMETER="" PROPERTY_TYPE="string"
PROPERTY_NAME="Name" VALUE="BAKTHA" CONDITION_TYPE="IsEqualTo"/>
</CONDITION>
</CONDITIONS>
</TRIGGER>
</TRIGGERS>
</RULES>
Dependency Property which fires the trigger:
The 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.
/// <summary>
/// Gets or sets the fire trigger.
/// </summary>
/// <value>The fire trigger.</value>
public object FireTrigger
{
get
{
return GetValue(FireTriggerProperty);
}
set
{
SetValue(FireTriggerProperty, value);
}
}
public static readonly DependencyProperty FireTriggerProperty =
DependencyProperty.Register("FireTrigger", typeof(object), typeof(Trigger),
new UIPropertyMetadata(null, TriggerCallBack));
/// <summary>
/// Call back method when the trigger fires.
/// </summary>
private static void TriggerCallBack(DependencyObject obj,
DependencyPropertyChangedEventArgs e)
{
((Trigger) obj).Execute();
}
The 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.
/// <summary>
/// Execute the trigger code.
/// </summary>
private void Execute()
{
bool blnIsConditionSatisfied = true;
if (null != triggerCondition)
{
blnIsConditionSatisfied = triggerCondition.IsConditionSatisfied();
}
if (blnIsConditionSatisfied)
{
triggerAction.Action();
}
else if (null != triggerElseAction)
{
triggerElseAction.Action();
}
}
The condition class evaluates the condition. Several conditions can be joined by using logical condition classes And and 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.
/// <summary>
/// Gets or sets the fire trigger.
/// </summary>
/// <value>The fire trigger.</value>
private object RequeryTriggerCondition
{
get
{
return GetValue(RequeryTriggerConditionProperty);
}
set
{
SetValue(RequeryTriggerConditionProperty, value);
}
}
private static readonly DependencyProperty RequeryTriggerConditionProperty =
DependencyProperty.Register("RequeryTriggerCondition", typeof(object),
typeof(Condition), new UIPropertyMetadata(null, ConditionRequeryCallBack));
/// <summary>
/// Call back method when the trigger fires.
/// </summary>
private static void ConditionRequeryCallBack(DependencyObject obj,
DependencyPropertyChangedEventArgs e)
{
var objCondition = (obj as Condition);
if (objCondition != null && null != objCondition.Trigger)
{
objCondition.Trigger.RequeryTriggerCondition();
}
}
Then the action part.
public abstract class TriggerAction
{
/// <summary
/// Actions this instance.
/// </summary>
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.
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 17 Jun 2009 Editor: Deeksha Shenoy |
Copyright 2009 by Deepak-VS Everything else Copyright © CodeProject, 1999-2009 Web18 | Advertise on the Code Project |