/*
* Copyright 2009 Peter Karich, peat_hal 'at' users 'dot' sourceforge 'dot' net.
*
* 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.
* under the License.
*/
package de.timefinder.algo.constraint;
import de.timefinder.data.algo.DataPoolSettings;
import de.timefinder.data.Event;
import de.timefinder.data.algo.Assignment;
import de.timefinder.data.algo.Constraint;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
/**
* This constraint tries to force that the added events
* won't be scheduled on the same day
*
* @author Peter Karich, peat_hal 'at' users 'dot' sourceforge 'dot' net
*/
public class DifferentDayConstraint extends Constraint {
private transient ArrayList<Assignment> assignments;
private transient DataPoolSettings settings;
private Collection<Event> events;
private DifferentDayConstraint() {
}
public DifferentDayConstraint(DataPoolSettings settings, Collection<Event> evList) {
this.settings = settings;
events = evList;
}
public Collection<Event> getEvents() {
return events;
}
@Override
public int getViolations(Object obj) {
Assignment assToCalc = (Assignment) obj;
int slotsPerDay = settings.getTimeslotsPerDay();
int day = assToCalc.getStart() / slotsPerDay;
int counter = 0;
for (Assignment ass : assignments) {
if (ass.getEvent() == assToCalc.getEvent())
continue;
if (ass.getStart() / slotsPerDay == day)
counter += ass.getEvent().getDuration();
}
return Math.round(counter * getWeight());
}
@Override
public void transformEvents(Map<Event, Assignment> map) {
assignments = new ArrayList<Assignment>(events.size());
for (Event ev : events) {
Assignment as = map.get(ev);
if (as == null)
throw new NullPointerException("Cannot find assignment for:" + ev);
assignments.add(as);
}
}
}