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


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.ExtractService;
import com.dituhui.pea.dispatch.service.SolveService;
import lombok.extern.slf4j.Slf4j;
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
    SolveService solveService;

    @Autowired
    ExtractService extractService;

    String groupId = "gsuzhou";
    String batchNo = "20230705-1500";


    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 test1() {

        log.info("init");

        DispatchSolution solution = solveService.prepareAndSolveSolution(groupId, batchNo);
        log.info("result:{}", solution);


        log.info("done");
    }

    @Test
    public void testAsync() throws InterruptedException {

        log.info("testAsync init");
        UUID problemId = solveService.generateProblemId(groupId, batchNo);

        log.info("testAsync problemId:{}", problemId);
        DispatchSolution problem = solveService.prepareSolution(groupId, batchNo);
        DispatchSolution solution = solver.solve(problem);
        solveService.saveSolutionWrp(solution);



       /* solverManager.solveAndListen(problemId, id -> problem,
                this.solveService::saveSolutionWrp);*/


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