0
votes

I am using drools 6.5.0.Final to create my drools project. I am trying to use the guided decision tables (.gdst) in the workbench. I want to use some java code in the WHEN and THEN part of the rules, like I am allowed to in the .drl files, like below:

rule "filter rule"
when
    //conditions
    $Cp : CpClass( name == "Tom",
                    Math.abs(score) > 10
                &&
                    // How do I use functions like below?
                    ($Cp.parseTime(CurrTime).getTime() - 
                        $Cp.parseTime(PrevTime).getTime())/1000 > 120
                )
then
    //actions
    System.out.println("Rule passed for : "+ $Cp.toString());
    $Cp.isGoodCp = true;
end

The object used in the above example:

public class CpClass {

    public String name;
    public String currTime;
    public String prevTime;
    public boolean isGoodCp = false;

    // Function to parse string to date
    public Date parseTime(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        Date parsedDate = null;
        try {
            if(time != null)
                parsedDate = sdf.parse(time.substring(11));
            else 
                parsedDate = sdf.parse("00:00:00");
        } catch (Exception e) {
            System.out.println("Error parsing:"+e);
        }
        return parsedDate;
    }
}

Is there a way to use the user-defined/built-in java functions like I have here in a guided decision table?

1
That's rather sketchy: What are $tickCp, CurrTime and PrevTime? Which of the values do you want to vary in the rows of your decision table? Have you thought about hiding the "Java functions" as methods of class CpClass?laune
Sorry, the tickCp was a typo. CurrTime and PrevTime are members of the user defined class CpClass, and Cp is an object of the same. If I make the functions a method of the Class, like parseTime() will I be able to use it in a guided table?ar7

1 Answers

0
votes

Is there a way to use the user-defined/built-in java functions like I have here in a guided decision table?

Yes, in all rules. They key is making the code available to them.

KIE WorkBench and KIE Execution Server both use the Maven build tool for dependency management; therefore, the answer is with Maven configuration and usage. [0]

Basically, build your supplemental code to a jar and deploy it to a remote Maven repository available to the KWB and KES. How you build, package, and deploy the jar to the remote repo is your choice; typically use Maven or Gradle. This is done external to the KWB.

Then, edit the pom.xml file for the KWB project, adding the jar dependency. The KWB "Project Editor" has a feature for adding a dependency or you can manually add it [1].

The KWB Project Editor does not have a UI feature to add the section, so change to the "repository view" on the cog settings icon and edit pom.xml file directly [2]. Also, if using a remote Maven repository with credentials, configure settings.xml per that Maven plugin page for KWB to access it.

Now the jar's contents are available for rules' use as with any Drools code.

[0] http://maven.apache.org/guides/index.html

[1] https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Management

[2] http://maven.apache.org/plugins/maven-deploy-plugin/usage.html