【java】drools入门之eclipse插件安装

本文参考自: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下的featuresplugins复制到刚才的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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值