SolveServiceTest.java
2.35 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
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.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;
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));
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);
solverManager.solveAndListen(problemId, id -> problem,
this.extractService::saveAndExtractSolution);
sleep(10*60*1000);
log.info("testAsync done");
}
}