gpt4 book ai didi

java - 在 JBoss Drools 中表达条件

转载 作者:行者123 更新时间:2023-11-30 04:32:44 25 4
gpt4 key购买 nike

我正在使用 JBoss Drools。我有一个业务需求,定义如下。我想将其转换为 JBoss Drools DRL 格式。

业务需求:我有两组位置。一是用户之前访问过的位置。我们将其称为 X。此信息是在运行时获取的。我将携带一些位置集,我们将其称为 Y。此位置:Y 在规则中预定义,这意味着 Y 是静态的。我必须有一个规则,如果 X 中的任何位置与 Y 中的任何位置匹配,那么它必须调用一些 java 代码。

在算法 View 中

rule "Check if Locations X matches with Locations Y"

When
X: It Contains locations visted by user previosuly (obtained at runtime)
Y: It contains some predefined locations
Check if any location in x matches with any location in Y
then
call some java code here to process this.

end;

现在,我如何以 JBoss-Drools DRL 方式表达上述规则?非常感谢在这方面的任何帮助。

最佳答案

好的,我准备根据您的解释尝试一下。如果这不完全是您想要的,我们可以努力寻找更合适的解决方案。另外,请原谅我有限的地理知识;)。

首先,我将定义我们的数据模型。我们有以下事实类来模拟位置:

package de.jannik.locationrules;

public class VisitedLocation {

private String name;

public VisitedLocation(String name) {
this.name = name;
}

public String getName() {
return name;
}
}

然后,我们有一个为用户建模的类。我们只需要您想要调用的方法:

package de.jannik.locationrules;

public class User {
public void handleVisitedContinent(String continentName) {
System.out.println("User has been to " + continentName + ".");
}
}

现在我们可以用这些模型类来描述业务需求:

package de.jannik.drltest

import de.jannik.locationrules.VisitedLocation;
import de.jannik.locationrules.User;

global User user;

rule "User has been to Europe"

when
exists VisitedLocation(name in ("Berlin", "Paris", "London", "Rome"))
then
user.handleVisitedContinent("Europe");
end

rule "User has been to Australia"

when
exists VisitedLocation(name in ("Melbourne", "Sydney"))
then
user.handleVisitedContinent("Australia");
end

rule "User has been to America"

when
exists VisitedLocation(name in ("San Francisco", "New York", "Buenos Aires"))
then
user.handleVisitedContinent("America");
end

在这里,我制定了一些规则,根据工作内存中存在的 VisitedLocation,使用不同的参数调用 User.handleVisitedContent(String) 方法。请注意,事实中并未明确对用户进行建模。相反,我们假设每次需要修改用户时都会创建一个新 session 。根据您的业务需求和性能考虑,您可能希望将其更改为仅对所有用户使用单个 session 。

这是我用来执行我定义的规则的代码:

...
@Test
public void testLocationRules() {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("locations.drl"), ResourceType.DRL);
if (kbuilder.hasErrors()) {
KnowledgeBuilderErrors errors = kbuilder.getErrors();
System.out.println(errors.toString());
throw new RuntimeException(errors.toString());
}
KnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
knowledgeBase.addKnowledgePackages(kbuilder.getKnowledgePackages());
StatelessKnowledgeSession session = knowledgeBase.newStatelessKnowledgeSession();
session.setGlobal("user", new User());
List<VisitedLocation> facts = new ArrayList<VisitedLocation>();
facts.add(new VisitedLocation("Berlin"));
facts.add(new VisitedLocation("Paris"));
facts.add(new VisitedLocation("San Francisco"));
facts.add(new VisitedLocation("Saigon"));
session.execute(facts);
}
...

这会产生以下输出:

User has been to America.
User has been to Europe.

如果这不是您想要的或者您需要进一步说明,请告诉我。另外,您可能需要咨询Drools Expert User Guide了解 Drools 概念的更多解释。

关于java - 在 JBoss Drools 中表达条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14257627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com