ILOG JRules User Guide > Customizing JRules > Tasks > Customizing Rule Team Server > Using the Rule Team Server API

Describes how to do certain tasks using the Rule Team Server API:

In this Section

Create a Package by API
Manage Ruleset Parameters by API
Modify the BOM by API

Create a Package by API

The following code sample shows how to create a rule package (referred to as folder in Rule Team Server) using the API.

You can pass the parent package in which you want to put your newly created package. Simply pass null if you want to create a root package.

public void createPackage(IlrSession session, String name, String documentation, IlrElementHandle parent) throws Exception
{
  final IlrBrmPackage brm = session.getBrmPackage();
  EClass rulePackageClass = brm.getRulePackage();
  IlrElementHandle packageHandle = session.createElement(rulePackageClass);
  IlrRulePackage myPackage = (IlrRulePackage)   session.getElementDetails(packageHandle);
  myPackage.setDocumentation(documentation);
  myPackage.setName(name);
  myPackage.setRawValue(brm.getRulePackage_Parent(), parent);
// alternatively, you can call myPackage.setParentValue(parent);
  IlrCommitableObject co = new IlrCommitableObject(myPackage);
  co.setRootDetails(myPackage);
  session.commit(co);
}

Note however that you can use a higher level API to do this:

IlrSessionHelper.createRulePackage(session, parent, name)

Manage Ruleset Parameters by API

The following code sample shows how to manage ruleset parameters using the API.

public void modifyParameters(IlrSession session) throws IlrApplicationException {
  IlrBrmPackage brm = session.getBrmPackage();
  IlrRuleProject project = IlrSessionHelper.getProjectNamed(session, "myproject");
  IlrBaseline currentBaseline = IlrSessionHelper.getCurrentBaseline(session, project);
  session.setWorkingBaseline(currentBaseline);
  IlrProjectInfo projectInfo = currentBaseline.getProjectInfo();
  List parameters = projectInfo.getParameters();
// Create a new committable object. You cannot commit a parameter alone because
// it is an aggregated object whose root is the project info, so you have to
// commit the project info, adding the parameters in the committable object.
IlrCommitableObject cobject = new IlrCommitableObject(projectInfo);
// Modify an existing parameter.
IlrParameter parameter = (IlrParameter) parameters.get(0);
// IlrParameter.setXXX are not officially documented (they appear with autocompletion, but not in the Javadoc).
// To be consistent with other artifacts API it is better to use the EMF API.
parameter.setRawValue(brm.getTypedElement_Name(), "foo");
parameter.setRawValue(brm.getTypedElement_Verbalization(), "the foo");
cobject.addModifiedElement(brm.getProjectInfo_Parameters(), parameter);
// Create a new parameter.
IlrParameter newParameter = (IlrParameter) session.getElementDetails(session.createElement(brm.getParameter()));
newParameter.setRawValue(brm.getTypedElement_Name(), "foo");
newParameter.setRawValue(brm.getTypedElement_Verbalization(), "the foo");
cobject.addModifiedElement(brm.getProjectInfo_Parameters(), newParameter);
// Commit the project information.
session.commit(projectInfo);
} 

Modify the BOM by API

The following code sample shows how to modify the Rule Team Server BOM using the API. You can also modify the vocabulary or the BOM to XOM mapping by changing, for example, getBOM to getVocabulary, IlrBOM to IlrVocabulary, and getBOM_Body to getVocabulary_Body.

public void modifyBOM()
{
  try {
    IlrRemoteSessionFactory factory = new IlrRemoteSessionFactory(IlrRemoteSessionFactory.WEBLOGIC81_NAME);
      factory.connect("rtsAdmin", "rtsAdmin", "t3://localhost:7001", "jdbc/ilogDataSource");
      IlrSession session = factory.getSession();
      // Get the rule project named myproject.
      IlrRuleProject project = IlrSessionHelper.getProjectNamed(session, "myproject");
      // Get the current baseline for the project.
      IlrBaseline currentBaseline = IlrSessionHelper.getCurrentBaseline(session, project);
      // Set the working baseline for the session as the project's current baseline.
      session.setWorkingBaseline(currentBaseline);
      IlrBrmPackage brm = session.getBrmPackage();
      // Build a search criteria that looks for the BOM object called "myBom".
      List properties = new ArrayList();
      properties.add(brm.getModelElement_Name());
      List values = new ArrayList();
      values.add("myBom");
      IlrDefaultSearchCriteria searchCriteria = new IlrDefaultSearchCriteria(brm.getBOM(), properties, values);
      // Run the search.
      List bomHandles = session.findElements(searchCriteria);
      if (bomHandles.size() > 0) {
        IlrElementHandle bomHandle = (IlrElementHandle) bomHandles.get(0);
        // Get the BOM details.
        IlrBOM bom = (IlrBOM) session.getElementDetails(bomHandle);
        String body = bom.getBody();
        IlrJavaSerializer javaSerializer = new IlrJavaSerializer();
        IlrDynamicObjectModel objectModel = new IlrDynamicObjectModel(IlrObjectModel.Kind.BUSINESS);
        javaSerializer.readObjectModel(objectModel, new StringReader(body));
        // Your code here ...
        // ...
        // Now commit the modified BOM.
        StringWriter sw = new StringWriter();
        javaSerializer.writeObjectModel(objectModel, sw);
        bom.setRawValue(brm.getBOM_Body(), sw.toString());
        session.commit(bom);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
}

Related Concepts

Rule Model API

Related Tasks

Customizing Rule Team Server
Implementing the Session Controller
Customizing the Rule Team Server GUI

Related Samples and Tutorials

Rule Team Server Business Rule Management Extensions