drools Decision Table决策表.docx

上传人:b****5 文档编号:12668026 上传时间:2023-04-21 格式:DOCX 页数:17 大小:35.29KB
下载 相关 举报
drools Decision Table决策表.docx_第1页
第1页 / 共17页
drools Decision Table决策表.docx_第2页
第2页 / 共17页
drools Decision Table决策表.docx_第3页
第3页 / 共17页
drools Decision Table决策表.docx_第4页
第4页 / 共17页
drools Decision Table决策表.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

drools Decision Table决策表.docx

《drools Decision Table决策表.docx》由会员分享,可在线阅读,更多相关《drools Decision Table决策表.docx(17页珍藏版)》请在冰豆网上搜索。

drools Decision Table决策表.docx

droolsDecisionTable决策表

Drools决策表的使用

1简单了解

这几天在研究drools,drools到底是怎么与决策表交互的呢,报这个疑问开始了寻找之路。

●何时使用决策表——如果规则能够被表达为模板+数据的格式,那你应该考虑使用决策表。

决策表中的每一行就是对应模板的一行数据,将产生一个规则。

●运行决策表——Drools引擎所部署的系统中执行决策表。

首先,决策表转换成的Drools规则语言(DRL),然后执行规则引擎需求。

这意味着它是可能的业务规则的变更,无需停止和启动,或重新部署任何软件。

2创建java类

●公共java类:

Caluculation.java

●执行java类及规则文件(drl文件):

CalculationDrl.java和calculation.drl

●执行java类及决策表(xml文件):

CalculationXls.java和calculation.xls(运行决策表方法一)

●执行java类及决策表(xml文件)生成规则,将得到的规则放入规则文件中(drl文件中):

GenerationRules.java、calculation.xls、calculations.drl、CalculationDrlXls(运行决策表方法二)

2.1.1Caluculation.java

packagecom.flagleader.bean;

publicclassCalculation{

privatedoublewage;//当月工资

privatedoubleactualwage;//当月税后工资

privatedoublewagemore;//全月应纳税所得额

privatedoublecess;//税率

privatedoublepreminus;//速算扣除数

privatedoublewageminus;//当月工资扣减数

/**

*当月工资

*@return

*/

publicdoublegetWage(){

returnwage;

}

publicvoidsetWage(doublewage){

this.wage=wage;

}

/**

*当月税后工资

*@return

*/

publicdoublegetActualwage(){

returnactualwage;

}

publicvoidsetActualwage(doubleactualwage){

this.actualwage=actualwage;

}

/**

*全月应纳税所得额

*@return

*/

publicdoublegetWagemore(){

returnwagemore;

}

publicvoidsetWagemore(doublewagemore){

this.wagemore=wagemore;

}

/**

*税率

*@return

*/

publicdoublegetCess(){

returncess;

}

publicvoidsetCess(doublecess){

this.cess=cess;

}

/**

*速算扣除数

*@return

*/

publicdoublegetPreminus(){

returnpreminus;

}

publicvoidsetPreminus(doublepreminus){

this.preminus=preminus;

}

/**

*当月工资扣减数

*@return

*/

publicdoublegetWageminus(){

returnwageminus;

}

publicvoidsetWageminus(doublewageminus){

this.wageminus=wageminus;

}

publicCalculation(){

}

publicCalculation(doublewage){

super();

this.wage=wage;

}

publicCalculation(doublewage,doublewagemore){

super();

this.wage=wage;

this.wagemore=wagemore;

}

@Override

publicStringtoString(){

return"当月工资="+wage+"\n当月税后工资="+actualwage+"\n全月应纳税所得额="+wagemore+

"\n税率="+cess+"\n速算扣除数="+preminus+"\n当月工资扣减数="+wageminus;

}

}

2.1.2calculation.drl

packagecom.flagleader.bean

globalCalculationcal;

no-looptrue

rule"全月应纳税额"

salience3

when

$cal:

Calculation(wage>0)

then

#System.out.println("-----全月应纳税额------");

cal.setWagemore(cal.getWage()-3500);

update(cal);

end

rule"设置税率"

salience2

when

eval(true)

then

#System.out.println("-----设置税率------");

if(cal.getWagemore()<=1500){

cal.setCess(0.03);

cal.setPreminus(0);

}

elseif(cal.getWagemore()>1500&&cal.getWagemore()<=4500){

cal.setCess(0.1);

cal.setPreminus(105);

}

elseif(cal.getWagemore()>4500&&cal.getWagemore()<=9000){

cal.setCess(0.2);

cal.setPreminus(555);

}

elseif(cal.getWagemore()>9000&&cal.getWagemore()<=35000){

cal.setCess(0.25);

cal.setPreminus(1005);

}

elseif(cal.getWagemore()>35000&&cal.getWagemore()<=55000){

cal.setCess(0.3);

cal.setPreminus(2755);

}

elseif((cal.getWagemore()>55000&&cal.getWagemore()<=80000)){

cal.setCess(0.35);

cal.setPreminus(5505);

}

elseif(cal.getWagemore()>80000){

cal.setCess(0.45);

cal.setPreminus(13505);

}

insert(cal);

end

rule"税后工资"

salience1

when

eval(true)

then

#System.out.println("-----税后工资------");

cal.setWageminus(cal.getWagemore()*cal.getCess()-cal.getPreminus());

cal.setActualwage(cal.getWage()-cal.getWageminus());

insert(cal);

end

2.1.3CalculationDrl.java

packagecom.flagleader.bean;

importorg.apache.log4j.Logger;

importorg.drools.KnowledgeBase;

importorg.drools.builder.KnowledgeBuilder;

importorg.drools.builder.KnowledgeBuilderFactory;

importorg.drools.builder.ResourceType;

importorg.drools.io.ResourceFactory;

importorg.drools.runtime.StatefulKnowledgeSession;

@SuppressWarnings("restriction")

publicclassCalculationDrl{

staticLoggerlogger=Logger.getLogger(CalculationDrl.class);

publicstaticvoidmain(String[]args){

logger.info("开始");

KnowledgeBuilderkBuilder=KnowledgeBuilderFactory.newKnowledgeBuilder();//规则生成器:

将编写好的规则进行编译

kBuilder.add(ResourceFactory.newClassPathResource("calculation.drl",CalculationDrl.class),ResourceType.DRL);//添加规则到规则生成其中进行编译

KnowledgeBasekBase=kBuilder.newKnowledgeBase();//收集应用当中的知识定义知识库对象

kBase.addKnowledgePackages(kBuilder.getKnowledgePackages());//添加规则包

StatefulKnowledgeSessionsession=kBase.newStatefulKnowledgeSession();//与规则的交互通道

Calculationcal=newCalculation(10000);//输入当月工资

session.setGlobal("cal",cal);

session.insert(cal);//将对象添加到交互通道中

session.fireAllRules();//执行规则

session.dispose();//释放资源

System.out.println(cal.toString());

logger.info("结束");

}

}

2.1.4calculation.xls

2.1.5CalculationXls.java

packagecom.flagleader.bean;

importorg.apache.log4j.Logger;

importorg.drools.KnowledgeBase;

importorg.drools.builder.DecisionTableConfiguration;

importorg.drools.builder.DecisionTableInputType;

importorg.drools.builder.KnowledgeBuilder;

importorg.drools.builder.KnowledgeBuilderFactory;

importorg.drools.builder.ResourceType;

importorg.drools.io.ResourceFactory;

importorg.drools.runtime.StatefulKnowledgeSession;

@SuppressWarnings("restriction")

publicclassCalculationXls{

staticLoggerlogger=Logger.getLogger(CalculationDrl.class);

publicstaticvoidmain(String[]args){

logger.info("开始");

DecisionTableConfigurationdtc=KnowledgeBuilderFactory.newDecisionTableConfiguration();

dtc.setInputType(DecisionTableInputType.XLS);

KnowledgeBuilderbuilder=KnowledgeBuilderFactory.newKnowledgeBuilder();

builder.add(ResourceFactory.newClassPathResource("com/flagleader/bean/calculation.xls",CalculationXls.class),ResourceType.DTABLE,dtc);

KnowledgeBasebase=builder.newKnowledgeBase();

base.addKnowledgePackages(base.getKnowledgePackages());

StatefulKnowledgeSessionsession=base.newStatefulKnowledgeSession();

Calculationcal=newCalculation(10000,6500.0);

session.insert(cal);

session.fireAllRules();

session.dispose();//释放资源

logger.info("结束");

}

}

2.1.6GenerationRules.java

packagecom.flagleader.bean;

importjava.io.*;

importorg.drools.decisiontable.InputType;

importorg.drools.decisiontable.SpreadsheetCompiler;

importorg.junit.Test;

publicclassGenerationRules{

@Test

publicvoidcompile()throwsFileNotFoundException{

Filefile=newFile(

"E:

\\qzxx_workspace\\calculation\\src\\main\\java\\com\\flagleader\\bean\\calculation.xls");

InputStreamis=newFileInputStream(file);

SpreadsheetCompilerconverter=newSpreadsheetCompiler();

Stringdrl=pile(is,InputType.XLS);

System.out.println("\n\n"+drl);

}

}

2.1.7calculations.drl

packagecom.flagleader.bean

//generatedfromDecisionTable

importcom.flagleader.bean.Calculation;

importjava.lang.Object;

functiondoublegetCes(doublecess){

//System.out.println("cess="+cess);

returncess;

}

functiondoublegetPre(doublepreminus){

//System.out.println("preminus="+preminus);

returnpreminus;

}

globalCalculationcal;

rule"全月应纳税额"

no-looptrue

salience65526

when

$cal:

Calculation(wage>0)

then

System.out.println("-----全月应纳税额------");

cal.setWagemore(cal.getWage()-3500);

update(cal);

end

//rulevaluesatD11,headeratD6

rule"test_11"

salience65525

when

Calculation(wagemore>0,wagemore<=1500)

then

cal.setCess(getCes(0.03));

cal.setPreminus(getPre(0.00));

insert(cal);

end

//rulevaluesatD12,headeratD6

rule"test_12"

salience65524

when

Calculation(wagemore>1500,wagemore<=4500)

then

cal.setCess(getCes(0.1));

cal.setPreminus(getPre(105.00));

insert(cal);

end

//rulevaluesatD13,headeratD6

rule"test_13"

salience65523

when

Calculation(wagemore>4500,wagemore<=9000)

then

cal.setCess(getCes(0.2));

cal.setPreminus(getPre(555.00));

insert(cal);

end

//rulevaluesatD14,headeratD6

rule"test_14"

salience65522

when

Calculation(wagemore>9000,wagemore<=35000)

then

cal.setCess(getCes(0.25));

cal.setPreminus(getPre(1005.00));

insert(cal);

end

//rulevaluesatD15,headeratD6

rule"test_15"

salience65521

when

Calculation(wagemore>35000,wagemore<=55000)

then

cal.setCess(getCes(0.3));

cal.setPreminus(getPre(2755.00));

insert(cal);

end

//rulevaluesatD16,headeratD6

rule"test_16"

salience65520

when

Calculation(wagemore>55000,wagemore<=80000)

then

cal.setCess(getCes(0.35));

cal.setPreminus(getPre(5505.00));

insert(cal);

end

//rulevaluesatD17,headeratD6

rule"test_17"

salience65519

when

Calculation(wagemore>80000,wagemore<=100000)

then

cal.setCess(getCes(0.45));

cal.setPreminus(getPre(13505.00));

insert(cal);

end

 

rule"税后工资"

salience65518

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);

end

2.1.8CalculationDrlXls.java

packagecom.flagleader.bean;

importorg.apache.log4j.Logger;

importorg.drools.KnowledgeBase;

importorg.drools.builder.KnowledgeBuilder;

importorg.drools.builder.KnowledgeBuilderFactory;

importorg.drools.builder.ResourceType;

importorg.drools.io.ResourceFactory;

importorg.drools.runtime.StatefulKnowledgeSession;

@SuppressWarnings("restriction")

publicclassCalculationDrlXls{

staticLoggerlogger=Logger.getLogger(CalculationDrlXls.class);

publicstaticvoidmain(String[]args){

logger.info("开始");

KnowledgeBuilderkBuilder=KnowledgeBuilderFactory.newKnowledgeBuilder();//规则生成器:

将编写好的规则进行编译

kBuilder.add(ResourceFactory.newClassPathResource("calculations.drl",CalculationDrlXls.class),ResourceType.DRL);//添加规

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程科技 > 材料科学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1