Show TOC

Procedure documentationInvoking a Ruleset Locate this document in the navigation structure

 

Using Ruleset Context

Procedure

  1. Create a RulesetContext instance by specifying the name of the project and Ruleset or Ruleset GUID.

    Example Example

    RulesetContext context = engine.createRulesetContext(<RULESET-GUID>);

    OR

    RulesetContext context = engine.createRulesetContext(<PROJECTNAME>,<RULESET-NAME>);

    End of the example.
  2. Add the business objects required to assert the Ruleset to RulesetContext object.

    Syntax Syntax

    1. context.add(<Your Business Object>);
    End of the code.

    Note Note

    If Rules are written using XML Schemas, then you should pass XMLObjects at the time of invocation. You can create the XML Objects using XMLObjectFactory.

    XMLObject xmlObject = XMLObjectFactory.createXMLObject(xmlString); context.add(xmlObject);

    Refer to javadocs for more methods on XMLObjectFactory.

    End of the note.
  3. Invoke the Rules Engine

    Syntax Syntax

    1. RulesetContext returnctxt=engine.invokeRuleset(context) 
    End of the code.

    The Engine returns a RulesetContext object after returning from the invoke method. When you use returned object your application works even when the Engine is in a distributed application environment (where the rules engine is looked up using a JNDI/RMI reference). Here rules engine works on the copies of the object.

    If an action uses an OrderItem, rule engine succeeds only if an OrderItem object is available in the <Business Objects asserted to the engine>. There are two ways of providing OrderItem object to this action:

    • pass in through the <Business Objects asserted to the engine>

    • assert a new OrderItem object in one of the rules before invoking this rule

    As the Engine supports batch mode, you can pass multiple OrderItem objects while execution.

    If an object that is needed for execution of a Ruleset is not passed in <Business Objects asserted to the engine> but created during the execution, the latest object is used for firing rules. If multiple objects are passed, Ruleset will use all the objects.

  4. Use the returned RulesetContext Object if needed. The following method gets the list of business objects from the RulsetContext after they are examined by the Engine.

    Syntax Syntax

    1. List l = returnctxt.getReturnedFacts(); 
    End of the code.

Note Note

  • The method returns the facts after they are examined by the Engine. Use this method only on the Context that is returned after invoking a Ruleset. When you use this method on a context that is not used for engine invocation results in an empty list.

  • The order of the facts will be in the order in which they were added to the Context object.

End of the note.
Setting/Getting the Values of Definitions

When you invoke the Rules Engine using Ruleset context, you can set values for both fixed and variable definitions in a Ruleset. However, you can retrieve the values of only variable definitions. You can retrieve the values of variable definition from the context after the invocation of the Ruleset.

The definition should be any one of the following types:

  • Primitive types - int, long, float, double, short, byte, and boolean. The values have to be wrapped into their corresponding wrapper classes.

  • String types - java.lang.String

  • Date types - java.util.Date

The value of the definition set from the context overrides the value of the definition defined in the Ruleset. The following steps describe the process for setting a value for a definition in the RulesetContext and getting a final value of a variable definition after invocation.

  1. Create the values to be used to create Primitive Wrapper.

    Syntax Syntax

    1. Number value1 = new Integer(41);
    2. String value2 = new String(“name”);
    End of the code.
  2. Get the Ruleset from the RuleEngine and the definitions from that Ruleset.

    Syntax Syntax

    1. AbstractRuleset ruleset = engine.getRuleset(<projectname>, <rulesetname>);
    2. AbstractDefinition definition1 = ruleset.getDefinition(<definition1–name>);
    3. AbstractDefinition definition2 = ruleset. getDefinition(<definition2–name>);
    End of the code.
  3. Set the values for the definitions.

    Syntax Syntax

    1. rulesetContext.setValueForDefinition(definition1,value1);
    2. rulesetContext.setValueForDefinition(definition2,value2);
    End of the code.
  4. Invoke the Ruleset and retrieve the final value of the variable definition as a Object.

    Syntax Syntax

    1. RulesetContext retContext = (RulesetContext)engine.invokeRuleset(rulesetContext);
    2. Object returnValue = retContext.getValueOfVariableDefinition(definition2);
    End of the code.
Using List

RuleEngine has a method / contract which is defined as invokeRuleset. Use the invokeRuleset contract to invoke the Engine.

Syntax Syntax

  1. List <list of changed objects>=engine.invokeRuleset(<PROJECTNAME>,<RULESETNAME>,<List
    of business objects>);
End of the code.
  • If an action uses an object called OrderItem, it succeeds only if an OrderItem object is available in the Business Objects asserted to the engine.

  • If an object that is needed for execution of a Ruleset is not passed in Business Objects asserted to the engine, but created during the execution, the latest object is used for firing rules. If multiple objects are passed, Ruleset will use all the objects.