Using a Dynamic Wrapper in the Run-time Context

This section describes the use of the run-time context for dispatching rules by dimension properties values, when rules are overloaded by properties.

For example, consider two rules overloaded by dimension properties. Both rules have the same name.

The first rule, covering an auto policy, follows:

Figure 38: The auto policy rule

The second rule, covering a homeowner policy, follows:

Figure 39: The homeowner policy rule

A dynamic wrapper enables the user to identify which of these rules must be executed.

// Getting runtime environment which contains context

IRuntimeEnv env = ((IEngineWrapper<simple>) rules).getRuntimeEnv();

// Creating context

IRulesRuntimeContext context = new DefaultRulesRuntimeContext();

env.setContext(context);

// define context

context.setLob("Home);

As a result, the code of the dynamic wrapper with the run-time context resembles the following:

import static java.lang.System.out;

import org.openl.rules.context.DefaultRulesRuntimeContext;

import org.openl.rules.context.IRulesRuntimeContext;

import org.openl.rules.runtime.RuleEngineFactory;

import org.openl.runtime.IEngineWrapper;

import org.openl.vm.IRuntimeEnv;

public class Dynamic_wrapper {

public static void main(String[] args) {

//define the interface

RuleEngineFactory<simple> rulesFactory = new RuleEngineFactory<simple>("rules/TemplateRules.xls", simple.class);

simple rules = rulesFactory.newInstance();

// Getting runtime environment which contains context

IRuntimeEnv env = ((IEngineWrapper<simple>) rules).getRuntimeEnv();

// Creating context (most probably in future, the code will be different)

IRulesRuntimeContext context = new DefaultRulesRuntimeContext();

env.setContext(context);

context.setLob("Home");

rules.hello1(12);

}

}

Run this class. In the console, ensure that the rule with lob = Home was executed. With the input parameter int = 12, the It is Afternoon, Guys phrase is displayed.