Rules, in Jetfire, are methods are are automatically invoked by changes to fields or properties allowing policies to be implemented. A rule method will typically test one or more conditions to determine if an action should be performed. Rules support forward chaining and using rules Jetfire becomes a Business Rule Engine.
Rules provide a simple programming model requiring no knowledge of complex programming structures such as events, delegates, threads and mutual exclusion.
Jetfire internally implements a data flow
architecture that allows a simple rule programming model to be presented to the programmer.
Rules can be used to implement Aspect-Oriented Programming
'concerns' involving data.
Rules are first class constructs
- A rule method is public method with a void return type and no parameters (see 'Rule Declaration' below).
- A rule method is guaranteed to be automatically executed if any instance properties, or fields, used by the rule method change.
- Jetfire optimizes calling rule methods to prevent unnecessary execution of a rule method; however rule methods must be designed to allow for execution even if no changes occur.
- A rule method may change instance properties or fields that cause other rules to execute.
- A rule method may be called like any standard method from Jetfire code or the application interface.
A rule method is method that has no parameters and has a void return type. To declare a rule method use the key word 'rule' as shown in the following example:
// normal jetfire code
Rule methods are always treated as public; however no access modifier is allowed to be specified.
In the following example "MyRule" will automatically set a property ("Completed") when all members of the List ("taskList") are true.
The method "MyRule" will execute automatically whenever the collection("taskList") changes (member added or removed) or whenever a member of the collection is modified. In this example other code is responsible for changing the List("taskList").
public workflow Tasks()
private List taskList;
public bool Completed
public Tasks(List taskList)
this.Completed = false;
this.taskList = taskList;
// MyRule is invoked whenever 'TaskList' changes or
// whenever a workflow in the 'TaskList' collection changes value.
// check all 'Task's (workflows) in the collection 'TaskList'
foreach(Task f in this.TaskList)
//if a single task is not completed
this.IsAllTasksCompleted = false;
// tasks must be completed
this.IsAllTasksCompleted = true;
Note: This example my be confusing to seasoned programmers because they might expect that more programming effort is required to have the rule method properly. The execution of the rule method is performed by the Jetfire system which optimizes the execution of the 'rule' method so it executes only when it is required.
A complete examples are available in the download at jetfire.codeplex.com
in the project JetfireCoreExampleTests.RuleTests.