Junjie
23 小时以前 ef776e9fd5e4f64e4ad09a3faa12fb7bb646c79c
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
package com.zy.core.thread.impl.v5;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zy.common.utils.RedisUtil;
import com.zy.core.enums.RedisKeyType;
import com.zy.core.model.command.StationCommand;
import com.zy.core.service.StationTaskLoopService;
import org.junit.jupiter.api.Test;
 
import java.util.HashSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
 
class StationV5RunBlockReroutePlannerTest {
 
    @Test
    void plan_prefersLongerCandidateWhenShortestPathIsOverusedInsideTriggeredLoop() {
        RedisUtil redisUtil = mock(RedisUtil.class);
        String stateKey = RedisKeyType.STATION_RUN_BLOCK_REROUTE_STATE_.key + "100_10";
        when(redisUtil.get(stateKey)).thenReturn(JSON.toJSONString(seedState(
                100,
                10,
                2,
                List.of(),
                Map.of("10->11->20", 2)
        )));
 
        StationV5RunBlockReroutePlanner planner = new StationV5RunBlockReroutePlanner(redisUtil);
        StationCommand shortest = moveCommand(100, 10, 20, 10, 11, 20);
        StationCommand longer = moveCommand(100, 10, 20, 10, 31, 32, 20);
 
        StationTaskLoopService.LoopIdentitySnapshot loopIdentity =
                new StationTaskLoopService.LoopIdentitySnapshot("10|11|20", new HashSet<>(List.of(10, 11, 20)), 3, 3, "wholeLoop");
        StationTaskLoopService.LoopEvaluation loopEvaluation =
                new StationTaskLoopService.LoopEvaluation(100, 10, loopIdentity, 2, 3, true);
 
        StationV5RunBlockReroutePlanner.PlanResult result = planner.plan(
                100,
                10,
                loopEvaluation,
                List.of(shortest, longer)
        );
 
        assertSame(longer, result.getCommand());
        assertEquals(3, result.getPlanCount());
    }
 
    @Test
    void plan_resetsIssuedRoutesWhenAllCandidatesHaveBeenTried() {
        RedisUtil redisUtil = mock(RedisUtil.class);
        String stateKey = RedisKeyType.STATION_RUN_BLOCK_REROUTE_STATE_.key + "100_10";
        when(redisUtil.get(stateKey)).thenReturn(JSON.toJSONString(seedState(
                100,
                10,
                1,
                List.of(List.of(10, 11, 20), List.of(10, 31, 32, 20)),
                Map.of("10->11->20", 1, "10->31->32->20", 1)
        )));
 
        StationV5RunBlockReroutePlanner planner = new StationV5RunBlockReroutePlanner(redisUtil);
        StationCommand first = moveCommand(100, 10, 20, 10, 11, 20);
        StationCommand second = moveCommand(100, 10, 20, 10, 31, 32, 20);
 
        StationTaskLoopService.LoopEvaluation loopEvaluation =
                new StationTaskLoopService.LoopEvaluation(100, 10, StationTaskLoopService.LoopIdentitySnapshot.empty(), 0, 0, false);
 
        StationV5RunBlockReroutePlanner.PlanResult result = planner.plan(
                100,
                10,
                loopEvaluation,
                List.of(first, second)
        );
 
        assertSame(first, result.getCommand());
        assertEquals(2, result.getPlanCount());
    }
 
    private static StationCommand moveCommand(Integer taskNo,
                                              Integer stationId,
                                              Integer targetStationId,
                                              Integer... path) {
        StationCommand command = new StationCommand();
        command.setTaskNo(taskNo);
        command.setStationId(stationId);
        command.setTargetStaNo(targetStationId);
        command.setNavigatePath(List.of(path));
        return command;
    }
 
    private static JSONObject seedState(Integer taskNo,
                                        Integer blockStationId,
                                        Integer planCount,
                                        List<List<Integer>> issuedRoutePathList,
                                        Map<String, Integer> routeIssueCountMap) {
        JSONObject state = new JSONObject();
        state.put("taskNo", taskNo);
        state.put("blockStationId", blockStationId);
        state.put("planCount", planCount);
        state.put("issuedRoutePathList", issuedRoutePathList);
        state.put("routeIssueCountMap", new HashMap<>(routeIssueCountMap));
        return state;
    }
}