SolveServiceTest.java 2.84 KB
package com.dituhui.pea.dispatch;


import com.dituhui.pea.common.Result;
import com.dituhui.pea.dispatch.constraint.DispatchConstraintProvider;
import com.dituhui.pea.dispatch.pojo.Customer;
import com.dituhui.pea.dispatch.pojo.DispatchSolution;
import com.dituhui.pea.dispatch.pojo.Technician;
import com.dituhui.pea.dispatch.service.BatchService;
import com.dituhui.pea.dispatch.service.ExtractService;
import com.dituhui.pea.dispatch.service.SolveService;
import lombok.extern.slf4j.Slf4j;
import org.drools.commands.fluent.Batch;
import org.junit.jupiter.api.Test;
import org.optaplanner.core.api.solver.Solver;
import org.optaplanner.core.api.solver.SolverFactory;
import org.optaplanner.core.api.solver.SolverManager;
import org.optaplanner.core.config.solver.SolverConfig;
import org.optaplanner.core.config.solver.SolverManagerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.Duration;
import java.util.Arrays;
import java.util.UUID;

import static java.lang.Thread.sleep;

@Slf4j
@SpringBootTest
class SolveServiceTest {

    @Autowired
    BatchService batchService;

    @Autowired
    SolveService solveService;

    @Autowired
    ExtractService extractService;

    String groupId = "gsuzhou";
    String day = "2023-03-20";


    private SolverManager<DispatchSolution, UUID> solverManager;
    private Solver<DispatchSolution> solver;

    public SolveServiceTest() {

        SolverConfig solverConfig = new SolverConfig().withSolutionClass(DispatchSolution.class);
        solverConfig.withEntityClassList(Arrays.asList(Technician.class, Customer.class));// 这里不能漏掉,否则约束不生效
        solverConfig.withConstraintProviderClass(DispatchConstraintProvider.class);
        solverConfig.withTerminationSpentLimit(Duration.ofSeconds(10));

        SolverFactory<DispatchSolution> solverFactory = SolverFactory.create(solverConfig);
        solver = solverFactory.buildSolver();
        solverManager = SolverManager.create(solverConfig, new SolverManagerConfig());
    }


    @Test
    public void testAsync() throws InterruptedException {

        log.info("testAsync init");

        String batchNo = batchService.buildBatchData(groupId, day);

        log.info("调用引擎处理, groupId:{}, day:{}, batchNo:{}", groupId, day, batchNo);

        DispatchSolution problem = solveService.prepareSolution(groupId, batchNo);
        if (problem.getCustomerList().size() <= 0) {
            log.info("调用引擎处理, 没有待指派工单 , group:{}, day:{}, batch:{},  order-size:{}", groupId, day, batchNo, 0);
            return;
        }

        DispatchSolution solution = solver.solve(problem);
        solveService.saveSolutionWrp(solution);
        extractService.extractDispatchToOrder(groupId, batchNo, false);


        log.info("testAsync done");
    }
}