本文参考自:Drools6.4 Eclipse插件安装 、Drools入门-实现评分专家系统、Drools6.3.0部署(MyEclipse安装与tomcat部署)
1. 下载eclipse插件
在官网下载eclipse插件 drools-distribution-6.3.0.Final.zip 和 droolsjbpm-tools-distribution-6.3.0.Final.zip:
在eclipse安装目录的dropins文件夹下新建文件夹drools,再将droolsjbpm-tools-distribution-6.3.0.Final.zip 解压,将droolsjbpm-tools-distribution-6.3.0.Final\binaries\org.drools.updatesite下的features和plugins复制到刚才的drools文件夹中;
重启MyEclipse,在Window->Preferences里可以看到Drools选项。接着,解压drools-distribution-6.3.0.Final.zip,点击上面的Add,选择binaries目录。打勾,apply。重启MyEclipse。出现下图的效果,代表安装成功。
2. 示例
试一下Drools入门-实现评分专家系统中的例子:
新建工程选择Drools,里面有官方的例子可以打勾试一试。
这里,笔者选择了空项目。
新建实体类文件Score.java
package com.sample;
public class Score {
public int score;
Score(int Score) {
score = Score;
}
public int getScore() {
return score;
}
}
新建测试类文件Test1.java
package com.sample;
import java.util.Scanner;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
public class Test1 {
public static final void main(String[] args) {
try {
// load up the knowledge base
KieServices ks = KieServices.Factory.get();
KieContainer kContainer = ks.getKieClasspathContainer();
KieSession kSession = kContainer.newKieSession("ksession-rules");
// go !
Scanner in = new Scanner(System.in);
System.out.println("请输入需要评级的分数:(输入0退出循环)");
int data = in.nextInt();
while (data != 0) {
Score s = new Score(data);
kSession.insert(s);
kSession.fireAllRules();
System.out.println("请输入需要评级的分数:(输入0退出循环)");
data = in.nextInt();
}
} catch (Throwable t) {
t.printStackTrace();
}
}
}
新建规则文件rule1.drl
package com.sample
import com.sample.DroolsTest.Score;
rule "ScoreA"
when
Score(score > 90)
then
System.out.println("A");
end
rule "ScoreB+"
when
Score(score >=87&&score<=89)
then
System.out.println("B+");
end
rule "ScoreB"
when
Score(score >=80&&score<=86)
then
System.out.println("B");
end
rule "ScoreC+"
when
Score(score >=77&&score<=79)
then
System.out.println("C+");
end
rule "ScoreC"
when
Score(score >=70&&score<=76)
then
System.out.println("C");
end
rule "ScoreD+"
when
Score(score >=67&&score<=69)
then
System.out.println("D+");
end
rule "ScoreD"
when
Score(score >=60&&score<=66)
then
System.out.println("D");
end
rule "ScoreF"
when
Score(score < 60)
then
System.out.println("F");
end