Skip to content

Commit

Permalink
chore: Use a unit test collector approach
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher-Chianelli committed Aug 27, 2024
1 parent 0d0ad32 commit 9167c5c
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 490 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ai.timefold.solver.core.impl.score.stream.collector.connected_ranges;

import java.util.Objects;

import ai.timefold.solver.core.api.score.stream.common.ConnectedRange;
import ai.timefold.solver.core.api.score.stream.common.RangeGap;

Expand Down Expand Up @@ -51,6 +53,21 @@ void setLength(Difference_ length) {
this.length = length;
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof RangeGapImpl<?, ?, ?> rangeGap))
return false;
return Objects.equals(getPreviousRangeEnd(), rangeGap.getPreviousRangeEnd()) &&
Objects.equals(getNextRangeStart(), rangeGap.getNextRangeStart());
}

@Override
public int hashCode() {
return Objects.hash(getPreviousRangeEnd(), getNextRangeStart());
}

@Override
public String toString() {
return "RangeGap{" +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ai.timefold.solver.core.impl.score.stream.collector;

import java.util.Arrays;
import java.util.Objects;

import ai.timefold.solver.core.api.score.stream.common.ConnectedRangeChain;
import ai.timefold.solver.core.api.score.stream.common.SequenceChain;
Expand Down Expand Up @@ -134,8 +135,56 @@ protected ConnectedRangeChain<Interval, Integer, Integer> buildConsecutiveUsage(
}).getConnectedRangeChain();
}

protected ConnectedRangeChain<DynamicInterval, Integer, Integer> buildDynamicConsecutiveUsage(DynamicInterval... data) {
return Arrays.stream(data).collect(
() -> new ConnectedRangeTracker<>(DynamicInterval::getStart, DynamicInterval::getEnd, (a, b) -> b - a),
(tree, datum) -> tree.add(tree.getRange(datum)),
(a, b) -> {
throw new UnsupportedOperationException();
}).getConnectedRangeChain();
}

public record Interval(int start, int end) {

}

public static final class DynamicInterval {
int start;

public DynamicInterval(int start) {
this.start = start;
}

public int getStart() {
return start;
}

public int getEnd() {
return start + 10;
}

public void setStart(int start) {
this.start = start;
}

@Override
public String toString() {
return "DynamicInterval(%d, %d)".formatted(getStart(), getEnd());
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof DynamicInterval that))
return false;
return start == that.start;
}

@Override
public int hashCode() {
return Objects.hashCode(start);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

import ai.timefold.solver.core.api.score.stream.ConstraintCollectors;
import ai.timefold.solver.core.api.score.stream.bi.BiConstraintCollector;
import ai.timefold.solver.core.api.score.stream.common.ConnectedRangeChain;
import ai.timefold.solver.core.api.score.stream.common.LoadBalance;
import ai.timefold.solver.core.impl.score.stream.collector.AbstractConstraintCollectorsTest;
import ai.timefold.solver.core.impl.util.Pair;
Expand Down Expand Up @@ -1071,19 +1070,19 @@ public void toConsecutiveSequences() {
@Override
@Test
public void consecutiveUsage() {
BiConstraintCollector<Integer, Integer, ?, ConnectedRangeChain<Interval, Integer, Integer>> collector =
var collector =
ConstraintCollectors.toConnectedRanges(Interval::new,
Interval::start,
Interval::end, (a, b) -> b - a);
var container = collector.supplier().get();
// Add first value, sequence is [(1,3)]
Runnable firstRetractor = accumulate(collector, container, 1, 3);
var firstRetractor = accumulate(collector, container, 1, 3);
assertResult(collector, container, buildConsecutiveUsage(new Interval(1, 3)));
// Add second value, sequence is [(1,3),(2,4)]
Runnable secondRetractor = accumulate(collector, container, 2, 4);
var secondRetractor = accumulate(collector, container, 2, 4);
assertResult(collector, container, buildConsecutiveUsage(new Interval(1, 3), new Interval(2, 4)));
// Add third value, same as the second. Sequence is [{1,1},2}]
Runnable thirdRetractor = accumulate(collector, container, 2, 4);
var thirdRetractor = accumulate(collector, container, 2, 4);
assertResult(collector, container, buildConsecutiveUsage(new Interval(1, 3), new Interval(2, 4), new Interval(2, 4)));
// Retract one instance of the second value; we only have two values now.
secondRetractor.run();
Expand All @@ -1094,6 +1093,38 @@ public void consecutiveUsage() {
// Retract last value; there are no values now.
firstRetractor.run();
assertResult(collector, container, buildConsecutiveUsage());

var dynamicCollector =
ConstraintCollectors.toConnectedRanges((DynamicInterval a, Object b) -> a,
DynamicInterval::getStart,
DynamicInterval::getEnd, (a, b) -> b - a);

var first = new DynamicInterval(0);
var second = new DynamicInterval(10);
var third = new DynamicInterval(20);
container = dynamicCollector.supplier().get();

// Add first value, sequence is [[(0, 10)]]
firstRetractor = accumulate(dynamicCollector, container, first, null);
assertResult(dynamicCollector, container, buildDynamicConsecutiveUsage(new DynamicInterval(0)));

// Add third value, sequence is [[(0, 10)], [(20, 30)]]
accumulate(dynamicCollector, container, third, null);
assertResult(dynamicCollector, container,
buildDynamicConsecutiveUsage(new DynamicInterval(0), new DynamicInterval(20)));

// Add second value, sequence is [[(0, 10), (10, 20), (20, 30)]]
accumulate(dynamicCollector, container, second, null);
assertResult(dynamicCollector, container,
buildDynamicConsecutiveUsage(new DynamicInterval(0), new DynamicInterval(10), new DynamicInterval(20)));

// Change first value, retract it, then re-add it
first.setStart(-5);
firstRetractor.run();
accumulate(dynamicCollector, container, first, null);

assertResult(dynamicCollector, container,
buildDynamicConsecutiveUsage(new DynamicInterval(-5), new DynamicInterval(10), new DynamicInterval(20)));
}

@Override
Expand Down
Loading

0 comments on commit 9167c5c

Please sign in to comment.