我不知道在Java中是否可行,但我需要一种方法来执行特殊的“添加功能”。让我解释,
简短版本:是否可以将 int 与 Integer 对象相加?我需要一个方法来检查该整数对象是否存在,如果该对象为空,则添加 0,否则添加正确的值,否则我尝试过
public static Integer isNull(Integer bonus){
if (bonus == null)
return Integer.valueOf(0);
else
return bonus;
}
Run Code Online (Sandbox Code Playgroud)
但我无法进入“null”分支。
完整版本:整数奖金值是触发 Drools 规则的结果。此规则使用 insertLogical(new object) 语句返回此整数奖金。根据流口水手册,
insertLogical(new Something ()); 与 insert 类似,但是当没有更多事实支持当前触发规则的真实性时,该对象将自动收回。换句话说,一旦该规则的条件不再为真,引擎就会撤回逻辑插入的事实。这通常被称为“真相维护”
因此,只要规则有效,Bonus 对象就存在并且正常总和有效;但是当规则被撤销并且奖金被销毁时,我需要这笔钱继续有效
出于成瘾,我可以说我正在尝试使用 Drools 来实现 Magic The Gathering 纸牌游戏;所以我尝试编写“当某事,所以某事”之类的卡片。对于 insertLogical,我想我只需要关心规则的激活,而不是它的删除。
我希望我的问题很清楚,很抱歉我的英语不好
更好的解释(也许):事情没那么简单,卡片有一个整数值,有时,根据触发的规则,这个值可能会改变。我不想手动更改该值,因为该值可以通过多种方式更改,因此我需要 insertLogical() ,以免在规则不再存在时再次更改这些值。
示例:我有一张牌在场,其数值为 3。然后我打出另一张牌,将该数值增加到 4,但前提是第二张牌仍在场中。通常,使用 Drools insert 语句,我必须编写一些代码来增加该卡的值字段(映射到 Java 类),然后编写一些代码来在第二张卡不再使用时减少该值。通过 insertLogical (如果我理解正确的话)我可以免费获得减少代码。问题是 inserteLogical 仅适用于新的 java 对象(不适用于现有对象,或者最重要的是不适用于对象字段);所以我声明一个新的 Bonus 对象(正或负整数),我可以添加到卡值中,但它可以随时销毁,并且“添加”功能必须仍然有效。
当第二张牌打出时(或者已经打出第一张牌)我必须像这样修改第一张牌的值
card.value=card.value + isNull(bonus)
Run Code Online (Sandbox Code Playgroud)
所以如果奖金(第二张卡)存在或不存在,它可以自动改变
我有一个对象如下:
class License{
private field1;
private field2;
private boolean active;
private String activeMessage;
private boolean processed = false;
//Getter and setter methods
}
Run Code Online (Sandbox Code Playgroud)
我想做的是,根据 field1 和 field2 的值,我需要设置 isActive 标志和相应的消息。但是,如果 field1 或 field2 的规则被触发,我需要停止规则处理。也就是说,我只需要执行 1 条成功的规则。
我在一篇文章中读到,执行 ksession.fireAllRules(1) 将解决此问题。但是 fireAllRules() 方法在 Drools 6 中不可用。我也尝试输入一个返回;每条规则末尾的声明。那对我也没有帮助。
最后,我最终向我的对象添加了一个名为“processed”的附加字段。因此,每当我执行任何规则时,我都会将已处理标志设置为 true。如果该标志已经设置,那么我不会执行任何规则。这是我的规则文件:
rule "Check field1"
when
$obj : License(getField1() == "abc" && isProcessed() == false)
then
System.out.println("isProcessed >>>>>> "+$obj.isProcessed());
$obj.setActive(true);
$order.setActiveMessage("...");
$order.setProcessed(true);
end
rule "Check field2"
when
$obj : License(getField2() == "def" && isProcessed() == false) …
Run Code Online (Sandbox Code Playgroud) 在 jess 中,从规则返回字符串的方法如下:
(defrule welcome-toddlers
"Give a special greeting to young children"
(person {age < 3})
=>
(printout t "Hello, little one!" crlf))
Run Code Online (Sandbox Code Playgroud)
我的问题是如何实现返回功能,这就是我想要的:
(defrule welcome-toddlers
"Give a special greeting to young children"
(person {age < 3})
=>
(return "Hello, little one!"))
Run Code Online (Sandbox Code Playgroud)
如果不可能怎么办?
我们已经使用 Tomcat 8.0 设置并部署了 Drool 7.15 应用程序。然而,问题是当我们尝试部署规则时,我们遇到了以下异常。java.lang.RuntimeException: Cannot find KieModule: com.tisl.mpl.ruleengine:rrt-kjar:1.0.0
有什么办法可以解决这个问题?同样的 jar 在 Wildfly(jBOSS) 中运行良好
2019 年 9 月 14 日 18:24:06.510 严重 [http-nio-8080-exec-8] org.kie.server.services.impl.KieServerImpl.createContainer 为模块“com.tisl.mpl”创建容器“RRTRULE”时出错。 Ruleengine:rrt-kjar:1.0.0' java.lang.RuntimeException: 找不到 KieModule: com.tisl.mpl.ruleengine:rrt-kjar:1.0.0 在 org.drools.compiler.kie.builder.impl.KieServicesImpl。 newKieContainer(KieServicesImpl.java:117) 在 org.drools.compiler.kie.builder.impl.KieServicesImpl.newKieContainer(KieServicesImpl.java:111) 在 org.kie.server.services.impl.KieServerImpl.createContainer(KieServerImpl.java: 214) 在 org.kie.server.remote.rest.common.resource.KieServerRestImpl.createContainer(KieServerRestImpl.java:90) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl. java:62)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)在java.lang.reflect.Method.invoke(Method.java:498)在org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl) .java:168) 在 org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:269) 在 org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:227) 在 org.jboss.resteasy。 core.ResourceMethod.invoke(ResourceMethod.java:216) 在 org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:541) 在 org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:523)在 org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:125) 在 org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208) 在 org.jboss.resteasy.plugins .server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55) 在 org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50) 在 javax.servlet.http.HttpServlet.service(HttpServlet) .java:741) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 在 org.apache.tomcat。 websocket.server.WsFilter.doFilter(WsFilter.java:52) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) …
我想执行一个有状态/无状态的 drool 会话?假设 Spring Boot 默认线程池大小。我该如何考虑服务器处理能力?
规则很简单:
when
Apple( color == red )
then
System.out.println("hello");
end
Run Code Online (Sandbox Code Playgroud)
规则引擎每秒可以支持的最大调用次数是多少?我如何进行计算?
关于流口水的新手问题.我试图在规则文件中访问以下java类.
Class A{
private String foo;
private SomeClass B;
//getter setter for foo and b
}
Class B{
private String bar;
private Integer value;
//getter setter for bar and value
}
Run Code Online (Sandbox Code Playgroud)
在规则文件中,我想访问该成员bar
.
到目前为止,这就是我所拥有的: -
package demo;
import my.test.A;
import my.test.B;
rule "Rule1"
when
$varB : A.B(bar.equals("hello"))
then
$varB.setValue(1);
update($varB);
end
Run Code Online (Sandbox Code Playgroud)
对于上述规则,我收到此错误 - "BuildError: Unable to resolve ObjectType A.B"
.我也试过用A.getB()
,但也没用.知道应该是什么吗?如果重要的话,我正在使用eclipse drools插件.
我已经查看了jboss规则文档,但它们并没有涵盖那些语法.有谁知道我可以查找drools语法的链接以及如何像上面那样?
我是新来的流口水,请帮助我了解,如果我更新对象后,我打电话火的所有规则,将其启动时机的规则,因为它在Drools的文件呢?
喜欢
rule "Hello World"
no-loop true
when
message:Message (type=='Hello')
then
modify(message){
setType("Hi")//Fires other rule below
}
message.setMsgtext("Msg: Hello World, Drools");
System.out.println("Hello World, Drools!");
end
rule "Hi World"
when
message:Message (type=='Hi')
then
modify(message){
setType("Hello")
}
System.out.println("Hi World, Drools!");
end
Run Code Online (Sandbox Code Playgroud)
而在JAVA代码中,
Message msg = new Message();
msg.setType("Hello");
//sessionObject = rbase.newStatefulSession();
sessionObject.insert(msg);
sessionobject.fireAllrules();
msg.setType("Hi"); //Here can it fire the rule? for me its not doing it
Run Code Online (Sandbox Code Playgroud)
如果它没有触发,那么drool引擎会将一个msg作为与Java运行时对象完全不同的会话对象,因此它不受影响.如果它不是背后的概念,请帮助我理解
谢谢Chakri
当包含此规则以供运行时,我想在 then 部分中动态插入对象列表。
rule "insert adresses"
when
$person:Person(state=="DC")
then
//insert all address of this person, person.getAddresses() return a set of Address
Run Code Online (Sandbox Code Playgroud) 我在jboss-as-7.1.1.Final上成功部署了kie-drools-wb-distribution-wars-6.0.1.Final-jboss-as7.0.war,使用jdk1.7.0_10运行.
但是这个配置......
..无法使用此错误进行部署:
14:58:06,531 WARN [org.jboss.weld.deployer] (MSC service thread 1-5) JBAS016011: Warning while parsing vfs:/C:/java/too
ls/wildfly-8.1.0.Final-guvnor/bin/content/kie-drools-wb-distribution-wars-6.0.1.Final-jboss-as7.0.war/WEB-INF/beans.xml:
1 cvc-elt.1 : DÚclaration de l'ÚlÚment 'beans' introuvable.
14:58:07,500 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-6) MSC000001: Failed to start service jboss.module
.service."deployment.kie-drools-wb-distribution-wars-6.0.1.Final-jboss-as7.0.war".main: org.jboss.msc.service.StartExcep
tion in service jboss.module.service."deployment.kie-drools-wb-distribution-wars-6.0.1.Final-jboss-as7.0.war".main: JBAS
018759: Failed to load module: deployment.kie-drools-wb-distribution-wars-6.0.1.Final-jboss-as7.0.war:main
at org.jboss.as.server.moduleservice.ModuleLoadService.start(ModuleLoadService.java:91) [wildfly-server-8.1.0.Fi
nal.jar:8.1.0.Final]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948) [jboss-ms
c-1.2.2.Final.jar:1.2.2.Final]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1881) [jboss-msc-1.2.2.F
inal.jar:1.2.2.Final]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) [rt.jar:1.7.0_10]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) [rt.jar:1.7.0_10]
at java.lang.Thread.run(Thread.java:722) [rt.jar:1.7.0_10]
Caused by: org.jboss.modules.ModuleNotFoundException: org.jboss.netty:main …
Run Code Online (Sandbox Code Playgroud) 在我学习drools的第一天.我遇到了这个奇怪的问题.
drl中的规则" Hello World "无法在Controller中运行,但在Junit测试用例中运行良好.规则" 另一条规则 "始终在Controller和Junit测试上运行.
控制器和junit测试中的代码完全相同.
任何有想法的人都欢迎.谢谢 .
Sample.drl:
package com.happylifeplat.checkin
import com.happylifeplat.checkin.managerorder.beans.RaBean1;
rule "Hello World"
when
$h : RaBean1( id == 1)
then
$h.setContent("from drl content");
System.out.println("-----Hello World rule called id == 1");
end
rule "Another rule"
when
then
System.out.println("-----Another rule called");
end
Run Code Online (Sandbox Code Playgroud)
kmodule.xml:
<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
<kbase name="rules" packages="rules">
<ksession name="ksession-rules"/>
</kbase>
</kmodule>
Run Code Online (Sandbox Code Playgroud)
RaBean1.java:
package com.happylifeplat.checkin.managerorder.beans;
public class RaBean1 {
private int id;
private String content;
public int getId() {
return id;
}
public …
Run Code Online (Sandbox Code Playgroud) 我们有一个使用 Drools 6.5 的正在运行的项目,并希望将其迁移到 7.0。
更改Drools依赖版本并运行项目时,抛出以下异常:
All accumulate functions must be registered before building a resourceUnknown accumulate function: 'countDistinct' on rule 'IPCPF001'. All accumulate functions must be registered before building a resource. : [Accumulate: input=[AND [[Pattern: id=null; objectType=MyFact]] ]]
Run Code Online (Sandbox Code Playgroud)
我们使用累加函数,它们在 META-INF/drools.packagebuilder.conf 文件中正确注册。7.0 版本有什么变化吗?