All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.drools.planner.examples.nurserostering.domain.NurseRoster Maven / Gradle / Ivy

Go to download

Drools Planner optimizes automated planning by combining metaheuristic search algorithms with rule engine powered score calculation. This is the drools-planner-examples module which contains examples on how to use Drools Planner.

There is a newer version: 6.0.0.Alpha9
Show newest version
/*
 * Copyright 2010 JBoss Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.drools.planner.examples.nurserostering.domain;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.drools.planner.api.domain.solution.PlanningEntityCollectionProperty;
import org.drools.planner.core.score.HardAndSoftScore;
import org.drools.planner.core.solution.Solution;
import org.drools.planner.examples.common.domain.AbstractPersistable;
import org.drools.planner.examples.nurserostering.domain.contract.Contract;
import org.drools.planner.examples.nurserostering.domain.contract.ContractLine;
import org.drools.planner.examples.nurserostering.domain.contract.PatternContractLine;
import org.drools.planner.examples.nurserostering.domain.request.DayOffRequest;
import org.drools.planner.examples.nurserostering.domain.request.DayOnRequest;
import org.drools.planner.examples.nurserostering.domain.request.ShiftOffRequest;
import org.drools.planner.examples.nurserostering.domain.request.ShiftOnRequest;

@XStreamAlias("NurseRoster")
public class NurseRoster extends AbstractPersistable implements Solution {

    private String code;

    private List skillList;
    private List shiftTypeList;
    private List shiftTypeSkillRequirementList;
    private List patternList;
    private List contractList;
    private List contractLineList;
    private List patternContractLineList;
    private List employeeList;
    private List skillProficiencyList;
    private List shiftDateList;
    private List shiftList;
    private List dayOffRequestList;
    private List dayOnRequestList;
    private List shiftOffRequestList;
    private List shiftOnRequestList;

    private List assignmentList;

    private HardAndSoftScore score;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public List getSkillList() {
        return skillList;
    }

    public void setSkillList(List skillList) {
        this.skillList = skillList;
    }

    public List getShiftTypeList() {
        return shiftTypeList;
    }

    public void setShiftTypeList(List shiftTypeList) {
        this.shiftTypeList = shiftTypeList;
    }

    public List getShiftTypeSkillRequirementList() {
        return shiftTypeSkillRequirementList;
    }

    public void setShiftTypeSkillRequirementList(List shiftTypeSkillRequirementList) {
        this.shiftTypeSkillRequirementList = shiftTypeSkillRequirementList;
    }

    public List getPatternList() {
        return patternList;
    }

    public void setPatternList(List patternList) {
        this.patternList = patternList;
    }

    public List getContractList() {
        return contractList;
    }

    public void setContractList(List contractList) {
        this.contractList = contractList;
    }

    public List getContractLineList() {
        return contractLineList;
    }

    public void setContractLineList(List contractLineList) {
        this.contractLineList = contractLineList;
    }

    public List getPatternContractLineList() {
        return patternContractLineList;
    }

    public void setPatternContractLineList(List patternContractLineList) {
        this.patternContractLineList = patternContractLineList;
    }

    public List getEmployeeList() {
        return employeeList;
    }

    public void setEmployeeList(List employeeList) {
        this.employeeList = employeeList;
    }

    public List getSkillProficiencyList() {
        return skillProficiencyList;
    }

    public void setSkillProficiencyList(List skillProficiencyList) {
        this.skillProficiencyList = skillProficiencyList;
    }

    public List getShiftDateList() {
        return shiftDateList;
    }

    public void setShiftDateList(List shiftDateList) {
        this.shiftDateList = shiftDateList;
    }

    public List getShiftList() {
        return shiftList;
    }

    public void setShiftList(List shiftList) {
        this.shiftList = shiftList;
    }

    public List getDayOffRequestList() {
        return dayOffRequestList;
    }

    public void setDayOffRequestList(List dayOffRequestList) {
        this.dayOffRequestList = dayOffRequestList;
    }

    public List getDayOnRequestList() {
        return dayOnRequestList;
    }

    public void setDayOnRequestList(List dayOnRequestList) {
        this.dayOnRequestList = dayOnRequestList;
    }

    public List getShiftOffRequestList() {
        return shiftOffRequestList;
    }

    public void setShiftOffRequestList(List shiftOffRequestList) {
        this.shiftOffRequestList = shiftOffRequestList;
    }

    public List getShiftOnRequestList() {
        return shiftOnRequestList;
    }

    public void setShiftOnRequestList(List shiftOnRequestList) {
        this.shiftOnRequestList = shiftOnRequestList;
    }

    @PlanningEntityCollectionProperty
    public List getAssignmentList() {
        return assignmentList;
    }

    public void setAssignmentList(List assignmentList) {
        this.assignmentList = assignmentList;
    }

    public HardAndSoftScore getScore() {
        return score;
    }

    public void setScore(HardAndSoftScore score) {
        this.score = score;
    }

    public Collection getProblemFacts() {
        List facts = new ArrayList();
        // TODO add RosterInfo as a property on NurseRoster
        facts.add(new RosterInfo(shiftDateList.get(0), shiftDateList.get(shiftDateList.size() - 1)));
        facts.addAll(skillList);
        facts.addAll(shiftTypeList);
        facts.addAll(shiftTypeSkillRequirementList);
        facts.addAll(patternList);
        facts.addAll(contractList);
        facts.addAll(contractLineList);
        facts.addAll(patternContractLineList);
        facts.addAll(employeeList);
        facts.addAll(skillProficiencyList);
        facts.addAll(shiftDateList);
        facts.addAll(shiftList);
        facts.addAll(dayOffRequestList);
        facts.addAll(dayOnRequestList);
        facts.addAll(shiftOffRequestList);
        facts.addAll(shiftOnRequestList);
        // Do not add the planning entity's (assignmentList) because that will be done automatically
        return facts;
    }

    /**
     * Clone will only deep copy the {@link #assignmentList}.
     */
    public NurseRoster cloneSolution() {
        NurseRoster clone = new NurseRoster();
        clone.id = id;
        clone.code = code;
        clone.skillList = skillList;
        clone.shiftTypeList = shiftTypeList;
        clone.shiftTypeSkillRequirementList = shiftTypeSkillRequirementList;
        clone.patternList = patternList;
        clone.contractList = contractList;
        clone.contractLineList = contractLineList;
        clone.patternContractLineList = patternContractLineList;
        clone.employeeList = employeeList;
        clone.skillProficiencyList = skillProficiencyList;
        clone.shiftDateList = shiftDateList;
        clone.shiftList = shiftList;
        clone.dayOffRequestList = dayOffRequestList;
        clone.dayOnRequestList = dayOnRequestList;
        clone.shiftOffRequestList = shiftOffRequestList;
        clone.shiftOnRequestList = shiftOnRequestList;
        List clonedAssignmentList = new ArrayList(
                assignmentList.size());
        for (Assignment assignment : assignmentList) {
            Assignment clonedAssignment = assignment.clone();
            clonedAssignmentList.add(clonedAssignment);
        }
        clone.assignmentList = clonedAssignmentList;
        clone.score = score;
        return clone;
    }

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (id == null || !(o instanceof NurseRoster)) {
            return false;
        } else {
            NurseRoster other = (NurseRoster) o;
            if (assignmentList.size() != other.assignmentList.size()) {
                return false;
            }
            for (Iterator it = assignmentList.iterator(), otherIt = other.assignmentList.iterator(); it.hasNext();) {
                Assignment assignment = it.next();
                Assignment otherAssignment = otherIt.next();
                // Notice: we don't use equals()
                if (!assignment.solutionEquals(otherAssignment)) {
                    return false;
                }
            }
            return true;
        }
    }

    public int hashCode() {
        HashCodeBuilder hashCodeBuilder = new HashCodeBuilder();
        for (Assignment assignment : assignmentList) {
            // Notice: we don't use hashCode()
            hashCodeBuilder.append(assignment.solutionHashCode());
        }
        return hashCodeBuilder.toHashCode();
    }

}