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文件
<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.drools.org/xsd/kmodule">
<kbase name="point-rulesKB" packages="rules">
<ksession name="point-rulesKS"/>
</kbase>
<kbase name="HelloWorldKB" packages="rules">
<ksession name="HelloWorldKS"/>
</kbase>
</kmodule>
先在resource目录下创建rules目录
添加Point-rules.drl文件
<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.drools.org/xsd/kmodule">
<kbase name="point-rulesKB" packages="rules">
<ksession name="point-rulesKS"/>
</kbase>
<kbase name="HelloWorldKB" packages="rules">
<ksession name="HelloWorldKS"/>
</kbase>
</kmodule>
如下:
然后开始写代码
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
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<Object>() );
// The application can also setup listeners
ksession.addEventListener( new DebugAgendaEventListener() );
ksession.addEventListener( new DebugRuleRuntimeEventListener() );
// To setup a file based audit logger, uncomment the next line
// KieRuntimeLogger logger = ks.getLoggers().newFileLogger( ksession, "./helloworld" );
// To setup a ThreadedFileLogger, so that the audit view reflects events whilst debugging,
// uncomment the next line
// KieRuntimeLogger logger = ks.getLoggers().newThreadedFileLogger( ksession, "./helloworld", 1000 );
// The application can insert facts into the session
final Message message = new Message();
message.setMessage( "Hello World" );
message.setStatus( Message.HELLO );
ksession.insert( message );
// and fire the rules
ksession.fireAllRules();
// Remove comment if using logging
// logger.close();
// and then dispose the session
ksession.dispose();
}
public static class Message {
public static final int HELLO = 0;
public static final int GOODBYE = 1;
private String message;
private int status;
public Message() {
}
public String getMessage() {
return this.message;
}
public void setMessage(final String message) {
this.message = message;
}
public int getStatus() {
return this.status;
}
public void setStatus(final int status) {
this.status = status;
}
public static Message doSomething(Message message) {
return message;
}
public boolean isSomething(String msg,
List<Object> list) {
list.add( this );
return this.message.equals( msg );
}
}
}
执行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<Person> peoples = new ArrayList<Person>();
peoples.add(new Person("张三",30));
peoples.add(new Person("李四",40));
peoples.add(new Person("王五",30));
ArrayList<Person> new_peoples = new ArrayList<Person>();
ks.insert(peoples);
ks.insert(new_peoples);
ks.fireAllRules();
System.out.println(new_peoples.toString());
ks.dispose();
kmodule.xml
<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.drools.org/xsd/kmodule">
<kbase name="rule1KB" packages="com.xu.drools.rule.rule1">
<ksession name="rule1KS"/>
</kbase>
<kbase name="rule2KB" packages="com.xu.drools.rule.rule2">
<ksession name="rule2KS"/>
</kbase>
<kbase name="mingKB" packages="com.xu.drools.rule.complexProblem">
<ksession name="mingKS"/>
</kbase>
<kbase name="worldKB" packages="com.xu.drools.rule.complexProblem">
<ksession name="worldKS"/>
</kbase>
<kbase name="HonestPoliticianKB" packages="com.xu.drools.rule.honestpolitician">
<ksession name="HonestPoliticianKS"/>
</kbase>
<kbase name="mapsKB" packages="com.xu.drools.rule.Map">
<ksession name="mapsKS"/>
</kbase>
<kbase name="listsKB" packages="com.xu.drools.rule.List">
<ksession name="listsKS"/>
</kbase>
</kmodule>
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<String, Person> 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)!");
}
}