1、drools Decision Table决策表Drools决策表的使用1简单了解 这几天在研究drools, drools到底是怎么与决策表交互的呢,报这个疑问开始了寻找之路。何时使用决策表如果规则能够被表达为模板+数据的格式,那你应该考虑使用决策表。决策表中的每一行就是对应模板的一行数据,将产生一个规则。运行决策表Drools引擎所部署的系统中执行决策表。首先,决策表转换成的Drools规则语言(DRL),然后执行规则引擎需求。这意味着它是可能的业务规则的变更,无需停止和启动,或重新部署任何软件。2创建java类公共java类:Caluculation.java执行java类及规则文件(d
2、rl文件):CalculationDrl.java和calculation.drl执行java类及决策表(xml文件):CalculationXls.java和calculation.xls(运行决策表方法一)执行java类及决策表(xml文件)生成规则,将得到的规则放入规则文件中(drl文件中):GenerationRules.java、calculation.xls、calculations.drl、CalculationDrlXls(运行决策表方法二)2.1.1Caluculation.javapackage com.flagleader.bean;public class Calcul
3、ation private double wage;/当月工资 private double actualwage;/当月税后工资 private double wagemore;/全月应纳税所得额 private double cess;/税率 private double preminus;/速算扣除数 private double wageminus;/当月工资扣减数 /* * 当月工资 * return */ public double getWage() return wage; public void setWage(double wage) this.wage = wage; /
4、* * 当月税后工资 * return */ public double getActualwage() return actualwage; public void setActualwage(double actualwage) this.actualwage = actualwage; /* * 全月应纳税所得额 * return */ public double getWagemore() return wagemore; public void setWagemore(double wagemore) this.wagemore = wagemore; /* * 税率 * retur
5、n */ public double getCess() return cess; public void setCess(double cess) this.cess = cess; /* * 速算扣除数 * return */ public double getPreminus() return preminus; public void setPreminus(double preminus) this.preminus = preminus; /* * 当月工资扣减数 * return */ public double getWageminus() return wageminus;
6、public void setWageminus(double wageminus) this.wageminus = wageminus; public Calculation() public Calculation(double wage) super(); this.wage = wage; public Calculation(double wage,double wagemore) super(); this.wage = wage; this.wagemore=wagemore; Override public String toString() return 当月工资=+wag
7、e+n当月税后工资=+actualwage+n全月应纳税所得额=+wagemore+ n税率=+cess+n速算扣除数=+preminus+n当月工资扣减数=+wageminus; 2.1.2calculation.drlpackage com.flagleader.beanglobal Calculation cal;no-loop truerule 全月应纳税额salience 3 when $cal : Calculation(wage0) then #System.out.println(-全月应纳税额-); cal.setWagemore(cal.getWage()-3500); u
8、pdate(cal);endrule 设置税率salience 2 when eval(true) then #System.out.println(-设置税率-); if ( cal.getWagemore()1500 & cal.getWagemore()4500 & cal.getWagemore()9000 & cal.getWagemore()35000 & cal.getWagemore()55000 & cal.getWagemore()80000 ) cal.setCess(0.45); cal.setPreminus(13505); insert(cal);endrule 税
9、后工资salience 1 when eval(true) then #System.out.println(-税后工资-); cal.setWageminus(cal.getWagemore()*cal.getCess()-cal.getPreminus(); cal.setActualwage(cal.getWage()-cal.getWageminus(); insert(cal);end2.1.3CalculationDrl.javapackage com.flagleader.bean;import org.apache.log4j.Logger;import org.drools.
10、KnowledgeBase;import org.drools.builder.KnowledgeBuilder;import org.drools.builder.KnowledgeBuilderFactory;import org.drools.builder.ResourceType;import org.drools.io.ResourceFactory;import org.drools.runtime.StatefulKnowledgeSession;SuppressWarnings(restriction)public class CalculationDrl static Lo
11、gger logger = Logger.getLogger(CalculationDrl.class); public static void main(String args) logger.info(开始); KnowledgeBuilder kBuilder=KnowledgeBuilderFactory.newKnowledgeBuilder();/规则生成器:将编写好的规则进行编译 kBuilder.add(ResourceFactory.newClassPathResource(calculation.drl, CalculationDrl.class), ResourceTyp
12、e.DRL);/添加规则到规则生成其中进行编译 KnowledgeBase kBase=kBuilder.newKnowledgeBase();/收集应用当中的知识定义知识库对象 kBase.addKnowledgePackages(kBuilder.getKnowledgePackages();/添加规则包 StatefulKnowledgeSession session=kBase.newStatefulKnowledgeSession();/与规则的交互通道 Calculation cal=new Calculation(10000);/输入当月工资 session.setGlobal(
13、cal, cal); session.insert(cal);/将对象添加到交互通道中 session.fireAllRules();/执行规则 session.dispose();/释放资源 System.out.println(cal.toString(); logger.info(结束); 2.1.4calculation.xls2.1.5CalculationXls.javapackage com.flagleader.bean;import org.apache.log4j.Logger;import org.drools.KnowledgeBase;import org.drool
14、s.builder.DecisionTableConfiguration;import org.drools.builder.DecisionTableInputType;import org.drools.builder.KnowledgeBuilder;import org.drools.builder.KnowledgeBuilderFactory;import org.drools.builder.ResourceType;import org.drools.io.ResourceFactory;import org.drools.runtime.StatefulKnowledgeSe
15、ssion;SuppressWarnings(restriction)public class CalculationXls static Logger logger = Logger.getLogger(CalculationDrl.class); public static void main(String args) logger.info(开始); DecisionTableConfiguration dtc=KnowledgeBuilderFactory.newDecisionTableConfiguration(); dtc.setInputType(DecisionTableIn
16、putType.XLS); KnowledgeBuilder builder=KnowledgeBuilderFactory.newKnowledgeBuilder(); builder.add(ResourceFactory.newClassPathResource(com/flagleader/bean/calculation.xls, CalculationXls.class),ResourceType.DTABLE,dtc); KnowledgeBase base=builder.newKnowledgeBase(); base.addKnowledgePackages(base.ge
17、tKnowledgePackages(); StatefulKnowledgeSession session=base.newStatefulKnowledgeSession(); Calculation cal=new Calculation(10000,6500.0); session.insert(cal); session.fireAllRules(); session.dispose();/释放资源 logger.info(结束); 2.1.6GenerationRules.javapackage com.flagleader.bean;import java.io.*;import
18、 org.drools.decisiontable.InputType;import org.drools.decisiontable.SpreadsheetCompiler;import org.junit.Test;public class GenerationRules Test public void compile() throws FileNotFoundException File file = new File( E:qzxx_workspacecalculationsrcmainjavacomflagleaderbeancalculation.xls); InputStrea
19、m is = new FileInputStream(file); SpreadsheetCompiler converter = new SpreadsheetCompiler(); String drl = pile(is, InputType.XLS); System.out.println(nn + drl); 2.1.7calculations.drlpackage com.flagleader.bean /generated from Decision Tableimport com.flagleader.bean.Calculation;import java.lang.Obje
20、ct;function double getCes(double cess) /System.out.println(cess=+cess); return cess; function double getPre(double preminus) /System.out.println(preminus=+preminus); return preminus; global Calculation cal;rule 全月应纳税额no-loop truesalience 65526 when $cal : Calculation(wage0) then System.out.println(-
21、全月应纳税额-); cal.setWagemore(cal.getWage()-3500); update(cal);end/ rule values at D11, header at D6rule test_11 salience 65525 when Calculation(wagemore0,wagemore1500,wagemore4500,wagemore9000,wagemore35000,wagemore55000,wagemore80000,wagemore=100000) then cal.setCess(getCes(0.45); cal.setPreminus(getP
22、re(13505.00); insert(cal);endrule 税后工资salience 65518 when eval(true) then System.out.println(-税后工资-+cal.getCess(); cal.setWageminus(cal.getWagemore()*cal.getCess()-cal.getPreminus(); cal.setActualwage(cal.getWage()-cal.getWageminus(); insert(cal);end2.1.8CalculationDrlXls.javapackage com.flagleader.
23、bean;import org.apache.log4j.Logger;import org.drools.KnowledgeBase;import org.drools.builder.KnowledgeBuilder;import org.drools.builder.KnowledgeBuilderFactory;import org.drools.builder.ResourceType;import org.drools.io.ResourceFactory;import org.drools.runtime.StatefulKnowledgeSession;SuppressWarn
24、ings(restriction)public class CalculationDrlXls static Logger logger = Logger.getLogger(CalculationDrlXls.class); public static void main(String args) logger.info(开始); KnowledgeBuilder kBuilder=KnowledgeBuilderFactory.newKnowledgeBuilder();/规则生成器:将编写好的规则进行编译 kBuilder.add(ResourceFactory.newClassPathResource(calculations.drl, CalculationDrlXls.class), ResourceType.DRL);/添加规
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1