Executing a child ruleset

To execute a ruleset from rules of another ruleset, you can add a utility class to the XOM of the master ruleset, and then use the method execute that executes the child ruleset. A utility class can be in the managed XOM of the master ruleset or in the caller application to execute a child ruleset.

Procedure

Use the following utility class RulesetChildRunner as an example to execute a ruleset in another execution unit (XU):
import java.io.*;
import java.util.*;
import java.util.logging.Level;

import ilog.rules.res.model.*;
import ilog.rules.res.session.*;
import ilog.rules.res.session.config.*;

public class RulesetChildRunner {
        private static IlrSessionFactory factory;
        public static IlrSessionResponse execute(String path, Map<String, Object> params) throws IlrSessionException, IlrFormatException {
                IlrSessionFactory f;
                IlrStatelessSession sess;
                IlrSessionRequest rq;

                f = getFactory();

                sess = f.createStatelessSession();
                rq = f.createRequest();
                rq.setRulesetPath(IlrPath.parsePath(path));

                if (params != null)
                        rq.setInputParameters(params);

                return sess.execute(rq);
        }

        private static IlrSessionFactory getFactory() {
                IlrSessionFactoryConfig cfg;
                IlrXUConfig xuCfg;

                synchronized (RulesetChildRunner.class) {
                        if (factory == null) {
                                cfg = IlrJ2SESessionFactory.createDefaultConfig();
                                xuCfg = cfg.getXUConfig();

                                xuCfg.setLogAutoFlushEnabled(true);
                                xuCfg.setLogLevel(Level.ALL);
                                xuCfg.setLogWriter(new PrefixedPrintWriter(System.out, "[XUCHILD] "));

                                cfg.setXOMClassLoader(RulesetChildRunner.class.getClassLoader());

                                factory = new SessionFactory(cfg);
                        }
                 }
                 return factory;
        }
        private static class PrefixedPrintWriter extends PrintWriter {
                private final String prefix;

                public PrefixedPrintWriter(PrintStream out, String prefix) {
                        super(out);
                        this.prefix = prefix;
                }
                @Override
                public void write(String s) {
                        super.write(prefix + s);
                }
        }
        private static class SessionFactory extends IlrJ2SESessionFactory {
                public SessionFactory(IlrSessionFactoryConfig cfg) {
                        super(cfg);
                }

                @Override
                public void finalize() {
                        release();
                }
        }
}

The following figure shows how the child ruleset is executed by using the RulesetChildRunner class:

Description of how a child ruleset is executed

It can take a long time to load the ruleset and create the child XU. To improve performance, avoid repetitive creation of the ruleset and XU. In the RulesetChildRunner sample, the singleton pattern is used to reuse and share the XU instance between the ruleset executions. The consequence is that the XU instance is associated to a class loader shared by all executions:
  • If the runner is in the application class loader, the XU will be unique for all executions of master rulesets.
  • If the runner is in the managed XOM class loader, the XU will be unique for all executions of an indicated master ruleset.
See the getFactory method of the RulesetChildRunner class in the preceding example.
Note: On the z/OS® platform, the child XU continues executing if you pause zRule Execution Server for z/OS. zRule Execution Server for z/OS pauses only when the master XU completes.