Java Drools5.1 规则流基础【示例】(下)

 

 

六、测试代码:

 

Employ:

 

package sample;



public class Employ {

	

	public static final double BONUS = 1000;

	

	private String eduInfo;

	private String resume;

	private String annualExam;

	private String awardPunish;

	

	private double basicSalary;

	private double dutySalary;

	private double bonus;

	private double percent;

	

	private double totalSalary;

    

	public String getEduInfo() {

		return eduInfo;

	}

	public void setEduInfo(String eduInfo) {

		this.eduInfo = eduInfo;

	}

	public String getResume() {

		return resume;

	}

	public void setResume(String resume) {

		this.resume = resume;

	}

	public String getAnnualExam() {

		return annualExam;

	}

	public void setAnnualExam(String annualExam) {

		this.annualExam = annualExam;

	}

	public String getAwardPunish() {

		return awardPunish;

	}

	public void setAwardPunish(String awardPunish) {

		this.awardPunish = awardPunish;

	}

	

	public double getBasicSalary() {

		return basicSalary;

	}

	public void setBasicSalary(double basicSalary) {

		this.basicSalary = basicSalary;

	}

	

	public double getDutySalary() {

		return dutySalary;

	}

	public void setDutySalary(double dutySalary) {

		this.dutySalary = dutySalary;

	}



	public double getBonus() {

		return bonus;

	}

	public void setBonus(double bonus) {

		this.bonus = bonus;

	}

	

	public double getPercent() {

		return percent;

	}

	public void setPercent(double percent) {

		this.percent = percent;

	}

	

	public double getTotalSalary() {

		return totalSalary;

	}

	public void setTotalSalary(double totalSalary) {

		this.totalSalary = totalSalary;

	}

    

    public String toString() {

    	return "[" + eduInfo + " " + resume + " " + annualExam + " " +

    	             awardPunish + " " + basicSalary + " " + dutySalary + " " +

    	             bonus + " " + totalSalary + "]";   

    }

    

}

 


EmployTest:

 

package sample;



import org.drools.KnowledgeBase;

import org.drools.KnowledgeBaseFactory;

import org.drools.builder.KnowledgeBuilder;

import org.drools.builder.KnowledgeBuilderError;

import org.drools.builder.KnowledgeBuilderErrors;

import org.drools.builder.KnowledgeBuilderFactory;

import org.drools.builder.ResourceType;

import org.drools.io.ResourceFactory;

import org.drools.logger.KnowledgeRuntimeLogger;

import org.drools.logger.KnowledgeRuntimeLoggerFactory;

import org.drools.runtime.StatefulKnowledgeSession;



public class EmployTest {

	

	public static void main(String[] args) {

		

		try {

			// load up the knowledge base

			KnowledgeBase kbase = readKnowledgeBase();

			StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

			KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");

			// go !

			Employ emp = new Employ();

			emp.setEduInfo("master");

			emp.setResume("tech");

			emp.setAnnualExam("good");

			emp.setAwardPunish("award");

			ksession.insert(emp);

			ksession.startProcess("myrules.salary");

			ksession.fireAllRules();	

			System.out.println("Basic Salary: " + emp.getBasicSalary());

			System.out.println("Duty Salary: " + emp.getDutySalary());

			System.out.println("Bonus      : " + emp.getBonus());

			System.out.println("rate       : " + emp.getPercent());

			System.out.printf("Total Salary: %.0f" , emp.getTotalSalary());

			logger.close();

		} catch (Throwable t) {

			t.printStackTrace();

		}			

		

	}

		

	private static KnowledgeBase readKnowledgeBase() throws Exception {

		KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();

		kbuilder.add(ResourceFactory.newClassPathResource("rules/EduInfoRule.drl"), ResourceType.DRL);

		kbuilder.add(ResourceFactory.newClassPathResource("rules/ResumeRule.drl"), ResourceType.DRL);

		kbuilder.add(ResourceFactory.newClassPathResource("rules/BonusRule.drl"), ResourceType.DRL);

		kbuilder.add(ResourceFactory.newClassPathResource("rules/AwardPunish.drl"), ResourceType.DRL);

	    kbuilder.add(ResourceFactory.newClassPathResource("rules/TotalRule.drl"), ResourceType.DRL);

		kbuilder.add(ResourceFactory.newClassPathResource("rules/salary.rf"), ResourceType.DRF);

		KnowledgeBuilderErrors errors = kbuilder.getErrors();

		if (errors.size() > 0) {

			for (KnowledgeBuilderError error: errors) {

				System.err.println(error);

			}

			throw new IllegalArgumentException("Could not parse knowledge.");

		}

		KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();

		kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

		return kbase;

	}	



}

 

 

              

测试结果:

               execrule Technician ...

           execrule Master ...

execrule Good ...

execrule Award ...

execrule Total ...

BasicSalary: 2500.0

DutySalary: 2000.0

Bonus : 900.0

rate : 1.1

TotalSalary: 5940

 

 

七、总结


Javadrools规则流基础:


1】掌握规则引擎的基本工作原理;

2】掌握规则文件的编写;

3】掌握规则流图的绘制及应用程序集成。

4】掌握规则组合的两种基本方式【并发和顺序】的实现方式。


下一步:


1】更复杂的规则组合;

2】规则文件的组织、管理;

3】规则文件的自动生成;

4】用户自定义规则;

 

NOTE:  关于XP下的示例作了一些修改,主要增加了XML格式的规则流文件及一个简单的自动生成顺序规则流的方法。已经上传至CSDN下载,请在下面网址下载:http://download.csdn.net/source/3462802  

 

 

你可能感兴趣的:(drools)