drools,规则引擎,把业务和规则进行分离。
pom.xml
org.drools
drools-core
6.0.1.Final
org.drools
drools-compiler
6.0.1.Final
junit
junit
4.12
在resources/META-INF下新建kmodule.xml
写规则
golfer.drl
package rules;
import com.shrm.drools.model.Golfer;
rule "find solution"
when
//1.高尔夫球员Fred,目前不知道他的位置和衣服颜色
$fred : Golfer( name == "Fred" )
//3.Joe排在第2个位置
$joe : Golfer( name == "Joe",
position == 2,
position != $fred.position,
color != $fred.color )
//4.Bob穿着格子短裤
$bob : Golfer( name == "Bob",
position != $fred.position,
position != $joe.position,
color == "plaid",
color != $fred.color,
color != $joe.color )
//5.Tom没有排在第1位或第4位,也没有穿橙色衣服
$tom : Golfer( name == "Tom",
position != 1,
position != 4,
position != $fred.position,
position != $joe.position,
position != $bob.position,
color != "orange",
color != $fred.color,
color != $joe.color,
color != $bob.color )
//2.Fred右边紧挨着的球员穿蓝色衣服
Golfer( position == ( $fred.position + 1 ),
color == "blue",
this in ( $joe, $bob, $tom ) )
then
System.out.println( "Fred " + $fred.getPosition() + " " + $fred.getColor() );
System.out.println( "Joe " + $joe.getPosition() + " " + $joe.getColor() );
System.out.println( "Bob " + $bob.getPosition() + " " + $bob.getColor() );
System.out.println( "Tom " + $tom.getPosition() + " " + $tom.getColor() );
end
Ming-drink.drl
package rules;
import com.shrm.drools.model.XiaoMing
// 小明喝汽水问题
// 1元钱一瓶汽水,喝完后两个空瓶换一瓶汽水,问:小明有20元钱,最多可以喝到几瓶汽水?
// 规则1:1元钱一瓶汽水
// 规则2:两个空瓶换一瓶汽水
//规则1:1元钱一瓶汽水。有钱就买水,空瓶+1,钱-1,喝水+1;
rule "rule1"
salience 3
when
$m:XiaoMing(money>0);
then
System.out.println("有钱即可喝水,钱:"+$m.getMoney());
$m.setBottle($m.getBottle()+1);
$m.setMoney($m.getMoney()-1);
$m.setDrink($m.getDrink()+1);
update($m)
end
//规则2:两个空瓶换一瓶汽水。有空瓶就换钱,空瓶-2,钱+1;
rule "rule2"
salience 2
when
$m:XiaoMing(bottle>=2);
then
System.out.println("有瓶子就换钱,瓶子:"+$m.getBottle());
$m.setBottle($m.getBottle()-2);
$m.setMoney($m.getMoney()+1);
update($m)
end
//规则3,打印已经喝掉的数量
rule "rule3"
salience 1
when
$m:XiaoMing();
then
System.out.println("总共喝掉:"+$m.getDrink());
end
Golfer.java
public class Golfer {
private String name;
private String color;
private int position;
public Golfer(String name, String color, int position) {
this.name = name;
this.color = color;
this.position = position;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
}
GolferTest.java
package com.shrm.drools.test;
import com.shrm.drools.model.Golfer;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
/**
* 使用kmodule的方式调用drools
* /resources/META-INF/kmodule.xml
* 高尔夫球员站位问题
*/
public class GolferTest {
/**
* 已知有四个高尔夫球员,他们的名字是Fred,Joe,Bob,Tom;
* 今天他们分别穿着红色,蓝色,橙色,以及格子衣服,并且他们按照从左往右的顺序站成一排。
* 我们将最左边的位置定为1,最右边的位置定为4,中间依次是2,3位置。
* 现在我们了解的情况是:
* 1.高尔夫球员Fred,目前不知道他的位置和衣服颜色
* 2.Fred右边紧挨着的球员穿蓝色衣服
* 3.Joe排在第2个位置
* 4.Bob穿着格子短裤
* 5.Tom没有排在第1位或第4位,也没有穿橙色衣服
* 请问,这四名球员的位置和衣服颜色。
*/
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("golferKS");
String[] names = new String[]{"Fred", "Joe", "Bob", "Tom"};
String[] colors = new String[]{"red", "blue", "plaid", "orange"};
int[] positions = new int[]{1, 2, 3, 4};
for (String name : names) {
for (String color : colors) {
for (int position : positions) {
ksession.insert(new Golfer(name, color, position));
}
}
}
ksession.fireAllRules();
ksession.dispose();
}
}
XiaoMing.java
package com.shrm.drools.model;
/**
* Created by dell-c on 2018/7/8.
*/
/**
* Created by xu on 2017/6/5.
* 小明喝水问题中的小明
*/
public class XiaoMing {
//总共的钱
private int money;
//空瓶子数目
private int bottle;
//已经喝掉的汽水
private int drink;
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public int getBottle() {
return bottle;
}
public void setBottle(int bottle) {
this.bottle = bottle;
}
public int getDrink() {
return drink;
}
public void setDrink(int drink) {
this.drink = drink;
}
}
MingDrinkTest.java
package com.shrm.drools.test;
import com.shrm.drools.model.XiaoMing;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
/**
* 使用kmodule的方式调用drools
* /resources/META-INF/kmodule.xml
* 小明喝水问题
*/
public class MingDrinkTest {
/**
* 小明喝汽水问题
*
* 1元钱一瓶汽水,喝完后两个空瓶换一瓶汽水,问:小明有20元钱,最多可以喝到几瓶汽水?
*/
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("mingKS");
XiaoMing xiaoMing=new XiaoMing();
xiaoMing.setMoney(3);
ksession.insert(xiaoMing);
ksession.fireAllRules();
ksession.dispose();
}
}