kBase3_validate_rule.drl 6.58 KB
package com.bsth.service.schedule.impl.plan.kBase3.validate.rule;

import accumulate com.bsth.service.schedule.impl.plan.kBase3.validate.rule.ErrorInfoFunction srif;
import accumulate com.bsth.service.schedule.impl.plan.kBase3.validate.timetable.ErrorBcCountFunction ecount;

import org.joda.time.*;
import java.util.*;

import com.bsth.service.schedule.impl.plan.kBase3.validate.rule.CalcuParam;
import com.bsth.service.schedule.impl.plan.kBase3.validate.rule.ValidateRuleResult;
import com.bsth.service.schedule.impl.plan.kBase3.validate.rule.ValidateRuleResult.ErrorInfo;
import com.bsth.service.schedule.impl.plan.kBase3.validate.rule.WrapInput;

import com.bsth.repository.schedule.ScheduleRule1FlatRepository
import com.bsth.repository.schedule.CarConfigInfoRepository;
import com.bsth.repository.schedule.GuideboardInfoRepository;
import com.bsth.repository.schedule.EmployeeConfigInfoRepository;

import com.bsth.entity.schedule.rule.ScheduleRule1Flat;
import com.bsth.entity.schedule.CarConfigInfo;
import com.bsth.entity.schedule.GuideboardInfo;
import com.bsth.entity.schedule.EmployeeConfigInfo;

import org.slf4j.Logger

// 全局日志
global Logger LOG;

// repository查询
global CarConfigInfoRepository ccRepo;
global GuideboardInfoRepository lpRepo;
global EmployeeConfigInfoRepository ecRepo;
global ScheduleRule1FlatRepository ruleRepo;

// return输出
global ValidateRuleResult result;


//------------------ 第一阶段、车辆信息,路牌信息,人员信息载入 -----------------//

// 1、车辆配置信息载入
declare CarConfig_Wraps
    xlId: Integer // 线路Id
    ccMap: Map // 车辆配置Map Map<车咯配置Id, CarConfigInfo>
end

rule "calcu_CarConfig_Wraps"
    salience 800
    when
        $param: CalcuParam($xlId: xlId)
    then
        List ccInfos = ccRepo.findByXlId($xlId);

        CarConfig_Wraps carConfig_wraps = new CarConfig_Wraps();
        carConfig_wraps.setXlId($xlId);
        carConfig_wraps.setCcMap(new HashMap());

        for (int i = 0; i < ccInfos.size(); i++) {
            CarConfigInfo ccInfo = (CarConfigInfo) ccInfos.get(i);
            if (!ccInfo.getIsCancel()) {
                carConfig_wraps.getCcMap().put(ccInfo.getId(), ccInfo);
            }
        }

        insert(carConfig_wraps);

        LOG.info("第一阶段 --> 1、车辆配置信息载入 calcu_CarConfig_Wraps 有效配置车辆数={}", ccInfos.size());
end

// 2、路牌信息载入
declare Lp_Wraps
    xlId: Integer // 线路Id
    lpMap: Map // 路牌Map Map<id, GuideboardInfo>
end

rule "calcu_Lp_Wraps"
    salience 800
    when
        $param: CalcuParam($xlId: xlId)
    then
        List lpInfos = lpRepo.findByXlId($xlId);
        Lp_Wraps lp_wraps = new Lp_Wraps();
        lp_wraps.setXlId($xlId);
        lp_wraps.setLpMap(new HashMap());

        for (int i = 0; i < lpInfos.size(); i++) {
            GuideboardInfo lpInfo = (GuideboardInfo) lpInfos.get(i);
            if (!lpInfo.getIsCancel()) {
                lp_wraps.getLpMap().put(lpInfo.getId(), lpInfo);
            }
        }

        insert(lp_wraps);

        LOG.info("第一阶段 --> 2、路牌信息载入 calcu_Lp_Wraps 有效路牌数={}", lpInfos.size());

end

// 3、人员信息载入
declare EmployeeConfig_Wraps
    xlId: Integer // 线路Id
    ecMap: Map // 人员配置Map Map<id, EmployeeConfigInfo>
end

rule "calcu_EmployeeConfig_Wraps"
    salience 800
    when
        $param: CalcuParam($xlId: xlId)
    then
        List ecInfos = ecRepo.findByXlId($xlId);

        EmployeeConfig_Wraps employeeConfig_wraps = new EmployeeConfig_Wraps();
        employeeConfig_wraps.setXlId($xlId);
        employeeConfig_wraps.setEcMap(new HashMap());

        for (int i = 0; i < ecInfos.size(); i++) {
            EmployeeConfigInfo ecInfo = (EmployeeConfigInfo) ecInfos.get(i);
            if (!ecInfo.getIsCancel()) {
                employeeConfig_wraps.getEcMap().put(ecInfo.getId(), ecInfo);
            }
        }

        insert(employeeConfig_wraps);

        LOG.info("第一阶段 --> 3、人员信息载入 calcu_EmployeeConfig_Wraps 有效人员配置数={}", ecInfos.size());
end

//------------------ 第二阶段、规则载入,计算相关数量 -----------------//

declare Rule_Wraps
    xlId: Integer // 线路Id
    qyrq: Date // 启用日期
    rule: ScheduleRule1Flat // ScheduleRule1Flat规则
end

rule "calcu_schedule_rule_wrap"
    salience 700
    when
        $param: CalcuParam($xlId: xlId)
        $rule: ScheduleRule1Flat($qyrq: qyrq) from ruleRepo.findByXlId($xlId)
    then
        Rule_Wraps rw = new Rule_Wraps();
        rw.setXlId($xlId);
        rw.setQyrq($qyrq);
        rw.setRule($rule);
        insert(rw);
end

rule "calcu_all_rule_count"
    salience 600
    when
        $param: CalcuParam($xlId: xlId, $fd: fromDate, $td: toDate)
        $allList: ArrayList() from collect(Rule_Wraps(xlId == $xlId))
    then
        result.setXlId($xlId);
        result.setCount($allList.size());

        LOG.info("第二阶段 --> 规则总数={}", $allList.size());
end

rule "calcu_all_qy_rule_count"
    salience 500
    when
        $param: CalcuParam($xlId: xlId, $fd: fromDate, $td: toDate)
        $qyList: ArrayList() from collect(
            Rule_Wraps(xlId == $xlId, qyrq.getTime() <= $td.getMillis()))
    then
        result.setXlId($xlId);
        result.setQyCount($qyList.size());

        LOG.info("第二阶段 --> 启用规则数={}", $qyList.size());

end

//------------------ 第三阶段、规则判定 -----------------//

rule "calcu_Wrap_input"
    salience 400
    when
        $param: CalcuParam($xlId: xlId, $fd: fromDate, $td: toDate)
        $qy_rule: Rule_Wraps(xlId == $xlId, qyrq.getTime() <= $td.getMillis())
        $cc: CarConfig_Wraps(xlId == $xlId)
        $lp: Lp_Wraps(xlId == $xlId)
        $ec: EmployeeConfig_Wraps(xlId == $xlId)
    then
        WrapInput wr = new WrapInput(
            $qy_rule.getRule(),
            $cc.getCcMap(),
            $lp.getLpMap(),
            $ec.getEcMap()
        );
        insert(wr);
end

rule "calcu_error_info"
    salience 300
    when
        $param: CalcuParam($xlId: xlId)
        $errorMap: Map() from accumulate ($wr: WrapInput(xlId == $xlId), srif($wr))
    then
        result.setQyErrorCount($errorMap.size());
        result.getErrorInfos().addAll($errorMap.values());

        LOG.info("第三阶段 --> 规则总数={}, 启用规则数={}, 错误的启用规则数={}",
            result.getCount(), result.getQyCount(), result.getQyErrorCount());

end