ILOG JRules User Guide > Integrating Application Data > Tasks > Using the XML Binding API > Adding XML Objects to the Working Memory

You can add XML objects to working memory using the API, or using an asserter. Using the API you can add only the top level (root) node, not any of the sub-nodes (leaves). Using an asserter, you can either add the whole tree to working memory, or just specific nodes.

A number of options are available when loading an XML document into the rule engine:

To add the root object of an XML document:

IlrXmlObject obj = driver.readObject(new FileReader(xmlFilename));
context.assert(obj);
By default, inserting the root object does not carry out a recursive add. If you want to process rules on sub objects of the graph, you must navigate the model using the in and from keywords in your rules.
Here is an extract from a rule named MatchSession1 rule:
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);
}
...

To add IlrXmlObject using the data driver:

IlrContext context = ... // an initialized rule engine
IlrXmlDefaultDataDriver driver = ...// an initialized driver
IlrXmlObject object = ... // an initialized object
 
try {
   driver.assertObjects ( objects, context, null );
 
} catch ( IlrXmlErrorException e ) {
   // some parsing errors
}
Alternatively, to add selected classes rather than the whole tree, replace:
driver.assertObjects ( objects, context, null );
with
driver.assertObjects ( objects, context, selectedClasses );

To use an IlrXmlObjectAsserter object:

IlrXmlObjectAsserter asserter = new
   IlrXmlObjectAsserter(context);
asserter.registerAllXmlClasses(); // Add all the objects
asserter.exploreObject (obj);
If you use an IlrXmlObjectAsserter, you can filter the objects inserted in the working memory. Here, all the objects have been inserted, so there is no need to use the in or from in your rules. Here is an extract fromthis type of rule:
rule processValidItem {
priority=high;
when {
   item: Item ( "waiting".equals(state) );
   product: Product ( id.equals( item.product ); stock >= item.quantity );
}
...

Related Concepts

XML Binding
Working Memory View

Related Tasks

Using XML Objects as Ruleset Parameters
Assigning the Root Element Name of an XML Object
Reading an XML Document
Writing to an XML Document
Managing Unknown Operators on Attributes and Elements
Configuring XML Binding