org.optaplanner.examples.nurserostering.persistence.NurseRosterSolutionFileIO Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of optaplanner-examples Show documentation
Show all versions of optaplanner-examples Show documentation
OptaPlanner solves planning problems.
This lightweight, embeddable planning engine implements powerful and scalable algorithms
to optimize business resource scheduling and planning.
This module contains the examples which demonstrate how to use it in a normal Java application.
package org.optaplanner.examples.nurserostering.persistence;
import java.io.File;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.optaplanner.examples.common.persistence.AbstractJsonSolutionFileIO;
import org.optaplanner.examples.nurserostering.domain.Employee;
import org.optaplanner.examples.nurserostering.domain.NurseRoster;
import org.optaplanner.examples.nurserostering.domain.Shift;
import org.optaplanner.examples.nurserostering.domain.ShiftDate;
public class NurseRosterSolutionFileIO extends AbstractJsonSolutionFileIO {
public NurseRosterSolutionFileIO() {
super(NurseRoster.class);
}
@Override
public NurseRoster read(File inputSolutionFile) {
NurseRoster nurseRoster = super.read(inputSolutionFile);
/*
* Replace the duplicate Shift/ShiftDate instances by references to instances from the shiftList/shiftDateList.
*/
var requestsById = nurseRoster.getShiftDateList().stream()
.collect(Collectors.toMap(ShiftDate::getId, Function.identity()));
var shiftsById = nurseRoster.getShiftList().stream()
.collect(Collectors.toMap(Shift::getId, Function.identity()));
for (Employee employee : nurseRoster.getEmployeeList()) {
employee.setDayOffRequestMap(deduplicateMap(employee.getDayOffRequestMap(), requestsById, ShiftDate::getId));
employee.setDayOnRequestMap(deduplicateMap(employee.getDayOnRequestMap(), requestsById, ShiftDate::getId));
employee.setShiftOffRequestMap(deduplicateMap(employee.getShiftOffRequestMap(), shiftsById, Shift::getId));
employee.setShiftOnRequestMap(deduplicateMap(employee.getShiftOnRequestMap(), shiftsById, Shift::getId));
}
return nurseRoster;
}
}