Moved StretchesFunction$Type to its own file

* Renamed it as StretchesFunctionTypeEnum
   * Renamed StretechesFunctionTypeEnum.DEFAULT to STRETCHES

FEA: ItEr74S04BugFixing
This commit is contained in:
Diego Pino Garcia 2011-04-14 13:09:49 +02:00
parent 55b63527cd
commit 89b1519d4b
9 changed files with 219 additions and 180 deletions

View file

@ -31,14 +31,10 @@ import java.util.Iterator;
import java.util.List;
import org.apache.commons.lang.Validate;
import org.apache.commons.math.FunctionEvaluationException;
import org.apache.commons.math.analysis.SplineInterpolator;
import org.apache.commons.math.analysis.UnivariateRealFunction;
import org.hibernate.validator.AssertTrue;
import org.hibernate.validator.Valid;
import org.joda.time.Days;
import org.joda.time.LocalDate;
import org.navalplanner.business.common.ProportionalDistributor;
/**
* Assignment function by stretches.
@ -46,146 +42,6 @@ import org.navalplanner.business.common.ProportionalDistributor;
*/
public class StretchesFunction extends AssignmentFunction {
public enum Type {
DEFAULT {
@Override
public void apply(ResourceAllocation<?> allocation,
List<Interval> intervalsDefinedByStreches,
LocalDate startInclusive, LocalDate endExclusive,
int totalHours) {
Interval.apply(allocation, intervalsDefinedByStreches,
startInclusive, endExclusive, totalHours);
}
},
INTERPOLATED {
@Override
public void apply(ResourceAllocation<?> allocation,
List<Interval> intervalsDefinedByStreches,
LocalDate startInclusive, LocalDate endExclusive,
int totalHours) {
double[] x = Interval.getDayPointsFor(startInclusive,
intervalsDefinedByStreches);
assert x.length == 1 + intervalsDefinedByStreches.size();
double[] y = Interval.getHoursPointsFor(totalHours,
intervalsDefinedByStreches);
assert y.length == 1 + intervalsDefinedByStreches.size();
int[] hoursForEachDay = hoursForEachDayUsingSplines(x, y,
startInclusive, endExclusive);
int[] reallyAssigned = getReallyAssigned(allocation,
startInclusive, hoursForEachDay);
// Because of calendars, really assigned hours can be less than
// the hours for each day specified by the interpolation. The
// remainder must be distributed.
distributeRemainder(allocation, startInclusive, totalHours,
reallyAssigned);
}
private int[] getReallyAssigned(ResourceAllocation<?> allocation,
LocalDate startInclusive, int[] hoursForEachDay) {
int[] reallyAssigned = new int[hoursForEachDay.length];
for (int i = 0; i < hoursForEachDay.length; i++) {
LocalDate day = startInclusive.plusDays(i);
LocalDate nextDay = day.plusDays(1);
allocation.withPreviousAssociatedResources()
.onIntervalWithinTask(day, nextDay)
.allocateHours(hoursForEachDay[i]);
reallyAssigned[i] = allocation.getAssignedHours(day,
nextDay);
}
return reallyAssigned;
}
private void distributeRemainder(ResourceAllocation<?> allocation,
LocalDate startInclusive, int totalHours,
int[] reallyAssigned) {
final int remainder = totalHours - sum(reallyAssigned);
if (remainder == 0) {
return;
}
int[] perDay = distributeRemainder(reallyAssigned, remainder);
for (int i = 0; i < perDay.length; i++) {
if (perDay[i] == 0) {
continue;
}
final int newHours = perDay[i] + reallyAssigned[i];
LocalDate day = startInclusive.plusDays(i);
LocalDate nextDay = day.plusDays(1);
allocation.withPreviousAssociatedResources()
.onIntervalWithinTask(day, nextDay)
.allocateHours(newHours);
}
}
private int[] distributeRemainder(int[] hoursForEachDay,
int remainder) {
ProportionalDistributor remainderDistributor = ProportionalDistributor
.create(hoursForEachDay);
return remainderDistributor.distribute(remainder);
}
};
public static int[] hoursForEachDayUsingSplines(double[] x, double[] y,
LocalDate startInclusive, LocalDate endExclusive) {
UnivariateRealFunction accumulatingFunction = new SplineInterpolator()
.interpolate(x, y);
int[] extractAccumulated = extractAccumulated(accumulatingFunction,
startInclusive, endExclusive);
return extractHoursShouldAssignForEachDay(ValleyFiller
.fillValley(extractAccumulated));
}
private static int[] extractAccumulated(
UnivariateRealFunction accumulatedFunction,
LocalDate startInclusive, LocalDate endExclusive) {
int[] result = new int[Days.daysBetween(startInclusive,
endExclusive).getDays()];
for (int i = 0; i < result.length; i++) {
result[i] = evaluate(accumulatedFunction, i + 1);
}
return result;
}
private static int[] extractHoursShouldAssignForEachDay(
int[] accumulated) {
int[] result = new int[accumulated.length];
int previous = 0;
for (int i = 0; i < result.length; i++) {
final int current = accumulated[i];
result[i] = current - previous;
previous = current;
}
return result;
}
private static int evaluate(UnivariateRealFunction accumulatedFunction,
int x) {
try {
return (int) accumulatedFunction.value(x);
} catch (FunctionEvaluationException e) {
throw new RuntimeException(e);
}
}
public void applyTo(ResourceAllocation<?> resourceAllocation,
StretchesFunction stretchesFunction) {
List<Interval> intervalsDefinedByStreches = stretchesFunction
.getIntervalsDefinedByStreches();
int totalHours = resourceAllocation.getAssignedHours();
Task task = resourceAllocation.getTask();
LocalDate start = LocalDate.fromDateFields(task.getStartDate());
LocalDate end = LocalDate.fromDateFields(task.getEndDate());
apply(resourceAllocation, intervalsDefinedByStreches, start, end,
totalHours);
}
protected abstract void apply(ResourceAllocation<?> allocation,
List<Interval> intervalsDefinedByStreches,
LocalDate startInclusive, LocalDate endExclusive, int totalHours);
}
public static class Interval {
private LocalDate start;
@ -306,14 +162,6 @@ public class StretchesFunction extends AssignmentFunction {
}
private static int sum(int[] array) {
int result = 0;
for (int each : array) {
result += each;
}
return result;
}
public static StretchesFunction create() {
return (StretchesFunction) create(new StretchesFunction());
}
@ -347,12 +195,12 @@ public class StretchesFunction extends AssignmentFunction {
private List<Stretch> stretches = new ArrayList<Stretch>();
private Type type;
private StretchesFunctionTypeEnum type;
/**
* This is a transient field. Not stored
*/
private Type desiredType;
private StretchesFunctionTypeEnum desiredType;
public StretchesFunction copy() {
StretchesFunction result = StretchesFunction.create();
@ -393,15 +241,15 @@ public class StretchesFunction extends AssignmentFunction {
return Collections.unmodifiableList(stretches);
}
public Type getType() {
return type == null ? Type.DEFAULT : type;
public StretchesFunctionTypeEnum getType() {
return type == null ? StretchesFunctionTypeEnum.STRETCHES : type;
}
public Type getDesiredType() {
public StretchesFunctionTypeEnum getDesiredType() {
return desiredType == null ? getType() : desiredType;
}
public void changeTypeTo(Type type) {
public void changeTypeTo(StretchesFunctionTypeEnum type) {
desiredType = type;
}
@ -481,7 +329,7 @@ public class StretchesFunction extends AssignmentFunction {
@Override
public String getName() {
if (StretchesFunction.Type.INTERPOLATED.equals(type)) {
if (StretchesFunctionTypeEnum.INTERPOLATED.equals(type)) {
return ASSIGNMENT_FUNCTION_NAME.INTERPOLATION.toString();
} else {
return ASSIGNMENT_FUNCTION_NAME.STRETCHES.toString();
@ -509,7 +357,7 @@ public class StretchesFunction extends AssignmentFunction {
}
public boolean ifInterpolatedMustHaveAtLeastTwoStreches() {
return getDesiredType() != Type.INTERPOLATED || stretches.size() >= 2;
return getDesiredType() != StretchesFunctionTypeEnum.INTERPOLATED || stretches.size() >= 2;
}
}

View file

@ -0,0 +1,191 @@
/*
* This file is part of NavalPlan
*
* Copyright (C) 2009-2010 Fundación para o Fomento da Calidade Industrial e
* Desenvolvemento Tecnolóxico de Galicia
* Copyright (C) 2010-2011 Igalia, S.L.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.navalplanner.business.planner.entities;
import java.util.List;
import org.apache.commons.math.FunctionEvaluationException;
import org.apache.commons.math.analysis.SplineInterpolator;
import org.apache.commons.math.analysis.UnivariateRealFunction;
import org.joda.time.Days;
import org.joda.time.LocalDate;
import org.navalplanner.business.common.ProportionalDistributor;
import org.navalplanner.business.planner.entities.StretchesFunction.Interval;
/**
*
* @author Manuel Rego Casasnovas <mrego@igalia.com>
* @author Diego Pino García <dpino@igalia.com>
*
*/
public enum StretchesFunctionTypeEnum {
STRETCHES {
@Override
public void apply(ResourceAllocation<?> allocation,
List<Interval> intervalsDefinedByStreches,
LocalDate startInclusive, LocalDate endExclusive,
int totalHours) {
Interval.apply(allocation, intervalsDefinedByStreches,
startInclusive, endExclusive, totalHours);
}
},
INTERPOLATED {
@Override
public void apply(ResourceAllocation<?> allocation,
List<Interval> intervalsDefinedByStreches,
LocalDate startInclusive, LocalDate endExclusive,
int totalHours) {
double[] x = Interval.getDayPointsFor(startInclusive,
intervalsDefinedByStreches);
assert x.length == 1 + intervalsDefinedByStreches.size();
double[] y = Interval.getHoursPointsFor(totalHours,
intervalsDefinedByStreches);
assert y.length == 1 + intervalsDefinedByStreches.size();
int[] hoursForEachDay = hoursForEachDayUsingSplines(x, y,
startInclusive, endExclusive);
int[] reallyAssigned = getReallyAssigned(allocation,
startInclusive, hoursForEachDay);
// Because of calendars, really assigned hours can be less than
// the hours for each day specified by the interpolation. The
// remainder must be distributed.
distributeRemainder(allocation, startInclusive, totalHours,
reallyAssigned);
}
private int[] getReallyAssigned(ResourceAllocation<?> allocation,
LocalDate startInclusive, int[] hoursForEachDay) {
int[] reallyAssigned = new int[hoursForEachDay.length];
for (int i = 0; i < hoursForEachDay.length; i++) {
LocalDate day = startInclusive.plusDays(i);
LocalDate nextDay = day.plusDays(1);
allocation.withPreviousAssociatedResources()
.onIntervalWithinTask(day, nextDay)
.allocateHours(hoursForEachDay[i]);
reallyAssigned[i] = allocation.getAssignedHours(day,
nextDay);
}
return reallyAssigned;
}
private void distributeRemainder(ResourceAllocation<?> allocation,
LocalDate startInclusive, int totalHours,
int[] reallyAssigned) {
final int remainder = totalHours - sum(reallyAssigned);
if (remainder == 0) {
return;
}
int[] perDay = distributeRemainder(reallyAssigned, remainder);
for (int i = 0; i < perDay.length; i++) {
if (perDay[i] == 0) {
continue;
}
final int newHours = perDay[i] + reallyAssigned[i];
LocalDate day = startInclusive.plusDays(i);
LocalDate nextDay = day.plusDays(1);
allocation.withPreviousAssociatedResources()
.onIntervalWithinTask(day, nextDay)
.allocateHours(newHours);
}
}
private int sum(int[] array) {
int result = 0;
for (int each : array) {
result += each;
}
return result;
}
private int[] distributeRemainder(int[] hoursForEachDay,
int remainder) {
ProportionalDistributor remainderDistributor = ProportionalDistributor
.create(hoursForEachDay);
return remainderDistributor.distribute(remainder);
}
};
public static int[] hoursForEachDayUsingSplines(double[] x, double[] y,
LocalDate startInclusive, LocalDate endExclusive) {
UnivariateRealFunction accumulatingFunction = new SplineInterpolator()
.interpolate(x, y);
int[] extractAccumulated = extractAccumulated(accumulatingFunction,
startInclusive, endExclusive);
return extractHoursShouldAssignForEachDay(ValleyFiller
.fillValley(extractAccumulated));
}
private static int[] extractAccumulated(
UnivariateRealFunction accumulatedFunction,
LocalDate startInclusive, LocalDate endExclusive) {
int[] result = new int[Days.daysBetween(startInclusive,
endExclusive).getDays()];
for (int i = 0; i < result.length; i++) {
result[i] = evaluate(accumulatedFunction, i + 1);
}
return result;
}
private static int[] extractHoursShouldAssignForEachDay(
int[] accumulated) {
int[] result = new int[accumulated.length];
int previous = 0;
for (int i = 0; i < result.length; i++) {
final int current = accumulated[i];
result[i] = current - previous;
previous = current;
}
return result;
}
private static int evaluate(UnivariateRealFunction accumulatedFunction,
int x) {
try {
return (int) accumulatedFunction.value(x);
} catch (FunctionEvaluationException e) {
throw new RuntimeException(e);
}
}
public void applyTo(ResourceAllocation<?> resourceAllocation,
StretchesFunction stretchesFunction) {
List<Interval> intervalsDefinedByStreches = stretchesFunction
.getIntervalsDefinedByStreches();
int totalHours = resourceAllocation.getAssignedHours();
Task task = resourceAllocation.getTask();
LocalDate start = LocalDate.fromDateFields(task.getStartDate());
LocalDate end = LocalDate.fromDateFields(task.getEndDate());
apply(resourceAllocation, intervalsDefinedByStreches, start, end,
totalHours);
}
protected abstract void apply(ResourceAllocation<?> allocation,
List<Interval> intervalsDefinedByStreches,
LocalDate startInclusive, LocalDate endExclusive, int totalHours);
}

View file

@ -312,7 +312,7 @@
</list>
<property name="type">
<type name="org.hibernate.type.EnumType">
<param name="enumClass">org.navalplanner.business.planner.entities.StretchesFunction$Type</param>
<param name="enumClass">org.navalplanner.business.planner.entities.StretchesFunctionTypeEnum</param>
</type>
</property>
</joined-subclass>

View file

@ -48,11 +48,10 @@ import org.navalplanner.business.planner.entities.AssignmentFunction;
import org.navalplanner.business.planner.entities.AssignmentFunction.ASSIGNMENT_FUNCTION_NAME;
import org.navalplanner.business.planner.entities.CalculatedValue;
import org.navalplanner.business.planner.entities.GenericResourceAllocation;
import org.navalplanner.business.planner.entities.NoneFunction;
import org.navalplanner.business.planner.entities.ResourceAllocation;
import org.navalplanner.business.planner.entities.SigmoidFunction;
import org.navalplanner.business.planner.entities.SpecificResourceAllocation;
import org.navalplanner.business.planner.entities.StretchesFunction.Type;
import org.navalplanner.business.planner.entities.StretchesFunctionTypeEnum;
import org.navalplanner.business.planner.entities.Task;
import org.navalplanner.business.planner.entities.TaskElement;
import org.navalplanner.business.resources.entities.Criterion;
@ -1356,8 +1355,8 @@ class Row {
}
@Override
protected Type getType() {
return Type.DEFAULT;
protected StretchesFunctionTypeEnum getType() {
return StretchesFunctionTypeEnum.STRETCHES;
}
@Override
@ -1379,8 +1378,8 @@ class Row {
}
@Override
protected Type getType() {
return Type.INTERPOLATED;
protected StretchesFunctionTypeEnum getType() {
return StretchesFunctionTypeEnum.INTERPOLATED;
}
@Override

View file

@ -30,7 +30,7 @@ import org.navalplanner.business.calendars.entities.BaseCalendar;
import org.navalplanner.business.planner.entities.Stretch;
import org.navalplanner.business.planner.entities.StretchesFunction;
import org.navalplanner.business.planner.entities.StretchesFunction.Interval;
import org.navalplanner.business.planner.entities.StretchesFunction.Type;
import org.navalplanner.business.planner.entities.StretchesFunctionTypeEnum;
import org.navalplanner.web.planner.allocation.streches.StretchesFunctionController.IGraphicGenerator;
import org.zkoss.zul.SimpleXYModel;
import org.zkoss.zul.XYModel;
@ -41,9 +41,9 @@ import org.zkoss.zul.XYModel;
*/
public abstract class GraphicForStreches implements IGraphicGenerator {
public static IGraphicGenerator forType(Type type) {
public static IGraphicGenerator forType(StretchesFunctionTypeEnum type) {
switch (type) {
case DEFAULT:
case STRETCHES:
return new ForDefaultStreches();
case INTERPOLATED:
return new ForInterpolation();
@ -204,7 +204,7 @@ public abstract class GraphicForStreches implements IGraphicGenerator {
double[] hourPoints = Interval.getHoursPointsFor(taskHours
.intValue(), intervals);
final Stretch lastStretch = stretches.get(stretches.size() - 1);
return StretchesFunction.Type.hoursForEachDayUsingSplines(
return StretchesFunctionTypeEnum.hoursForEachDayUsingSplines(
dayPoints, hourPoints, startDate, lastStretch.getDate());
}

View file

@ -32,7 +32,7 @@ import org.navalplanner.business.planner.entities.AssignmentFunction;
import org.navalplanner.business.planner.entities.ResourceAllocation;
import org.navalplanner.business.planner.entities.Stretch;
import org.navalplanner.business.planner.entities.StretchesFunction;
import org.navalplanner.business.planner.entities.StretchesFunction.Type;
import org.navalplanner.business.planner.entities.StretchesFunctionTypeEnum;
@ -48,7 +48,8 @@ public interface IStretchesFunctionModel {
*/
void init(StretchesFunction stretchesFunction,
ResourceAllocation<?> resourceAllocation, Type type);
ResourceAllocation<?> resourceAllocation,
StretchesFunctionTypeEnum type);
/*
* Intermediate conversation steps

View file

@ -25,7 +25,7 @@ import java.util.HashMap;
import org.navalplanner.business.planner.entities.AssignmentFunction;
import org.navalplanner.business.planner.entities.ResourceAllocation;
import org.navalplanner.business.planner.entities.StretchesFunction;
import org.navalplanner.business.planner.entities.StretchesFunction.Type;
import org.navalplanner.business.planner.entities.StretchesFunctionTypeEnum;
import org.navalplanner.web.common.Util;
import org.navalplanner.web.planner.allocation.IAssignmentFunctionConfiguration;
import org.navalplanner.web.planner.allocation.streches.StretchesFunctionController.IGraphicGenerator;
@ -69,7 +69,7 @@ public abstract class StrechesFunctionConfiguration implements
return GraphicForStreches.forType(getType());
}
protected abstract Type getType();
protected abstract StretchesFunctionTypeEnum getType();
protected abstract boolean getChartsEnabled();

View file

@ -34,7 +34,7 @@ import org.navalplanner.business.planner.entities.AssignmentFunction;
import org.navalplanner.business.planner.entities.ResourceAllocation;
import org.navalplanner.business.planner.entities.Stretch;
import org.navalplanner.business.planner.entities.StretchesFunction;
import org.navalplanner.business.planner.entities.StretchesFunction.Type;
import org.navalplanner.business.planner.entities.StretchesFunctionTypeEnum;
import org.navalplanner.web.common.Util;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.SuspendNotAllowedException;
@ -94,7 +94,7 @@ public class StretchesFunctionController extends GenericForwardComposer {
}
public void setResourceAllocation(ResourceAllocation<?> resourceAllocation,
Type type) {
StretchesFunctionTypeEnum type) {
AssignmentFunction assignmentFunction = resourceAllocation
.getAssignmentFunction();
stretchesFunctionModel.init((StretchesFunction) assignmentFunction,

View file

@ -41,7 +41,7 @@ import org.navalplanner.business.planner.entities.ResourceAllocation;
import org.navalplanner.business.planner.entities.Stretch;
import org.navalplanner.business.planner.entities.StretchesFunction;
import org.navalplanner.business.planner.entities.StretchesFunction.Interval;
import org.navalplanner.business.planner.entities.StretchesFunction.Type;
import org.navalplanner.business.planner.entities.StretchesFunctionTypeEnum;
import org.navalplanner.business.planner.entities.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
@ -96,7 +96,7 @@ public class StretchesFunctionModel implements IStretchesFunctionModel {
public void init(
StretchesFunction stretchesFunction,
ResourceAllocation<?> resourceAllocation,
org.navalplanner.business.planner.entities.StretchesFunction.Type type) {
StretchesFunctionTypeEnum type) {
if (stretchesFunction != null) {
assignmentFunctionDAO.reattach(stretchesFunction);
this.originalStretchesFunction = stretchesFunction;
@ -145,7 +145,7 @@ public class StretchesFunctionModel implements IStretchesFunctionModel {
throw new ValidationException(
_("For interpolation at least two stretches are needed"));
}
if (stretchesFunction.getDesiredType() == Type.INTERPOLATED) {
if (stretchesFunction.getDesiredType() == StretchesFunctionTypeEnum.INTERPOLATED) {
if (!atLeastTwoStreches(getStretches())) {
throw new ValidationException(
_("There must be at least 2 stretches for doing interpolation"));