From 8774482141d606fafee055ea57d545913d25d315 Mon Sep 17 00:00:00 2001 From: 75349 <753495441@qq.com> Date: Fri, 8 Jul 2022 14:42:41 +0800 Subject: [PATCH] =?UTF-8?q?1.=E6=8F=90=E4=BA=A4=E6=88=90=E6=9C=AC=E4=B8=AD?= =?UTF-8?q?=E5=BF=83=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/wezone/drools/model/CostCenter.java | 27 +++++++ src/main/resources/rules/cost-center.drl | 74 +++++++++++++++++++ .../wezone/drools/test/CostCenterTest.java | 69 +++++++++++++++++ .../wezone/drools/test/ProfitCenterTest.java | 5 +- 4 files changed, 172 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/wezone/drools/model/CostCenter.java create mode 100644 src/main/resources/rules/cost-center.drl create mode 100644 src/test/java/com/wezone/drools/test/CostCenterTest.java diff --git a/src/main/java/com/wezone/drools/model/CostCenter.java b/src/main/java/com/wezone/drools/model/CostCenter.java new file mode 100644 index 0000000..61ee4d8 --- /dev/null +++ b/src/main/java/com/wezone/drools/model/CostCenter.java @@ -0,0 +1,27 @@ +package com.wezone.drools.model; + +import lombok.Data; + +/** + * @author: 陈韦龙 + * @date: 2022年07月06日 12:08 + */ +@Data +public class CostCenter { + /** + * 业务类型 + */ + private String businessType; + /** + * 业务员 + */ + private String salesman; + /** + * 业务类别 + */ + private String businessCategory; + /** + * 科目分类 + */ + private String sectionClassification; +} diff --git a/src/main/resources/rules/cost-center.drl b/src/main/resources/rules/cost-center.drl new file mode 100644 index 0000000..7c17f9b --- /dev/null +++ b/src/main/resources/rules/cost-center.drl @@ -0,0 +1,74 @@ +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 costCenter01Code; +global java.lang.String costCenter01Name; +global java.lang.String costCenter02Code; +global java.lang.String costCenter02Name; + +/* +( + 科目分类 IN (汇差;手续费;成本科目) + AND 业务类型 IN (货代;报关;船代;散杂货 ) + AND 业务员 IN (仓干配物流部(物流)) + AND 业务类别(GHWL) IN (全货船业务;进出岛仓干配) +) +*/ +rule "costCenter01" + activation-group "costCenter" +when + $map:Map( + this['sectionClassification'] memberOf globalSectionClassificationList + && this['businessType'] memberOf globalBusinessTypeList + && this['salesman'] memberOf globalSalesmanList + && this['businessCategory'] memberOf globalBusinessCategoryList + ); +then + System.out.println("匹配成功-costCenter01"); + $map.put("code", costCenter01Code); + $map.put("name", costCenter01Name); + $map.put("generateCode", costCenter01Code + costCenter01Name); +end +/* +( + 业务类型 IN (货代;船代;散杂货) + AND 业务员 = 商品车物流部(物流) +) +OR ( + 业务类型 IN (货代;船代;散杂货) + AND 科目分类 IN (银行科目;预付科目;预收科目) +) +*/ +rule "costCenter02" + activation-group "costCenter" +when + $map:Map( + ( + this['businessType'] memberOf globalBusinessTypeList + && this['salesman'] == globalSalesman + ) + || + ( + this['businessType'] memberOf globalBusinessTypeList + + ) + ); +then + $map.put("code", costCenter02Code); + $map.put("name", costCenter02Name); + $map.put("generateCode", costCenter02Code + costCenter02Name); + System.out.println("匹配成功-costCenter02"); +end \ No newline at end of file diff --git a/src/test/java/com/wezone/drools/test/CostCenterTest.java b/src/test/java/com/wezone/drools/test/CostCenterTest.java new file mode 100644 index 0000000..dabe3c9 --- /dev/null +++ b/src/test/java/com/wezone/drools/test/CostCenterTest.java @@ -0,0 +1,69 @@ +package com.wezone.drools.test; + +import cn.hutool.core.util.ReflectUtil; +import cn.hutool.json.JSONUtil; +import com.wezone.drools.model.CostCenter; +import com.wezone.drools.model.ProfitCenter; +import org.assertj.core.util.Lists; +import org.drools.core.base.RuleNameStartsWithAgendaFilter; +import org.junit.jupiter.api.Test; +import org.kie.api.KieServices; +import org.kie.api.runtime.KieContainer; +import org.kie.api.runtime.KieSession; + +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +public class CostCenterTest { + @Test + public void testCostCenter() { + // 事实对象 + CostCenter costCenter = new CostCenter(); + costCenter.setSectionClassification("成本科目"); + costCenter.setBusinessType("货代"); + costCenter.setSalesman("仓干配物流部(物流)"); + costCenter.setBusinessCategory("全货船业务"); + + LinkedHashMap map = new LinkedHashMap<>(); + Field[] fields = ReflectUtil.getFields(CostCenter.class); + for (Field field : fields) { + Object fieldValue = ReflectUtil.getFieldValue(costCenter, field); + String name = field.getName(); + map.put(name, fieldValue == null ? null : String.valueOf(fieldValue)); + } + System.out.println(JSONUtil.toJsonStr(map)); + List sectionClassificationList = Lists.newArrayList("汇差", "手续费", "成本科目"); + List businessTypeList = Lists.newArrayList("货代", "报关", "船代", "散杂货"); + List salesmanList = Lists.newArrayList("仓干配物流部(物流)"); + List businessCategoryList = Lists.newArrayList("全货船业务", "进出岛仓干配"); + //第一步:获取服务 + KieServices kieServices = KieServices.Factory.get(); + // 第二步:通过服务获取容器 + KieContainer container = kieServices.getKieClasspathContainer(); + // 第三步:通过容器获取KieSession,由KieSession去和规则引擎交互 + KieSession kieSession = container.newKieSession(); + + kieSession.setGlobal("globalBusinessTypeList", businessTypeList); + kieSession.setGlobal("globalSalesmanList", salesmanList); + kieSession.setGlobal("globalBusinessCategoryList", businessCategoryList); + kieSession.setGlobal("globalSectionClassificationList", sectionClassificationList); + kieSession.setGlobal("globalSalesman", "商品车物流部(物流)"); + kieSession.setGlobal("costCenter01Code", "P350401028"); + kieSession.setGlobal("costCenter01Name", "仓干配物流部利润中心"); + kieSession.setGlobal("costCenter02Code", "P350401026"); + kieSession.setGlobal("costCenter02Name", "商品车物流部利润中心"); + + kieSession.insert(map); + + + // 第五步:执行规则引擎,触发规则 +// kieSession.fireAllRules(); + kieSession.fireAllRules(new RuleNameStartsWithAgendaFilter("costCenter")); + // 第六步:关闭容器 + kieSession.dispose(); + System.out.println(JSONUtil.toJsonPrettyStr(map)); + } +} diff --git a/src/test/java/com/wezone/drools/test/ProfitCenterTest.java b/src/test/java/com/wezone/drools/test/ProfitCenterTest.java index 45a569e..269e7f7 100644 --- a/src/test/java/com/wezone/drools/test/ProfitCenterTest.java +++ b/src/test/java/com/wezone/drools/test/ProfitCenterTest.java @@ -11,9 +11,8 @@ import org.kie.api.runtime.KieContainer; import org.kie.api.runtime.KieSession; import java.lang.reflect.Field; -import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; -import java.util.Map; public class ProfitCenterTest { @Test @@ -24,7 +23,7 @@ public class ProfitCenterTest { profitCenter.setSalesman("商品车物流部(物流)"); // profitCenter.setBusinessCategory("运输业务"); // profitCenter.setSectionClassification("银行科目"); - Map map = new HashMap<>(); + LinkedHashMap map = new LinkedHashMap<>(); Field[] fields = ReflectUtil.getFields(ProfitCenter.class); for (Field field : fields) { Object fieldValue = ReflectUtil.getFieldValue(profitCenter, field);