我正在开发一个项目,在Windows上的WAS 8.5.9上部署了Red Hat Drools,我试图弄清楚如何在我的会话中触发规则后获取知识库中的事实和全局变量REST调用 .
我正在使用的REST API是:
[POST] http://localhost:9080/kie-server/services/rest/server/containers/instances/TargaRuleContainer
“TargaRuleContainer”是我容器的id . 这是我的应用程序的代码:
kmodule.xml
<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
<kbase name="targa" packages="com.sample">
<ksession name="targaSession" type="stateless">
</ksession>
</kbase>
</kmodule>
规则文件:
package com.sample
import com.sample.targaRule.model.Targa;
import static com.sample.targaRule.util.Utils.targaPari;
global java.lang.String city
rule "Targa default"
salience -10
activation-group "targaActivation"
ruleflow-group "targa"
when
then
city = "No city";
System.out.println("No City!");
end
rule "Targa OK Milano"
salience 10
activation-group "targaActivation"
ruleflow-group "targa"
when
t : Targa( targaPari(targa) )
then
t.setCity("Milano");
city = "Milano";
System.out.println("Milano!!");
update(t)
end
rule "Targa NO Milano"
salience 10
activation-group "targaActivation"
ruleflow-group "targa"
when
t : Targa( !targaPari(targa) )
then
t.setCity("Roma");
city = "Roma";
System.out.println("Roma!!");
update(t)
end
POJO课程:
package com.sample.targaRule.model;
import java.io.Serializable;
public class Targa implements Serializable{
private static final long serialVersionUID = 1L;
private String nome, cognome, targa, city;
public Targa() {
super();
}
public Targa(String nome, String cognome, String targa) {
this.nome = nome;
this.cognome = cognome;
this.targa = targa;
this.city = "";
}
public Targa(String nome, String cognome, String targa, String city) {
this.nome = nome;
this.cognome = cognome;
this.targa = targa;
this.city = city;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getCognome() {
return cognome;
}
public void setCognome(String cognome) {
this.cognome = cognome;
}
public String getTarga() {
return targa;
}
public void setTarga(String targa) {
this.targa = targa;
}
}
实用工具类:
package com.sample.targaRule.util;
public class Utils {
public static boolean targaPari(String t) {
int lastNum = Integer.parseInt(t.substring(4,5));
if (lastNum%2==0) {
System.out.println("Checking plate "+t+": last number is "+lastNum+", so even");
return true;
}
System.out.println("Checking plate "+t+": last number is "+lastNum+", so odd");
return false;
}
}
以下是我的要求:
{
"lookup": "targaSession",
"commands": [{
"insert": {
"object": {
"com.sample.targaRule.model.Targa": {
"nome": "Donald",
"cognome": "Trump",
"targa": "ED222ED",
"city": ""
}
},
"out-identifier": "Input"
}
}, {
"fire-all-rules": {
"out-identifier": "FireAllRules"
}
}, {
"get-objects": {
"out-identifier": "Output"
}
}, {
"get-global": {
"out-identifier": "Global",
"identifier": "city"
}
}
]
}
以下是服务器的响应:
{
"type" : "SUCCESS",
"msg" : "Container TargaRuleContainer successfully called.",
"result" : {
"execution-results" : {
"results" : [ {
"key" : "Input",
"value" : {"com.sample.targaRule.model.Targa":{
"nome" : "Donald",
"cognome" : "Trump",
"targa" : "ED222ED",
"city" : ""
}}
}, {
"key" : "Output",
"value" : [{"com.sample.targaRule.model.Targa":{
"nome" : "Donald",
"cognome" : "Trump",
"targa" : "ED222ED",
"city" : ""
}}]
}, {
"key" : "FireAllRules",
"value" : 0
}, {
"key" : "Global"
} ],
"facts" : [ {
"key" : "Input",
"value" : {"org.drools.core.common.DefaultFactHandle":{
"external-form" : "0:1:-1324533475:-1324533475:1:DEFAULT:NON_TRAIT:com.sample.targaRule.model.Targa"
}}
} ]
}
}
}
该规则只检查pojo类的“targa”字符串字段(检查倒数第三个字符是奇数还是偶数),并相应地设置另一个字符串字段“city” . 它还设置一个名为“city”的字符串全局变量,并执行System.out.println() .
正如您在响应中看到的那样,调用成功,但我无法在插入的对象中看到编辑,也无法看到全局变量 .
而且,前两个规则后果中的 System.out.println()
没有显示在服务器日志中,我只能看到在我在规则条件中调用的Utls类静态方法中完成的打印 .
我很确定我在请求语法中做错了什么,但是我很难在网上找到一些例子,因为Red Hat文档并不那么详细 .
EDIT1:我发现了POJO字段“city”的输出问题 . 问题是规则体中设置的“ruleflow-group”参数(我需要这个因为我在jbpm进程中也使用相同的规则) . 我通过在规则中插入“自动对焦”参数解决了问题 .
对于涉及全局变量的问题,进行一些逆向工程我发现(可能)全局变量仅在有状态会话中被允许,因此我相应地更改了我的kmodule.xml,但仍然无法在输出中获得全局变量“city” .