- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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/
我希望 Eclipse 调试器观察某个表达式何时变为真(其值发生变化)并在发生这种情况时停止。这在我看来非常有用和基本,所以我认为必须有一种方法可以在 eclipse 中做到这一点(如在 gdb 中)
我正在使用 SPEL 表达式,当我的应用程序启动时,我收到此错误。 ftpOutboundGateway.setRenameExpressionString("'/processed/headers[
我不是 visual studio 程序员,但我需要快速创建一个应用程序来显示图像并使用 slider 操纵它的高度和宽度。我已经完成了快速谷歌和所有关于使用 picturebox(picbox) 作
我在玩弄 Boost Xpressive,但遇到了以下代码片段的问题 #include #include #include using namespace std; using namespac
今年我一直在想,2022年我想做些什么,做哪方面的改变,这周末在家终于想到了! 2021 轻描淡写 年底就一直想对2021年写一篇总结的,起码不得写个千八百字,可是思来想去不知道怎么写,直到最后都没想
这个问题在这里已经有了答案: Italic greek letters / latex-style math in plot titles (2 个回答) 8年前关闭。 我应该怎么做才能让 R 在图例
有没有办法为 jade 模板错误提供自定义异常处理程序? 例如 // server.js app = express(); app.set('view engine', jade); app.loca
我需要了解使用路由在 Express 应用程序中流程的工作原理,我有这些路线 app.use(require('./routes/reportsRouter')); app.use(require('
在一条 route 我有这个: if (elements.length <= 0) { var msg = 'no elements found
完整的代码如下 - 很简单,我想添加、删除或更新帖子 - 当我自己做其中一件事时,它可以工作,但一起它会中断 我在 NodeJS MySQL 中搜索了很多我用它来查询数据库 var mysql = r
下面的CSS选择器有没有更优雅的写法? input:not([type="image"]):not([type="submit"]) 我想匹配所有不是 image 类型或 submit 类型的 HTM
当我尝试全局安装 express-generator 时,它说它成功了。我什至可以在我的全局应用程序列表中看到它但是当我尝试运行 express我遇到标题问题:zsh: command not fou
这不是一个很好的问题,但它是这样的: 我正在结合 DataFrame 的几列进行非常简单的计算,但前提是一列具有特定值。想法是: if df.x==1: df.y = df.y - df.a/
我有一个网站 -https://example.com- 调用 API -https://api.example.com-。该 API 是用 Express 编写的,其使用 CORS package允
当我使用 Express 框架时,我将“.html”文件放入文件夹 View 中,然后将“.js”和“.css”文件放入 public 中。当我运行我的应用程序时,我可以获得 js 和 css,但我无
有一个简单的应用程序 var express = require('express'); var http = require('http'); var path=require('path'); v
我正在构建一个基于express的REST API,我想在用户的请求中使用错误的HTTP动词时发出警告。 目前,我正在添加 app.all('/', (req, res) => { answe
对于用于在 connect/express 中对 session 数据进行加密散列的 key 长度有什么建议吗?我看到了从 60 多个字符串到规范的“键盘猫”的所有内容。 对于我的直接用例,我打算对整
Redis 的HDEL 的时间复杂度是O(N)(其中N 是要移除的字段数)。我有一个用例,其中字段必须为每个操作进行类型转换。我将如何表达新的时间复杂度? O(N*2)? 最佳答案 时间复杂度将相同:
我看到我可以像这样指定 gradle 依赖项: dependencies { compile 'com.fasterxml.jackson.core:jackson-annotations:2
我是一名优秀的程序员,十分优秀!