Page History: Rule Method
Compare Page Revisions
Page Revision: 2012/01/20 19:50
Overview
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.
Rule Method
- 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.
Rule Declaration
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:
rule MyRule()
{
// normal jetfire code
}
Rule methods are always treated as public; however no access modifier is allowed to be specified.
Rule Example
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").
namespace RuleExample
{
public workflow Tasks()
{
private List taskList;
public bool Completed
{
get;
private set;
}
// constructor
public Tasks(List taskList)
{
this.Completed = false;
this.taskList = taskList;
}
// MyRule is invoked whenever 'TaskList' changes or
// whenever workflow in the 'TaskList' collection changes value.
rule MyRule()
{
// check all 'Task's (workflows) in the collection 'TaskList'
foreach(Task f in this.TaskList)
{
if(!f.IsCompleted)
{
//if a single task is not completed
this.IsAllTasksCompleted = false;
return;
}
}
// 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 execute the rule method. Complete Example
A complete examples are available in the download at
jetfire.codeplex.com in the project JetfireCoreExampleTests.RuleTests.