Commit ae363739256022f1541113cd834e4378fa0898d4

Authored by 徐烜
1 parent eb7d970f

update

src/main/java/com/bsth/entity/schedule/SchedulePlanInfo.java
... ... @@ -3,10 +3,10 @@ package com.bsth.entity.schedule;
3 3 import com.bsth.entity.Line;
4 4 import com.bsth.entity.sys.SysUser;
5 5 import com.bsth.service.schedule.rules.shiftloop.ScheduleResult_output;
6   -import com.fasterxml.jackson.annotation.JsonIgnore;
7 6  
8 7 import javax.persistence.*;
9 8 import java.util.Date;
  9 +import java.util.List;
10 10  
11 11 /**
12 12 * 排班计划明细。
... ... @@ -150,7 +150,7 @@ public class SchedulePlanInfo {
150 150 ScheduleResult_output scheduleResult_output,
151 151 TTInfoDetail ttInfoDetail,
152 152 CarConfigInfo carConfigInfo,
153   - EmployeeConfigInfo employeeConfigInfo,
  153 + List<EmployeeConfigInfo> employeeConfigInfoList,
154 154 SchedulePlan schedulePlan) {
155 155  
156 156 // TODO:关联的公司名称
... ... @@ -180,6 +180,17 @@ public class SchedulePlanInfo {
180 180  
181 181 // TODO:报道时间,出场时间没有
182 182 // 关联的驾驶员
  183 + EmployeeConfigInfo employeeConfigInfo = null;
  184 + if (ttInfoDetail.getIsFB()) {
  185 + if (employeeConfigInfoList.size() > 1) {
  186 + employeeConfigInfo = employeeConfigInfoList.get(1);
  187 + } else {
  188 + employeeConfigInfo = employeeConfigInfoList.get(0);
  189 + }
  190 + } else {
  191 + employeeConfigInfo = employeeConfigInfoList.get(0);
  192 + }
  193 +
183 194 this.j = employeeConfigInfo.getJsy().getId();
184 195 this.jGh = employeeConfigInfo.getJsy().getJobCode();
185 196 this.jName = employeeConfigInfo.getJsy().getPersonnelName();
... ... @@ -207,15 +218,13 @@ public class SchedulePlanInfo {
207 218 this.zdz = ttInfoDetail.getTcc().getId(); // 终点站-停车场id
208 219 this.zdzCode = ttInfoDetail.getTcc().getParkCode(); // 终点站-停车场code
209 220 this.zdzName = ttInfoDetail.getTcc().getParkName(); // 终点站-停车场name
210   - } else if ("normal".equals(this.bcType)) { // 正常班次
  221 + } else { // 其他班次
211 222 this.qdz = ttInfoDetail.getQdz().getId(); // 起点站id
212 223 this.qdzCode = ttInfoDetail.getQdz().getStationCod(); // 起点站code
213 224 this.qdzName = ttInfoDetail.getQdz().getStationName(); // 起点站name
214 225 this.zdz = ttInfoDetail.getZdz().getId(); // 终点站id
215 226 this.zdzCode = ttInfoDetail.getZdz().getStationCod(); // 终点站code
216 227 this.zdzName = ttInfoDetail.getZdz().getStationName(); // 终点站name
217   - } else {
218   - throw new RuntimeException("排班计划数据,未知班次类型:" + this.bcType);
219 228 }
220 229  
221 230 this.fcsj = ttInfoDetail.getFcsj(); // 发车时间
... ...
src/main/java/com/bsth/service/schedule/SchedulePlanServiceImpl.java
... ... @@ -47,8 +47,8 @@ public class SchedulePlanServiceImpl extends BaseServiceImpl&lt;SchedulePlan, Long&gt;
47 47 // 1-1、查找线路具体信息
48 48 Line xl = strategy.getLine(schedulePlan.getXl().getId());
49 49 // 1-2、查出指定线路的所有规则
50   - TTInfo ttInfo = strategy.getTTInfo(xl.getId()); // 时刻表id
51   - schedulePlan.setTtInfo(ttInfo); // 关联的时刻表
  50 + TTInfo ttInfo = strategy.getTTInfo(xl.getId()).get(0); // 时刻表id
  51 + schedulePlan.setTtInfo(ttInfo); // TODO:关联的时刻表,之后改掉
52 52  
53 53 // 2-1、构造drools规则输入数据,输出数据
54 54 // 全局计算参数
... ... @@ -89,7 +89,9 @@ public class SchedulePlanServiceImpl extends BaseServiceImpl&lt;SchedulePlan, Long&gt;
89 89  
90 90 // 3、根据规则返回,组合最后的输出数据
91 91 // 3-1、根据注入的策略服务,获取原始数据
92   - Multimap<Long, TTInfoDetail> gbdTTinfoMaps = strategy.getGuideboardXlTTInfoDetailMaps(xl.getId()); // 路牌对应时刻明细
  92 + Map<Date, Multimap<Long, TTInfoDetail>> gbdTTinfoMaps = strategy.getGuideboardXlTTInfoDetailMaps(
  93 + xl.getId(), schedulePlan.getScheduleFromTime(), schedulePlan.getScheduleToTime());
  94 +
93 95 Map<Long, CarConfigInfo> carConfigMaps = strategy.getCarConfigMaps(xl.getId()); // 车辆配置对应车辆信息
94 96 Map<Long, EmployeeConfigInfo> employeeConfigMaps = strategy.getEmployeeConfigMaps(xl.getId()); // 人员配置对应的人员信息
95 97  
... ... @@ -97,18 +99,23 @@ public class SchedulePlanServiceImpl extends BaseServiceImpl&lt;SchedulePlan, Long&gt;
97 99 List<SchedulePlanInfo> schedulePlanInfos = new ArrayList<>();
98 100 for (ScheduleResult_output scheduleResult_output : scheduleResults_output.getResults()) {
99 101 // 车辆配置对应的车辆
100   - CarConfigInfo configInfo = carConfigMaps.get(scheduleResult_output.getCarConfigId());
101   - // 人员配置对应的人员
102   - EmployeeConfigInfo employeeConfigInfo = employeeConfigMaps.get(scheduleResult_output.getEmployeeConfigId());
  102 + CarConfigInfo configInfo = carConfigMaps.get(Long.valueOf(scheduleResult_output.getCarConfigId()));
  103 + // 人员配置对应的人员,这里需要分班处理的
  104 + List<EmployeeConfigInfo> employeeConfigInfoList = new ArrayList<>();
  105 + String[] eids = scheduleResult_output.getEmployeeConfigId().split("-");
  106 + for (String eid : eids) {
  107 + employeeConfigInfoList.add(employeeConfigMaps.get(Long.valueOf(eid)));
  108 + }
103 109 // 排班明细(这个要迭代的)
104   - Collection<TTInfoDetail> ttInfoDetails = gbdTTinfoMaps.get(Long.parseLong(scheduleResult_output.getGuideboardId()));
  110 + Collection<TTInfoDetail> ttInfoDetails = gbdTTinfoMaps.get(scheduleResult_output.getSd().toDate()).get(
  111 + Long.parseLong(scheduleResult_output.getGuideboardId()));
105 112 for (TTInfoDetail ttInfoDetail : ttInfoDetails) {
106 113 SchedulePlanInfo schedulePlanInfo = new SchedulePlanInfo(
107 114 xl,
108 115 scheduleResult_output,
109 116 ttInfoDetail,
110 117 configInfo,
111   - employeeConfigInfo,
  118 + employeeConfigInfoList,
112 119 schedulePlan);
113 120 schedulePlanInfos.add(schedulePlanInfo);
114 121 }
... ...
src/main/java/com/bsth/service/schedule/rules/MyDroolsConfiguration.java
... ... @@ -58,6 +58,9 @@ public class MyDroolsConfiguration {
58 58 kfs.write("src/main/resources/shiftloop.drl", kieServices.getResources()
59 59 .newInputStreamResource(this.getClass().getResourceAsStream(
60 60 "/rules/shiftloop.drl"), "UTF-8"));
  61 + kfs.write("src/main/resources/ttinfo.drl", kieServices.getResources()
  62 + .newInputStreamResource(this.getClass().getResourceAsStream(
  63 + "/rules/ttinfo.drl"), "UTF-8"));
61 64 // TODO:还有其他drl....
62 65  
63 66 // 4、创建KieBuilder,使用KieFileSystem构建
... ...
src/main/java/com/bsth/service/schedule/rules/shiftloop/ScheduleCalcuParam_input.java
... ... @@ -11,15 +11,12 @@ public class ScheduleCalcuParam_input {
11 11 private DateTime fromDate;
12 12 /** 结束计算日期 */
13 13 private DateTime toDate;
14   - /** 时刻表id */
15   - private Long ttinfoId;
16 14  
17 15 public ScheduleCalcuParam_input() {}
18 16  
19 17 public ScheduleCalcuParam_input(SchedulePlan schedulePlan) {
20 18 this.fromDate = new DateTime((schedulePlan.getScheduleFromTime()));
21 19 this.toDate = new DateTime((schedulePlan.getScheduleToTime()));
22   - this.ttinfoId = schedulePlan.getTtInfo().getId();
23 20 }
24 21  
25 22 public DateTime getFromDate() {
... ... @@ -38,11 +35,4 @@ public class ScheduleCalcuParam_input {
38 35 this.toDate = toDate;
39 36 }
40 37  
41   - public Long getTtinfoId() {
42   - return ttinfoId;
43   - }
44   -
45   - public void setTtinfoId(Long ttinfoId) {
46   - this.ttinfoId = ttinfoId;
47   - }
48 38 }
... ...
src/main/java/com/bsth/service/schedule/rules/strategy/IStrategy.java
... ... @@ -8,6 +8,8 @@ import com.bsth.entity.schedule.TTInfoDetail;
8 8 import com.bsth.entity.schedule.rule.ScheduleRule1Flat;
9 9 import com.google.common.collect.Multimap;
10 10  
  11 +import java.util.Date;
  12 +import java.util.List;
11 13 import java.util.Map;
12 14  
13 15 /**
... ... @@ -27,7 +29,14 @@ public interface IStrategy {
27 29 * @param xlId 线路id
28 30 * @return 时刻表
29 31 */
30   - TTInfo getTTInfo(Integer xlId);
  32 + List<TTInfo> getTTInfo(Integer xlId);
  33 +
  34 + /**
  35 + * 获取指定线路的时刻表的明细。
  36 + * @param xlId 线路id
  37 + * @return
  38 + */
  39 + List<TTInfoDetail> getTTInfoDetail(Integer xlId);
31 40  
32 41 /**
33 42 * 获取指定线路下,可用的排班规则。
... ... @@ -37,11 +46,13 @@ public interface IStrategy {
37 46 Iterable<ScheduleRule1Flat> getScheduleRule(Integer xlId);
38 47  
39 48 /**
40   - * 获取指定线路下,路牌与时刻明细对应的Map。
  49 + * 获取指定线路下,日期与路牌与时刻明细对应的Map。
41 50 * @param xlId 线路id
  51 + * @param fromDate 开始日期
  52 + * @param toDate 结束日期
42 53 * @return 路牌id为key,时刻明细 Collection<TTInfoDetail> 为value
43 54 */
44   - Multimap<Long, TTInfoDetail> getGuideboardXlTTInfoDetailMaps(Integer xlId);
  55 + Map<Date, Multimap<Long, TTInfoDetail>> getGuideboardXlTTInfoDetailMaps(Integer xlId, Date fromDate, Date toDate);
45 56  
46 57 /**
47 58 * 获取指定线路下,车辆配置与车辆信息对应的Map。
... ...
src/main/java/com/bsth/service/schedule/rules/strategy/IStrategyImpl.java
... ... @@ -8,14 +8,21 @@ import com.bsth.entity.schedule.TTInfoDetail;
8 8 import com.bsth.entity.schedule.rule.ScheduleRule1Flat;
9 9 import com.bsth.service.LineService;
10 10 import com.bsth.service.schedule.*;
  11 +import com.bsth.service.schedule.rules.ttinfo.TTInfoCalcuParam_input;
  12 +import com.bsth.service.schedule.rules.ttinfo.TTInfoResult_output;
  13 +import com.bsth.service.schedule.rules.ttinfo.TTInfoResults_output;
  14 +import com.bsth.service.schedule.rules.ttinfo.TTInfo_input;
11 15 import com.google.common.collect.ArrayListMultimap;
12 16 import com.google.common.collect.Multimap;
  17 +import org.joda.time.DateTime;
  18 +import org.kie.api.KieBase;
  19 +import org.kie.api.runtime.KieSession;
  20 +import org.slf4j.Logger;
  21 +import org.slf4j.LoggerFactory;
13 22 import org.springframework.beans.factory.annotation.Autowired;
14 23 import org.springframework.stereotype.Service;
15 24  
16   -import java.util.HashMap;
17   -import java.util.Iterator;
18   -import java.util.Map;
  25 +import java.util.*;
19 26  
20 27 /**
21 28 * Created by xu on 16/7/10.
... ... @@ -35,6 +42,12 @@ public class IStrategyImpl implements IStrategy {
35 42 @Autowired
36 43 private ScheduleRule1FlatService scheduleRule1FlatService;
37 44  
  45 + /** 日志记录器 */
  46 + private Logger logger = LoggerFactory.getLogger(IStrategyImpl.class);
  47 +
  48 + @Autowired
  49 + private KieBase kieBase;
  50 +
38 51 @Override
39 52 public Line getLine(Integer xlId) {
40 53 Line xl = lineService.findById(xlId); // 查找线路具体信息
... ... @@ -42,18 +55,39 @@ public class IStrategyImpl implements IStrategy {
42 55 }
43 56  
44 57 @Override
45   - public TTInfo getTTInfo(Integer xlId) {
46   - // TODO:本来要使用规则判定到底使用哪张时刻表,这里选用第一张
47   - Map<String, Object> param = new HashMap<>(); // 查询参数
48   - param.clear();
49   - param.put("xl.id_eq", xlId); // 线路id
50   - param.put("isCancel_eq", false); // 没有作废
51   - param.put("isEnableDisTemplate_eq", true); // 是否启用
52   - Iterable<TTInfo> ttInfoIterable = ttInfoService.list(param);
53   - Iterator<TTInfo> ttInfoIterator = ttInfoIterable.iterator();
54   - if (!ttInfoIterator.hasNext())
55   - throw new RuntimeException("线路id=" + xlId + ",下没有任何时刻表数据!");
56   - return ttInfoIterator.next();
  58 + public List<TTInfo> getTTInfo(Integer xlId) {
  59 + // 查询参数
  60 + Map<String, Object> param = new HashMap<>();
  61 + param.put("xl.id_eq", xlId); // 线路Id
  62 + param.put("isCancel_eq", false); // 作废的过滤掉
  63 + Iterator<TTInfo> ttInfoIterator = ttInfoService.list(param).iterator();
  64 + if (!ttInfoIterator.hasNext()) {
  65 + throw new RuntimeException("线路id=" + xlId + " 没有时刻表!");
  66 + }
  67 + List<TTInfo> ttInfos = new ArrayList<>();
  68 + while (ttInfoIterator.hasNext()) {
  69 + TTInfo ttInfo = ttInfoIterator.next();
  70 + ttInfos.add(ttInfo);
  71 + }
  72 + return ttInfos;
  73 + }
  74 +
  75 + @Override
  76 + public List<TTInfoDetail> getTTInfoDetail(Integer xlId) {
  77 + List<TTInfoDetail> ttInfoDetails = new ArrayList<>();
  78 +
  79 + List<TTInfo> ttInfos = getTTInfo(xlId);
  80 + Map<String, Object> param = new HashMap<>();
  81 + for (TTInfo ttInfo : ttInfos) {
  82 + param.clear();
  83 + param.put("ttinfo.id_eq", ttInfo.getId());
  84 + Iterator<TTInfoDetail> ttInfoDetailIterator = ttInfoDetailService.list(param).iterator();
  85 + while (ttInfoDetailIterator.hasNext()) {
  86 + ttInfoDetails.add(ttInfoDetailIterator.next());
  87 + }
  88 + }
  89 +
  90 + return ttInfoDetails;
57 91 }
58 92  
59 93 @Override
... ... @@ -70,23 +104,59 @@ public class IStrategyImpl implements IStrategy {
70 104 }
71 105  
72 106 @Override
73   - public Multimap<Long, TTInfoDetail> getGuideboardXlTTInfoDetailMaps(Integer xlId) {
74   - TTInfo ttInfo = getTTInfo(xlId);
75   - // 查询参数
  107 + public Map<Date, Multimap<Long, TTInfoDetail>> getGuideboardXlTTInfoDetailMaps(
  108 + Integer xlId, Date fromDate, Date toDate) {
  109 + // 获取线路的所有时刻表
  110 + List<TTInfo> ttInfos = getTTInfo(xlId);
  111 +
  112 + // 执行规则,判定每天使用的时刻表
  113 + KieSession session = kieBase.newKieSession();
  114 +
  115 + session.setGlobal("log", logger);
  116 + TTInfoResults_output ttInfoResults_output = new TTInfoResults_output();
  117 + session.setGlobal("results", ttInfoResults_output);
  118 +
  119 + TTInfoCalcuParam_input ttInfoCalcuParam_input = new TTInfoCalcuParam_input(
  120 + new DateTime(fromDate), new DateTime(toDate));
  121 + session.insert(ttInfoCalcuParam_input);
  122 + for (TTInfo ttInfo : ttInfos) {
  123 + TTInfo_input ttInfo_input = new TTInfo_input(ttInfo);
  124 + session.insert(ttInfo_input);
  125 + }
  126 +
  127 + session.fireAllRules();
  128 + session.dispose();
  129 +
  130 + // 获取ttinfoDetail
  131 + List<TTInfoDetail> ttInfoDetails = getTTInfoDetail(xlId);
  132 +
  133 + // 规则输出结果
  134 + Multimap<DateTime, TTInfoResult_output> outputMultimap =
  135 + ttInfoResults_output.getResults().get(String.valueOf(xlId));
  136 + // return结果输出
  137 + Map<Date, Multimap<Long, TTInfoDetail>> ttInfoDetailMultimap = new HashMap<>();
  138 +
76 139 Map<String, Object> param = new HashMap<>();
77   - param.put("ttinfo.id_eq", ttInfo.getId());
78   - Iterable<TTInfoDetail> ttInfoDetailIterable = ttInfoDetailService.list(param);
79   - Iterator<TTInfoDetail> ttInfoDetailIterator = ttInfoDetailIterable.iterator();
80   - if (!ttInfoDetailIterator.hasNext())
81   - throw new RuntimeException("时刻表id=" + ttInfo.getId() + ",下没有明细数据!");
82   -
83   - Multimap<Long, TTInfoDetail> gtmaps = ArrayListMultimap.create();
84   - while (ttInfoDetailIterator.hasNext()) {
85   - TTInfoDetail ttInfoDetail = ttInfoDetailIterator.next();
86   - gtmaps.put(ttInfoDetail.getLp().getId(), ttInfoDetail);
  140 + for (DateTime dateTime : outputMultimap.keySet()) {
  141 + Collection<TTInfoResult_output> ttInfoResult_outputs = outputMultimap.get(dateTime);
  142 + // 如果有多个,使用第一个
  143 + Iterator<TTInfoResult_output> ttInfoResult_outputIterator = ttInfoResult_outputs.iterator();
  144 + if (ttInfoResult_outputIterator.hasNext()) {
  145 + // 同一天,多张时刻表只取第一张
  146 + TTInfoResult_output ttInfoResult_output = ttInfoResult_outputIterator.next();
  147 + // 查找时刻表明细
  148 + Multimap<Long, TTInfoDetail> ttinfodetailMap2 = ArrayListMultimap.create();
  149 + for (TTInfoDetail ttInfoDetail : ttInfoDetails) {
  150 + if (ttInfoDetail.getTtinfo().getId() == Long.valueOf(ttInfoResult_output.getTtInfoId())) {
  151 + ttinfodetailMap2.put(ttInfoDetail.getLp().getId(), ttInfoDetail);
  152 + }
  153 + }
  154 +
  155 + ttInfoDetailMultimap.put(dateTime.toDate(), ttinfodetailMap2);
  156 + }
87 157 }
88 158  
89   - return gtmaps;
  159 + return ttInfoDetailMultimap;
90 160 }
91 161  
92 162 @Override
... ...
src/main/java/com/bsth/service/schedule/rules/ttinfo/TTInfoCalcuParam_input.java 0 → 100644
  1 +package com.bsth.service.schedule.rules.ttinfo;
  2 +
  3 +import org.joda.time.DateTime;
  4 +
  5 +/**
  6 + * 时刻表计算参数_输入。
  7 + */
  8 +public class TTInfoCalcuParam_input {
  9 + /** 开始计算日期 */
  10 + private DateTime fromDate;
  11 + /** 结束计算日期 */
  12 + private DateTime toDate;
  13 +
  14 + public TTInfoCalcuParam_input() {}
  15 +
  16 + public TTInfoCalcuParam_input(DateTime fromDate, DateTime toDate) {
  17 + this.fromDate = fromDate;
  18 + this.toDate = toDate;
  19 + }
  20 +
  21 + public DateTime getFromDate() {
  22 + return fromDate;
  23 + }
  24 +
  25 + public void setFromDate(DateTime fromDate) {
  26 + this.fromDate = fromDate;
  27 + }
  28 +
  29 + public DateTime getToDate() {
  30 + return toDate;
  31 + }
  32 +
  33 + public void setToDate(DateTime toDate) {
  34 + this.toDate = toDate;
  35 + }
  36 +}
... ...
src/main/java/com/bsth/service/schedule/rules/ttinfo/TTInfoResult_output.java 0 → 100644
  1 +package com.bsth.service.schedule.rules.ttinfo;
  2 +
  3 +import org.joda.time.DateTime;
  4 +
  5 +/**
  6 + * 时刻表选择规则出的结果_输出。
  7 + */
  8 +public class TTInfoResult_output {
  9 + /** 具体日期 */
  10 + private DateTime dateTime;
  11 + /** 时刻表Id */
  12 + private String ttInfoId;
  13 + /** 线路Id */
  14 + private String xlId;
  15 +
  16 + public DateTime getDateTime() {
  17 + return dateTime;
  18 + }
  19 +
  20 + public void setDateTime(DateTime dateTime) {
  21 + this.dateTime = dateTime;
  22 + }
  23 +
  24 + public String getTtInfoId() {
  25 + return ttInfoId;
  26 + }
  27 +
  28 + public void setTtInfoId(String ttInfoId) {
  29 + this.ttInfoId = ttInfoId;
  30 + }
  31 +
  32 + public String getXlId() {
  33 + return xlId;
  34 + }
  35 +
  36 + public void setXlId(String xlId) {
  37 + this.xlId = xlId;
  38 + }
  39 +
  40 + @Override
  41 + public String toString() {
  42 + return String.format(
  43 + "<日期=%s 线路id=%s 时刻表id=%s>\n",
  44 + dateTime.toString("yyyy-MM-dd"),
  45 + xlId,
  46 + ttInfoId);
  47 + }
  48 +}
... ...
src/main/java/com/bsth/service/schedule/rules/ttinfo/TTInfoResults_output.java 0 → 100644
  1 +package com.bsth.service.schedule.rules.ttinfo;
  2 +
  3 +import com.google.common.collect.ArrayListMultimap;
  4 +import com.google.common.collect.Multimap;
  5 +import org.joda.time.DateTime;
  6 +
  7 +import java.util.HashMap;
  8 +import java.util.List;
  9 +import java.util.Map;
  10 +
  11 +/**
  12 + * 输出结果集合。
  13 + */
  14 +public class TTInfoResults_output {
  15 +
  16 + /** 输出列表,key为线路id,value是key为日期,value为排序的时刻表output列表 */
  17 + private Map<String, Multimap<DateTime, TTInfoResult_output>> results = new HashMap<>();
  18 +
  19 + public Map<String, Multimap<DateTime, TTInfoResult_output>> getResults() {
  20 + return results;
  21 + }
  22 +
  23 + public void setResults(Map<String, Multimap<DateTime, TTInfoResult_output>> results) {
  24 + this.results = results;
  25 + }
  26 +
  27 + public void addXlTTInfos(String xlid, DateTime dt, List<TTInfo_input> ttInfo_inputList) {
  28 + Multimap<DateTime, TTInfoResult_output> map;
  29 + if (results.get(xlid) == null) {
  30 + map = ArrayListMultimap.create();
  31 + results.put(xlid, map);
  32 + } else {
  33 + map = results.get(xlid);
  34 + }
  35 +
  36 + for (TTInfo_input ttInfo_input : ttInfo_inputList) {
  37 + TTInfoResult_output ttInfoResult_output = new TTInfoResult_output();
  38 + ttInfoResult_output.setDateTime(dt);
  39 + ttInfoResult_output.setTtInfoId(ttInfo_input.getTtInfoId());
  40 + ttInfoResult_output.setXlId(xlid);
  41 + map.put(dt, ttInfoResult_output);
  42 + }
  43 + }
  44 +
  45 + /**
  46 + * 输出计算后的时刻表
  47 + * @return
  48 + */
  49 + public String showTTInfoDesc1() {
  50 + StringBuilder str = new StringBuilder();
  51 + for (String key : results.keySet()) {
  52 + str.append("线路id=" + key);
  53 + str.append("\n");
  54 + str.append("时刻表=" + results.get(key));
  55 + str.append("\n");
  56 + }
  57 +
  58 + return str.toString();
  59 + }
  60 +
  61 +}
... ...
src/main/java/com/bsth/service/schedule/rules/ttinfo/TTInfo_input.java 0 → 100644
  1 +package com.bsth.service.schedule.rules.ttinfo;
  2 +
  3 +import com.bsth.entity.schedule.TTInfo;
  4 +import org.joda.time.DateTime;
  5 +import org.joda.time.format.DateTimeFormat;
  6 +
  7 +import java.util.ArrayList;
  8 +import java.util.List;
  9 +
  10 +/**
  11 + * 时刻表_输入
  12 + */
  13 +public class TTInfo_input {
  14 + /** 时刻表id */
  15 + private String ttInfoId;
  16 + /** 线路Id */
  17 + private String xlId;
  18 + /** 周一到周日是否启用 */
  19 + private List<Boolean> weekdays = new ArrayList<>();
  20 + /** 特殊节假日 */
  21 + private List<DateTime> specialDays = new ArrayList<>();
  22 + /** 最新修改时间 */
  23 + private DateTime updateDate;
  24 + /** 是否启用 */
  25 + private Boolean isEnable;
  26 + /** 启用日期 */
  27 + private DateTime qyDate;
  28 +
  29 + public TTInfo_input() {
  30 +
  31 + }
  32 +
  33 + public TTInfo_input(TTInfo ttInfo) {
  34 + this.ttInfoId = String.valueOf(ttInfo.getId());
  35 + this.xlId = String.valueOf(ttInfo.getXl().getId());
  36 + String[] days = ttInfo.getRule_days().split(",");
  37 + System.out.println(days.length);
  38 + for (int i = 0; i < 7; i++) {
  39 + if ("1".equals(days[i])) {
  40 + weekdays.add(true);
  41 + } else {
  42 + weekdays.add(false);
  43 + }
  44 + }
  45 + String[] sdays = ttInfo.getSpecial_days().split(",");
  46 + for (int i = 0; i < sdays.length; i++) {
  47 + specialDays.add(DateTimeFormat.forPattern("yyyy-MM-dd").
  48 + parseDateTime(sdays[i]));
  49 + }
  50 + this.updateDate = new DateTime(ttInfo.getUpdateDate());
  51 + this.isEnable = ttInfo.getIsEnableDisTemplate();
  52 + this.qyDate = new DateTime(ttInfo.getQyrq());
  53 +
  54 + }
  55 +
  56 + public String getTtInfoId() {
  57 + return ttInfoId;
  58 + }
  59 +
  60 + public void setTtInfoId(String ttInfoId) {
  61 + this.ttInfoId = ttInfoId;
  62 + }
  63 +
  64 + public String getXlId() {
  65 + return xlId;
  66 + }
  67 +
  68 + public void setXlId(String xlId) {
  69 + this.xlId = xlId;
  70 + }
  71 +
  72 + public List<Boolean> getWeekdays() {
  73 + return weekdays;
  74 + }
  75 +
  76 + public void setWeekdays(List<Boolean> weekdays) {
  77 + this.weekdays = weekdays;
  78 + }
  79 +
  80 + public List<DateTime> getSpecialDays() {
  81 + return specialDays;
  82 + }
  83 +
  84 + public void setSpecialDays(List<DateTime> specialDays) {
  85 + this.specialDays = specialDays;
  86 + }
  87 +
  88 + public DateTime getUpdateDate() {
  89 + return updateDate;
  90 + }
  91 +
  92 + public void setUpdateDate(DateTime updateDate) {
  93 + this.updateDate = updateDate;
  94 + }
  95 +
  96 + public Boolean getIsEnable() {
  97 + return isEnable;
  98 + }
  99 +
  100 + public void setIsEnable(Boolean isEnable) {
  101 + this.isEnable = isEnable;
  102 + }
  103 +
  104 + public DateTime getQyDate() {
  105 + return qyDate;
  106 + }
  107 +
  108 + public void setQyDate(DateTime qyDate) {
  109 + this.qyDate = qyDate;
  110 + }
  111 +}
... ...
src/main/java/com/bsth/service/schedule/rules/ttinfo/readme.txt 0 → 100644
  1 +时刻表选择规则,每天的时刻表都不一样
0 2 \ No newline at end of file
... ...
src/main/resources/rules/shiftloop.drl
1   -package com.bsth.service.schedule;
  1 +package com.bsth.service.schedule.shiftloop;
2 2  
3 3 import org.joda.time.*;
4 4 import java.util.*;
... ...
src/main/resources/rules/ttinfo.drl 0 → 100644
  1 +package com.bsth.service.schedule.ttinfo;
  2 +
  3 +import org.joda.time.*;
  4 +import java.util.*;
  5 +
  6 +import com.bsth.service.schedule.rules.ttinfo.TTInfoCalcuParam_input;
  7 +import com.bsth.service.schedule.rules.ttinfo.TTInfo_input;
  8 +import com.bsth.service.schedule.rules.ttinfo.TTInfoResult_output;
  9 +import com.bsth.service.schedule.rules.ttinfo.TTInfoResults_output;
  10 +
  11 +import org.slf4j.Logger;
  12 +
  13 +// 全局日志
  14 +global Logger log;
  15 +// return输出
  16 +global TTInfoResults_output results
  17 +
  18 +
  19 +/*
  20 + TODO:规则说明,以后待说明
  21 +*/
  22 +
  23 +//----------------- 第一阶段、计算规则准备数据(天数)----------------//
  24 +
  25 +declare Calcu_days_result
  26 + calcu_day : Integer // 该计算第几天
  27 + calcu_weekday : Integer // 星期几(1到7)
  28 + calcu_date : DateTime // 该计算的具体日期
  29 + calcu_days : Integer // 总共需要计算的天数
  30 + calcu_start_date : DateTime // 开始计算日期
  31 + calcu_end_date : DateTime // 结束计算日期
  32 +end
  33 +
  34 +rule "calcu_days"
  35 + when
  36 + TTInfoCalcuParam_input($fromDate : fromDate, $toDate : toDate, $fromDate.isBefore($toDate))
  37 + then
  38 + // 构造Calcu_days_result对象,进行下一阶段计算
  39 + Calcu_days_result cdr = new Calcu_days_result();
  40 + Period p = new Period($fromDate, $toDate, PeriodType.days());
  41 +
  42 + cdr.setCalcu_day(1);
  43 + cdr.setCalcu_date($fromDate);
  44 + cdr.setCalcu_days(p.getDays() + 1);
  45 + cdr.setCalcu_weekday($fromDate.getDayOfWeek());
  46 + cdr.setCalcu_start_date($fromDate);
  47 + cdr.setCalcu_end_date(($toDate));
  48 +
  49 + log.info("总共塑腰计算的天数 calcu_days={} 之后的计算从第1天开始 ", p.getDays() + 1);
  50 +
  51 + insert(cdr); // 插入fact数据,进入下一个阶段
  52 +end
  53 +
  54 +//----------------- 第二阶段、判定时刻表是否启用 ----------------//
  55 +
  56 +declare Calcu_ttinfo_enable_result
  57 + xlid : String // 线路id
  58 + ttid : String // 时刻表id
  59 + calcu_date : DateTime // 计算日期
  60 +end
  61 +
  62 +rule "calcu_ttinfo_enable"
  63 + salience 900
  64 + when
  65 + $calcu_days_result : Calcu_days_result($calcu_date : calcu_date, calcu_day <= calcu_days)
  66 + $tTInfo_input : TTInfo_input($xlid : xlId, $ttid : ttInfoId, isEnable == true)
  67 + then
  68 + // 构造Calcu_ttinfo_enable_result对象,进行下一步计算
  69 + Calcu_ttinfo_enable_result cter = new Calcu_ttinfo_enable_result();
  70 + cter.setXlid($xlid);
  71 + cter.setTtid($ttid);
  72 + cter.setCalcu_date($calcu_date);
  73 +
  74 + log.info("启用的时刻表:xlid={} ttid={} 计算日期={}", $xlid, $ttid, $calcu_date);
  75 +
  76 + insert(cter);
  77 +end
  78 +
  79 +//----------------- 第三阶段 -------------------//
  80 +
  81 +rule "calcu_ttinfo_special_day"
  82 + salience 800
  83 + when
  84 + $calcu_ttinfo_enable_result : Calcu_ttinfo_enable_result($xlid : xlid, $calcu_date : calcu_date)
  85 + $calcu_days_result : Calcu_days_result(calcu_date == $calcu_date, $calcu_day : calcu_day)
  86 + $ttinfolist : ArrayList(size >= 1) from collect (TTInfo_input(xlId == $xlid, isEnable == true, specialDays contains $calcu_date))
  87 + then
  88 + // 更新Calcu_days_result对象
  89 + int new_calcu_day = $calcu_day + 1;
  90 + $calcu_days_result.setCalcu_day(new_calcu_day);
  91 + DateTime new_calcu_date = $calcu_date.plusDays(1);
  92 + $calcu_days_result.setCalcu_date(new_calcu_date);
  93 + $calcu_days_result.setCalcu_weekday(new_calcu_date.getDayOfWeek());
  94 +
  95 + log.info("启用特殊日期时刻表:xlid={} 时刻表个数={} 特殊日期={}", $xlid, $ttinfolist.size(), $calcu_date);
  96 +
  97 + // result输出
  98 + results.addXlTTInfos($xlid, $calcu_date, $ttinfolist);
  99 +
  100 + update($calcu_days_result);
  101 +end
  102 +
  103 +rule "calcu_ttinfo_normal_day"
  104 + salience 700
  105 + when
  106 + $calcu_ttinfo_enable_result : Calcu_ttinfo_enable_result($xlid : xlid, $calcu_date : calcu_date)
  107 + $calcu_days_result : Calcu_days_result(calcu_date == $calcu_date, $calcu_day : calcu_day, $calcu_weekday : calcu_weekday)
  108 + $ttinfolist : ArrayList(size >= 1) from collect (TTInfo_input(xlId == $xlid, isEnable == true, specialDays not contains $calcu_date, weekdays[$calcu_weekday - 1] == true))
  109 + then
  110 + // 更新Calcu_days_result对象
  111 + int new_calcu_day = $calcu_day + 1;
  112 + $calcu_days_result.setCalcu_day(new_calcu_day);
  113 + DateTime new_calcu_date = $calcu_date.plusDays(1);
  114 + $calcu_days_result.setCalcu_date(new_calcu_date);
  115 + $calcu_days_result.setCalcu_weekday(new_calcu_date.getDayOfWeek());
  116 +
  117 + log.info("启用常规日期时刻表:xlid={} 时刻表个数={} 常规日期={} 星期几={}", $xlid, $ttinfolist.size(), $calcu_date, $calcu_weekday);
  118 +
  119 + // result输出
  120 + results.addXlTTInfos($xlid, $calcu_date, $ttinfolist);
  121 +
  122 + update($calcu_days_result);
  123 +end
  124 +
  125 +rule "calcu_ttinfo_other_day"
  126 + salience 500
  127 + when
  128 + $calcu_ttinfo_enable_result : Calcu_ttinfo_enable_result($xlid : xlid, $calcu_date : calcu_date)
  129 + $calcu_days_result : Calcu_days_result(calcu_date == $calcu_date, $calcu_day : calcu_day, $calcu_weekday : calcu_weekday)
  130 + $ttinfolist : ArrayList(size >= 1) from collect (TTInfo_input(xlId == $xlid, isEnable == true, specialDays not contains $calcu_date, weekdays[$calcu_weekday - 1] == false))
  131 + then
  132 + // 更新Calcu_days_result对象
  133 + int new_calcu_day = $calcu_day + 1;
  134 + $calcu_days_result.setCalcu_day(new_calcu_day);
  135 + DateTime new_calcu_date = $calcu_date.plusDays(1);
  136 + $calcu_days_result.setCalcu_date(new_calcu_date);
  137 + $calcu_days_result.setCalcu_weekday(new_calcu_date.getDayOfWeek());
  138 +
  139 + log.info("启用默认日期时刻表:xlid={} 时刻表个数={} 常规日期={} 星期几={}", $xlid, $ttinfolist.size(), $calcu_date, $calcu_weekday);
  140 +
  141 + // result输出
  142 + results.addXlTTInfos($xlid, $calcu_date, $ttinfolist);
  143 +
  144 + update($calcu_days_result);
  145 +
  146 +end
0 147 \ No newline at end of file
... ...
src/test/java/com/bsth/service/schedule/rules/DroolsRulesTest.java
... ... @@ -4,11 +4,16 @@ import com.bsth.Application;
4 4 import com.bsth.service.schedule.rules.shiftloop.ScheduleCalcuParam_input;
5 5 import com.bsth.service.schedule.rules.shiftloop.ScheduleResults_output;
6 6 import com.bsth.service.schedule.rules.shiftloop.ScheduleRule_input;
  7 +import com.bsth.service.schedule.rules.ttinfo.TTInfoCalcuParam_input;
  8 +import com.bsth.service.schedule.rules.ttinfo.TTInfoResults_output;
  9 +import com.bsth.service.schedule.rules.ttinfo.TTInfo_input;
7 10 import org.joda.time.DateTime;
8 11 import org.junit.Test;
9 12 import org.junit.runner.RunWith;
10 13 import org.kie.api.KieBase;
11 14 import org.kie.api.runtime.KieSession;
  15 +import org.slf4j.Logger;
  16 +import org.slf4j.LoggerFactory;
12 17 import org.springframework.beans.factory.annotation.Autowired;
13 18 import org.springframework.boot.test.SpringApplicationConfiguration;
14 19 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
... ... @@ -21,10 +26,13 @@ import java.util.List;
21 26 @SpringApplicationConfiguration(classes = {Application.class})
22 27 public class DroolsRulesTest {
23 28  
  29 + /** 日志记录器 */
  30 + private final static Logger logger = LoggerFactory.getLogger(DroolsRulesTest.class);
  31 +
24 32 @Autowired
25 33 private KieBase kieBase;
26 34  
27   - @Test
  35 +// @Test
28 36 public void helloWorldDrlTest() throws Exception {
29 37 // 1、创建session,内部配置的是stateful
30 38 KieSession session = kieBase.newKieSession();
... ... @@ -51,6 +59,61 @@ public class DroolsRulesTest {
51 59 }
52 60  
53 61 @Test
  62 + public void ttinfoDrlTest() throws Exception {
  63 + logger.info("------------ttinfoDrlTest 测试---------------");
  64 +
  65 + // 1、创建session,内部配置的是stateful
  66 + KieSession session = kieBase.newKieSession();
  67 +
  68 + // 1.1 设置gloable对象,在drl中通过别人使用
  69 + session.setGlobal("log", logger);
  70 + TTInfoResults_output ttInfoResults_output = new TTInfoResults_output();
  71 + session.setGlobal("results", ttInfoResults_output);
  72 +
  73 + // 1.2 可以设置一些监听器,再议
  74 +
  75 + // 2、创建fact对象
  76 + TTInfoCalcuParam_input ttInfoCalcuParam_input = new TTInfoCalcuParam_input(
  77 + new DateTime(2016, 8, 1, 0, 0),
  78 + new DateTime(2016, 8, 10, 0, 0)
  79 + );
  80 +
  81 + TTInfo_input ttInfo_input1 = new TTInfo_input();
  82 + ttInfo_input1.setTtInfoId("1");
  83 + ttInfo_input1.setXlId("1");
  84 + ttInfo_input1.setWeekdays(Arrays.asList(true, true, true, true, true, false, false));
  85 + ttInfo_input1.getSpecialDays().add(new DateTime(2016, 8, 1, 0, 0));
  86 + ttInfo_input1.setUpdateDate(new DateTime(2016, 1, 1, 0, 0));
  87 + ttInfo_input1.setIsEnable(true);
  88 + ttInfo_input1.setQyDate(new DateTime(2016, 1, 1, 0, 0));
  89 +
  90 + TTInfo_input ttInfo_input2 = new TTInfo_input();
  91 + ttInfo_input2.setTtInfoId("2");
  92 + ttInfo_input2.setXlId("1");
  93 + ttInfo_input2.setWeekdays(Arrays.asList(true, false, false, false, false, true, false));
  94 + ttInfo_input2.getSpecialDays().add(new DateTime(2016, 8, 11, 0, 0));
  95 + ttInfo_input2.setUpdateDate(new DateTime(2016, 1, 1, 0, 0));
  96 + ttInfo_input2.setIsEnable(true);
  97 + ttInfo_input2.setQyDate(new DateTime(2016, 1, 1, 0, 0));
  98 +
  99 + session.insert(ttInfoCalcuParam_input);
  100 + session.insert(ttInfo_input1);
  101 + session.insert(ttInfo_input2);
  102 +
  103 +
  104 +
  105 + // 3、执行rule
  106 + session.fireAllRules();
  107 +
  108 + // 4、执行完毕销毁,有日志的也要关闭
  109 + session.dispose();
  110 +
  111 + // 打印global结果
  112 + logger.info(ttInfoResults_output.showTTInfoDesc1());
  113 +
  114 + }
  115 +
  116 +// @Test
54 117 public void shiftloopDrlTest() throws Exception {
55 118 // 1、创建session,内部配置的是stateful
56 119 KieSession session = kieBase.newKieSession();
... ... @@ -66,7 +129,6 @@ public class DroolsRulesTest {
66 129 ScheduleCalcuParam_input scheduleCalcuParam_input = new ScheduleCalcuParam_input();
67 130 scheduleCalcuParam_input.setFromDate(new DateTime(2016, 8, 1, 0, 0));
68 131 scheduleCalcuParam_input.setToDate(new DateTime(2016, 8, 10, 0, 0));
69   - scheduleCalcuParam_input.setTtinfoId(1L);
70 132  
71 133 ScheduleRule_input scheduleRule_input1 = new ScheduleRule_input();
72 134 scheduleRule_input1.setRuleId("1");
... ...