Retrieving ruleset execution traces

Here is a typical code sample to set up a ruleset execution trace on the client side.

Before you begin

If the deployed ruleset is set to execute in sequential mode with the classic rule engine, you must first set the ruleset.sequential.trace.enabled property to true in the Ruleset View of the Rule Execution Server console.

About this task

To define a ruleset execution trace on the client side, you create a session factory and a session request object, enable the trace, set the input parameters, create a response object, and retrieve the trace.

You can also trace ruleset execution on the server side by extending the EventPlugin class. See Extending the plug-in.

Procedure

  1. Create a session factory and a session request object.
    • Java™ SE Plain Old Java Object (POJO) rule session:
      IlrSessionFactory sessionFactory = new IlrJ2SESessionFactory();
    • Java EE/POJO rule session:
      IlrSessionFactory sessionFactory = new IlrPOJOSessionFactory();
    • Java EE/EJB rule session:
      IlrSessionFactory sessionFactory = new IlrEJB3SessionFactory();
  2. Create a session request object.
    IlrSessionRequest sessionRequest = sessionFactory.createRequest();
    String rulesetPath = "/miniloanruleapp/miniloanrules";
    }
    sessionRequest.setRulesetPath(IlrPath.parsePath(rulesetPath));
  3. Enable the trace and retrieve all the traces on the executed rules.
    sessionRequest.setTraceEnabled(true);
    sessionRequest.getTraceFilter().setInfoAllFilters(true); 
  4. Optional: If not all XOM objects are serializable, add the following lines.
    sessionRequest.setTraceEnabled(true);
    	sessionRequest.getTraceFilter().setInfoBoundObjectByRule(true); 
  5. Set the input parameters for the execution of the rules.
    Map<String,Object> inputParameters = sessionRequest.getInputParameters();
    inputParameters.put("loan", loan);
    inputParameters.put("borrower", borrower);
    IlrStatelessSession session = sessionFactory.createStatelessSession();
  6. Execute and get the response for this request.
    IlrSessionResponse response = session.execute(sessionRequest); 
  7. Get the execution trace and the number of rules executed.
    IlrExecutionTrace sessionTrace = response.getRulesetExecutionTrace();
    int rulesNumber = sessionTrace.getTotalRulesFired();  
  8. Get the business version of the execution trace and the list of executed rules with their business names, including the state of the output parameters.
    IlrBusinessExecutionTrace execResult = new IlrBusinessExecutionTrace(response.getRulesetExecutionTrace());
    List<String> rulesFired = execResult.getRuleFiredBusinessNames();
    loan = (Loan) response.getOutputParameters().get("loan"); 

Results

Here is the entire typical code sample for a Java SE rule session:
IlrSessionFactory sessionFactory = new IlrJ2SESessionFactory();

IlrSessionRequest sessionRequest = sessionFactory.createRequest();
String rulesetPath = "/miniloanruleapp/miniloanrules";
}
sessionRequest.setRulesetPath(IlrPath.parsePath(rulesetPath));

sessionRequest.setTraceEnabled(true);
sessionRequest.getTraceFilter().setInfoAllFilters(true);

Map<String,Object> inputParameters = sessionRequest.getInputParameters();
inputParameters.put("loan", loan);
inputParameters.put("borrower", borrower);

IlrStatelessSession session = sessionFactory.createStatelessSession();
IlrSessionResponse response = session.execute(sessionRequest);

IlrExecutionTrace sessionTrace = response.getRulesetExecutionTrace();
int rulesNumber = sessionTrace.getTotalRulesFired(); 

IlrBusinessExecutionTrace execResult = new IlrBusinessExecutionTrace(response.getRulesetExecutionTrace());
List<String> rulesFired = execResult.getRuleFiredBusinessNames();
loan = (Loan) response.getOutputParameters().get("loan");