17.12. Using Stateful and Stateless RuleSessions

Procedure 17.3. Task

  1. Get the runtime by accessing the RuleServiceProvider as shown:
    RuleRuntime ruleRuntime = ruleServiceProvider.getRuleRuntime();
  2. To create a rule session, use one of the two RuleRuntime public constants. These are "RuleRuntime.STATEFUL_SESSION_TYPE" and "RuleRuntime.STATELESS_SESSION_TYPE", accompanying the URI to the RuleExecutionSet you wish to instantiate a RuleSession for.
  3. Optionally, access the properties to specify globals.
  4. The createRuleSession(...) method will return a RuleSession instance. You should cast it to StatefulRuleSession or StatelessRuleSession:
    (StatefulRuleSession) session =
      ruleRuntime.createRuleSession( uri,
                                     null,
                                     RuleRuntime.STATEFUL_SESSION_TYPE );
    session.addObject( new PurchaseOrder( "cheese" ) );
    session.executeRules();
  5. When using a StatelessRuleSession, you can only call executeRules(List list) passing a list of objects, and an optional filter, the resulting objects are then returned:
    (StatelessRuleSession) session =
      ruleRuntime.createRuleSession( uri,
                                     null,
                                     RuleRuntime.STATELESS_SESSION_TYPE );
    List list = new ArrayList();
    list.add( new PurchaseOrder( "even more cheese" ) );
    
    List results = new ArrayList();
    results = session.executeRules( list );