Click here to Skip to main content
15,867,330 members
Articles / Programming Languages / C#
Article

Use Rules In Your Applications

Rate me:
Please Sign up or sign in to vote.
4.84/5 (19 votes)
29 Aug 2006CPOL2 min read 111.3K   2.7K   93   25
A Rules Engine class library with source code and documentation
Overview of first Use Case

Introduction

This Rules Engine was formulated from my need to provide automation based on external rules. The rules were to govern how the graphical user interface (GUI) behaved in response to Rule execution. De-coupling the rules of the application enabled me to then develop the GUI independently of the Rules and to develop the Rules independently of the application. The connection between the application and the rules is a rules engine whose responsibility is the execution of the rules and notifying the application of changes to variables.

This is the original intent of this Rules Engine, it has since evolved in scope and applicability.

Background

The original intent of this library precluded any notion of ambiguous or non-determinant rule execution. The original need required absolute determination of results since the automation of a GUI cannot resolve ambiguity. I decided to define Rules such that the Rule maker made the decision tree(s).

Rules engines that I have seen (Corticon's offering excepted) are built upon the RETE algorithm. This engine does not utilize the RETE algorithm. RETE can produce non-determinant results which can be disastrous to automation. RETE is better suited to analysis than automation.

This engine has evolved to accommodate various use cases.

Image 2

Thread-safety is integral to the engine and is implemented using the ReaderWriter lock in .NET. Rule execution based on variable changes is implemented by defining "Triggers". Events for rule execution as well as variable changes are also defined.

Using the Code

The Rules Engine is utilized by instantiating a Rule Engine object from the class. The Rules Engine can then be loaded with Rules.

Instantiating a Rules Engine and adding delegates:

C#
Variable _oVariableEngine = new VariableEngine();
RulesEngine _oRulesEngine = new RulesEngine();
_oRulesEngine.VarEngine = _oVariableEngine;

_oRulesEngine.VariableChanging += 
    new RulesEngine.VariableChangingDelegate(_oRulesEngine_VariableChanging);
_oRulesEngine.VariableChanged +=
    new RulesEngine.VariableChangedDelegate(_oRulesEngine_VariableChanged);
_oRulesEngine.VariableAdded +=
    new RulesEngine.VariableAddedDelegate(_oRulesEngine_VariableAdded);
_oRulesEngine.RuleEntered +=
    new RulesEngine.RuleEnteredDelegate(_oRulesEngine_RuleEntered);
_oRulesEngine.RuleComplete += 
    new RulesEngine.RuleCompleteDelegate(_oRulesEngine_RuleComplete);

Rules can be defined and loaded into the engine from any source. The source code contains a Reader/Writer Visual Studio Solution as well as an XML Schema (with documentation) for using an XML file to define and persist Rules.

Links of Interest

Points of Interest

  • Type-safe collections are implemented by extending CollectionBase and using a binary search algorithm to provide high performance access to any item in the collection.
  • Variables are class objects that have overloaded operators.
  • There is an Interface that allows any object to be used as a variable. Using DataSets as variables are implemented using this methodology
  • The source code contains the complete Visual Studio Solution of Jaxlab Rules Engine, complete Visual Studio Solution for Reader/Writer class, Rule XML Schema and documentation, complete documentation of Rules Engine (HTML and CHM).
  • A Rule can contain Rules to create rule trees to organize rules and rule processing.

History

  • 29th August, 2006: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
Website: http://www.somedeveloper.us

Comments and Discussions

 
GeneralThe License is LGPL not CPOL Pin
CyborgDE11-Jun-11 3:38
CyborgDE11-Jun-11 3:38 
QuestionHow to match variables plz. help me [modified] Pin
ShyamPrasadP5-Dec-10 20:05
ShyamPrasadP5-Dec-10 20:05 
GeneralQuestion Pin
chazim12-Nov-10 8:20
chazim12-Nov-10 8:20 
GeneralRe: Question Pin
Jeff Bramlett12-Nov-10 15:03
Jeff Bramlett12-Nov-10 15:03 
GeneralRe: Question Pin
terryterryd14-Apr-11 2:08
terryterryd14-Apr-11 2:08 
GeneralRe: Question Pin
Jeff Bramlett12-Nov-10 15:05
Jeff Bramlett12-Nov-10 15:05 
QuestionHow to use loops Pin
Sumit Prakash Sharma20-Apr-09 2:17
professionalSumit Prakash Sharma20-Apr-09 2:17 
QuestionNeed Help ! Pin
rajalakshmiam16-Apr-09 23:51
rajalakshmiam16-Apr-09 23:51 
GeneralBroken Links Pin
chris lambrou1-Mar-09 19:05
chris lambrou1-Mar-09 19:05 
GeneralGUI for making new rules and editing the existing one Pin
Banty Nigam12-Feb-09 20:29
Banty Nigam12-Feb-09 20:29 
NewsGood article Pin
Arash Aghlara2-Aug-08 22:12
Arash Aghlara2-Aug-08 22:12 
GeneralAuthor's Web Site Pin
vextant10-Feb-08 18:15
vextant10-Feb-08 18:15 
GeneralRe: Author's Web Site Pin
ARUVAL3-Mar-08 6:21
ARUVAL3-Mar-08 6:21 
GeneralNxBre Pin
Tim Schwallie26-Jan-07 11:03
Tim Schwallie26-Jan-07 11:03 
Have you done any comparisons against Nxbre?
GeneralRe: NxBre Pin
Jeff Bramlett29-Jan-07 8:33
Jeff Bramlett29-Jan-07 8:33 
GeneralThank you, this is impressive. Pin
Grav-Vt29-Nov-06 13:48
Grav-Vt29-Nov-06 13:48 
QuestionHow to create rules? Pin
victorvieira1-Nov-06 4:53
victorvieira1-Nov-06 4:53 
AnswerRe: How to create rules? Pin
jeff_bramlett@hotmail.com2-Nov-06 0:40
jeff_bramlett@hotmail.com2-Nov-06 0:40 
AnswerRe: How to create rules? Pin
jeff_bramlett@hotmail.com2-Nov-06 0:48
jeff_bramlett@hotmail.com2-Nov-06 0:48 
QuestionReturning a String result Pin
Rob_Jurado8-Sep-06 12:49
Rob_Jurado8-Sep-06 12:49 
Questionlooking for a Trigger by name Pin
Rob_Jurado6-Sep-06 10:53
Rob_Jurado6-Sep-06 10:53 
AnswerRe: looking for a Trigger by name Pin
Jeff Bramlett6-Sep-06 14:06
Jeff Bramlett6-Sep-06 14:06 
GeneralNice work Pin
Rob_Jurado5-Sep-06 12:42
Rob_Jurado5-Sep-06 12:42 
GeneralThanks for posting Pin
jruvelIGN30-Aug-06 7:15
jruvelIGN30-Aug-06 7:15 
GeneralRe: Thanks for posting Pin
Jeff Bramlett30-Aug-06 7:27
Jeff Bramlett30-Aug-06 7:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.