1.提交利润中心测试用例

This commit is contained in:
chenweilong 2022-07-05 18:12:19 +08:00
parent d27482f529
commit 13c8a9af82
4 changed files with 153 additions and 45 deletions

View File

@ -24,8 +24,4 @@ public class ProfitCenter {
* 科目分类 * 科目分类
*/ */
private String sectionClassification; private String sectionClassification;
/**
* 是否校验通过
*/
private boolean checkPass = false;
} }

View File

@ -0,0 +1,89 @@
package rules;
import java.util.Map;
import java.util.List;
import com.wezone.drools.model.*
// 集合
global java.util.List globalBusinessTypeList;
global java.util.List globalSalesmanList;
global java.util.List globalBusinessCategoryList;
global java.util.List globalSectionClassificationList;
// 对象
global java.lang.String globalBusinessType;
global java.lang.String globalSalesman;
global java.lang.String globalBusinessCategory;
global java.lang.String globalSectionClassification;
// 输出代码和名称
global java.lang.String profitCenter01Code;
global java.lang.String profitCenter01Name;
global java.lang.String profitCenter02Code;
global java.lang.String profitCenter02Name;
/*
(
业务类型 IN (货代;船代;散杂货)
AND 业务员 IN 仓干配物流部(物流)
AND 业务类别(GHWL) IN (运输业务;全货船业务;进出岛仓干配)
)
OR (
业务类型 IN (货代;船代;散杂货 )
AND 科目分类 IN (银行科目;预付科目;预收科目)
)
*/
rule "profitCenter01"
activation-group "profitCenter"
when
$map:Map(
(
this['businessType'] memberOf globalBusinessTypeList
&& this['salesman'] memberOf globalSalesmanList
&& this['businessCategory'] memberOf globalBusinessCategoryList
)
||
(
this['businessType'] memberOf globalBusinessTypeList
&& this['sectionClassification'] memberOf globalSectionClassificationList
)
);
then
System.out.println("匹配成功-profitCenter01");
$map.put("code", profitCenter01Code);
$map.put("name", profitCenter01Name);
end
/*
(
业务类型 IN (货代;船代;散杂货)
AND 业务员 = 商品车物流部(物流)
)
OR (
业务类型 IN (货代;船代;散杂货)
AND 科目分类 IN (银行科目;预付科目;预收科目)
)
*/
rule "profitCenter02"
activation-group "profitCenter"
when
$map:Map(
(
this['businessType'] memberOf globalBusinessTypeList
&& this['salesman'] == globalSalesman
)
||
(
this['businessType'] memberOf globalBusinessTypeList
&& this['sectionClassification'] memberOf globalSectionClassificationList
)
);
then
$map.put("code", profitCenter02Code);
$map.put("name", profitCenter02Name);
System.out.println("匹配成功-profitCenter02");
end
rule "profitCenter03"
activation-group "profitCenter"
when
eval( true )
then
System.out.println("匹配成功-profitCenter03");
end

View File

@ -4,10 +4,8 @@ import java.util.Map;
import java.util.List; import java.util.List;
import com.wezone.drools.model.* import com.wezone.drools.model.*
global java.util.List businessTypeList; global java.util.List globalList;
global java.util.List salesmanList;
global java.util.List businessCategoryList;
global java.util.List sectionClassificationList;
rule "测试map" rule "测试map"
no-loop true no-loop true
when when
@ -36,31 +34,13 @@ then
System.out.println("触发规则-测试memberOf"); System.out.println("触发规则-测试memberOf");
end end
/* rule "测试map2"
(
业务类型 IN (货代;船代;散杂货)
AND 业务员 IN 仓干配物流部(物流)
AND 业务类别(GHWL) IN (运输业务;全货船业务;进出岛仓干配)
)
OR (
业务类型 IN (货代;船代;散杂货 )
AND 科目分类 IN (银行科目;预付科目;预收科目)
)
*/
rule "profitCenter"
when when
$profitCenter:ProfitCenter $map:Map(get("businessType") memberOf globalList);
( // $str:String(getBytes() == "货代") from $map.get(["businessType"])
(
businessType memberOf businessTypeList
&& salesman memberOf salesmanList
&& businessCategory memberOf businessCategoryList
)
|| (
businessType memberOf businessTypeList
&& sectionClassification memberOf sectionClassificationList
)
)
then then
$profitCenter.setCheckPass(true); System.out.println("测试map2");
end System.out.println(globalList);
$map.put("score", "100");
// retract($map);
end

View File

@ -1,16 +1,24 @@
package com.wezone.drools.test; package com.wezone.drools.test;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.wezone.drools.model.Customer; import com.wezone.drools.model.Customer;
import com.wezone.drools.model.Order; import com.wezone.drools.model.Order;
import com.wezone.drools.model.ProfitCenter; import com.wezone.drools.model.ProfitCenter;
import org.assertj.core.util.Lists; import org.assertj.core.util.Lists;
import org.drools.core.base.RuleNameEqualsAgendaFilter; import org.drools.core.base.RuleNameEqualsAgendaFilter;
import org.drools.core.base.RuleNameStartsWithAgendaFilter;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.kie.api.KieBase;
import org.kie.api.KieServices; import org.kie.api.KieServices;
import org.kie.api.io.Resource;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieContainer; import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession; import org.kie.api.runtime.KieSession;
import org.kie.internal.io.ResourceFactory;
import org.kie.internal.utils.KieHelper;
import java.lang.reflect.Field;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -102,10 +110,17 @@ public class RuleTest {
// 事实对象 // 事实对象
ProfitCenter profitCenter = new ProfitCenter(); ProfitCenter profitCenter = new ProfitCenter();
profitCenter.setBusinessType("货代"); profitCenter.setBusinessType("货代");
profitCenter.setSalesman("仓干配物流部(物流)"); profitCenter.setSalesman("商品车物流部(物流)");
// profitCenter.setBusinessCategory("运输业务"); // profitCenter.setBusinessCategory("运输业务");
// profitCenter.setSectionClassification("银行科目"); // profitCenter.setSectionClassification("银行科目");
Map<String, String> map = new HashMap<>();
Field[] fields = ReflectUtil.getFields(ProfitCenter.class);
for (Field field : fields) {
Object fieldValue = ReflectUtil.getFieldValue(profitCenter, field);
String name = field.getName();
map.put(name, fieldValue == null ? null : String.valueOf(fieldValue));
}
System.out.println(JSONUtil.toJsonStr(map));
List<String> businessTypeList = Lists.newArrayList("货代", "船代", "散杂货"); List<String> businessTypeList = Lists.newArrayList("货代", "船代", "散杂货");
List<String> salesmanList = Lists.newArrayList("仓干配物流部(物流)"); List<String> salesmanList = Lists.newArrayList("仓干配物流部(物流)");
List<String> businessCategoryList = Lists.newArrayList("运输业务", "全货船业务", "进出岛仓干配"); List<String> businessCategoryList = Lists.newArrayList("运输业务", "全货船业务", "进出岛仓干配");
@ -116,20 +131,48 @@ public class RuleTest {
KieContainer container = kieServices.getKieClasspathContainer(); KieContainer container = kieServices.getKieClasspathContainer();
// 第三步通过容器获取KieSession由KieSession去和规则引擎交互 // 第三步通过容器获取KieSession由KieSession去和规则引擎交互
KieSession kieSession = container.newKieSession(); KieSession kieSession = container.newKieSession();
// 第四步:将数据交给规则引擎,规则引擎会根据提供的数据进行规则匹配
kieSession.insert(profitCenter); kieSession.setGlobal("globalBusinessTypeList", businessTypeList);
kieSession.setGlobal("businessTypeList", businessTypeList); kieSession.setGlobal("globalSalesmanList", salesmanList);
kieSession.setGlobal("salesmanList", salesmanList); kieSession.setGlobal("globalBusinessCategoryList", businessCategoryList);
kieSession.setGlobal("businessCategoryList", businessCategoryList); kieSession.setGlobal("globalSectionClassificationList", sectionClassificationList);
kieSession.setGlobal("sectionClassificationList", sectionClassificationList); kieSession.setGlobal("globalSalesman", "商品车物流部(物流)");
kieSession.setGlobal("profitCenter01Code", "profitCenter01Code");
kieSession.setGlobal("profitCenter01Name", "profitCenter01Name");
kieSession.setGlobal("profitCenter02Code", "profitCenter02Code");
kieSession.setGlobal("profitCenter02Name", "profitCenter02Name");
kieSession.insert(map);
// 第五步:执行规则引擎,触发规则 // 第五步:执行规则引擎,触发规则
// kieSession.fireAllRules(); // kieSession.fireAllRules();
kieSession.fireAllRules(new RuleNameEqualsAgendaFilter("profitCenter")); kieSession.fireAllRules(new RuleNameStartsWithAgendaFilter("profitCenter"));
// 第六步:关闭容器 // 第六步:关闭容器
kieSession.dispose(); kieSession.dispose();
System.out.println("校验结果" + profitCenter.isCheckPass()); System.out.println(JSONUtil.toJsonPrettyStr(map));
} }
@Test
public void testMapAndStr() {
// 事实对象
Map<String, String> map = new HashMap<>();
map.put("businessType", "货代");
// map.put("score", "0");
//第一步:获取服务
KieServices kieServices = KieServices.Factory.get();
// 第二步:通过服务获取容器
KieContainer container = kieServices.getKieClasspathContainer();
// 第三步通过容器获取KieSession由KieSession去和规则引擎交互
KieSession kieSession = container.newKieSession();
// 第四步:将数据交给规则引擎,规则引擎会根据提供的数据进行规则匹配
kieSession.setGlobal("globalList", Lists.newArrayList("货代"));
kieSession.insert(map);
// 第五步:执行规则引擎,触发规则
// kieSession.fireAllRules();
kieSession.fireAllRules(new RuleNameEqualsAgendaFilter("测试map2"));
// 第六步:关闭容器
kieSession.dispose();
System.out.println(JSONUtil.toJsonStr(map));
}
} }