public VaccinationSchedule toSchedule()

in use-cases/vaccination-scheduling/src/main/java/org/acme/vaccinationscheduler/domain/solver/VaccinationSolution.java [169:204]


    public VaccinationSchedule toSchedule() {
        Map<VaccinationSlot, List<Appointment>> appointmentListMap =
                vaccinationSlotList.stream().collect(toMap(vaccinationSlot -> vaccinationSlot,
                        // Shallow clone the appointmentList so the best solution event consumer doesn't corrupt the working solution
                        vaccinationSlot -> new ArrayList<>(vaccinationSlot.getUnscheduledAppointmentList())));
        List<Person> personList = new ArrayList<>(personAssignmentList.size());
        for (PersonAssignment personAssignment : personAssignmentList) {
            Person person = personAssignment.getPerson();
            if (!person.isPinned()) {
                VaccinationSlot vaccinationSlot = personAssignment.getVaccinationSlot();
                Appointment appointment;
                if (vaccinationSlot == null) {
                    appointment = null;
                } else {
                    List<Appointment> appointmentList = appointmentListMap.get(vaccinationSlot);
                    if (appointmentList.isEmpty()) {
                        logger.error("The solution is infeasible: the person (" + personAssignment
                                + ") is assigned to vaccinationSlot (" + vaccinationSlot
                                + ") but all the appointments are already taken, so leaving that person unassigned.\n"
                                + "Impossible situation: even if the problem has no feasible solution,"
                                + " the capacity hard constraint should force the all-but-one person to remain unassigned"
                                + " because the planning variable has nullable=true.");
                        appointment = null;
                    } else {
                        appointment = appointmentList.remove(0);
                    }
                }
                // No need to clone Person because during solving, the constraints ignore Person.appointment
                person.setAppointment(appointment);
            }
            personList.add(person);
        }
        VaccinationSchedule schedule = new VaccinationSchedule(vaccineTypeList, vaccinationCenterList, appointmentList, personList);
        schedule.setScore(score);
        return schedule;
    }