Commit bbb3ad44040cb710444a086ff1bca906d49a58af

Authored by 徐烜
1 parent 96f32069

嘉定公交调度系统计划调度功能优化3

1、修正时刻表信息验证,添加时刻表冲突信息(同一天多张时刻表)和时刻表空信息(当天没有时刻表)
2、修正时刻表信息验证测试用例
src/main/java/com/bsth/service/schedule/plan/process/_1_validate/_1_timetable/PlanProcessValidateTimetableService.java
1 package com.bsth.service.schedule.plan.process._1_validate._1_timetable; 1 package com.bsth.service.schedule.plan.process._1_validate._1_timetable;
2 2
3 -import com.bsth.service.schedule.plan.process._1_validate._1_timetable.drools.KBaseValidateTimeTable_Result;  
4 - 3 +import java.util.ArrayList;
5 import java.util.Date; 4 import java.util.Date;
  5 +import java.util.List;
  6 +import java.util.Objects;
6 7
7 /** 8 /**
8 * 验证时刻表服务。 9 * 验证时刻表服务。
@@ -15,5 +16,188 @@ public interface PlanProcessValidateTimetableService { @@ -15,5 +16,188 @@ public interface PlanProcessValidateTimetableService {
15 * @param to 排班计划结束时间 16 * @param to 排班计划结束时间
16 * @return 验证输出 17 * @return 验证输出
17 */ 18 */
18 - KBaseValidateTimeTable_Result validateTTInfovalidateTTInfo(Integer xlid, Date from, Date to); 19 + Result validateTTInfovalidateTTInfo(Integer xlid, Date from, Date to);
  20 +
  21 + class Result {
  22 + /** 时刻表信息 */
  23 + private List<StatInfo> infos = new ArrayList<>();
  24 + public List<StatInfo> getInfos() {
  25 + return infos;
  26 + }
  27 +
  28 + /** 冲突信息 */
  29 + private List<ConflictStatInfo> conflictStatInfos = new ArrayList<>();
  30 + public List<ConflictStatInfo> getConflictStatInfos() {
  31 + return conflictStatInfos;
  32 + }
  33 + /** 空信息 */
  34 + private List<EmptyStatInfo> emptyStatInfos = new ArrayList<>();
  35 + public List<EmptyStatInfo> getEmptyStatInfos() {
  36 + return emptyStatInfos;
  37 + }
  38 + }
  39 +
  40 + /**
  41 + * 冲突描述信息(同一天匹配到多张时刻表)
  42 + */
  43 + class ConflictStatInfo {
  44 + /** 日期描述(yyyy年MM月dd日) */
  45 + private String dateDesc;
  46 + /** 冲突时刻表Id列表 */
  47 + private List<Long> ttIdList = new ArrayList<>();
  48 + /** 冲突时刻表名字列表 */
  49 + private List<String> ttNameList = new ArrayList<>();
  50 +
  51 + public String getDateDesc() {
  52 + return dateDesc;
  53 + }
  54 +
  55 + public void setDateDesc(String dateDesc) {
  56 + this.dateDesc = dateDesc;
  57 + }
  58 +
  59 + public List<Long> getTtIdList() {
  60 + return ttIdList;
  61 + }
  62 +
  63 + public void setTtIdList(List<Long> ttIdList) {
  64 + this.ttIdList = ttIdList;
  65 + }
  66 +
  67 + public List<String> getTtNameList() {
  68 + return ttNameList;
  69 + }
  70 +
  71 + public void setTtNameList(List<String> ttNameList) {
  72 + this.ttNameList = ttNameList;
  73 + }
  74 +
  75 + @Override
  76 + public boolean equals(Object o) {
  77 + if (this == o) return true;
  78 + if (o == null || getClass() != o.getClass()) return false;
  79 + ConflictStatInfo that = (ConflictStatInfo) o;
  80 + return Objects.equals(getDateDesc(), that.getDateDesc()) &&
  81 + Objects.equals(getTtIdList(), that.getTtIdList()) &&
  82 + Objects.equals(getTtNameList(), that.getTtNameList());
  83 + }
  84 +
  85 + @Override
  86 + public int hashCode() {
  87 +
  88 + return Objects.hash(getDateDesc(), getTtIdList(), getTtNameList());
  89 + }
  90 + }
  91 +
  92 + /**
  93 + * 空描述信息(当天没有时刻表匹配,注意:业务上允许的话不是错误)
  94 + */
  95 + class EmptyStatInfo {
  96 + /** 日期描述(yyyy年MM月dd日) */
  97 + private String dateDesc;
  98 + /** 描述(可选字段) */
  99 + private String desc;
  100 +
  101 + public String getDateDesc() {
  102 + return dateDesc;
  103 + }
  104 +
  105 + public void setDateDesc(String dateDesc) {
  106 + this.dateDesc = dateDesc;
  107 + }
  108 +
  109 + public String getDesc() {
  110 + return desc;
  111 + }
  112 +
  113 + public void setDesc(String desc) {
  114 + this.desc = desc;
  115 + }
  116 + }
  117 +
  118 + class StatInfo {
  119 + /** 时刻表id */
  120 + private Long ttid;
  121 + /** 时刻表名字 */
  122 + private String ttname;
  123 + /** 线路版本 */
  124 + private Integer lineVersion;
  125 +
  126 + /** 所有班次数 */
  127 + private Long allbc;
  128 + /** 进场班次数 */
  129 + private Long inbc;
  130 + /** 出场班次数 */
  131 + private Long outbc;
  132 + /** 营运班次数 */
  133 + private Long yybc;
  134 +
  135 + /** 错误班次数 */
  136 + private Long errorbc;
  137 +
  138 + public Long getTtid() {
  139 + return ttid;
  140 + }
  141 +
  142 + public void setTtid(Long ttid) {
  143 + this.ttid = ttid;
  144 + }
  145 +
  146 + public String getTtname() {
  147 + return ttname;
  148 + }
  149 +
  150 + public void setTtname(String ttname) {
  151 + this.ttname = ttname;
  152 + }
  153 +
  154 + public Long getAllbc() {
  155 + return allbc;
  156 + }
  157 +
  158 + public void setAllbc(Long allbc) {
  159 + this.allbc = allbc;
  160 + }
  161 +
  162 + public Long getInbc() {
  163 + return inbc;
  164 + }
  165 +
  166 + public void setInbc(Long inbc) {
  167 + this.inbc = inbc;
  168 + }
  169 +
  170 + public Long getOutbc() {
  171 + return outbc;
  172 + }
  173 +
  174 + public void setOutbc(Long outbc) {
  175 + this.outbc = outbc;
  176 + }
  177 +
  178 + public Long getYybc() {
  179 + return yybc;
  180 + }
  181 +
  182 + public void setYybc(Long yybc) {
  183 + this.yybc = yybc;
  184 + }
  185 +
  186 + public Long getErrorbc() {
  187 + return errorbc;
  188 + }
  189 +
  190 + public void setErrorbc(Long errorbc) {
  191 + this.errorbc = errorbc;
  192 + }
  193 +
  194 + public Integer getLineVersion() {
  195 + return lineVersion;
  196 + }
  197 +
  198 + public void setLineVersion(Integer lineVersion) {
  199 + this.lineVersion = lineVersion;
  200 + }
  201 +
  202 + }
19 } 203 }
src/main/java/com/bsth/service/schedule/plan/process/_1_validate/_1_timetable/PlanProcessValidateTimetableServiceDroolsImpl.java
@@ -42,7 +42,7 @@ public class PlanProcessValidateTimetableServiceDroolsImpl implements PlanProces @@ -42,7 +42,7 @@ public class PlanProcessValidateTimetableServiceDroolsImpl implements PlanProces
42 "where td.ttinfo in (:ttInfoIds)"; 42 "where td.ttinfo in (:ttInfoIds)";
43 43
44 @Override 44 @Override
45 - public KBaseValidateTimeTable_Result validateTTInfovalidateTTInfo(final Integer xlid, Date from, Date to) { 45 + public Result validateTTInfovalidateTTInfo(final Integer xlid, Date from, Date to) {
46 LOGGER.info("KieBase[{}],线路id={},开始日期={},结束日期={} 开始验证", 46 LOGGER.info("KieBase[{}],线路id={},开始日期={},结束日期={} 开始验证",
47 "前置验证相关时刻表信息", xlid, from, to); 47 "前置验证相关时刻表信息", xlid, from, to);
48 48
@@ -52,7 +52,7 @@ public class PlanProcessValidateTimetableServiceDroolsImpl implements PlanProces @@ -52,7 +52,7 @@ public class PlanProcessValidateTimetableServiceDroolsImpl implements PlanProces
52 // 设置gloable对象,在drl中通过别名使用 52 // 设置gloable对象,在drl中通过别名使用
53 session.setGlobal("LOGGER", LOGGER); 53 session.setGlobal("LOGGER", LOGGER);
54 54
55 - KBaseValidateTimeTable_Result rs = new KBaseValidateTimeTable_Result(); // 输出gloable对象 55 + Result rs = new Result(); // 输出gloable对象
56 session.setGlobal("rs", rs); 56 session.setGlobal("rs", rs);
57 57
58 // 载入数据,参数数据,线路数据,时刻表数据,时刻表明细数据 58 // 载入数据,参数数据,线路数据,时刻表数据,时刻表明细数据
src/main/java/com/bsth/service/schedule/plan/process/_1_validate/_1_timetable/drools/KBaseValidateTimeTable_Fact_Internal_WrapTTInfo.java 0 → 100644
  1 +package com.bsth.service.schedule.plan.process._1_validate._1_timetable.drools;
  2 +
  3 +import org.joda.time.DateTime;
  4 +
  5 +import java.util.List;
  6 +
  7 +/**
  8 + * 包装时刻表fact(规则内部产生)。
  9 + */
  10 +public class KBaseValidateTimeTable_Fact_Internal_WrapTTInfo {
  11 + /** 具体的日期 */
  12 + private DateTime calcuDate;
  13 + /** 时刻表列表-平日匹配 */
  14 + private List<KBaseValidateTimeTable_Fact_TTInfo> ttInfoNormalList;
  15 + /** 时刻表类标-特殊日期匹配 */
  16 + private List<KBaseValidateTimeTable_Fact_TTInfo> ttInfoSpecialList;
  17 +
  18 + public DateTime getCalcuDate() {
  19 + return calcuDate;
  20 + }
  21 +
  22 + public void setCalcuDate(DateTime calcuDate) {
  23 + this.calcuDate = calcuDate;
  24 + }
  25 +
  26 + public List<KBaseValidateTimeTable_Fact_TTInfo> getTtInfoNormalList() {
  27 + return ttInfoNormalList;
  28 + }
  29 +
  30 + public void setTtInfoNormalList(List<KBaseValidateTimeTable_Fact_TTInfo> ttInfoNormalList) {
  31 + this.ttInfoNormalList = ttInfoNormalList;
  32 + }
  33 +
  34 + public List<KBaseValidateTimeTable_Fact_TTInfo> getTtInfoSpecialList() {
  35 + return ttInfoSpecialList;
  36 + }
  37 +
  38 + public void setTtInfoSpecialList(List<KBaseValidateTimeTable_Fact_TTInfo> ttInfoSpecialList) {
  39 + this.ttInfoSpecialList = ttInfoSpecialList;
  40 + }
  41 +}
src/main/java/com/bsth/service/schedule/plan/process/_1_validate/_1_timetable/drools/KBaseValidateTimeTable_Function_AccumulateConflict.java
1 package com.bsth.service.schedule.plan.process._1_validate._1_timetable.drools; 1 package com.bsth.service.schedule.plan.process._1_validate._1_timetable.drools;
2 2
3 -import com.bsth.service.schedule.plan.process._1_validate._1_timetable.drools.KBaseValidateTimeTable_Result.StatInfo;  
4 -import org.apache.commons.lang3.StringUtils; 3 +import com.bsth.service.schedule.plan.process._1_validate._1_timetable.PlanProcessValidateTimetableService.ConflictStatInfo;
  4 +import org.joda.time.DateTime;
5 import org.kie.api.runtime.rule.AccumulateFunction; 5 import org.kie.api.runtime.rule.AccumulateFunction;
6 import org.springframework.util.CollectionUtils; 6 import org.springframework.util.CollectionUtils;
7 7
8 -import java.io.*; 8 +import java.io.IOException;
  9 +import java.io.ObjectInput;
  10 +import java.io.ObjectOutput;
  11 +import java.io.Serializable;
9 import java.util.ArrayList; 12 import java.util.ArrayList;
10 import java.util.HashMap; 13 import java.util.HashMap;
11 import java.util.List; 14 import java.util.List;
12 import java.util.Map; 15 import java.util.Map;
13 16
14 /** 17 /**
15 - * 聚合时刻表冲突函数(去除重复的时刻表,优先提取冲突的时刻表)。 18 + * 聚合时刻表冲突函数(当天多张时刻表)。
16 */ 19 */
17 public class KBaseValidateTimeTable_Function_AccumulateConflict implements AccumulateFunction { 20 public class KBaseValidateTimeTable_Function_AccumulateConflict implements AccumulateFunction {
18 @Override 21 @Override
@@ -23,61 +26,47 @@ public class KBaseValidateTimeTable_Function_AccumulateConflict implements Accum @@ -23,61 +26,47 @@ public class KBaseValidateTimeTable_Function_AccumulateConflict implements Accum
23 } 26 }
24 27
25 protected static class AccumulateConflictData implements Serializable { 28 protected static class AccumulateConflictData implements Serializable {
26 - /** 聚合后的时刻表统计信息(key为时刻表Id) */  
27 - private Map<Long, StatInfo> statInfoGroupMap;  
28 - /** 聚合后的冲突描述信息(key为时刻表Id) */  
29 - private Map<Long, List<String>> conflictDescGroupMap; 29 + /** 聚合后的时刻表冲突信息(key为日期) */
  30 + private Map<DateTime, ConflictStatInfo> conflictStatInfoGroupMap;
30 31
31 public AccumulateConflictData() { 32 public AccumulateConflictData() {
32 - statInfoGroupMap = new HashMap<>();  
33 - conflictDescGroupMap = new HashMap<>(); 33 + this.conflictStatInfoGroupMap = new HashMap<>();
34 } 34 }
35 35
36 - public void remove(Long ttInfoId) {  
37 - statInfoGroupMap.remove(ttInfoId);  
38 - conflictDescGroupMap.remove(ttInfoId); 36 + public void remove(KBaseValidateTimeTable_Fact_Internal_WrapTTInfo wrapTTInfo) {
  37 + this.conflictStatInfoGroupMap.remove(wrapTTInfo.getCalcuDate());
39 } 38 }
40 39
41 - public List<StatInfo> result() {  
42 - // 输出时,需要讲聚合后的冲突信息合并到时刻表统计信息中  
43 - List<StatInfo> rs = new ArrayList<>();  
44 - for (Long ttInfoId : statInfoGroupMap.keySet()) {  
45 - StatInfo statInfo = statInfoGroupMap.get(ttInfoId);  
46 - List<String> conflictDescList = conflictDescGroupMap.get(ttInfoId);  
47 - if (!CollectionUtils.isEmpty(conflictDescGroupMap)) {  
48 - // 使用逗号分隔  
49 - String conflictDesc = StringUtils.join(conflictDescList, ",");  
50 - statInfo.setConflictDesc(conflictDesc);  
51 - }  
52 - rs.add(statInfo); 40 + public List<ConflictStatInfo> result() {
  41 + List<ConflictStatInfo> rs = new ArrayList<>();
  42 + for (DateTime dateTime : this.conflictStatInfoGroupMap.keySet()) {
  43 + rs.add(this.conflictStatInfoGroupMap.get(dateTime));
53 } 44 }
54 return rs; 45 return rs;
55 } 46 }
56 - public void add(StatInfo statInfo) {  
57 - /**  
58 - * 聚合时刻表统计信息,  
59 - * 1、第一次直接添加时刻表统计信息  
60 - * 2、如果下一个时刻表统计信息中有冲突标识,则覆盖添加  
61 - */  
62 - Long ttInfoId = statInfo.getTtid();  
63 - boolean conflict = statInfo.getConflict();  
64 - if (statInfoGroupMap.get(ttInfoId) == null) {  
65 - statInfoGroupMap.put(ttInfoId, statInfo);  
66 - } else {  
67 - if (conflict) {  
68 - statInfoGroupMap.put(ttInfoId, statInfo); 47 + public void add(KBaseValidateTimeTable_Fact_Internal_WrapTTInfo wrapTTInfo) {
  48 + List<KBaseValidateTimeTable_Fact_TTInfo> conflictTTInfoList = new ArrayList<>();
  49 + if (!CollectionUtils.isEmpty(wrapTTInfo.getTtInfoNormalList())) {
  50 + if (wrapTTInfo.getTtInfoNormalList().size() > 1) {
  51 + conflictTTInfoList.addAll(wrapTTInfo.getTtInfoNormalList());
69 } 52 }
70 } 53 }
71 - /**  
72 - * 聚合时刻表冲突信息。  
73 - */  
74 - if (conflict) {  
75 - if (conflictDescGroupMap.get(ttInfoId) == null) {  
76 - conflictDescGroupMap.put(ttInfoId, new ArrayList<String>()); 54 + if (!CollectionUtils.isEmpty(wrapTTInfo.getTtInfoSpecialList())) {
  55 + if (wrapTTInfo.getTtInfoSpecialList().size() > 1) {
  56 + conflictTTInfoList.addAll(wrapTTInfo.getTtInfoSpecialList());
77 } 57 }
78 - conflictDescGroupMap.get(ttInfoId).add(statInfo.getConflictDesc());  
79 } 58 }
80 59
  60 + if (!CollectionUtils.isEmpty(conflictTTInfoList)) {
  61 + // 当天出现2张及以上时刻表时,定义为冲突
  62 + ConflictStatInfo conflictStatInfo = new ConflictStatInfo();
  63 + conflictStatInfo.setDateDesc(wrapTTInfo.getCalcuDate().toString("yyyy年MM月dd日"));
  64 + for (KBaseValidateTimeTable_Fact_TTInfo ttInfo : conflictTTInfoList) {
  65 + conflictStatInfo.getTtIdList().add(ttInfo.getId());
  66 + conflictStatInfo.getTtNameList().add(ttInfo.getName());
  67 + }
  68 + this.conflictStatInfoGroupMap.put(wrapTTInfo.getCalcuDate(), conflictStatInfo);
  69 + }
81 } 70 }
82 } 71 }
83 72
@@ -93,15 +82,17 @@ public class KBaseValidateTimeTable_Function_AccumulateConflict implements Accum @@ -93,15 +82,17 @@ public class KBaseValidateTimeTable_Function_AccumulateConflict implements Accum
93 @Override 82 @Override
94 public void accumulate(Serializable context, Object value) { 83 public void accumulate(Serializable context, Object value) {
95 AccumulateConflictData accumulateConflictData = (AccumulateConflictData) context; 84 AccumulateConflictData accumulateConflictData = (AccumulateConflictData) context;
96 - StatInfo statInfo = (StatInfo) value;  
97 - accumulateConflictData.add(statInfo); 85 + KBaseValidateTimeTable_Fact_Internal_WrapTTInfo wrapTTInfo =
  86 + (KBaseValidateTimeTable_Fact_Internal_WrapTTInfo) value;
  87 + accumulateConflictData.add(wrapTTInfo);
98 } 88 }
99 89
100 @Override 90 @Override
101 public void reverse(Serializable context, Object value) throws Exception { 91 public void reverse(Serializable context, Object value) throws Exception {
102 AccumulateConflictData accumulateConflictData = (AccumulateConflictData) context; 92 AccumulateConflictData accumulateConflictData = (AccumulateConflictData) context;
103 - StatInfo statInfo = (StatInfo) value;  
104 - accumulateConflictData.remove(statInfo.getTtid()); 93 + KBaseValidateTimeTable_Fact_Internal_WrapTTInfo wrapTTInfo =
  94 + (KBaseValidateTimeTable_Fact_Internal_WrapTTInfo) value;
  95 + accumulateConflictData.remove(wrapTTInfo);
105 } 96 }
106 97
107 @Override 98 @Override
src/main/java/com/bsth/service/schedule/plan/process/_1_validate/_1_timetable/drools/KBaseValidateTimeTable_Function_AccumulateEmpty.java 0 → 100644
  1 +package com.bsth.service.schedule.plan.process._1_validate._1_timetable.drools;
  2 +
  3 +import com.bsth.service.schedule.plan.process._1_validate._1_timetable.PlanProcessValidateTimetableService.EmptyStatInfo;
  4 +import org.joda.time.DateTime;
  5 +import org.kie.api.runtime.rule.AccumulateFunction;
  6 +import org.springframework.util.CollectionUtils;
  7 +
  8 +import java.io.IOException;
  9 +import java.io.ObjectInput;
  10 +import java.io.ObjectOutput;
  11 +import java.io.Serializable;
  12 +import java.util.ArrayList;
  13 +import java.util.HashMap;
  14 +import java.util.List;
  15 +import java.util.Map;
  16 +
  17 +/**
  18 + * 聚合时刻表空信息函数(当天没有时刻表)。
  19 + */
  20 +public class KBaseValidateTimeTable_Function_AccumulateEmpty implements AccumulateFunction {
  21 + @Override
  22 + public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
  23 + }
  24 + @Override
  25 + public void writeExternal(ObjectOutput out) throws IOException {
  26 + }
  27 +
  28 + protected static class AccumulateEmptyData implements Serializable {
  29 + /** 聚合后的时刻表空信息(key为日期) */
  30 + private Map<DateTime, EmptyStatInfo> emptyStatInfoGroupMap;
  31 +
  32 + public AccumulateEmptyData() {
  33 + this.emptyStatInfoGroupMap = new HashMap<>();
  34 + }
  35 +
  36 + public void remove(KBaseValidateTimeTable_Fact_Internal_WrapTTInfo wrapTTInfo) {
  37 + this.emptyStatInfoGroupMap.remove(wrapTTInfo.getCalcuDate());
  38 + }
  39 +
  40 + public List<EmptyStatInfo> result() {
  41 + List<EmptyStatInfo> rs = new ArrayList<>();
  42 + for (DateTime dateTime : this.emptyStatInfoGroupMap.keySet()) {
  43 + rs.add(this.emptyStatInfoGroupMap.get(dateTime));
  44 + }
  45 + return rs;
  46 + }
  47 +
  48 + public void add(KBaseValidateTimeTable_Fact_Internal_WrapTTInfo wrapTTInfo) {
  49 + if (CollectionUtils.isEmpty(wrapTTInfo.getTtInfoNormalList()) &&
  50 + CollectionUtils.isEmpty(wrapTTInfo.getTtInfoSpecialList())) {
  51 + // 当天没有时刻表匹配
  52 + EmptyStatInfo emptyStatInfo = new EmptyStatInfo();
  53 + emptyStatInfo.setDateDesc(wrapTTInfo.getCalcuDate().toString("yyyy年MM月dd日"));
  54 + this.emptyStatInfoGroupMap.put(wrapTTInfo.getCalcuDate(), emptyStatInfo);
  55 + }
  56 + }
  57 + }
  58 +
  59 + @Override
  60 + public Serializable createContext() {
  61 + return new AccumulateEmptyData();
  62 + }
  63 +
  64 + @Override
  65 + public void init(Serializable context) throws Exception {
  66 + }
  67 +
  68 + @Override
  69 + public void accumulate(Serializable context, Object value) {
  70 + AccumulateEmptyData accumulateEmptyData = (AccumulateEmptyData) context;
  71 + KBaseValidateTimeTable_Fact_Internal_WrapTTInfo wrapTTInfo =
  72 + (KBaseValidateTimeTable_Fact_Internal_WrapTTInfo) value;
  73 + accumulateEmptyData.add(wrapTTInfo);
  74 + }
  75 +
  76 + @Override
  77 + public void reverse(Serializable context, Object value) throws Exception {
  78 + AccumulateEmptyData accumulateEmptyData = (AccumulateEmptyData) context;
  79 + KBaseValidateTimeTable_Fact_Internal_WrapTTInfo wrapTTInfo =
  80 + (KBaseValidateTimeTable_Fact_Internal_WrapTTInfo) value;
  81 + accumulateEmptyData.remove(wrapTTInfo);
  82 + }
  83 +
  84 + @Override
  85 + public boolean supportsReverse() {
  86 + return true;
  87 + }
  88 +
  89 + @Override
  90 + public Class<?> getResultType() {
  91 + return List.class;
  92 + }
  93 +
  94 + @Override
  95 + public Object getResult(Serializable context) throws Exception {
  96 + AccumulateEmptyData accumulateEmptyData = (AccumulateEmptyData) context;
  97 + return accumulateEmptyData.result();
  98 + }
  99 +}
src/main/java/com/bsth/service/schedule/plan/process/_1_validate/_1_timetable/drools/KBaseValidateTimeTable_Function_AccumulateStatInfo.java 0 → 100644
  1 +package com.bsth.service.schedule.plan.process._1_validate._1_timetable.drools;
  2 +
  3 +import com.bsth.service.schedule.plan.process._1_validate._1_timetable.PlanProcessValidateTimetableService.StatInfo;
  4 +import org.kie.api.runtime.rule.AccumulateFunction;
  5 +import org.springframework.util.CollectionUtils;
  6 +
  7 +import java.io.IOException;
  8 +import java.io.ObjectInput;
  9 +import java.io.ObjectOutput;
  10 +import java.io.Serializable;
  11 +import java.util.ArrayList;
  12 +import java.util.HashMap;
  13 +import java.util.List;
  14 +import java.util.Map;
  15 +
  16 +/**
  17 + * 聚合时刻表(去除重复的时刻表)。
  18 + */
  19 +public class KBaseValidateTimeTable_Function_AccumulateStatInfo implements AccumulateFunction {
  20 + @Override
  21 + public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
  22 + }
  23 + @Override
  24 + public void writeExternal(ObjectOutput out) throws IOException {
  25 + }
  26 +
  27 + protected static class AccumulateStatInfoData implements Serializable {
  28 + /** 聚合后的时刻表统计信息(key为时刻表Id) */
  29 + private Map<Long, StatInfo> statInfoGroupMap;
  30 +
  31 + public AccumulateStatInfoData() {
  32 + this.statInfoGroupMap = new HashMap<>();
  33 + }
  34 +
  35 + public void remove(KBaseValidateTimeTable_Fact_Internal_WrapTTInfo wrapTTInfo) {
  36 + if (!CollectionUtils.isEmpty(wrapTTInfo.getTtInfoNormalList())) {
  37 + for (KBaseValidateTimeTable_Fact_TTInfo ttInfo : wrapTTInfo.getTtInfoNormalList()) {
  38 + this.statInfoGroupMap.remove(ttInfo.getId());
  39 + }
  40 + }
  41 + if (!CollectionUtils.isEmpty(wrapTTInfo.getTtInfoSpecialList())) {
  42 + for (KBaseValidateTimeTable_Fact_TTInfo ttInfo : wrapTTInfo.getTtInfoSpecialList()) {
  43 + this.statInfoGroupMap.remove(ttInfo.getId());
  44 + }
  45 + }
  46 + }
  47 +
  48 + public List<StatInfo> result() {
  49 + // 输出时,需要讲聚合后的冲突信息合并到时刻表统计信息中
  50 + List<StatInfo> rs = new ArrayList<>();
  51 + for (Long ttInfoId : statInfoGroupMap.keySet()) {
  52 + StatInfo statInfo = statInfoGroupMap.get(ttInfoId);
  53 + rs.add(statInfo);
  54 + }
  55 + return rs;
  56 + }
  57 + public void add(KBaseValidateTimeTable_Fact_Internal_WrapTTInfo wrapTTInfo) {
  58 + List<KBaseValidateTimeTable_Fact_TTInfo> allTTInfo = new ArrayList<>();
  59 + if (!CollectionUtils.isEmpty(wrapTTInfo.getTtInfoNormalList())) {
  60 + allTTInfo.addAll(wrapTTInfo.getTtInfoNormalList());
  61 + }
  62 + if (!CollectionUtils.isEmpty(wrapTTInfo.getTtInfoSpecialList())) {
  63 + allTTInfo.addAll(wrapTTInfo.getTtInfoSpecialList());
  64 + }
  65 + for (KBaseValidateTimeTable_Fact_TTInfo ttInfo : allTTInfo) {
  66 + if (this.statInfoGroupMap.get(ttInfo.getId()) == null) {
  67 + StatInfo statInfo = new StatInfo();
  68 + statInfo.setTtid(ttInfo.getId());
  69 + statInfo.setTtname(ttInfo.getName());
  70 + statInfo.setLineVersion(ttInfo.getLineVersion());
  71 + this.statInfoGroupMap.put(ttInfo.getId(), statInfo);
  72 + }
  73 + }
  74 +
  75 + }
  76 + }
  77 +
  78 + @Override
  79 + public Serializable createContext() {
  80 + return new AccumulateStatInfoData();
  81 + }
  82 +
  83 + @Override
  84 + public void init(Serializable context) throws Exception {
  85 + }
  86 +
  87 + @Override
  88 + public void accumulate(Serializable context, Object value) {
  89 + AccumulateStatInfoData accumulateConflictData = (AccumulateStatInfoData) context;
  90 + KBaseValidateTimeTable_Fact_Internal_WrapTTInfo wrapTTInfo =
  91 + (KBaseValidateTimeTable_Fact_Internal_WrapTTInfo) value;
  92 + accumulateConflictData.add(wrapTTInfo);
  93 + }
  94 +
  95 + @Override
  96 + public void reverse(Serializable context, Object value) throws Exception {
  97 + AccumulateStatInfoData accumulateConflictData = (AccumulateStatInfoData) context;
  98 + KBaseValidateTimeTable_Fact_Internal_WrapTTInfo wrapTTInfo =
  99 + (KBaseValidateTimeTable_Fact_Internal_WrapTTInfo) value;
  100 + accumulateConflictData.remove(wrapTTInfo);
  101 + }
  102 +
  103 + @Override
  104 + public boolean supportsReverse() {
  105 + return true;
  106 + }
  107 +
  108 + @Override
  109 + public Class<?> getResultType() {
  110 + return List.class;
  111 + }
  112 +
  113 + @Override
  114 + public Object getResult(Serializable context) throws Exception {
  115 + AccumulateStatInfoData accumulateConflictData = (AccumulateStatInfoData) context;
  116 + return accumulateConflictData.result();
  117 + }
  118 +}
src/main/java/com/bsth/service/schedule/plan/process/_1_validate/_1_timetable/drools/KBaseValidateTimeTable_Result.java deleted 100644 → 0
1 -package com.bsth.service.schedule.plan.process._1_validate._1_timetable.drools;  
2 -  
3 -import java.util.ArrayList;  
4 -import java.util.List;  
5 -  
6 -/**  
7 - * drools规则验证时刻表输出结果。  
8 - */  
9 -public class KBaseValidateTimeTable_Result {  
10 - private List<StatInfo> infos = new ArrayList<>();  
11 -  
12 - public List<StatInfo> getInfos() {  
13 - return infos;  
14 - }  
15 -  
16 - public void setInfos(List<StatInfo> infos) {  
17 - this.infos = infos;  
18 - }  
19 -  
20 - public static class StatInfo {  
21 - /** 时刻表id */  
22 - private Long ttid;  
23 - /** 时刻表名字 */  
24 - private String ttname;  
25 - /** 线路版本 */  
26 - private Integer lineVersion;  
27 -  
28 - /** 所有班次数 */  
29 - private Long allbc;  
30 - /** 进场班次数 */  
31 - private Long inbc;  
32 - /** 出场班次数 */  
33 - private Long outbc;  
34 - /** 营运班次数 */  
35 - private Long yybc;  
36 -  
37 - /** 错误班次数 */  
38 - private Long errorbc;  
39 -  
40 - /** 是否冲突(同一天多张时刻表) */  
41 - private Boolean conflict;  
42 - /** 冲突描述(在drools规则中写入) */  
43 - private String conflictDesc;  
44 - /** 是否进行了冲突聚合计算(针对drools规则的聚合函数) */  
45 - private Boolean conflictAccumulate;  
46 -  
47 - public Long getTtid() {  
48 - return ttid;  
49 - }  
50 -  
51 - public void setTtid(Long ttid) {  
52 - this.ttid = ttid;  
53 - }  
54 -  
55 - public String getTtname() {  
56 - return ttname;  
57 - }  
58 -  
59 - public void setTtname(String ttname) {  
60 - this.ttname = ttname;  
61 - }  
62 -  
63 - public Long getAllbc() {  
64 - return allbc;  
65 - }  
66 -  
67 - public void setAllbc(Long allbc) {  
68 - this.allbc = allbc;  
69 - }  
70 -  
71 - public Long getInbc() {  
72 - return inbc;  
73 - }  
74 -  
75 - public void setInbc(Long inbc) {  
76 - this.inbc = inbc;  
77 - }  
78 -  
79 - public Long getOutbc() {  
80 - return outbc;  
81 - }  
82 -  
83 - public void setOutbc(Long outbc) {  
84 - this.outbc = outbc;  
85 - }  
86 -  
87 - public Long getYybc() {  
88 - return yybc;  
89 - }  
90 -  
91 - public void setYybc(Long yybc) {  
92 - this.yybc = yybc;  
93 - }  
94 -  
95 - public Long getErrorbc() {  
96 - return errorbc;  
97 - }  
98 -  
99 - public void setErrorbc(Long errorbc) {  
100 - this.errorbc = errorbc;  
101 - }  
102 -  
103 - public Integer getLineVersion() {  
104 - return lineVersion;  
105 - }  
106 -  
107 - public void setLineVersion(Integer lineVersion) {  
108 - this.lineVersion = lineVersion;  
109 - }  
110 -  
111 - public Boolean getConflict() {  
112 - return conflict;  
113 - }  
114 -  
115 - public void setConflict(Boolean conflict) {  
116 - this.conflict = conflict;  
117 - }  
118 -  
119 - public String getConflictDesc() {  
120 - return conflictDesc;  
121 - }  
122 -  
123 - public void setConflictDesc(String conflictDesc) {  
124 - this.conflictDesc = conflictDesc;  
125 - }  
126 -  
127 - public Boolean getConflictAccumulate() {  
128 - return conflictAccumulate;  
129 - }  
130 -  
131 - public void setConflictAccumulate(Boolean conflictAccumulate) {  
132 - this.conflictAccumulate = conflictAccumulate;  
133 - }  
134 - }  
135 -}  
src/main/resources/drools/KBase/validate/timetable/KBase_validate_timetable.drl
@@ -6,20 +6,23 @@ import java.util.*; @@ -6,20 +6,23 @@ import java.util.*;
6 import org.apache.commons.lang3.StringUtils; 6 import org.apache.commons.lang3.StringUtils;
7 7
8 import com.bsth.service.schedule.plan.process._1_validate._1_timetable.drools.KBaseValidateTimeTable_Fact_CalcuParam; 8 import com.bsth.service.schedule.plan.process._1_validate._1_timetable.drools.KBaseValidateTimeTable_Fact_CalcuParam;
9 -import com.bsth.service.schedule.plan.process._1_validate._1_timetable.drools.KBaseValidateTimeTable_Result;  
10 -import com.bsth.service.schedule.plan.process._1_validate._1_timetable.drools.KBaseValidateTimeTable_Result.StatInfo;  
11 9
12 import com.bsth.service.schedule.plan.process._1_validate._1_timetable.drools.KBaseValidateTimeTable_Fact_Line; 10 import com.bsth.service.schedule.plan.process._1_validate._1_timetable.drools.KBaseValidateTimeTable_Fact_Line;
13 import com.bsth.service.schedule.plan.process._1_validate._1_timetable.drools.KBaseValidateTimeTable_Fact_TTInfo; 11 import com.bsth.service.schedule.plan.process._1_validate._1_timetable.drools.KBaseValidateTimeTable_Fact_TTInfo;
14 import com.bsth.service.schedule.plan.process._1_validate._1_timetable.drools.KBaseValidateTimeTable_Fact_TTInfoDetail; 12 import com.bsth.service.schedule.plan.process._1_validate._1_timetable.drools.KBaseValidateTimeTable_Fact_TTInfoDetail;
15 13
  14 +import com.bsth.service.schedule.plan.process._1_validate._1_timetable.drools.KBaseValidateTimeTable_Fact_Internal_WrapTTInfo;
  15 +
  16 +import com.bsth.service.schedule.plan.process._1_validate._1_timetable.PlanProcessValidateTimetableService.Result;
  17 +import com.bsth.service.schedule.plan.process._1_validate._1_timetable.PlanProcessValidateTimetableService.StatInfo;
  18 +
16 import org.slf4j.Logger; 19 import org.slf4j.Logger;
17 20
18 // 全局日志类(使用PlanPreValidateServiceImpl里定义的LOGGER) 21 // 全局日志类(使用PlanPreValidateServiceImpl里定义的LOGGER)
19 global Logger LOGGER; 22 global Logger LOGGER;
20 23
21 // 输出 24 // 输出
22 -global KBaseValidateTimeTable_Result rs; 25 +global Result rs;
23 26
24 /* 27 /*
25 规则说明: 28 规则说明:
@@ -88,12 +91,6 @@ rule &quot;calcu_iter_days&quot; @@ -88,12 +91,6 @@ rule &quot;calcu_iter_days&quot;
88 end 91 end
89 92
90 //-------------- 第二阶段、时刻表的日期匹配 ------------// 93 //-------------- 第二阶段、时刻表的日期匹配 ------------//
91 -declare TTInfoList_wrap // 时刻表封装对象  
92 - calcu_date: DateTime // 具体的日期  
93 - ttInfoNormalList: List // 时刻表列表-平日匹配 List<KBaseValidateTimeTable_Fact_TTInfo>  
94 - ttInfoSpecialList: List // 时刻表类标-特殊日期匹配 List<KBaseValidateTimeTable_Fact_TTInfo>  
95 -end  
96 -  
97 rule "Calcu_iter_days_special_day" // 日期匹配 94 rule "Calcu_iter_days_special_day" // 日期匹配
98 salience 800 95 salience 800
99 when 96 when
@@ -103,24 +100,22 @@ rule &quot;Calcu_iter_days_special_day&quot; // 日期匹配 @@ -103,24 +100,22 @@ rule &quot;Calcu_iter_days_special_day&quot; // 日期匹配
103 $calcu_day: calcu_day, 100 $calcu_day: calcu_day,
104 calcu_day <= calcu_days 101 calcu_day <= calcu_days
105 ) 102 )
106 - $ttInfoSpecialList : ArrayList() from collect ( 103 + $ttInfoSpecialList : ArrayList($ss: size) from collect (
107 KBaseValidateTimeTable_Fact_TTInfo( 104 KBaseValidateTimeTable_Fact_TTInfo(
108 specialDayList contains $calcu_date 105 specialDayList contains $calcu_date
109 ) 106 )
110 ) 107 )
111 $ttInfoNormalList : ArrayList() from collect ( 108 $ttInfoNormalList : ArrayList() from collect (
112 KBaseValidateTimeTable_Fact_TTInfo( 109 KBaseValidateTimeTable_Fact_TTInfo(
  110 + $ss == 0, // 注意:条件=没有特殊日期被匹配到
113 specialDayList not contains $calcu_date, 111 specialDayList not contains $calcu_date,
114 weekdayList[$calcu_weekday - 1] == Boolean.TRUE 112 weekdayList[$calcu_weekday - 1] == Boolean.TRUE
115 ) 113 )
116 ) 114 )
117 then 115 then
118 - LOGGER.debug("日期={}, 一般日期匹配时刻表数量={}, 特殊日期时刻表匹配数量={}",  
119 - $calcu_date, $ttInfoNormalList.size(), $ttInfoSpecialList.size());  
120 -  
121 // 插入时刻表封装对象 116 // 插入时刻表封装对象
122 - TTInfoList_wrap fact = new TTInfoList_wrap();  
123 - fact.setCalcu_date(new DateTime($calcu_date)); 117 + KBaseValidateTimeTable_Fact_Internal_WrapTTInfo fact = new KBaseValidateTimeTable_Fact_Internal_WrapTTInfo();
  118 + fact.setCalcuDate(new DateTime($calcu_date));
124 // 注意:如果要赋值List,必须重新建ArrayList,直接用drools的变量有问题 119 // 注意:如果要赋值List,必须重新建ArrayList,直接用drools的变量有问题
125 List ttInfoNormalList = new ArrayList(); 120 List ttInfoNormalList = new ArrayList();
126 ttInfoNormalList.addAll($ttInfoNormalList); 121 ttInfoNormalList.addAll($ttInfoNormalList);
@@ -132,6 +127,9 @@ rule &quot;Calcu_iter_days_special_day&quot; // 日期匹配 @@ -132,6 +127,9 @@ rule &quot;Calcu_iter_days_special_day&quot; // 日期匹配
132 127
133 insert(fact); 128 insert(fact);
134 129
  130 + LOGGER.debug("日期={}, 一般日期匹配时刻表数量={}, 特殊日期时刻表匹配数量={}",
  131 + $calcu_date, ttInfoNormalList.size(), ttInfoSpecialList.size());
  132 +
135 // 更新迭代对象 133 // 更新迭代对象
136 Integer new_calcu_day = Integer.valueOf($calcu_day + 1); 134 Integer new_calcu_day = Integer.valueOf($calcu_day + 1);
137 $cid.setCalcu_day(new_calcu_day); 135 $cid.setCalcu_day(new_calcu_day);
@@ -142,136 +140,26 @@ rule &quot;Calcu_iter_days_special_day&quot; // 日期匹配 @@ -142,136 +140,26 @@ rule &quot;Calcu_iter_days_special_day&quot; // 日期匹配
142 update($cid); 140 update($cid);
143 end 141 end
144 142
145 -//----------------- 第三阶段、组装待处理时刻表统计信息 ----------------//  
146 -rule "Calcu_StatInfo_wrap_special_not_conflict" // 当天有特殊日期匹配的时刻表,只有一张 143 +//----------------- 第三阶段、聚合统计时刻表信息 ----------------//
  144 +rule "group_statinfo_accumulate"
147 salience 750 145 salience 750
148 when 146 when
149 - TTInfoList_wrap(  
150 - ttInfoSpecialList.size == 1,  
151 - $calcu_date: calcu_date,  
152 - $ttInfoSpecialList: ttInfoSpecialList  
153 - )  
154 - $ttInfo: KBaseValidateTimeTable_Fact_TTInfo($tid: id, $tname: name, $lineVersion: lineVersion) from $ttInfoSpecialList  
155 - then  
156 - LOGGER.debug("特殊日期匹配,同一天单一时刻表:" +  
157 - "时刻表id={} 日期={}",  
158 - $tid, $calcu_date);  
159 -  
160 - StatInfo statInfo = new StatInfo();  
161 - statInfo.setTtid($tid);  
162 - statInfo.setTtname($tname);  
163 - statInfo.setLineVersion($lineVersion);  
164 - statInfo.setConflict(false);  
165 - statInfo.setConflictAccumulate(false);  
166 - insert(statInfo);  
167 -end  
168 -  
169 -rule "Calcu_StatInfo_wrap_special_conflict" // 当天有特殊日期匹配的时刻表, 多张时刻表  
170 - salience 740  
171 - when  
172 - TTInfoList_wrap(  
173 - ttInfoSpecialList.size > 1,  
174 - $calcu_date: calcu_date,  
175 - $ttInfoSpecialList: ttInfoSpecialList  
176 - )  
177 - $ttInfo: KBaseValidateTimeTable_Fact_TTInfo($tid: id, $tname: name, $lineVersion: lineVersion) from $ttInfoSpecialList  
178 - then  
179 - LOGGER.debug("特殊日期匹配,同一天多张时刻表:" +  
180 - "时刻表id={} 日期={}",  
181 - $tid, $calcu_date);  
182 -  
183 - StatInfo statInfo = new StatInfo();  
184 - statInfo.setTtid($tid);  
185 - statInfo.setTtname($tname);  
186 - statInfo.setLineVersion($lineVersion);  
187 - statInfo.setConflict(true);  
188 - statInfo.setConflictDesc("特殊日期冲突_" + $calcu_date.toString("yyyy-MM-dd"));  
189 - statInfo.setConflictAccumulate(false);  
190 - insert(statInfo);  
191 -end  
192 -  
193 -rule "Calcu_StatInfo_wrap_normal_not_conflict" // 当天一般日期匹配的时刻表,只有一张  
194 - salience 730  
195 - when  
196 - TTInfoList_wrap(  
197 - ttInfoSpecialList.size < 1,  
198 - ttInfoNormalList.size == 1,  
199 - $calcu_date: calcu_date,  
200 - $ttInfoNormalList: ttInfoNormalList  
201 - )  
202 - $ttInfo: KBaseValidateTimeTable_Fact_TTInfo($tid: id, $tname: name, $lineVersion: lineVersion) from $ttInfoNormalList  
203 - then  
204 - LOGGER.debug("一般日期匹配,同一天单一时刻表:" +  
205 - "时刻表id={} 日期={}",  
206 - $tid, $calcu_date);  
207 -  
208 - StatInfo statInfo = new StatInfo();  
209 - statInfo.setTtid($tid);  
210 - statInfo.setTtname($tname);  
211 - statInfo.setLineVersion($lineVersion);  
212 - statInfo.setConflict(false);  
213 - statInfo.setConflictAccumulate(false);  
214 - insert(statInfo);  
215 -end  
216 -  
217 -rule "Calcu_StatInfo_wrap_normal_conflict" // 当天一般日期匹配的时刻表,多张时刻表  
218 - salience 730  
219 - when  
220 - TTInfoList_wrap(  
221 - ttInfoSpecialList.size < 1,  
222 - ttInfoNormalList.size > 1,  
223 - $calcu_date: calcu_date,  
224 - $ttInfoNormalList: ttInfoNormalList  
225 - )  
226 - $ttInfo: KBaseValidateTimeTable_Fact_TTInfo($tid: id, $tname: name, $lineVersion: lineVersion) from $ttInfoNormalList  
227 - then  
228 - LOGGER.debug("一般日期匹配,同一天多张时刻表:" +  
229 - "时刻表id={} 日期={}",  
230 - $tid, $calcu_date);  
231 -  
232 - StatInfo statInfo = new StatInfo();  
233 - statInfo.setTtid($tid);  
234 - statInfo.setTtname($tname);  
235 - statInfo.setLineVersion($lineVersion);  
236 - statInfo.setConflict(true);  
237 - statInfo.setConflictDesc("一般日期冲突_" + $calcu_date.toString("yyyy-MM-dd"));  
238 - statInfo.setConflictAccumulate(false);  
239 - insert(statInfo);  
240 -end  
241 -  
242 -//-------------- 第四阶段、时刻表聚合统计 ------------//  
243 -rule "statinfo_accumulate_conflict" // 聚合统计  
244 - salience 300  
245 - no-loop  
246 - when  
247 - $accList: List() from accumulate (  
248 - $statInfo: StatInfo(conflictAccumulate == false),  
249 - ac($statInfo)  
250 - )  
251 - StatInfo(  
252 - $tid: ttid,  
253 - $tname: ttname,  
254 - $lineVersion: lineVersion,  
255 - $conflict: conflict,  
256 - $conflictDesc: ConflictDesc) from $accList 147 + $conflictInfoList: List() from accumulate ($ttd: KBaseValidateTimeTable_Fact_Internal_WrapTTInfo(), ac($ttd))
  148 + $emptyInfoList: List() from accumulate ($ttd: KBaseValidateTimeTable_Fact_Internal_WrapTTInfo(), ae($ttd))
257 then 149 then
258 - LOGGER.info("ttid={}, conflictDesc={}", $tid, $conflictDesc); 150 + LOGGER.debug("冲突匹配时刻表信息={}", $conflictInfoList.size()); // 当天多张时刻表
  151 + LOGGER.debug("空匹配时刻表信息={}", $emptyInfoList.size()); // 当天没有时刻表
259 152
260 - StatInfo statInfo = new StatInfo();  
261 - statInfo.setTtid($tid);  
262 - statInfo.setTtname($tname);  
263 - statInfo.setLineVersion($lineVersion);  
264 - statInfo.setConflict($conflict);  
265 - statInfo.setConflictDesc($conflictDesc);  
266 - statInfo.setConflictAccumulate(true);  
267 - insert(statInfo); 153 + rs.getConflictStatInfos().addAll($conflictInfoList);
  154 + rs.getEmptyStatInfos().addAll($emptyInfoList);
268 end 155 end
269 156
270 -rule "statinfo_result" // 统计计算结果  
271 - salience 300 157 +rule "group_statinfo"
  158 + salience 700
272 no-loop 159 no-loop
273 when 160 when
274 - $statInfo: StatInfo(conflictAccumulate == true, $tid: ttid, $lineVersion: lineVersion, $conflict: conflict, $conflictDesc: conflictDesc) 161 + $statInfoList: List() from accumulate ($ttd: KBaseValidateTimeTable_Fact_Internal_WrapTTInfo(), as($ttd))
  162 + $statInfo : StatInfo($tid: ttid, $lineVersion: lineVersion) from $statInfoList
275 $bcInfoList : ArrayList() from collect (KBaseValidateTimeTable_Fact_TTInfoDetail(ttinfo.id == $tid)) 163 $bcInfoList : ArrayList() from collect (KBaseValidateTimeTable_Fact_TTInfoDetail(ttinfo.id == $tid))
276 $allbc: Long() from accumulate (KBaseValidateTimeTable_Fact_TTInfoDetail() from $bcInfoList, count()) 164 $allbc: Long() from accumulate (KBaseValidateTimeTable_Fact_TTInfoDetail() from $bcInfoList, count())
277 $inbc: Long() from accumulate (KBaseValidateTimeTable_Fact_TTInfoDetail(bcType == "in") from $bcInfoList, count()) 165 $inbc: Long() from accumulate (KBaseValidateTimeTable_Fact_TTInfoDetail(bcType == "in") from $bcInfoList, count())
@@ -279,8 +167,8 @@ rule &quot;statinfo_result&quot; // 统计计算结果 @@ -279,8 +167,8 @@ rule &quot;statinfo_result&quot; // 统计计算结果
279 $yybc: Long() from accumulate (KBaseValidateTimeTable_Fact_TTInfoDetail(bcType != "out", bcType != "in") from $bcInfoList, count()) 167 $yybc: Long() from accumulate (KBaseValidateTimeTable_Fact_TTInfoDetail(bcType != "out", bcType != "in") from $bcInfoList, count())
280 $errorbc: Long() from accumulate ($ttd: KBaseValidateTimeTable_Fact_TTInfoDetail() from $bcInfoList, ecount($ttd)) 168 $errorbc: Long() from accumulate ($ttd: KBaseValidateTimeTable_Fact_TTInfoDetail() from $bcInfoList, ecount($ttd))
281 then 169 then
282 - LOGGER.info("时刻表={},id={},线路版本={},班次数={},进场={},出场={},营运={},错误={},是否冲突={},冲突描述={}",  
283 - $statInfo.getTtname(), $statInfo.getTtid(), $lineVersion, $allbc, $inbc, $outbc, $yybc, $errorbc, $conflict, $conflictDesc); 170 + LOGGER.info("时刻表={},id={},线路版本={},班次数={},进场={},出场={},营运={},错误={}",
  171 + $statInfo.getTtname(), $statInfo.getTtid(), $lineVersion, $allbc, $inbc, $outbc, $yybc, $errorbc);
284 172
285 $statInfo.setAllbc($allbc); 173 $statInfo.setAllbc($allbc);
286 $statInfo.setInbc($inbc); 174 $statInfo.setInbc($inbc);
@@ -290,18 +178,3 @@ rule &quot;statinfo_result&quot; // 统计计算结果 @@ -290,18 +178,3 @@ rule &quot;statinfo_result&quot; // 统计计算结果
290 178
291 rs.getInfos().add($statInfo); 179 rs.getInfos().add($statInfo);
292 end 180 end
293 -  
294 -  
295 -/*  
296 -rule "test"  
297 - when  
298 - $test: TTInfoList_wrap(  
299 - $calcu_date: calcu_date,  
300 - $ttInfoNormalList: ttInfoNormalList,  
301 - $ttInfoSpecialList: ttInfoSpecialList  
302 - )  
303 - then  
304 - LOGGER.debug("日期={}, nsize={}, ssize={}", $calcu_date, $ttInfoNormalList.size(), $ttInfoSpecialList.size());  
305 -  
306 -end  
307 -*/  
src/test/java/com/bsth/service/schedule/PlanValidateTest.java
@@ -2,10 +2,8 @@ package com.bsth.service.schedule; @@ -2,10 +2,8 @@ package com.bsth.service.schedule;
2 2
3 import com.bsth.BaseTest_junit4; 3 import com.bsth.BaseTest_junit4;
4 import com.bsth.TestApplication; 4 import com.bsth.TestApplication;
5 -import com.bsth.control_v2.plan_module.common.service.schedule.validate.ValidateRuleResult;  
6 import com.bsth.service.schedule.plan.process._1_validate._1_timetable.PlanProcessValidateTimetableService; 5 import com.bsth.service.schedule.plan.process._1_validate._1_timetable.PlanProcessValidateTimetableService;
7 import com.bsth.service.schedule.plan.process._1_validate._1_timetable.PlanProcessValidateTimetableServiceDroolsImpl; 6 import com.bsth.service.schedule.plan.process._1_validate._1_timetable.PlanProcessValidateTimetableServiceDroolsImpl;
8 -import com.bsth.service.schedule.plan.process._1_validate._1_timetable.drools.KBaseValidateTimeTable_Result;  
9 import com.bsth.service.schedule.plan.process._1_validate._2_rule.PlanProcessValidateRuleService; 7 import com.bsth.service.schedule.plan.process._1_validate._2_rule.PlanProcessValidateRuleService;
10 import com.bsth.service.schedule.plan.process._1_validate._2_rule.PlanProcessValidateRuleServiceDroolsImpl; 8 import com.bsth.service.schedule.plan.process._1_validate._2_rule.PlanProcessValidateRuleServiceDroolsImpl;
11 import org.joda.time.format.DateTimeFormat; 9 import org.joda.time.format.DateTimeFormat;
@@ -19,8 +17,7 @@ import org.springframework.boot.test.SpringApplicationConfiguration; @@ -19,8 +17,7 @@ import org.springframework.boot.test.SpringApplicationConfiguration;
19 import org.springframework.jdbc.core.JdbcTemplate; 17 import org.springframework.jdbc.core.JdbcTemplate;
20 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 18 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
21 19
22 -import java.util.HashMap;  
23 -import java.util.Map; 20 +import java.util.*;
24 21
25 /** 22 /**
26 * 排班计划,前置验证测试。 23 * 排班计划,前置验证测试。
@@ -61,19 +58,125 @@ public class PlanValidateTest extends BaseTest_junit4 { @@ -61,19 +58,125 @@ public class PlanValidateTest extends BaseTest_junit4 {
61 58
62 /* 59 /*
63 验证时刻表信息服务: 60 验证时刻表信息服务:
64 - 计算指定时间范围内所有的匹配时刻表,并计算时刻表的详细信息  
65 - 61 + 1、计算指定时间内匹配的所有时刻表的详细信息
  62 + 2、计算时刻表冲突信息(同一天多张时刻表)
  63 + 3、计算时刻表空信息(当天没有时刻表)
66 */ 64 */
67 - KBaseValidateTimeTable_Result result = planProcessValidateTimetableService.validateTTInfovalidateTTInfo( 65 + PlanProcessValidateTimetableService.Result result = planProcessValidateTimetableService.validateTTInfovalidateTTInfo(
68 1, 66 1,
69 DateTimeFormat.forPattern("yyyy-MM-dd").parseDateTime("2019-01-01").toDate(), 67 DateTimeFormat.forPattern("yyyy-MM-dd").parseDateTime("2019-01-01").toDate(),
70 DateTimeFormat.forPattern("yyyy-MM-dd").parseDateTime("2019-01-14").toDate() 68 DateTimeFormat.forPattern("yyyy-MM-dd").parseDateTime("2019-01-14").toDate()
71 ); 69 );
72 70
  71 + //--------------- 测试1 ----------------//
73 Assert.assertArrayEquals( 72 Assert.assertArrayEquals(
74 "时刻表数量不一致", 73 "时刻表数量不一致",
75 new Object[] {4}, 74 new Object[] {4},
76 new Object[] {result.getInfos().size()}); 75 new Object[] {result.getInfos().size()});
  76 +
  77 + //--------------- 测试2 ----------------//
  78 + PlanProcessValidateTimetableService.ConflictStatInfo expectConflict1 = new PlanProcessValidateTimetableService.ConflictStatInfo();
  79 + expectConflict1.setDateDesc("2019年01月04日");
  80 + expectConflict1.getTtIdList().addAll(Arrays.asList(1L, 2L));
  81 + expectConflict1.getTtNameList().addAll(Arrays.asList("时刻表1", "时刻表2"));
  82 + PlanProcessValidateTimetableService.ConflictStatInfo expectConflict2 = new PlanProcessValidateTimetableService.ConflictStatInfo();
  83 + expectConflict2.setDateDesc("2019年01月05日");
  84 + expectConflict2.getTtIdList().addAll(Arrays.asList(3L, 4L));
  85 + expectConflict2.getTtNameList().addAll(Arrays.asList("时刻表3", "时刻表4"));
  86 + PlanProcessValidateTimetableService.ConflictStatInfo expectConflict3 = new PlanProcessValidateTimetableService.ConflictStatInfo();
  87 + expectConflict3.setDateDesc("2019年01月06日");
  88 + expectConflict3.getTtIdList().addAll(Arrays.asList(3L, 4L));
  89 + expectConflict3.getTtNameList().addAll(Arrays.asList("时刻表3", "时刻表4"));
  90 + PlanProcessValidateTimetableService.ConflictStatInfo expectConflict4 = new PlanProcessValidateTimetableService.ConflictStatInfo();
  91 + expectConflict4.setDateDesc("2019年01月07日");
  92 + expectConflict4.getTtIdList().addAll(Arrays.asList(3L, 4L));
  93 + expectConflict4.getTtNameList().addAll(Arrays.asList("时刻表3", "时刻表4"));
  94 + PlanProcessValidateTimetableService.ConflictStatInfo expectConflict5 = new PlanProcessValidateTimetableService.ConflictStatInfo();
  95 + expectConflict5.setDateDesc("2019年01月11日");
  96 + expectConflict5.getTtIdList().addAll(Arrays.asList(1L, 2L));
  97 + expectConflict5.getTtNameList().addAll(Arrays.asList("时刻表1", "时刻表2"));
  98 +
  99 + // 注意:排序是为了测试需要
  100 + List<PlanProcessValidateTimetableService.ConflictStatInfo> expectConflictList = new ArrayList<>();
  101 + expectConflictList.add(expectConflict1);
  102 + expectConflictList.add(expectConflict2);
  103 + expectConflictList.add(expectConflict3);
  104 + expectConflictList.add(expectConflict4);
  105 + expectConflictList.add(expectConflict5);
  106 + for (PlanProcessValidateTimetableService.ConflictStatInfo cInfo : expectConflictList) {
  107 + Collections.sort(cInfo.getTtIdList());
  108 + Collections.sort(cInfo.getTtNameList());
  109 + }
  110 + Collections.sort(expectConflictList, new Comparator<PlanProcessValidateTimetableService.ConflictStatInfo>() {
  111 + @Override
  112 + public int compare(PlanProcessValidateTimetableService.ConflictStatInfo o1, PlanProcessValidateTimetableService.ConflictStatInfo o2) {
  113 + return o1.getDateDesc().compareTo(o2.getDateDesc());
  114 + }
  115 + });
  116 +
  117 + List<PlanProcessValidateTimetableService.ConflictStatInfo> actualConflictList = result.getConflictStatInfos();
  118 + for (PlanProcessValidateTimetableService.ConflictStatInfo cInfo : actualConflictList) {
  119 + Collections.sort(cInfo.getTtIdList());
  120 + Collections.sort(cInfo.getTtNameList());
  121 + }
  122 + Collections.sort(actualConflictList, new Comparator<PlanProcessValidateTimetableService.ConflictStatInfo>() {
  123 + @Override
  124 + public int compare(PlanProcessValidateTimetableService.ConflictStatInfo o1, PlanProcessValidateTimetableService.ConflictStatInfo o2) {
  125 + return o1.getDateDesc().compareTo(o2.getDateDesc());
  126 + }
  127 + });
  128 +
  129 + Assert.assertArrayEquals(
  130 + "时刻表冲突信息数量不一致",
  131 + new Object[] {expectConflictList.size()},
  132 + new Object[] {actualConflictList.size()});
  133 + for (int i = 0; i < expectConflictList.size(); i++) {
  134 + PlanProcessValidateTimetableService.ConflictStatInfo expectConflictInfo = expectConflictList.get(i);
  135 + PlanProcessValidateTimetableService.ConflictStatInfo actualConflictInfo = actualConflictList.get(i);
  136 +
  137 + // 日期描述
  138 + Assert.assertEquals("日期描述不一致",
  139 + expectConflictInfo.getDateDesc(), actualConflictInfo.getDateDesc());
  140 + // 时刻表Id信息
  141 + Assert.assertEquals(expectConflictInfo.getDateDesc() + "时刻表Id列表信息不一致",
  142 + expectConflictInfo.getTtIdList(), actualConflictInfo.getTtIdList());
  143 + // 时刻表名字信息
  144 + Assert.assertEquals(expectConflictInfo.getDateDesc() + "时刻表名称列表信息不一致",
  145 + expectConflictInfo.getTtNameList(), actualConflictInfo.getTtNameList());
  146 + }
  147 +
  148 + //-------------- 测试3 --------------//
  149 + PlanProcessValidateTimetableService.EmptyStatInfo expectEmptyInfo1 = new PlanProcessValidateTimetableService.EmptyStatInfo();
  150 + expectEmptyInfo1.setDateDesc("2019年01月01日");
  151 + PlanProcessValidateTimetableService.EmptyStatInfo expectEmptyInfo2 = new PlanProcessValidateTimetableService.EmptyStatInfo();
  152 + expectEmptyInfo2.setDateDesc("2019年01月08日");
  153 +
  154 + // 注意,排序是为了测试需要
  155 + List<PlanProcessValidateTimetableService.EmptyStatInfo> expectEmptyInfoList = new ArrayList<>();
  156 + expectEmptyInfoList.add(expectEmptyInfo1);
  157 + expectEmptyInfoList.add(expectEmptyInfo2);
  158 + Collections.sort(expectEmptyInfoList, new Comparator<PlanProcessValidateTimetableService.EmptyStatInfo>() {
  159 + @Override
  160 + public int compare(PlanProcessValidateTimetableService.EmptyStatInfo o1, PlanProcessValidateTimetableService.EmptyStatInfo o2) {
  161 + return o1.getDateDesc().compareTo(o2.getDateDesc());
  162 + }
  163 + });
  164 +
  165 + List<PlanProcessValidateTimetableService.EmptyStatInfo> actualEmptyInfoList = result.getEmptyStatInfos();
  166 + Collections.sort(actualEmptyInfoList, new Comparator<PlanProcessValidateTimetableService.EmptyStatInfo>() {
  167 + @Override
  168 + public int compare(PlanProcessValidateTimetableService.EmptyStatInfo o1, PlanProcessValidateTimetableService.EmptyStatInfo o2) {
  169 + return o1.getDateDesc().compareTo(o2.getDateDesc());
  170 + }
  171 + });
  172 +
  173 + Assert.assertEquals("空信息数量不一致", expectEmptyInfoList.size(), actualEmptyInfoList.size());
  174 + for (int i = 0; i < expectEmptyInfoList.size(); i++) {
  175 + PlanProcessValidateTimetableService.EmptyStatInfo expectEmptyInfo = expectEmptyInfoList.get(i);
  176 + PlanProcessValidateTimetableService.EmptyStatInfo actualEmptyInfo = actualEmptyInfoList.get(i);
  177 + Assert.assertEquals("日期描述不一致",
  178 + expectEmptyInfo.getDateDesc(), actualEmptyInfo.getDateDesc());
  179 + }
77 } 180 }
78 181
79 @Test 182 @Test
src/test/resources/META-INF/drools.packagebuilder.conf
@@ -2,6 +2,8 @@ @@ -2,6 +2,8 @@
2 2
3 # kBase_validate_timetable.drl使用 3 # kBase_validate_timetable.drl使用
4 drools.accumulate.function.ecount = com.bsth.service.schedule.plan.process._1_validate._1_timetable.drools.KBaseValidateTimeTable_Function_ErrorBcCount 4 drools.accumulate.function.ecount = com.bsth.service.schedule.plan.process._1_validate._1_timetable.drools.KBaseValidateTimeTable_Function_ErrorBcCount
  5 +drools.accumulate.function.as = com.bsth.service.schedule.plan.process._1_validate._1_timetable.drools.KBaseValidateTimeTable_Function_AccumulateStatInfo
5 drools.accumulate.function.ac = com.bsth.service.schedule.plan.process._1_validate._1_timetable.drools.KBaseValidateTimeTable_Function_AccumulateConflict 6 drools.accumulate.function.ac = com.bsth.service.schedule.plan.process._1_validate._1_timetable.drools.KBaseValidateTimeTable_Function_AccumulateConflict
  7 +drools.accumulate.function.ae = com.bsth.service.schedule.plan.process._1_validate._1_timetable.drools.KBaseValidateTimeTable_Function_AccumulateEmpty
6 # kBase_validate_rule.drl使用 8 # kBase_validate_rule.drl使用
7 drools.accumulate.function.srif = com.bsth.service.schedule.plan.process._1_validate._2_rule.drools.KBaseValidateRule_Function_ErrorInfo 9 drools.accumulate.function.srif = com.bsth.service.schedule.plan.process._1_validate._2_rule.drools.KBaseValidateRule_Function_ErrorInfo
src/test/resources/testdata/_0_validate_timetable.xml
@@ -55,7 +55,7 @@ @@ -55,7 +55,7 @@
55 <!-- 时刻表信息 --> 55 <!-- 时刻表信息 -->
56 <bsth_c_s_ttinfo id="1" xl="1" name="时刻表1" xl_dir="2" qyrq="2019-01-01" is_enable_dis_template="1" 56 <bsth_c_s_ttinfo id="1" xl="1" name="时刻表1" xl_dir="2" qyrq="2019-01-01" is_enable_dis_template="1"
57 is_cancel="0" line_version="1" lp_count = "10" loop_count = "6" 57 is_cancel="0" line_version="1" lp_count = "10" loop_count = "6"
58 - rule_days="1,1,1,1,1,0,0" 58 + rule_days="1,0,1,1,1,0,0"
59 create_by="1" create_date="2019-02-01" update_by="1" update_date="2019-02-01" 59 create_by="1" create_date="2019-02-01" update_by="1" update_date="2019-02-01"
60 /> 60 />
61 <bsth_c_s_ttinfo id="2" xl="1" name="时刻表2" xl_dir="2" qyrq="2019-01-01" is_enable_dis_template="1" 61 <bsth_c_s_ttinfo id="2" xl="1" name="时刻表2" xl_dir="2" qyrq="2019-01-01" is_enable_dis_template="1"