6. Test code:
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 baseKnowledgeBase 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;}}
Test results:
Execrule technician...
Execrule master...
Execrule good...
Execrule Award...
Execrule total...
Basicsalary 2500.0
Dutysalary: 2000.0
Bonus: 900.0
Rate: 1.1
Totalsalary: 5940
VII. Summary
Javadrools rule flow basics:
[1] master the basic working principles of the Rule Engine;
[2] write Rule files;
[3] Master the rule flow chart rendering and application integration.
[4] Master the two basic methods of Rule combination: concurrency and order.
Next step:
[1] more complex rule combinations;
[2] Organization and Management of Rule files;
[3] Automatic Generation of Rule files;
[4] custom rules;
Note: Some modifications are made to the example in XP, mainly adding the rule stream file in XML format and a simple method for automatically generating the sequence rule stream. Has been uploaded to csdn download, please download at the following URL: http://download.csdn.net/source/3462802