Copyright © TIBCO Software Inc. All Rights Reserved
Copyright © TIBCO Software Inc. All Rights Reserved


Chapter 2 Project Design Tutorial : Add BadCreateAccount and CreateAccount Rules

Add BadCreateAccount and CreateAccount Rules
In this task you create two rules, one called CreateAccount and one called BadCreateAccount. Both rules can fire when a CreateAccount event is asserted into the Rete network. However, the BadCreateAccount rule has a higher priority, so it will fire before the CreateAccount rule.
The BadCreateAccount rule checks whether the account ID provided in the CreateAccount event matches the account ID of any Account instance already in the Rete network. One of the two following situations must occur:
A matching ID exists in the Rete network: the BadCreateAccount rule prints a message to the console, and "consumes" — that is, deletes — the CreateAccount event. Because that event is consumed, the CreateAccount rule cannot fire.
No matching ID exists in the Rete network: the BadCreateAccount rule does nothing, and then the CreateAccount rule fires, and creates the Account concept instance.
Learning Points
What are rules and how are they created?  Rules define actions to take when certain conditions are met. Rules are written in the TIBCO BusinessEvents rule language, which is similar to the Java language. Rules are declarative and are generally narrow in scope. A rule has three parts: the declaration (declare), the conditions (when), and the actions (then). As with rule functions, you can work in a source view, which displays the Java-like code, or in a form view.
How are rules used at runtime?  The rule engine checks all changes and additions to the Rete network and evaluates or reevaluates rules, using their declaration and conditions, as needed. 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 and rank (if these features are used). As the contents of the Rete network 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 Architect’s Guide for details.
How can you prioritize rule execution?  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. Within a set of rules that has the same priority, a ranking feature enables you to determine which fire before others. It uses a callback rule function that enables you to specify business logic to establish the rank. When there is no reason to force rules to execute in a particular order, leave the Priority and Rank fields set to the default and let the runtime engine determine rule order.
How can you create concept instances?  In this task you instantiate an object instance using its ontology function. Another way is using the Instance.CreateInstance() function. This function uses the XSLT mapper to map any of the scope variables (such as properties, attributes and event payloads) to the new instance properties. You can also create event instances in a similar way, using the Event.CreateEvent() function.
More Information
Chapter 5, Run-time Inferencing Behavior in TIBCO BusinessEvents Architect’s Guide.
Task L Add the BadCreateAccount Rule
1.
Right click the Rules folder, and select New > Rule.
2.
You see the New Rule Wizard. In the Filename field, type BadCreateAccount. In the Description field, type Checks for an existing account with the specified ID. Click Finish.
3.
In the Form tab, set the Priority field to 3. It’s higher priority than 5, the default. You’ll set the CreateAccount rule to priority 5, so that the BadCreateAccount rule always fires before CreateAccount rule.
4.
Each of the sections can be expanded and contracted as needed.
5.
6.
Similarly, drag the CreateAccount event into the next available row.
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 in lower case letters. You can change it as desired.
7.

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

 
Notice that when you type the At sign (@), a pick list of concept attributes appears. Attributes are built-in. You cannot add or remove attributes. The id attribute value is set internally. However you can set the external ID, extId.
8.

 
System.debugOut("#### Account already exists: " + createaccount.AccountID;
Event.consumeEvent(createaccount);

 
These actions are done only if the conditions are met. If not, then the next rule in the agenda fires — and that is likely to be the CreateAccount rule, which you’ll define next.
You are also consuming the event, so it cannot trigger any other rules.
9.
Task M Add the CreateAccount Rule
1.
Right click the Rules folder again, and select New > Rule.
2.
In the New Rule Wizard Filename field, type CreateAccount. It doesn’t really need a description does it? Click Finish.
3.
In the Form tab, set the Priority field to 5. (This is a lower priority than the BadCreateAccount rule.)
4.
Drag the CreateAccount event from the TIBCO BusinessEvents Studio Explorer tree into the first empty row in the Declaration section.
This rule has no conditions — the BadCreateAccount rule means there is no need. You could have combined the two rules into one. There are many ways to write rules for a project. You have to use your judgment.
In the Actions panel, you’ll use an ontology function to create the Account concept instance. You can also get to ontology functions using the function catalog.
5.
From the top menu select Window > Show View > Other > TIBCO BusinessEvents > Catalog Functions. The Catalog Functions view displays on the right (unless your Eclipse IDE is configured differently — it could display along the bottom, for example).
6.
Below each concept, event, and rule function is its ontology function. Expand Ontology Functions > FraudDetection > Concepts > Account > Account:
7.

 
Concepts.Account.Account(/*extId String */,/*Balance double */,/*Debits double */,/*Status String */,/*AvgMonthlyBalance double */)

 
8.

 
    Concepts.Account.Account(createaccount.AccountID/*extId String */,
              createaccount.Balance/*Balance double */,
              0/*Debits double */,
              "Normal"/*Status String */,
              createaccount.AvgMonthlyBalance/*AvgMonthlyBalance double */);
 
    Event.consumeEvent(createaccount);
 
    System.debugOut("#### Created account " + createaccount.AccountID);

 
9.
Note that in this case it is not really necessary to consume the event. No other rules have this event in their scope. Events with time to live (TTL) set to zero (0) are consumed at the end of an RTC. However it does no harm and makes the rules more consistent, reducing chances of error.

Copyright © TIBCO Software Inc. All Rights Reserved
Copyright © TIBCO Software Inc. All Rights Reserved