drools规则引擎编译异常,原因是xtream版本有高危漏洞,需要升级版本,由1.4.7升级到1.4.20
异常缘由是,规则引擎需要加载kmodule.xml,由于xtream版本代码差异,导致drools规则引擎编译异常
xtream版本1.4.7版本
xstream.fromXML(xmlStr);
升级到1.4.20 需要
xStream.addPermission(AnyTypePermission.ANY);
xstream.fromXML(xmlStr);
我使用drools版本是6.4.0.Final,依赖xtream的1.4.7版本,在不升级drools版本的情况下,请问是否有解决办法?
我们以客户购买东西打折为例,年龄大于60的,购买金额超过100元,打9折,年龄小于等于60的,购买金额超过100元,打95折
package com.drools7.model;
public class Customer {
private String name;
private int age;
public Customer(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
}
package com.drools7.model;
public class Discount {
// 打折,默认100,即不打折
private int discount = 100;
// 用户
private Customer customer;
// 购买的总金额
private int amount = 100;
public int getDiscount() {
return discount;
}
public void setDiscount(int discount) {
this.discount = discount;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
}
写规则文件
package drools
import com.drools7.model.Discount;
rule "customer-discount1"
when
$discount : Discount(customer.age > 60 && amount >= 100)
then
$discount.setDiscount(90);
end
rule "customer-discount2"
when
$discount : Discount(customer.age <= 60 && amount >= 100)
then
$discount.setDiscount(95);
end
编写测试类并运行
package com.drools7.model;
import org.junit.Test;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
public class Demo {
@Test
public void test() {
KieServices kieServices = KieServices.Factory.get();
// 默认自动加载 META-INF/kmodule.xml
KieContainer kieContainer = kieServices.getKieClasspathContainer();
// kmodule.xml 中定义的 ksession name,默认是以有状态的会话来生成KieSession
KieSession kieSession = kieContainer.newKieSession("all-rules");
// new一组事实数据
Customer customer = new Customer("Anna", 29);
Discount discount = new Discount();
discount.setCustomer(customer);
discount.setAmount(100);
/**
* 与有状态会话进行交互的方式是:<br/>
* 我们将一组事实数据插入其中,执行任何激活的规则,然后提取我们正在寻找的响应
*/
// 将一组事实数据插入其中
kieSession.insert(discount);
// 执行激活的规则
int count = kieSession.fireAllRules();
System.out.println(count);
System.out.println(discount.getDiscount());
// After the application finishes using the session, though, it must call the
// dispose() method in order to free the resources and used memory.
// 但是,在应用程序完成使用会话之后,它必须调用dispose()方法以释放资源和使用的内存
kieSession.dispose();
}
}
运行结果如下:可以看到执行了一条规则,事实数据执行后,打折是95
[DEBUG] 2019-09-01 11:39:14,646 org.drools.core.common.DefaultAgenda - State was INACTIVE is now FIRING_ALL_RULES
[DEBUG] 2019-09-01 11:39:14,752 org.drools.core.common.DefaultAgenda - State was FIRING_ALL_RULES is now HALTING
[DEBUG] 2019-09-01 11:39:14,754 org.drools.core.common.DefaultAgenda - State was HALTING is now INACTIVE
1
95
[DEBUG] 2019-09-01 11:39:14,759 org.drools.core.common.DefaultAgenda - State was INACTIVE is now DISPOSED
整个目录如下图