Referencing a function from another drl file in a separate maven project.

huangapple go评论88阅读模式
英文:

Referencing a function from another drl file in a separate maven project

问题

我有多个包含DROOLs drl文件的Maven项目。我想把像辅助函数这样的东西放在一个中央位置,然后让其他项目中的drl文件能够使用它们,但这并没有起作用。

通用项目是其他项目中的Maven依赖项。我可以证明这是有效的,因为我可以访问我在通用项目中定义的事实,但我无法访问函数。

  1. 我最初尝试创建一个名为:
    helperfunctions.drl的文件,将函数直接放入该文件中,认为在构建时它们将可用,但它们未被找到。

  2. 然后我尝试将函数包装在declare HelperFunctions end中,但这个语法不起作用。

  3. 最后,我尝试将文件更改为HelperFunctions.java,并创建了一个public class HelperFunctions,并将所有方法设为静态方法。然后在其他项目的drl文件中,我使用了命名空间com.myproject.common进行导入。

我已经尝试了各种选项,还有其他我可以尝试的方法吗,还是这不可能实现?

英文:

I have multiple maven projects with DROOLs drl files in them. 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.

The common project is a maven dependency in the other projects. 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.

  2. I then tried wrapping the functions in a declare HelperFunctions end, but this syntax doesn't work.

  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.

I am out of options, is there anything else I can try or is this not possible?

答案1

得分: 1

我对你尝试的内容有一点不太清楚,但是你提到的从依赖项中调用Java代码(你的第三点)可以在你的类路径中有Jar文件的情况下实现。

假设你的项目设置如下:

|-rule-utils(项目名称)
|-- src\main\java\com\mycompany\common\HelperFunctions.java
|--pom.xml

并且你在HelperFunctions类中定义了一些实用函数 public static void doSomethingUseful()

在你的其他项目中,你可以将 project1 的Jar包作为依赖项包含进来,可能在你的pom文件中如下所示:

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

然后你可以导入和使用HelperFunctions及其 doSomethingUseful 方法,就像在drl文件中使用任何其他Java代码一样:

import com.mycompany.common.HelperFunctions;

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

根据我的经验,通常会以这种方式调用第三方实用程序代码,例如Apache Commons的实用程序类,如 StringUtilsCollectionUtils(尽管更常见的是在条件的左侧而不是在规则的结论中使用它们)。

英文:

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.

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.

In your other project where your rules exist, you can include your project1 jar as a dependency, possibly as follows in your pom:

&lt;dependency&gt;
  &lt;artifactId&gt;rule-utils&lt;/artifactId&gt;
  &lt;groupId&gt;com.mycompany&lt;/groupId&gt;
&lt;/dependency&gt;

And then you can import and use HelperFunctions and its doSomethingUseful method as you would any other Java code in your drl:

import com.mycompany.common.HelperFunctions;

rule &quot;Example rule&quot;
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.)

huangapple
  • 本文由 发表于 2020年8月4日 00:22:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63233205.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定