/
ResponseTimeUpdatingVariableListener.java
64 lines (49 loc) · 2.28 KB
/
ResponseTimeUpdatingVariableListener.java
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
package org.acme.callcenter.solver;
import java.time.Duration;
import org.acme.callcenter.domain.Call;
import org.acme.callcenter.domain.CallCenter;
import org.acme.callcenter.domain.PreviousCallOrAgent;
import org.optaplanner.core.api.domain.variable.VariableListener;
import org.optaplanner.core.api.score.director.ScoreDirector;
public class ResponseTimeUpdatingVariableListener implements VariableListener<CallCenter, Call> {
@Override
public void beforeEntityAdded(ScoreDirector<CallCenter> scoreDirector, Call call) {
}
@Override
public void afterEntityAdded(ScoreDirector<CallCenter> scoreDirector, Call call) {
updateResponseTime(scoreDirector, call);
}
@Override
public void beforeVariableChanged(ScoreDirector<CallCenter> scoreDirector, Call call) {
}
@Override
public void afterVariableChanged(ScoreDirector<CallCenter> scoreDirector, Call call) {
updateResponseTime(scoreDirector, call);
}
@Override
public void beforeEntityRemoved(ScoreDirector<CallCenter> scoreDirector, Call call) {
}
@Override
public void afterEntityRemoved(ScoreDirector<CallCenter> scoreDirector, Call call) {
}
protected void updateResponseTime(ScoreDirector<CallCenter> scoreDirector, Call call) {
PreviousCallOrAgent previous = call.getPreviousCallOrAgent();
Call shadowCall = call;
Duration previousDurationTillPickUp = (previous == null ? null : previous.getDurationTillPickUp());
Duration estimatedWaiting = calculateWaitingTimeEstimate(shadowCall, previousDurationTillPickUp);
while (shadowCall != null) {
scoreDirector.beforeVariableChanged(shadowCall, "estimatedWaiting");
shadowCall.setEstimatedWaiting(estimatedWaiting);
scoreDirector.afterVariableChanged(shadowCall, "estimatedWaiting");
previousDurationTillPickUp = shadowCall.getDurationTillPickUp();
shadowCall = shadowCall.getNextCall();
estimatedWaiting = calculateWaitingTimeEstimate(shadowCall, previousDurationTillPickUp);
}
}
private Duration calculateWaitingTimeEstimate(Call call, Duration previousEndTime) {
if (call == null || previousEndTime == null) {
return null;
}
return previousEndTime;
}
}