如何在Java中动态构建Drools规则?

发布于 2024-09-06 20:51:42 字数 116 浏览 5 评论 0原文

如何在Java中动态构建Drools规则而不是提供静态drl文件?

我在 xml 中定义了一组匹配规则,我需要解析这些规则并能够在运行时重新加载它。有没有办法动态构建Drools规则?在文档中找不到它。

How to build Drools rules in Java dynamically instead of providing static drl file?

I have a set of matching rules defined in xml that I need to parse and be able to reload it in runtime. Is there any way to build Drools rules dynamically? Couldn't find it in the docs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

非要怀念 2024-09-13 20:51:42

我同意罗曼的评论 - 如果你有某种声明形式的规则 - 你可以直接从中生成代码 - 除非这些规则中隐含着更高阶的逻辑(我不太可能发现),或者也许你想做一个从 XML 迁移到规则语言的时间。

I agree with Romain's comment - if you have the rules in some declarative form - you could just generate code directly from that - unless there is higher order logic implicit in those rules (unlikely I have found), or perhaps you want to do a one time migration to a rule language out of the XML.

奈何桥上唱咆哮 2024-09-13 20:51:42

我同意以前的答案和评论,即规则被修改为“静态”部分。当规则是按需动态创建时,规则引擎的使用是有问题的。然而,在某些情况下,规则并未以 drools 可以立即处理的形式/格式提供。在这种情况下,需要在初始化阶段以编程方式创建规则。

说了这么多,下面是如何向 drools 添加规则(以字符串形式给出)。

public void addRule(String myRuleStatement, String myPackage, RuleBase myRuleBase ) {
    PackageBuilder packageBuilder = new PackageBuilder(new Package(myPackage));
    packageBuilder.addPackgeFromDrl( new StringReader( myRuleStatement ) );
    myRuleBase.addPackage ( packageBuilder.getPackage() );
}

I agree with former answers and comments that rules are ment to be the "static" part. When rules are created dynamically on demand, the usage of a rules engine is questionable. However, there are cases where rules are not provided in form / format that drools can handle out of the box. In such cases programmatical creation of rules during the initialization phase is needed.

These things said, here is how you can add a rule (given as a String) to drools.

public void addRule(String myRuleStatement, String myPackage, RuleBase myRuleBase ) {
    PackageBuilder packageBuilder = new PackageBuilder(new Package(myPackage));
    packageBuilder.addPackgeFromDrl( new StringReader( myRuleStatement ) );
    myRuleBase.addPackage ( packageBuilder.getPackage() );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文