ILOG JRules User Guide > Integrating Application Data > Tasks > Using the Dynamic XOM API > Invoking Dynamic Classes

When dynamic classes have been defined (see Creating Your Own Class Driver) and the property set indicates the name of their driver class, you can invoke these classes, as well as their fields, methods, and constructors. ILOG JRules provides an API that hides the implementation details of the class drivers. This API also works for Java classes and is defined as methods in the following classes:

The BOM methods can create an object using an IlrClass and arguments. You can then read the created object using the Reflect API, or it can be read by the rule engine. The following example shows how to use the methods for creating and manipulating objects and arrays.

import ilog.rules.factory.*;
import ilog.rules.bom.*;
public class Main
{
  public static void main(String[] args)
  {
    IlrRuleset ruleset = ... // existing ruleset or new IlrRuleset();
    IlrReflect reflect = ruleset.getReflect();
    IlrClass aclass = reflect.getClass("mypackage.Person");
    IlrAttribute name = aclass.getAttribute("name");
    IlrAttribute age = aclass.getAttribute("age");
    aclass.setPersistentProperty("ilog.rules.engine.driver", "ilog.rules.factory.IlrHashDriver");
    try
    {
      // Call the zero-argument constructor.
      Object obj = reflect.newInstance(aclass);
      // Setting a field, using one of the set methods.
      reflect.set(obj,name,"Michael");
      // Setting a field, using the 'int' set method.
      reflect.setInt(obj,age,25);
      // Getting a field, we have to cast the returned object.
      String svalue = (String)reflect.get(obj,name);
      // Getting a field, the methods returns an int.
      int ivalue = reflect.getInt(obj,age);
      // Asking again for the class of an object. This should return
      // a class.
      IlrClass aclass2 = reflect.getXOMClass(obj);
      // This should return true.
      boolean isinstance = reflect.isInstance(aclass,obj);
      // Creates an array of length 3 of this class.
      Object arr = reflect.newArrayInstance(aclass, 3);
      // This should return 3.
      int len = IlrArray.getLength(arr);
      // Setting the first element of the array.
      IlrArray.set(arr,1,obj);
      // Getting the element.
      Object el = IlrArray.get(arr,1);
    }
    catch(InstantiationException ex)
    {
      // Exception thrown during object creation, maybe no
      // zero-argument constructor is defined.
      ex.printStackTrace();
    }
    catch(IllegalAccessException ex)
    {
      // One of the calls throws an access exception, maybe
      // because the members are not public.
      ex.printStackTrace();
    }
  }
};

Related Concepts

XML Binding
Web Service Binding

Related Tasks

Creating a Dynamic Class
Writing a XOM using Java-Like Syntax
Setting the Classpath to Contain the XOM and the Rule Engine
Writing a Ruleset Execution Method