/
PlannedSurgery.java
267 lines (231 loc) · 8.28 KB
/
PlannedSurgery.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package org.openmrs.module.operationtheater.scheduler.domain;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
//import org.joda.time.DateTime;
//import org.joda.time.Interval;
//import org.joda.time.format.DateTimeFormat;
//import org.joda.time.format.DateTimeFormatter;
import org.openmrs.Location;
import org.openmrs.module.operationtheater.Procedure;
import org.openmrs.module.operationtheater.SchedulingData;
import org.openmrs.module.operationtheater.Surgery;
import org.openmrs.module.operationtheater.api.OperationTheaterService;
import org.openmrs.module.operationtheater.scheduler.solver.MovablePlannedSurgerySelectionFilter;
import org.openmrs.module.operationtheater.scheduler.solver.PlannedSurgeryDifficultyComparator;
import org.optaplanner.core.api.domain.entity.PlanningEntity;
import org.optaplanner.core.api.domain.variable.PlanningVariable;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import org.threeten.extra.Interval;
/**
* This class is the Planning entity of this optimization problem
* This means it contains attributes which will be changed by the solver during the solution finding process
* This attributes are called Planning Variables - see the @PlanningVariable annotation on the corresponding setters
* possible values of a planning variable is defined by the value range provider
*/
@PlanningEntity(movableEntitySelectionFilter = MovablePlannedSurgerySelectionFilter.class,
difficultyComparatorClass = PlannedSurgeryDifficultyComparator.class)
public class PlannedSurgery implements TimetableEntry {
private OperationTheaterService otService; //= Context.getService(OperationTheaterService.class);
//
private Surgery surgery;
// Planning variables: will change during planning (between score calculations)
private TimetableEntry previousTimetableEntry;
//shadow variable - automatically calculated if the underlying genuine planning variable (start) changes its value
//not changed by the solver
private ZonedDateTime start;
private ZonedDateTime end;
private Location location;
private TimetableEntry nextTimetableEntry;
public PlannedSurgery(OperationTheaterService otService) {
this.otService = otService;
}
public PlannedSurgery() {
}
/**
* @param other
* @return true if intervals overlap
* @should return false if any of the date object are null
* @should return if the two intervals overlap
*/
public boolean isOverlapping(PlannedSurgery other) {
if (start == null || end == null || other.start == null || other.end == null) {
return false;
}
//if Interval is constructed with a null parameter it assumes a current timestamp
return Interval.of(this.start.toInstant(), this.end.toInstant())
.overlaps(Interval.of(other.start.toInstant(), other.end.toInstant()));
}
/**
* convenient method used by drool file and helps to keep this file readable
*
* @return
* @should return true if location start or end variables are null
* @should return if current scheduling is outside available times
*/
public boolean isOutsideAvailableTimes() {
if (start == null || end == null || location == null) {
return true;
}
Interval available = otService.getLocationAvailableTime(location, start.toLocalDate());
Interval scheduled = Interval.of(start.toInstant(), end.toInstant());
Interval overlap = scheduled.intersection(available);
return !scheduled.equals(overlap);
}
public Surgery getSurgery() {
return surgery;
}
public void setSurgery(Surgery surgery) {
this.surgery = surgery;
}
@PlanningVariable(chained = true, valueRangeProviderRefs = { "anchorRange", "plannedSurgeryRange" })
public TimetableEntry getPreviousTimetableEntry() {
return previousTimetableEntry;
}
/**
* @param previousTimetableEntry
* @should set all shadow variables to null if previousTimetableEntry is null
* @should update all shadow variables
* @should set values of shadow variables correctly if surgery has been started
*/
public void setPreviousTimetableEntry(TimetableEntry previousTimetableEntry) {
this.previousTimetableEntry = previousTimetableEntry;
if (previousTimetableEntry == null) {
location = null;
start = null;
end = null;
return;
}
setStart(previousTimetableEntry.getEnd());
//if surgery has already been started correct surgery end time, because otPreparation has already been performed
if (surgery.getDateStarted() != null) {
int interventionDuration = surgery.getProcedure().getInterventionDuration();
end = ZonedDateTime.of(surgery.getDateStarted(), ZoneId.systemDefault()).plusMinutes(interventionDuration);
}
location = previousTimetableEntry.getLocation();
}
@Override
public TimetableEntry getNextTimetableEntry() {
return nextTimetableEntry;
}
@Override
public void setNextTimetableEntry(TimetableEntry nextTimeTableEntry) {
this.nextTimetableEntry = nextTimeTableEntry;
}
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
public ZonedDateTime getStart() {
return start;
}
/**
* @param start
* @should set start and update end accordingly
*/
public void setStart(ZonedDateTime start) {
setStart(start, true);
}
/**
* convenient method used for junit testing
*
* @param start
* @param calculateEndTime
*/
public void setStart(ZonedDateTime start, boolean calculateEndTime) {
this.start = start;
if (calculateEndTime) {
if (start == null) {
end = null;
} else {
int interventionDuration = surgery.getProcedure().getInterventionDuration();
int otPreparationDuration = surgery.getProcedure().getOtPreparationDuration();
ZonedDateTime endDate = start.plusMinutes(interventionDuration + otPreparationDuration);
setEnd(endDate);
}
}
}
public ZonedDateTime getEnd() {
return end;
}
public void setEnd(ZonedDateTime end) {
this.end = end;
}
/**
* @return
* @should return the entire duration this surgery occupies the ot when nextTimeTableEntry is null
* @should return value from its successor in the chain added to the entire duration this surgery occupies the ot
*/
@Override
public int getChainLengthInMinutes() {
Procedure procedure = surgery.getProcedure();
int duration = procedure.getInterventionDuration() + procedure.getOtPreparationDuration();
return nextTimetableEntry == null ? duration : nextTimetableEntry.getChainLengthInMinutes() + duration;
}
@Override
public String toString() {
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd.MM HH:mm");
String startStr = start == null ? "null " : fmt.format(start);
String endStr = end == null ? "null " : fmt.format(end);
return "\n PS {" +
"surgery=" + surgery.getUuid() +
"} -> " + previousTimetableEntry;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
} else if (o instanceof PlannedSurgery) {
PlannedSurgery other = (PlannedSurgery) o;
return new EqualsBuilder()
.append(surgery, other.surgery)
.append(previousTimetableEntry, other.previousTimetableEntry)
.append(nextTimetableEntry, other.nextTimetableEntry)
.append(start, other.start)
.append(end, other.end)
.append(location, other.location)
.isEquals();
} else {
return false;
}
}
@Override
public int hashCode() {
return new HashCodeBuilder()
.append(surgery)
.append(previousTimetableEntry)
.append(nextTimetableEntry)
.append(start)
.append(end)
.append(location)
.toHashCode();
}
/**
* updates the begin and finish times of the surgery object
* and stores it in the db
*
* @param service
* @should update schedulingData object and persist it into the db if location is not null
* @should set set start end and location fields to null if location is null
*/
public void persist(OperationTheaterService service) {
SchedulingData scheduling = surgery.getSchedulingData();
if (scheduling == null) {
scheduling = new SchedulingData();
surgery.setSchedulingData(scheduling);
}
if (location != null) {
scheduling.setStart(start.toLocalDateTime());
scheduling.setEnd(end.toLocalDateTime());
scheduling.setLocation(location);
} else {
scheduling.setStart(null);
scheduling.setEnd(null);
scheduling.setLocation(null);
}
service.saveSurgery(surgery);
}
}