Adding the root object of an XML data file

You can also add the root object of an XML data file.

To add the root object of an XML data file, use the following code:

IlrXmlObject obj = engine.loadXMLFile(xmlFileName);
engine.assert(obj);

By default, adding the root object does not perform a recursive add. Therefore, if you want to process rules on the other objects, you must use the in and from keywords in your rules to reach the objects that have not been added.

Note:

The function ilrmain is not called when you use Rule Execution Server. If you want to add objects or parameters to the working memory, add the code in the initial actions of the ruleflow so as to make sure that that code will be called no matter how the ruleset is executed.

Example : a rule that uses the ‘in’ and ‘from’ keywords

Here is an extract from a rule named MatchSession1 to illustrate the use of these keywords:

rule MatchSession1
{
when
{
?c: Conference();
?s: Session(?t: title.toLowerCase()) in ?c.sessionList;
?p: Participant() in ?c.participantList;
collect String(?t.indexOf(toLowerCase()) != -1) in ?p.interestList
where (size() >= 2);
}
...

Example : a rule that does not use the in and from keywords

Using an IlrXmlObjectAsserter object, you can add all the objects:

IlrXmlObjectAsserter asserter = new IlrXmlObjectAsserter(engine);
asserter.exploreObject (obj);

You can use an IlrXmlObjectAsserter object to 'filter’ the added objects in the working memory. In this example, all the objects have been added, hence there is no need to use the in or from keywords in your rules.

Here is an extract from a rule that does not use these keywords:

rule processValidItem
{
priority=100;
when {
item: Item ( "waiting".equals(state) );
product: Product ( id.equals(item.product); stock >= item.quantity 
);
}
...

Use ILOG® utility classes as follows to pass a ruleset parameter as an instance of the class .w3c.dom.Element:

helper.addXMLDocumentAsParameter("Data", string, IlrRuleSessionHelper.getXmlBindingType());

The string representation can be built from an XML file by a call to the static method getContentsAsText.

Example: a ruleset execution class

You can find a complete ruleset execution class in the SimpleRuleEngineRunner.java class file used in the xmlbinding code sample and located in <InstallDir>\studio\samples\architecture\xmlbinding\application\src, where InstallDir is the directory to which you installed the product.