Chapter 2 Project Design Tutorial : Configure the ProcessDebits Rule Set and ApplyDebit Rule

Configure the ProcessDebits Rule Set and ApplyDebit Rule
In this task you create a rule set called ProcessDebits, and in it, create a rule called ApplyDebit. This rule fires when certain conditions are met.
When a Debit event is asserted into working memory, the engine checks the ApplyDebit rule because it has a Debit event in its scope. The engine checks the rule conditions. If working memory contains an Account concept instance whose ID matches the ID in the debit, the rule is eligible to execute. When the rule executes, if the account status is not “Suspended,” the rule action debits the account instance by the amount specified in the Debit event.
Learning Points
What is a rule set?  A rule set is a collection of rules. Rules must belong to a rule set so that when you build the project EAR for deployment, you can select which rule sets to include, and you can select different rule sets to be used in different rule sessions (inference agents). Rule functions, on the other hand, do not belong to rule sets. A rule function is available in all rule sessions (inference agents).
What are rules and how are they created?  Rules define actions to take when certain conditions are met. Rules are written in the BusinessEvents rule language, which is similar to the Java language. Rules are declarative and are generally narrow in scope. The rule editor UI has three panels: the Declaration panel, the Conditions panel, and the Actions panel, which you will use in the next task.
How are rules used at runtime?  The rule engine checks all changes and additions to working memory and evaluates or reevaluates rules, using their Declaration and Conditions as appropriate. Eligible rules are added to the rule agenda.
What is the rule agenda   A rule fires when it is at the top of the agenda. The engine determines the order of firing using rule declarations and conditions, and each rule’s priority. As the contents of working memory change, the engine reevaluates rules and removes any that are no longer eligible to fire. See Understanding Conflict Resolution and Run to Completion Cycles in TIBCO BusinessEvents User’s Guide for details.
Task H Configure the ProcessDebits Ruleset and ApplyDebit Rule
1.
Open the Rules folder, right-click in the design panel, and select Add Resource > BusinessEvents Workbench > RuleSet. Name the rule set ProcessDebits.
2.
With the rule set open, right-click in the design panel, and select Add Resource > BusinessEvents Workbench > Rule. Name the rule ApplyDebit.
3.
The Priority setting is used by the runtime engine when determining the order in which rules are fired. Those with a number closer to one fire first. When there is no reason to force rules to execute in a particular order, leave the Priority set to the default and let the runtime engine determine rule order.
4.
Double-click the ApplyDebit rule icon to open the rule editor.
5.
The Declaration panel now looks like this:
Declaration  The Declaration provides the scope of the rule. It lists all the entity types to be used in the rule, and their aliases. By default the alias is set to the entity name, but you can change it as desired.
6.

 
//Checks whether the extId of an Account instance in working memory
//matches the incoming event's account ID
account@extId == debit.AccountId;

 
Notice that when you type the At sign (@), a pick list of concept attributes appears. Attributes are built-in. You can’t add or remove attributes.
7.

 
//If Account Status is not Suspended, debits the account
if (account.Status!="Suspended") {
    account.Debits=debit.Amount;
    System.debugOut("############### Debiting account <" +account@extId+ "> by $" +
        debit.Amount);
    account.Balance=account.Balance - debit.Amount;
    System.debugOut("############### New Balance: $" + account.Balance);
} else {
    System.debugOut("############### Cannot debit the suspended account
        <"+account@extId+">");
}

 
When the rule engine executes the above actions, it first checks whether the matching account is suspended. If it is not suspended, the engine performs the debit. In either case, it prints information to the console.
8.
   Event.consumeEvent(debit);
9.
Click Apply, then save the project. The completed rule looks like this:
If you do not consume events when their work is done, their presence (along with other conditions) might trigger rules erroneously.
Summary and Next Steps
In this task you have seen how to configure rules using the rule editor. The next task continues the activity of rule building. You will define the rule that implements the fraud detection tests.