简体   繁体   English

如何在Drools工作台中创建的规则中使用Java代码

[英]How to use java code in rules created in drools workbench

I am using drools 6.5.0.Final to create my drools project. 我正在使用Drools 6.5.0.Final创建我的Drools项目。 I am trying to use the guided decision tables (.gdst) in the workbench. 我正在尝试在工作台中使用引导式决策表(.gdst) 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: 我想在规则的WHEN和THEN部分中使用一些Java代码,例如允许在.drl文件中使用,如下所示:

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? 有没有办法使用用户定义的/内置的Java函数(如我在指导决策表中所示)?

Is there a way to use the user-defined/built-in java functions like I have here in a guided decision table? 有没有办法使用用户定义的/内置的Java函数(如我在指导决策表中所示)?

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; KIE WorkBench和KIE Execution Server都使用Maven构建工具进行依赖性管理。 therefore, the answer is with Maven configuration and usage. 因此,答案在于Maven的配置和用法。 [0] [0]

Basically, build your supplemental code to a jar and deploy it to a remote Maven repository available to the KWB and KES. 基本上,将补充代码构建到jar中,然后将其部署到KWB和KES可用的远程Maven存储库中。 How you build, package, and deploy the jar to the remote repo is your choice; 您可以选择如何构建,打包和将jar部署到远程仓库。 typically use Maven or Gradle. 通常使用Maven或Gradle。 This is done external to the KWB. 这是在KWB外部进行的。

Then, edit the pom.xml file for the KWB project, adding the jar dependency. 然后,编辑KWB项目的pom.xml文件,添加jar依赖项。 The KWB "Project Editor" has a feature for adding a dependency or you can manually add it [1]. KWB“项目编辑器”具有添加依赖项的功能,也可以手动添加依赖项[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]. KWB项目编辑器没有用于添加该部分的UI功能,因此请更改为齿轮设置图标上的“存储库视图”,然后直接编辑pom.xml文件[2]。 Also, if using a remote Maven repository with credentials, configure settings.xml per that Maven plugin page for KWB to access it. 另外,如果使用带有凭据的远程Maven存储库,请在该Maven插件页面上配置settings.xml,以便KWB访问它。

Now the jar's contents are available for rules' use as with any Drools code. 现在,与任何Drools代码一样,罐子的内容可用于规则使用。

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

[1] https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Management [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 [2] http://maven.apache.org/plugins/maven-deploy-plugin/usage.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM