简体   繁体   English

从单独的 maven 项目中的另一个 drl 文件引用 function

[英]Referencing a function from another drl file in a separate maven project

I have multiple maven projects with DROOLs drl files in them.我有多个 maven 项目,其中包含 DROOLs drl 文件。 I would like to put things like helper functions in a central location and then have the drls in other projects be able to use them, but it isn't working.我想将辅助函数之类的东西放在一个中央位置,然后让其他项目中的 drls 能够使用它们,但它不起作用。

The common project is a maven dependency in the other projects.公共项目是其他项目中的 maven 依赖项。 I can prove this is working because I have access to the facts that I define in the common project, but I don't have access to functions.我可以证明这是有效的,因为我可以访问我在公共项目中定义的事实,但我无权访问函数。

  1. I initially tried creating a file called: helperfunctions.drl and put the functions directly in the file thinking they would be available without any imports when building and they are not found.我最初尝试创建一个名为:helperfunctions.drl 的文件,并将这些函数直接放在文件中,认为它们在构建时无需任何导入就可以使用,但找不到。

  2. I then tried wrapping the functions in a declare HelperFunctions end, but this syntax doesn't work.然后我尝试将函数包装在 declare HelperFunctions 结尾,但这种语法不起作用。

  3. Finally, I tried changing the file to HelperFunctions.java and did public class HelperFunctions and made all of the methods static. Then in the other project drls I imported using the namespace com.myproject.common.最后,我尝试将文件更改为 HelperFunctions.java 并执行 public class HelperFunctions 并制作所有方法 static。然后在其他项目 drls 中,我使用命名空间 com.myproject.common 导入。

I am out of options, is there anything else I can try or is this not possible?我没有选择,还有什么我可以尝试的吗?或者这是不可能的?

I'm a little unclear about what you've attempted, but Java code from a dependency (your point #3) can be invoked from the rules if you have the Jar on your classpath.我有点不清楚你尝试了什么,但是如果你的类路径上有 Jar,则可以从规则中调用依赖项中的 Java 代码(你的第 3 点)。

Imagine you have your project set up as follows:假设您的项目设置如下:

|-rule-utils (project name)
|-- src\main\java\com\mycompany\common\HelperFunctions.java
|--pom.xml

And you have defined some utility function public static void doSomethingUseful() in your HelperFunctions class.并且您在 HelperFunctions class 中定义了一些实用程序 function public static void doSomethingUseful()

In your other project where your rules exist, you can include your project1 jar as a dependency, possibly as follows in your pom:在存在规则的其他项目中,您可以将project1 jar 作为依赖项包含在内,可能在您的 pom 中如下所示:

<dependency>
  <artifactId>rule-utils</artifactId>
  <groupId>com.mycompany</groupId>
</dependency>

And then you can import and use HelperFunctions and its doSomethingUseful method as you would any other Java code in your drl:然后您可以导入和使用 HelperFunctions 及其doSomethingUseful方法,就像您在 drl 中使用任何其他 Java 代码一样:

import com.mycompany.common.HelperFunctions;

rule "Example rule"
when
then
  HelperFunctions.doSomethingUseful();
end

In my experience, it's pretty common to invoke third-party utility code this way, for example the Apache commons' utility classes like StringUtils and CollectionUtils (though more often on the left hand side than in the consequences.)根据我的经验,以这种方式调用第三方实用程序代码是很常见的,例如 Apache 公共实用程序类,如StringUtilsCollectionUtils (尽管在左侧比在结果中更常见。)

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

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