Generating a Wrapper

Access to rules and data in Excel tables is realized through OpenL Tablets API. OpenL Tablets provides wrappers to developers to facilitate easier usage.

Generating a Dynamic Wrapper

Only an interface must be defined when creating a project for a dynamic wrapper. OpenL Tablets users can clearly define rules displayed in the application by using this interface. Using dynamic wrappers is a recommended practice.

This section illustrates the creation of a dynamic wrapper for a Simple project in Eclipse with the OpenL Tablets Eclipse Update Site installed. Only one rule hello1 is created in the Simple project by default.

Figure 4: The hello1 rule table

Proceed as follows:

  1. In the project src folder, create an interface as follows:

  2. public interface Simple {

    void hello1(int i);

    }

  3. Create a class for a wrapper as follows:

  4. package template;

    import static java.lang.System.out;

    import org.openl.rules.runtime.RuleEngineFactory;

    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();

    rules.hello1(12);

    }

    }

    When the class is run, it executes and displays Good Afternoon, World!

Generating a Static Wrapper

To generate a static wrapper class, proceed as follows:

  1. Configure the Ant task file as described in Configuring the Ant Task File.

  2. Execute the Ant task file as described in Executing the Ant Task File.

For an example of how to use a static and dynamic wrapper, see Example of Using Static and Dynamic Wrappers.

Configuring the Ant Task File

Create Ant build file which will be contains task to generate wrapper.

The following is an example of the build file (GenerateJavaWrapper.build.xml file):

<project name="GenJavaWrapper" default="generate" basedir="../">

<taskdef name="openlgen" classname="org.openl.conf.ant.JavaWrapperAntTask"/>

<target name="generate">

<echo message="Generating wrapper classes..."/>

<openlgen openlName="org.openl.xls" userHome="."

srcFile="rules/Rules.xls"

targetClass="com.exigen.claims.RulesWrapper"

displayName="Rule table wrapper"

targetSrcDir="gen"

</openlgen>

<openlgen openlName="org.openl.xls" userHome="."

srcFile="rules/Data.xls"

targetClass=" com.exigen.claims.DataWrapper"

displayName="Data table wrapper"

targetSrcDir="gen"

</openlgen>

</target>

</project>

For each Excel file, an individual <openlgen> section must be added between the <target> and </target> tags.

Each <openlgen> section has a number of parameters that must be adjusted. The following table describes <openlgen> section parameters:

Parameters in the <openlgen> section

Parameter

Description

openlName

OpenL configuration to be used. For OpenL Tablets, the following value must always be used:

org.openl.xls

userHome

Location of user defined resources relative to the current OpenL Tablets project.

srcFile

Reference to the Excel or Word file for which a wrapper class must be generated.

targetClass

Full name of the wrapper class to be generated.

OpenL Web Studio recognizes modules in projects by wrapper classes and uses their names in the user interface. If there are multiple wrappers with identical names, only one of them is recognized as a module in OpenL Web Studio.

displayName

End user oriented title of the file that appears in OpenL Web Studio.

targetSrcDir

Folder where the generated wrapper class must be placed.

Executing the Ant Task File

To execute the Ant task file and generate wrappers, proceed as follows:

  1. In Eclipse, refresh the project.

  2. Execute the Ant task XML file as an Ant build.

  3. Refresh the project again so that wrapper classes are displayed in Eclipse.

Once wrappers are generated, the corresponding Excel or Word files can be used in the solution.

Draw your attention that wrapper classes are generated before your project will be compiled. It means that if you use data types what are defined in OpenL rules file they will be generated into your project and then all classes (including generated ones) will be compiled. If you are going to use data types what are defined out of OpenL rules file they have to be compiled before you run the Ant task.

Example of Using a Static and Dynamic Wrapper

The following example illustrates the use of static and dynamic wrappers:

public class Tutorial1Main {

public interface Tutorial1Rules {

void hello1(int i);

}

public static void main(String[] args)

{

out.println("\n* OpenL Tutorial 1\n");

out.println("Working using static wrapper...\n");

callRulesWithStaticWrapper();

out.println("\nWorking using dynamic wrapper...\n");

allRulesWithDynamicWrapper();

}

private static void callRulesWithStaticWrapper() {

//Get current hour

Calendar calendar = Calendar.getInstance();

int hour = calendar.get(Calendar.HOUR_OF_DAY);

//Creates new instance of Java Wrapper for our lesson

Tutorial_1Wrapper tut1 = new Tutorial_1Wrapper();

//Step 1

out.println("* Executing OpenL rules...\n");

// Call the method wrapping Decision Table "hello1"

tut1.hello1(hour);

}

private static void callRulesWithDynamicWrapper(){

// Creates new instance of OpenL Rules Factory

RuleEngineFactory<Tutorial1Rules> rulesFactory =

new RuleEngineFactory<Tutorial1Rules>("rules/Tutorial_1.xls", Tutorial1Rules.class);

//Creates new instance of dynamic Java Wrapper for our lesson

Tutorial1Rules rules = rulesFactory.newInstance();

//Get current hour

Calendar calendar = Calendar.getInstance();

int hour = calendar.get(Calendar.HOUR_OF_DAY);

out.println("* Executing OpenL rules...\n");

rules.hello1(hour);

}

}