DroolsSchedulePlan.java 21.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
package com.bsth.service.schedule.plan;

import com.bsth.entity.Line;
import com.bsth.entity.schedule.SchedulePlan;
import com.bsth.entity.schedule.SchedulePlanInfo;
import com.bsth.entity.schedule.TTInfo;
import com.bsth.entity.schedule.rule.ScheduleRule1Flat;
import com.bsth.repository.BusinessRepository;
import com.bsth.repository.LineRepository;
import com.bsth.repository.schedule.*;
import com.bsth.service.schedule.rules.ScheduleRuleService;
import com.bsth.service.schedule.rules.plan.PlanCalcuParam_input;
import com.bsth.service.schedule.rules.plan.PlanResult;
import com.bsth.service.schedule.rules.rerun.RerunRule_input;
import com.bsth.service.schedule.rules.rerun.RerunRule_param;
import com.bsth.service.schedule.rules.shiftloop.ScheduleCalcuParam_input;
import com.bsth.service.schedule.rules.shiftloop.ScheduleResults_output;
import com.bsth.service.schedule.rules.shiftloop.ScheduleRule_input;
import com.bsth.service.schedule.rules.ttinfo.*;
import com.bsth.service.schedule.rules.validate.ValidateParam;
import com.bsth.service.schedule.rules.validate.ValidateResults_output;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.kie.api.KieBase;
import org.kie.api.runtime.KieSession;
import org.slf4j.Logger;

import java.util.*;

/**
 * 排班计划(使用Drools)。
 */
public class DroolsSchedulePlan {
    /** 主线路 */
    private Line mainLine;
    /** 套跑辅线路规则 */
    private List<RerunRule_input> rerunRule_inputs;

    /** 开始排班时间 */
    private Date from;
    /** 结束排班时间 */
    private Date to;
    /** 排班计划entity */
    private SchedulePlan schedulePlan;

    //-------------------- 相关的Repo,service服务 --------------------//
    /** 线路Repo */
    private LineRepository lineRepository;
    /** 排班规则Repo */
    private ScheduleRule1FlatRepository scheduleRule1FlatRepository;
    /** 时刻表Repo */
    private TTInfoRepository ttInfoRepository;
    /** 时刻表明细Repo */
    private TTInfoDetailRepository ttInfoDetailRepository;
    /** 车辆配置Repo */
    private CarConfigInfoRepository carConfigInfoRepository;
    /** 人员配置Repo */
    private EmployeeConfigInfoRepository employeeConfigInfoRepository;
    /** 套跑规则Repo */
    private RerunRuleRepository rerunRuleRepository;
    /** 营运状态Repo */
    private BusinessRepository businessRepository;

    /** 排班规则service */
    private ScheduleRuleService scheduleRuleService;

    //-------------------- Drools KBase实例 ------------------//
    /** 排班预处理KBase */
    private KieBase preKBase;
    /** 排班核心KBase */
    private KieBase coreKBase;

    //-------------------- 日志记录器 ---------------------//
    private Logger logger;

    public DroolsSchedulePlan(
            SchedulePlan schedulePlan,
            LineRepository lineRepository, ScheduleRule1FlatRepository scheduleRule1FlatRepository,
            TTInfoRepository ttInfoRepository, TTInfoDetailRepository ttInfoDetailRepository,
            CarConfigInfoRepository carConfigInfoRepository,
            EmployeeConfigInfoRepository employeeConfigInfoRepository,
            RerunRuleRepository rerunRuleRepository,
            BusinessRepository businessRepository,
            ScheduleRuleService scheduleRuleService,
            KieBase preKBase, KieBase coreKBase,
            Logger logger) {

        // 验证SchedulePlan实体
        if (schedulePlan == null) {
            throw new RuntimeException("排班用SchedulePlan为空!");
        }
        if (schedulePlan.getXl() == null) {
            throw new RuntimeException("排班线路为空!");
        } else {
            // 获取主线路
            this.mainLine = lineRepository.findOne(schedulePlan.getXl().getId());
            if (this.mainLine == null) {
                throw new RuntimeException("线路id=" + schedulePlan.getXl().getId() + "不存在!");
            }
            // 获取主线路套跑信息
            this.rerunRule_inputs = scheduleRuleService.findRerunrule(this.mainLine.getId());
        }
        if (schedulePlan.getScheduleFromTime() == null) {
            throw new RuntimeException("排班开始时间为空!");
        }
        if (schedulePlan.getScheduleToTime() == null) {
            throw new RuntimeException("排班结束时间为空!");
        }
        this.from = schedulePlan.getScheduleFromTime();
        this.to = schedulePlan.getScheduleToTime();
        if (schedulePlan.getTtInfoIds() == null) {
            throw new RuntimeException("排班关联的时刻表ids为空!");
        }
        if (schedulePlan.getTtInfoNames() == null) {
            throw new RuntimeException("排班关联的时刻表名字s为空!");
        }
        this.schedulePlan = schedulePlan;

        this.lineRepository = lineRepository;
        this.scheduleRule1FlatRepository = scheduleRule1FlatRepository;
        this.ttInfoRepository = ttInfoRepository;
        this.ttInfoDetailRepository = ttInfoDetailRepository;
        this.carConfigInfoRepository = carConfigInfoRepository;
        this.employeeConfigInfoRepository = employeeConfigInfoRepository;
        this.rerunRuleRepository = rerunRuleRepository;
        this.businessRepository = businessRepository;
        this.scheduleRuleService = scheduleRuleService;
        this.preKBase = preKBase;
        this.coreKBase = coreKBase;

        this.logger = logger;

    }

    public void generatePlan() {
        logger.info("<--- 排班master线路 id={}, name={}, 开始排班",
                this.mainLine.getId(), this.mainLine.getName());

        // 1、确定主线路排班(包含完全套跑路牌规则,所谓完全套跑路牌规则指整个路牌的班次都是套跑规则指定的)
        PlanResult planResult = this.schedulePlanWithOutRerun();

        // 2、确定套跑规则
        this.rerunPlanResult(planResult);

        // TODO:3-1、验证排班结果
        this.validPlanResult(planResult);

        // TODO:3-2、去除完全套跑遗漏班次(以后放到规则中执行)
        Iterator<SchedulePlanInfo> infoIterator = planResult.getSchedulePlanInfos().iterator();
        while (infoIterator.hasNext()) {
            SchedulePlanInfo schedulePlanInfo = infoIterator.next();
            if (schedulePlanInfo.getCl() == null) {
                infoIterator.remove();
            }
        }

        // 4、保存数据(jdbcTemplate 批量插入)
        Date start4 = new Date();
        this.scheduleRuleService.generateSchedulePlan(
                this.schedulePlan, planResult.getSchedulePlanInfos());
        Date end4 = new Date();

        this.logger.info("保存主线路数据 {} 条 耗时 {} ms --->",
                planResult.getSchedulePlanInfos().size(),
                end4.getTime() - start4.getTime());
    }

    /**
     * 计算规则输入。
     * @return
     */
    private List<ScheduleRule_input> calcuSrfList(Line line) {
        // 1-1、构造drools规则输入数据,输出数据
        // 全局计算参数
        SchedulePlan schedulePlan = new SchedulePlan();
        schedulePlan.setXl(line);
        schedulePlan.setScheduleFromTime(this.from);
        schedulePlan.setScheduleToTime(this.to);
        ScheduleCalcuParam_input scheduleCalcuParam_input = new ScheduleCalcuParam_input(schedulePlan);

        // 规则输出数据
        List<ScheduleRule_input> scheduleRule_inputs = new ArrayList<>();

        // 1-2、构造drools session->载入数据->启动规则->计算->销毁session
        // 创建session,内部配置的是stateful
        KieSession session = preKBase.newKieSession();
        // 设置gloable对象,在drl中通过别名使用
        session.setGlobal("sriList", scheduleRule_inputs);
        session.setGlobal("log", logger); // 设置日志

        session.setGlobal("srf", scheduleRule1FlatRepository);
        session.setGlobal("rrr", rerunRuleRepository);
        session.setGlobal("srservice", scheduleRuleService);

        // 载入数据
        session.insert(scheduleCalcuParam_input);

        // 执行rule
        session.fireAllRules();

        // 执行完毕销毁,有日志的也要关闭
        session.dispose();

        return scheduleRule_inputs;
    }

    /**
     * 时刻表选择(判定每天使用的时刻表,以及路牌数据输出)。
     * @return [TTInfoResults_output, LpInfoResults_output]
     */
    private Object[] ttInfoOutput(Line line) {
        // 获取线路的所有未作废的时刻表
        List<TTInfo> ttInfos = ttInfoRepository.findInCanceledByXl(line);

        // 1-1、构造drools规则输入数据,输出数据
        // 全局计算参数
        TTInfoCalcuParam_input ttInfoCalcuParam_input =
                new TTInfoCalcuParam_input(
                        new DateTime(this.from),
                        new DateTime(this.to),
                        String.valueOf(line.getId())
                );
        // 规则输出数据
        TTInfoResults_output ttInfoResults_output = new TTInfoResults_output();
        LpInfoResults_output lpInfoResults_output = new LpInfoResults_output();

        // 1-2、构造drools session->载入数据->启动规则->计算->销毁session
        // 创建session,内部配置的是stateful
        KieSession session = coreKBase.newKieSession();

        // 设置gloable对象,在drl中通过别名使用
        session.setGlobal("results", ttInfoResults_output);
        session.setGlobal("lpInfoResults_output", lpInfoResults_output);
        session.setGlobal("log", logger); // 设置日志
        session.setGlobal("tTInfoDetailRepository", ttInfoDetailRepository);

        // 载入数据
        session.insert(ttInfoCalcuParam_input);
        for (TTInfo ttInfo : ttInfos) {
            TTInfo_input ttInfo_input = new TTInfo_input(ttInfo);
            session.insert(ttInfo_input);
        }

        // 载入数据2(计算规则最早启用时间)
        List<ScheduleRule1Flat> scheduleRule1Flats = scheduleRule1FlatRepository.findByXl(line);

        for (ScheduleRule1Flat scheduleRule1Flat: scheduleRule1Flats) {
            ScheduleRule_input scheduleRule_input = new ScheduleRule_input(scheduleRule1Flat);
            session.insert(scheduleRule_input);
        }

        // 执行rule
        session.fireAllRules();

        // 执行完毕销毁,有日志的也要关闭
        session.dispose();

        return new Object[] {ttInfoResults_output, lpInfoResults_output};

    }

    /**
     * 循环规则输出。
     * @param lpInfoResults_output 时刻表每日路牌的情况
     */
    private ScheduleResults_output loopRuleOutput(Line line, LpInfoResults_output lpInfoResults_output) {
        // 1-1、构造drools规则输入数据,输出数据
        // 全局计算参数
        SchedulePlan schedulePlan = new SchedulePlan();
        schedulePlan.setXl(line);
        schedulePlan.setScheduleFromTime(this.from);
        schedulePlan.setScheduleToTime(this.to);
        schedulePlan.setIsHistoryPlanFirst(this.schedulePlan.getIsHistoryPlanFirst());
        schedulePlan.setCreateBy(this.schedulePlan.getCreateBy());
        ScheduleCalcuParam_input scheduleCalcuParam_input = new ScheduleCalcuParam_input(schedulePlan);
        // 每个规则对应的输入参数
        List<ScheduleRule_input> scheduleRule_inputs = this.calcuSrfList(line);

        // 规则输出数据
        ScheduleResults_output scheduleResults_output = new ScheduleResults_output();

        // 1-2、构造drools session->载入数据->启动规则->计算->销毁session
        // 创建session,内部配置的是stateful
        KieSession session = coreKBase.newKieSession();
        // 设置gloable对象,在drl中通过别名使用
        session.setGlobal("scheduleResult", scheduleResults_output);
        session.setGlobal("log", logger); // 设置日志
        session.setGlobal("scheduleRuleService", scheduleRuleService);

        // 载入数据
        session.insert(scheduleCalcuParam_input);
        for (ScheduleRule_input scheduleRule_input : scheduleRule_inputs) {
            session.insert(scheduleRule_input);
        }
        // 每日时刻表路牌数据
        for (LpInfoResult_output lpInfoResult_output: lpInfoResults_output.getLpInfoResult_outputs()) {
            session.insert(lpInfoResult_output);
        }
        // 执行rule
        session.fireAllRules();

        // 执行完毕销毁,有日志的也要关闭
        session.dispose();

        // 保存循环规则结果数据
        scheduleRuleService.generateRuleResult(scheduleResults_output.getSchedulePlanRuleResults());

//        logger.info("循环规则输出={}", scheduleResults_output.showGuideboardDesc1());

        return scheduleResults_output;
    }

    /**
     * 排班生成。
     * @param scheduleResults_output loopRuleOutput方法规则输出
     * @param ttInfoResults_output ttInfoOutput方法规则输出
     * @return PlanResult
     */
    private PlanResult planResultOutput(
            Line line,
            ScheduleResults_output scheduleResults_output,
            TTInfoResults_output ttInfoResults_output) {

        SchedulePlan schedulePlan = new SchedulePlan();
        schedulePlan.setXl(line);
        schedulePlan.setScheduleFromTime(this.from);
        schedulePlan.setScheduleToTime(this.to);
        schedulePlan.setCreateBy(this.schedulePlan.getCreateBy());
        schedulePlan.setUpdateBy(this.schedulePlan.getUpdateBy());

        // 1-1、构造drools规则输入数据,输出数据
        PlanCalcuParam_input planCalcuParam_input = new PlanCalcuParam_input(
                schedulePlan,
                scheduleResults_output,
                ttInfoResults_output
        );
        // 规则输出数据
        PlanResult planResult = new PlanResult();
        planResult.setXlId(schedulePlan.getXl().getId().toString());

        // 1-2、构造drools session->载入数据->启动规则->计算->销毁session
        // 创建session,内部配置的是stateful
        KieSession session = this.coreKBase.newKieSession();

        // 设置gloable对象,在drl中通过别名使用
        session.setGlobal("planResult", planResult);
        session.setGlobal("log", this.logger); // 设置日志

        session.setGlobal("tTInfoDetailRepository", this.ttInfoDetailRepository);
        session.setGlobal("carConfigInfoRepository", this.carConfigInfoRepository);
        session.setGlobal("employeeConfigInfoRepository", this.employeeConfigInfoRepository);
        session.setGlobal("lineRepository", this.lineRepository);
        session.setGlobal("businessRepository", this.businessRepository);

        // 载入数据
        session.insert(planCalcuParam_input);

        // 执行rule
        session.fireAllRules();

        // 执行完毕销毁,有日志的也要关闭
        session.dispose();

        return planResult;

    }

    /**
     * 生成线路排班(不含套跑规则)。
     * @param schedulePlan
     * @return
     */
    private PlanResult schedulePlanWithOutRerun() {
        // 1、时刻表数据及每日路牌数据计算
        Date start1 = new Date();
        Object[] ttInfoRets = this.ttInfoOutput(this.mainLine);
        TTInfoResults_output ttInfoResults_output = (TTInfoResults_output) ttInfoRets[0];
        LpInfoResults_output lpInfoResults_output = (LpInfoResults_output) ttInfoRets[1];
        Date end1 = new Date();
        // 2、循环规则计算输出
        Date start2 = new Date();
        ScheduleResults_output scheduleResults_output = this.loopRuleOutput(
                this.mainLine, lpInfoResults_output);
        Date end2 = new Date();

        logger.info("规则计算结果={}", scheduleResults_output.showGuideboardDesc1());

        // 3、计划输出
        Date start3 = new Date();
        PlanResult planResult = planResultOutput(
                this.mainLine, scheduleResults_output, ttInfoResults_output);
        Date end3 = new Date();

        logger.info("drool时刻表每日路牌计算 {} ms,drool循环规则计算 {} ms,drool计划数据 {} ms",
                end1.getTime() - start1.getTime(),
                end2.getTime() - start2.getTime(),
                end3.getTime() - start3.getTime());

        // TODO:将lpInfoResults_output 也要返回

        return planResult;
    }

    /**
     * 套跑计划排班数据。
     * @param planResult
     */
    private void rerunPlanResult(PlanResult planResult) {
        logger.info("套跑数量 {} 组", this.rerunRule_inputs.size());

        if (this.rerunRule_inputs.size() > 0) {
            // 找出是对应路牌类型的线路,计算循环规则输出
            Set<String> dylpxlids = new HashSet<>();
            for (RerunRule_input rerunRule_input: rerunRule_inputs) {
                if ("dylp".equals(rerunRule_input.getType())) {
                    dylpxlids.add(rerunRule_input.getS_xl()); // 参与套跑的线路
                }
            }

            List<ScheduleResults_output> scheduleResults_outputs = new ArrayList<>();
            Date start1 = new Date();
            for (String xlid: dylpxlids) {
                Line dylpline = new Line(); // 套跑的线路默认跟着主线路设定走
                dylpline.setId(Integer.parseInt(xlid));
                // 获取套跑线路的循环规则计算输出
                Object[] ttInfoRets = this.ttInfoOutput(dylpline);
                LpInfoResults_output lpInfoResults_output = (LpInfoResults_output) ttInfoRets[1];
                ScheduleResults_output scheduleResults_output = this.loopRuleOutput(
                        dylpline, lpInfoResults_output);
                scheduleResults_outputs.add(scheduleResults_output);
            }

            // 1-2、构造drools session->载入数据->启动规则->计算->销毁session
            // 创建session,内部配置的是stateful
            KieSession session = this.coreKBase.newKieSession();

            // 设置gloable对象,在drl中通过别名使用
            session.setGlobal("planResult", planResult);
            session.setGlobal("log", this.logger); // 设置日志

            session.setGlobal("carConfigInfoRepository", this.carConfigInfoRepository);
            session.setGlobal("employeeConfigInfoRepository", this.employeeConfigInfoRepository);

            // 载入数据
            RerunRule_param rerunRule_param = new RerunRule_param();
            rerunRule_param.setMxlid(planResult.getXlId());
            rerunRule_param.setXlIds_dylp(dylpxlids);
            session.insert(rerunRule_param);
            for (RerunRule_input rri: this.rerunRule_inputs) {
                session.insert(rri);
            }
            for (SchedulePlanInfo spi: planResult.getSchedulePlanInfos()) {
                session.insert(spi);
            }
            for (ScheduleResults_output sro: scheduleResults_outputs) {
                session.insert(sro);
            }

            // 执行rule
            session.fireAllRules();

            // 执行完毕销毁,有日志的也要关闭
            session.dispose();

            Date end1 = new Date();
            logger.info("套跑规则计算,耗时 {} ms", end1.getTime() - start1.getTime());

        }

    }

    /**
     * 验证排班结果。
     * @param planResult
     * @param schedulePlan
     */
    public void validPlanResult(PlanResult planResult) {
        // 1-0、获取路牌信息
        LpInfoResults_output lpInfoResults_output = (LpInfoResults_output) this.ttInfoOutput(this.mainLine)[1];

        // 1-1、构造drools规则输入数据,输出数据
        ValidateParam validateParam = new ValidateParam(
                new DateTime(this.from), new DateTime(this.to));
        // 规则输出数据
        ValidateResults_output result = new ValidateResults_output();

        // 1-2、构造drools session->载入数据->启动规则->计算->销毁session
        // 创建session,内部配置的是stateful
        KieSession session = this.coreKBase.newKieSession();

        // 设置gloable对象,在drl中通过别名使用
        session.setGlobal("validResult", result);
        session.setGlobal("log", this.logger); // 设置日志

        // 载入数据
        session.insert(validateParam);
        for (SchedulePlanInfo schedulePlanInfo: planResult.getSchedulePlanInfos()) {
            session.insert(schedulePlanInfo);
        }
        for (LpInfoResult_output lpInfoResult_output: lpInfoResults_output.getLpInfoResult_outputs()) {
            session.insert(lpInfoResult_output);
        }

        // 执行rule
        session.fireAllRules();

        // 执行完毕销毁,有日志的也要关闭
        session.dispose();

//        logger.info("错误总数={}", result.getInfos().size());
//        for (ValidateResults_output.ValidInfo validInfo: result.getInfos()) {
//            logger.info(validInfo.getDesc());
//        }

        // 取10条错误
        int size = result.getInfos().size() > 10 ? 10: result.getInfos().size();
        List<String> desclist = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            desclist.add(result.getInfos().get(i).getDesc());
        }
        if (desclist.size() > 0) {
            this.schedulePlan.setPlanResult(StringUtils.join(desclist, "</br>"));
        } else {
            this.schedulePlan.setPlanResult("ok");
        }

        // TODO:设定错误信息
    }

}