ItEr45S10CUAsignacionRecursosEspecificosAPlanificacionItEr44S16: Extracting method that can be reused

This commit is contained in:
Óscar González Fernández 2010-01-25 23:06:01 +01:00
parent f2fa604225
commit 066305ec19

View file

@ -69,10 +69,8 @@ public class StretchesFunction extends AssignmentFunction {
double[] y = Interval.getHoursPointsFor(totalHours,
intervalsDefinedByStreches);
assert y.length == 1 + intervalsDefinedByStreches.size();
UnivariateRealFunction accumulatingFunction = new SplineInterpolator()
.interpolate(x, y);
int[] hoursForEachDay = extractHoursShouldAssignForEachDay(
accumulatingFunction, startInclusive, endExclusive);
int[] hoursForEachDay = hoursForEachDayUsingSplines(x, y,
startInclusive, endExclusive);
int[] reallyAssigned = getReallyAssigned(allocation,
startInclusive, hoursForEachDay);
// Because of calendars, really assigned hours can be less than
@ -83,29 +81,6 @@ public class StretchesFunction extends AssignmentFunction {
}
private int[] extractHoursShouldAssignForEachDay(
UnivariateRealFunction accumulatedFunction,
LocalDate startInclusive, LocalDate endExclusive) {
int[] result = new int[Days.daysBetween(startInclusive,
endExclusive).getDays()];
int previous = 0;
for (int i = 0; i < result.length; i++) {
int accumulated = evaluate(accumulatedFunction, i);
result[i] = accumulated - previous;
previous = accumulated;
}
return result;
}
private int evaluate(UnivariateRealFunction accumulatedFunction,
int x) {
try {
return (int) accumulatedFunction.value(x);
} catch (FunctionEvaluationException e) {
throw new RuntimeException(e);
}
}
private int[] getReallyAssigned(ResourceAllocation<?> allocation,
LocalDate startInclusive, int[] hoursForEachDay) {
int[] reallyAssigned = new int[hoursForEachDay.length];
@ -150,6 +125,38 @@ public class StretchesFunction extends AssignmentFunction {
}
};
public static int[] hoursForEachDayUsingSplines(double[] x, double[] y,
LocalDate startInclusive, LocalDate endExclusive) {
UnivariateRealFunction accumulatingFunction = new SplineInterpolator()
.interpolate(x, y);
int[] hoursForEachDay = extractHoursShouldAssignForEachDay(
accumulatingFunction, startInclusive, endExclusive);
return hoursForEachDay;
}
private static int[] extractHoursShouldAssignForEachDay(
UnivariateRealFunction accumulatedFunction,
LocalDate startInclusive, LocalDate endExclusive) {
int[] result = new int[Days.daysBetween(startInclusive,
endExclusive).getDays()];
int previous = 0;
for (int i = 0; i < result.length; i++) {
int accumulated = evaluate(accumulatedFunction, i);
result[i] = accumulated - previous;
previous = accumulated;
}
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) {