Drools规则引擎就是根据规则判断和筛选出符合规则的数据。
这哥们讲的很生动,而且看了一下他的git写的很不错(收藏了)
https://www.cnblogs.com/ityouknow/archive/2017/08/07/7297524.html
在使用drools时会涉及到规则语法
https://blog.csdn.net/u012373815/article/details/53872025/
drools底层算法rate(看了一下大概是官方文档的中文版)
https://blog.csdn.net/u012373815/article/details/53869097
入门demo
需求:根据用户购买商品的价格为用户添加额外的积分
先在resource目录下创建META-INF目录
添加kmodule.xml文件
先在resource目录下创建rules目录
添加Point-rules.drl文件
如下:
然后开始写代码
org.springframework.boot
spring-boot-devtools
true
public class Order {
private Date bookingDate;//下单日期
private int amout;//订单原价金额
private User user;//下单人
private int score;
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public Date getBookingDate() {
return bookingDate;
}
public void setBookingDate(Date bookingDate) {
this.bookingDate = bookingDate;
}
public int getAmout() {
return amout;
}
public void setAmout(int amout) {
this.amout = amout;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
public class User {
private String name;//姓名
private int level;//用户级别
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
}
import org.kie.api.KieServices;
import org.kie.api.event.rule.DebugAgendaEventListener;
import org.kie.api.event.rule.DebugRuleRuntimeEventListener;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import java.util.ArrayList;
import java.util.List;
/**
* This is a sample file to launch a rule package from a rule source file.
*/
public class HelloWorldExample {
public static final void main(final String[] args) {
// KieServices is the factory for all KIE services
KieServices ks = KieServices.Factory.get();
// From the kie services, a container is created from the classpath
KieContainer kc = ks.getKieClasspathContainer();
execute( kc );
}
public static void execute( KieContainer kc ) {
// From the container, a session is created based on
// its definition and configuration in the META-INF/kmodule.xml file
KieSession ksession = kc.newKieSession("HelloWorldKS");
// Once the session is created, the application can interact with it
// In this case it is setting a global as defined in the
// org/drools/examples/helloworld/HelloWorld.drl file
ksession.setGlobal( "list",
new ArrayList
执行main方法输出:
用户Name1享受额外增加积分: 0
用户Name2享受额外增加积分: 100
用户Name3享受额外增加积分: 500
用户Name4享受额外增加积分: 1000
Process finished with exit code 0
实例二
drools 集合操作: List Map
List
helloworld.drl
package helloworld;
import com.neo.drools.model.Person;
import java.util.ArrayList;
import java.util.Map
import java.util.HashSet;
rule "HelloWorld"
when
$set:ArrayList();
$ps:ArrayList()
$p:Person(age==30) from $ps;
then
$set.add($p);
end
java
KieSession ks = ReloadDroolsRulesService.kieContainer.newKieSession();
ArrayList peoples = new ArrayList();
peoples.add(new Person("张三",30));
peoples.add(new Person("李四",40));
peoples.add(new Person("王五",30));
ArrayList new_peoples = new ArrayList();
ks.insert(peoples);
ks.insert(new_peoples);
ks.fireAllRules();
System.out.println(new_peoples.toString());
ks.dispose();
kmodule.xml
Map
maps.drl
package com.rules
import java.util.Map;
import com.xu.drools.bean.Person;
rule "map-usage"
agenda-group "map-group"
when
$obj : Object();
$map : Map(["p1"] != $obj);
then
System.out.println("p1's age is " + ((Person)$map.get("p1")).getAge());
System.out.println("p2's age is " + ((Person)$map.get("p2")).getAge());
end
MapTest
package com.xu.drools.rule.complexProblem;
import com.xu.drools.bean.Golfer;
import com.xu.drools.bean.Person;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import java.util.HashMap;
import java.util.Map;
public class MapTest {
public static void main(final String[] args) {
KieContainer kc = KieServices.Factory.get().getKieClasspathContainer();
System.out.println(kc.verify().getMessages().toString());
execute(kc);
}
private static void execute(KieContainer kc) {
KieSession ksession = kc.newKieSession("mapsKS");
ksession.getAgenda().getAgendaGroup("map-group").setFocus();
// KieSession kieSession = this.getKieSession("map-group");
Map map = new HashMap<>();
Person p1 = new Person();
p1.setAge(10);
map.put("p1",p1);
Person p2 = new Person();
p2.setAge(20);
map.put("p2",p2);
ksession.insert(map);
int count = ksession.fireAllRules();
System.out.println("Fire " + count + " rule(s)!");
}
}