我在写一个与lhs中的枚举值匹配的规则时遇到了困难.
例如,如果我有以下枚举:
public enum EStatus {
OK,
NOT_OK
}
Run Code Online (Sandbox Code Playgroud)
我想用这样的东西:
rule "my rule"
dialect "java"
when
status : EStatus() // --> this works, but I want to be more specific
// status : EStatus(this == EStatus.OK) // --> doesn't work. How can I make it work?
then
// ...
end
Run Code Online (Sandbox Code Playgroud)
这在Drools中甚至可能吗?我使用的是5.1.1版.
Drools 文档提到规则可以使用date-effective
和等属性date-expires
来指定绝对规则有效期.
例如
rule "Date-restricted rule"
date-effective "20.2.2013 8:00" # 8 AM
date-expires "20.2.2013 16:00" # 4 PM
when
then
end
Run Code Online (Sandbox Code Playgroud)
Drools还支持使用interval as timer(int:)
和cron as 定期重复的规则,timer(cron:)
但这意味着规则在这些点中被触发.
如果有任何选项如何指定具有时间限制的定期可用(未触发)规则,我感兴趣.例如,让我们想象某个公司的营业时间 - 只能在正式工作期间而不是在工作时间之后执行操作.
我想要这样的东西,但这不是Drools的有效规则
rule "Time-restricted rule"
time-effective "8:00" # 8 AM
time-expires "16:00" # 4 PM
when
then
end
Run Code Online (Sandbox Code Playgroud)
是否可以将此规则延长至周一至周五上午8点至下午4点?
Drools没有直接支持基于时间的关键字,但它们使用Quartz库提供了更强大的日历机制.StatefulSession
或者WorkingMemory
通过StatelessSession
具有定义这些日历的方法创建,这些日历可以限制可以触发规则的日期和时间.
示例: 规则定义
rule "Business hours only"
calendars "business-hours"
when
SomeAttachedClass()
then
System.out.println("Rule is …
Run Code Online (Sandbox Code Playgroud) 我一直在研究如何简化我用DRL手动编写的一些规则,这些规则变得难以维护.
通过谷歌搜索导致"决策表是最好的方式去forawad".
但遗憾的是我们的事实非常复杂,所以当下drools电子书转换器,无法处理如此复杂的事实,
所以第一个问题是开发人员通常如何处理drools知识库中非常复杂的事实?
例如,我们有类似的事实
Person->List<Cars>->List<Insurances>->Each insurance Has List<History>
Run Code Online (Sandbox Code Playgroud)
现在我必须写一条规则,说人有他的保险索赔的糟糕历史.然后我发现将它放在speadsheet中非常困难,因为它更容易在drl文件上手动编写此规则.
谢谢您的帮助.对上述示例的任何帮助也都非常好.
我正在尝试Drools规则引擎,我是一个初学者.
我在单个规则文件中有以下规则:
rule "A stand alone rule"
salience 2
no-loop
when
$account : Account()
Account($account.balance>100)
then
System.out.println("balance>100");
System.out.println($account.getBalance());
System.out.println($account.getCustomer().getName());
end
rule "A second Rule"
salience 1
no-loop
when
$account : Account()
Account($account.balance<100)
then
System.out.println("balance<100");
System.out.println($account.getBalance());
System.out.println($account.getCustomer().getName());
end
Run Code Online (Sandbox Code Playgroud)
在StatefulKnowledgeSession中,我通过了两个账户,一个账户余额为15000,另一个账户余额为15,
Account account=new Account(7l,15000l);
Account account1=new Account(5l,15l);
Customer customer = new Customer("Samrat", 28, "Sector51", account);
Customer customer1 = new Customer("Alexi", 28, "Sector50", account1);
account.setCustomer(customer);
account1.setCustomer(customer1);
session.insert(account);
session.insert(account1);
session.fireAllRules();
Run Code Online (Sandbox Code Playgroud)
根据我的预期结果应该是每个规则应该只被触发一次,并且应该打印相应的对象.
但我得到的结果是:
balance>100
15000
Samrat
balance>100
15000
Samrat
balance<100
15
Alexi
balance<100
15
Alexi
Run Code Online (Sandbox Code Playgroud)
我无法理解为什么每条规则都运行两次????
我正在尝试将drools6.0用于现有的代码库(它是eclipse下的maven项目).我以前没有必要学习流口水或者maven(尽管它们是我之前项目的一部分),足以说我迷失了我想做的事情.根据我的理解(谷歌搜索),java类文件被绑定到基于包名称(?)的规则.这会解决编译时问题.但我在运行时看到空指针异常.为了让drools适应我现有的代码库:I 1)创建了helloworld drools项目,成功运行它2)将java文件复制到我现有的包中,3)在Eclipse中使用正确的包创建规则文件:FIle-> New-> other - >规则资源; 3)通过右键单击项目并将configure-> convert转换为drools项目,将现有项目转换为drools包
这一切都处理编译问题,但我得到以下运行时错误
[main] ERROR org.drools.compiler.kie.builder.impl.KieContainerImpl - Unknown KieSession name: ksession-rules
java.lang.NullPointerException
at main.java.com.harmonia.cbm.afloat.dataaquisition.dql.DroolsTest.main(DroolsTest.java:23)
Run Code Online (Sandbox Code Playgroud)
这是因为从kcontainer返回的ksession为null,并在最后一行抛出空指针异常
KieServices ks = KieServices.Factory.get();
KieContainer kContainer = ks.getKieClasspathContainer();
KieSession kSession = kContainer.newKieSession("ksession-rules");
// above line is returning null
Message message = new Message();
message.setMessage("Hello World");
message.setStatus(Message.HELLO);
kSession.insert(message);
Run Code Online (Sandbox Code Playgroud)
已经花了一天多的时间试图找出流氓是如何工作的,以及如何解决上述问题.Pl建议
1)我采取正确的方法将现有项目转换为drools项目.我想要我的代码库的所有现有功能; 但是希望为将来的增强添加基于规则的方法.遇到以下链接,但不清楚是否有助于我的情况 http://drools.46999.n3.nabble.com/Retrofitting-a-project-with-JBoss-Rules-td48656.html
2)任何有用的drools教程,以便更好地理解3行(除了java文档)
KieServices ks = KieServices.Factory.get();
KieContainer kContainer = ks.getKieClasspathContainer();
KieSession kSession = kContainer.newKieSession("ksession-rules");
Run Code Online (Sandbox Code Playgroud)
3)解决空指针异常的任何提示(假设我采用正确的方法将现有项目转换为drools项目)
更新 @David:感谢您的详细帖子.我意识到将现有项目转换为maven项目虽然有效,但由于保留了现有的目录结构/命名(很可能与maven默认创建的不同),因此对我没有吸引力.我发布了替代解决方案,我认为这个问题与类路径问题有关http://drools.46999.n3.nabble.com/Null-pointer-exception-when-adding-drools-to-existing-project-td4027944.html #a4028011
我使用KIE Workbench(6.1.0.Beta3)来编辑我的规则.由于Workbench还没有支持某些功能(例如将规则移到其他包中),我想在KIE-WB之外做这些事情.
为此,我克隆了我的存储库
git clone git://localhost:9418/my-kie-repository
Run Code Online (Sandbox Code Playgroud)
哪个工作正常.我编辑一些文件,在本地提交,然后尝试git push
.但是我得到了错误
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.
Run Code Online (Sandbox Code Playgroud)
因为我几乎可以肯定它是第一个问题,关于凭证,我在.git/config
文件中添加了行
[credential "git://localhost:9418"]
username = admin
password = admin
Run Code Online (Sandbox Code Playgroud)
不过,我得到了前面提到的错误.是的,KIE Workbench仍在运行,我也可以从那里获取/拉取没有问题.
有没有办法如何推回到KIE Workbench存储库?根据这条消息,一个"可能需要重新配置原点",但我不确定如何更改它.
有人知道如何正确设置吗?
我目前开始使用 jbpm/drools 并尝试使用“业务规则任务”从我的 DRL 修改一些流程变量。我尝试了以下过程,该过程声明了“MyCustomObject”类型的变量“var”。
根据此问题和此建议的结果,我创建了一个任务,该任务应执行规则流组“testgroup”并具有以下 onEntry 脚本:
kcontext.getKnowledgeRuntime().insert(kcontext.getProcessInstance());
Run Code Online (Sandbox Code Playgroud)
我的 DRL 现在看起来像这样:
import mypackage.MyCustomObject;
import org.kie.api.runtime.process.WorkflowProcessInstance;
rule "generate object"
ruleflow-group "testgroup"
when
//some stuff applies
then
insert(new MyCustomObject());
end
rule "modify variable"
ruleflow-group "testgroup"
when
$process: WorkflowProcessInstance()
$obj: MyCustomObject()
then
WorkflowProcessInstance $p = (WorkflowProcessInstance)kcontext.getKieRuntime().getProcessInstance($process.getId());
$p.setVariable( "var", $obj);
System.out.println("Value of object in memory: "+$obj);
System.out.println("Value of object in variable:+$p.getVariable("var"));
retract($process);
end
Run Code Online (Sandbox Code Playgroud)
在业务规则任务之后,我放置了一个简单的脚本任务:
if(var != null) {
System.out.println("var: "+var);
} else{
System.out.println("var is null!");
}
Run Code Online (Sandbox Code Playgroud)
我现在得到的输出是(注意:MyCustomObject 不会覆盖 toString):
内存中对象的值:MyCustomObject@XYZ …
我有一个相当简单的情况,我想检查规则条件中属性是否不为空。
rule "only do action if attribute is not null"
when
$fact : Fact(attribute!=null, $attribute : attribute)
then
rulesLogger.debug("Rule fires = " + $attribute);
end
Run Code Online (Sandbox Code Playgroud)
我已经在调试中跟踪了这一点。正在插入属性为 null 的事实,但规则仍会触发。控制台输出如下。
Rule fires = null
Run Code Online (Sandbox Code Playgroud)
如果我将条件更改为,attribute==null
则规则不会触发。所以它的表现似乎与我的预期完全相反。
我们确实有一个使用函数来解决这个问题的方法,但它有点丑陋,我不明白为什么它首先不起作用。
function Boolean attributeExists(Fact fact)
{
if(fact.getAttribute() == null)
{
return Boolean.FALSE;
}
else
{
return Boolean.TRUE;
}
}
rule "only do action if attribute is not null"
when
$fact : Fact($attribute : attribute)
Boolean(booleanValue == true) from attributeExists($fact)
then
rulesLogger.debug("Rule fires = " + …
Run Code Online (Sandbox Code Playgroud) 因此,我们正在探索使用Drools / JBPM,并且发现很难从JBPM中获取数据。我觉得我们缺少明显的东西。
我们正在使用RESTful接口启动JBPM流程并创建一个流程实例:POST /server/containers/{containerId}/processes/{processId}/instances
但是,在流程实例完成之后,我们需要检索结果。你是怎样做的?JBPM流程中是否需要设置某些内容(例如变量或其他变量)?
在这一点上,我们已经倾倒了大量的示例和文档,但是似乎找不到简单的答案。
谢谢-乔纳森
我了解最新版本的 Drools (7.47) 取消了 Eclipse Java 编译器的范围(https://docs.jboss.org/drools/release/7.47.0.Final/drools-docs/html_single/#drools-ecj),但是这个破坏了与 Eclipse 的集成。我不再在 .drl 文件中进行语法/错误解析,无法显示 Rete 树,并且该项目存在构建错误:
java.lang.ClassNotFoundException: org.drools.ecj.EclipseJavaCompiler cannot be found by org.drools.eclipse_7.47.0.Final
(这个错误对于Rete树和构建项目是一样的)
这是 Drools 中的一个错误,将在下一版本中修复,还是我需要解决的配置问题?如果是配置问题,我需要做什么?