This sample shows how to write BAL rules to process XML data. The sample application reads a batch XML file of financial events, executes a set of rules on those events, then outputs a modified event batch to an XML file.
The sample can only be opened and run in Rule Studio.
Key Features
Key features include:
-
Creation of a Dynamic Execution Object Model (XOM) from an XML Schema
-
Default Verbalization of a Dynamic XOM
-
BAL expressiveness (definitions, concatenation of strings and numbers, iteration on collections, if-then-else...)
-
Use of BOM virtual methods and BOM to XOM mapping to extend the language expressiveness, specifically to support more IRL actions and keywords
-
Use of ruleset variables to manipulate local variables in rule actions
-
Engine RetePlus mode
Importing this Sample
To import the xmlbinding sample:
-
Click File > Import.
-
In the Import Wizard, select ILOG Rule Studio Samples and Tutorials then click Next.
-
Select the sample
samples > architecture > xmlbinding.
-
Click Finish.
-
If one or more files already exist in your workspace, a warning message is displayed indicating the file(s) were not re-imported. Click OK to close the message window.
The imported sample project files are displayed in the Rule Explorer. You should see three new projects:
Running this Sample
Start by running the xmlbinding-rules rule project, then run the xmlbinding-application Java Application.
To run the xmlbinding-rules rule project:
-
Click Run > Run.
-
Select the
Rule Project > xmlbinding-rules launch configuration, then click Run.
Output is displayed in the Console view:
Start processing of a new event of class : financial.CashEvent
Start processing of a new event of class : financial.CashEvent
Start processing of a new event of class : financial.CashEvent
Start processing of a new event of class : financial.FinancialEvent
Acknowledge processing of event ID_R2 by event ID1
Start processing of a new event of class : financial.FinancialEvent
Finish processing of event ID1
Finish processing of event ID_R2
Start processing of a new event of class : financial.CashEvent
Start processing of a new event of class : financial.FinancialEvent
Start processing of a new event of class : financial.CashEvent
Start processing of a new event of class : financial.CashEvent
Acknowledge processing of event ID5 by event ID6
Start processing of a new event of class : financial.FinancialEvent
Finish processing of event ID6
Finish processing of event ID5
Finish processing of event ID4
Finish processing of event ID3
Finish processing of event ID2
Finish processing of event ID_R4
Start processing of a new event of class : financial.CashEvent
Start processing of a new event of class : financial.CashEvent
Acknowledge processing of event ID8 by event ID9
Start processing of a new event of class : financial.FinancialEvent
Finish processing of event ID9
Finish processing of event ID8
Acknowledge processing of event ID7 by event ID10
Start processing of a new event of class : financial.FinancialEvent
Finish processing of event ID10
Finish processing of event ID7
Finish processing of event ID_R3
Error: event ID_R1 is unprocessed
Balance of account 456 = 358.75 USD
Balance of account 123 = 527.00 EUR
To run the xmlbinding-application Java application:
-
Click Run > Run.
-
Select the
Java Project > xmlbinding-application launch configuration, then click Run.
Output is displayed in the Console view and is identical to the previous output, with the addition of one extra line:
Both these launch configurations work in a similar manner. The main difference is that the xmlbinding-rules configuration runs the ilrmain main function, whereas xmlbinding-application runs a java main class.
In each case, the program:
-
reads a batch XML file of financial events, stored in
xmlbinding-rules/data/financial.xml. For that, it creates an IlrXmlDefaultDataDriver in the current engine. This driver loads the XML file as a single root object, and sets it as a ruleset input parameter.
-
executes the rules:
-
the first rule to fire is
DeclareEvents; its role is to populate the working memory with all the events of the event batch
-
the cascaded processing of the events starts: the rules are the same as in the
engine > patternmatching sample, but here they are written using the BAL syntax; insert/retract/update keywords are used in IRL but invisible in BAL.
-
during the processing, each event created is displayed by the rule
EventDisplay and added to the event batch by the rule AddEvents
-
the final balances of the accounts are displayed at the end by the rule
AccountBalance
-
stores the modified event batch in the
data/results.xml file
Both launch configurations can be run in debug mode to step through the agenda, browse the contents of the working memory and so on.
Highlights
The model BOM is created from the schema financial.xsd stored in the xmlbinding-xom project. This BOM is verbalized with the default verbalization, and enhanced with a few manual additions that rely on BOM to XOM mappings (for creating events, adding amounts to an account and so on).
The util BOM is created and verbalized using the Helper class in the xmlbinding-xom project. Other virtual members have been added (mainly declare/undeclare methods to support insert/retract in the working memory).
By combining ruleset variables and static BOM methods that use BOM to XOM mapping to encapsulate constructors and the insert keyword, you can create the following type of construction for creating objects with rules:
set 'new cash event' to a new empty cash event ;
set the type of 'new cash event' to "CREDIT" ;
...
insert 'new cash event' ;
Related Concepts
Related Tasks
Related Reference
Related Samples and Tutorials