Junjie
2 天以前 d7d7e0edf4d8dc422402be9a1fbb6e535ae3761e
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package com.zy.ai.domain.autotune;
 
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
 
public final class AutoTuneRuleDefinition {
 
    private static final String DEFAULT_RULE_NOTE = "单次调整幅度不能超过 maxStep。";
    private static final String STATION_OUT_TASK_LIMIT_NOTE =
            "单次调整幅度不能超过 maxStep;outBufferCapacity 代表出库站缓存位置数量,超过该容量可能使任务出现在主干道,仅作为风险参考,不是合法性上限。";
 
    private static final Map<String, Rule> RULE_MAP = buildRuleMap();
 
    private AutoTuneRuleDefinition() {
    }
 
    public static Rule findRule(String targetType, String targetKey) {
        if (targetType == null || targetKey == null) {
            return null;
        }
        return RULE_MAP.get(buildKey(targetType.trim(), targetKey.trim()));
    }
 
    public static Map<String, Rule> rules() {
        return RULE_MAP;
    }
 
    private static Map<String, Rule> buildRuleMap() {
        LinkedHashMap<String, Rule> ruleMap = new LinkedHashMap<>();
        add(ruleMap, rule(AutoTuneTargetType.SYS_CONFIG, "aiAutoTuneIntervalMinutes")
                .minValue(5)
                .maxValue(60)
                .maxStep(5)
                .cooldownMinutes(30));
        add(ruleMap, rule(AutoTuneTargetType.SYS_CONFIG, "conveyorStationTaskLimit")
                .minValue(5)
                .maxValue(200)
                .maxStep(5)
                .cooldownMinutes(20));
        add(ruleMap, rule(AutoTuneTargetType.SYS_CONFIG, "crnOutBatchRunningLimit")
                .minValue(1)
                .maxValue(20)
                .maxStep(3)
                .cooldownMinutes(20));
        add(ruleMap, rule(AutoTuneTargetType.STATION, "outTaskLimit")
                .minValue(0)
                .maxStep(3)
                .cooldownMinutes(10)
                .note(STATION_OUT_TASK_LIMIT_NOTE));
        add(ruleMap, rule(AutoTuneTargetType.CRN, "maxOutTask")
                .minValue(0)
                .maxValue(10)
                .maxStep(3)
                .cooldownMinutes(10));
        add(ruleMap, rule(AutoTuneTargetType.CRN, "maxInTask")
                .minValue(0)
                .maxValue(10)
                .maxStep(3)
                .cooldownMinutes(10));
        add(ruleMap, rule(AutoTuneTargetType.DUAL_CRN, "maxOutTask")
                .minValue(0)
                .maxValue(10)
                .maxStep(3)
                .cooldownMinutes(10));
        add(ruleMap, rule(AutoTuneTargetType.DUAL_CRN, "maxInTask")
                .minValue(0)
                .maxValue(10)
                .maxStep(3)
                .cooldownMinutes(10));
 
        return Collections.unmodifiableMap(ruleMap);
    }
 
    private static RuleSpec rule(AutoTuneTargetType targetType, String targetKey) {
        return new RuleSpec(targetType, targetKey);
    }
 
    private static void add(LinkedHashMap<String, Rule> ruleMap, RuleSpec ruleSpec) {
        Rule rule = ruleSpec.toRule();
        ruleMap.put(buildKey(rule.getTargetType().getCode(), rule.getTargetKey()), rule);
    }
 
    private static String buildKey(String targetType, String targetKey) {
        return targetType + ":" + targetKey;
    }
 
    public static final class Rule {
        private final AutoTuneTargetType targetType;
        private final String targetKey;
        private final Integer minValue;
        private final Integer maxValue;
        private final int maxStep;
        private final int cooldownMinutes;
        private final boolean dynamicMaxValue;
        private final String dynamicMaxSource;
        private final String note;
 
        private Rule(AutoTuneTargetType targetType,
                     String targetKey,
                     Integer minValue,
                     Integer maxValue,
                     int maxStep,
                     int cooldownMinutes,
                     boolean dynamicMaxValue,
                     String dynamicMaxSource,
                     String note) {
            this.targetType = targetType;
            this.targetKey = targetKey;
            this.minValue = minValue;
            this.maxValue = maxValue;
            this.maxStep = maxStep;
            this.cooldownMinutes = cooldownMinutes;
            this.dynamicMaxValue = dynamicMaxValue;
            this.dynamicMaxSource = dynamicMaxSource;
            this.note = note;
        }
 
        public AutoTuneTargetType getTargetType() {
            return targetType;
        }
 
        public String getTargetKey() {
            return targetKey;
        }
 
        public Integer getMinValue() {
            return minValue;
        }
 
        public Integer getMaxValue() {
            return maxValue;
        }
 
        public int getMaxStep() {
            return maxStep;
        }
 
        public int getCooldownMinutes() {
            return cooldownMinutes;
        }
 
        public boolean isDynamicMaxValue() {
            return dynamicMaxValue;
        }
 
        public String getDynamicMaxSource() {
            return dynamicMaxSource;
        }
 
        public String getNote() {
            return note;
        }
    }
 
    private static final class RuleSpec {
        private final AutoTuneTargetType targetType;
        private final String targetKey;
        private Integer minValue;
        private Integer maxValue;
        private int maxStep;
        private int cooldownMinutes;
        private String dynamicMaxSource;
        private String note = DEFAULT_RULE_NOTE;
 
        private RuleSpec(AutoTuneTargetType targetType, String targetKey) {
            this.targetType = targetType;
            this.targetKey = targetKey;
        }
 
        private RuleSpec minValue(Integer minValue) {
            this.minValue = minValue;
            return this;
        }
 
        private RuleSpec maxValue(Integer maxValue) {
            this.maxValue = maxValue;
            return this;
        }
 
        private RuleSpec maxStep(int maxStep) {
            this.maxStep = maxStep;
            return this;
        }
 
        private RuleSpec cooldownMinutes(int cooldownMinutes) {
            this.cooldownMinutes = cooldownMinutes;
            return this;
        }
 
        private RuleSpec dynamicMaxSource(String dynamicMaxSource) {
            this.dynamicMaxSource = dynamicMaxSource;
            return this;
        }
 
        private RuleSpec note(String note) {
            this.note = note;
            return this;
        }
 
        private Rule toRule() {
            return new Rule(
                    targetType,
                    targetKey,
                    minValue,
                    maxValue,
                    maxStep,
                    cooldownMinutes,
                    dynamicMaxSource != null,
                    dynamicMaxSource,
                    note
            );
        }
    }
}