-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
346 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
core/src/main/java/ai/timefold/solver/core/impl/bavet/AbstractSession.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package ai.timefold.solver.core.impl.bavet; | ||
|
||
import java.util.IdentityHashMap; | ||
import java.util.Map; | ||
|
||
import ai.timefold.solver.core.impl.bavet.uni.AbstractForEachUniNode; | ||
|
||
public abstract class AbstractSession { | ||
|
||
protected final NodeNetwork nodeNetwork; | ||
private final Map<Class<?>, AbstractForEachUniNode<Object>[]> effectiveClassToNodeArrayMap; | ||
|
||
protected AbstractSession(NodeNetwork nodeNetwork) { | ||
this.nodeNetwork = nodeNetwork; | ||
this.effectiveClassToNodeArrayMap = new IdentityHashMap<>(nodeNetwork.forEachNodeCount()); | ||
} | ||
|
||
public final void insert(Object fact) { | ||
var factClass = fact.getClass(); | ||
for (var node : findNodes(factClass)) { | ||
node.insert(fact); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private AbstractForEachUniNode<Object>[] findNodes(Class<?> factClass) { | ||
// Map.computeIfAbsent() would have created lambdas on the hot path, this will not. | ||
var nodeArray = effectiveClassToNodeArrayMap.get(factClass); | ||
if (nodeArray == null) { | ||
nodeArray = nodeNetwork.getForEachNodes(factClass) | ||
.filter(AbstractForEachUniNode::supportsIndividualUpdates) | ||
.toArray(AbstractForEachUniNode[]::new); | ||
effectiveClassToNodeArrayMap.put(factClass, nodeArray); | ||
} | ||
return nodeArray; | ||
} | ||
|
||
public final void update(Object fact) { | ||
var factClass = fact.getClass(); | ||
for (var node : findNodes(factClass)) { | ||
node.update(fact); | ||
} | ||
} | ||
|
||
public final void retract(Object fact) { | ||
var factClass = fact.getClass(); | ||
for (var node : findNodes(factClass)) { | ||
node.retract(fact); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...solver/core/impl/bavet/common/Stream.java → ...r/core/impl/bavet/common/BavetStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
core/src/main/java/ai/timefold/solver/core/impl/bavet/common/BavetStreamBinaryOperation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
core/src/main/java/ai/timefold/solver/core/impl/bavet/uni/ForEachFromSolutionUniNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package ai.timefold.solver.core.impl.bavet.uni; | ||
|
||
import java.util.Collections; | ||
import java.util.IdentityHashMap; | ||
import java.util.Objects; | ||
|
||
import ai.timefold.solver.core.api.move.SolutionExtractor; | ||
import ai.timefold.solver.core.api.move.SolutionView; | ||
import ai.timefold.solver.core.impl.bavet.common.tuple.TupleLifecycle; | ||
import ai.timefold.solver.core.impl.bavet.common.tuple.UniTuple; | ||
|
||
public final class ForEachFromSolutionUniNode<Solution_, A> | ||
extends ForEachUniNode<A> { | ||
|
||
private final SolutionExtractor<Solution_, A> solutionExtractor; | ||
|
||
public ForEachFromSolutionUniNode(Class<A> forEachClass, SolutionExtractor<Solution_, A> solutionExtractor, | ||
TupleLifecycle<UniTuple<A>> nextNodesTupleLifecycle, int outputStoreSize) { | ||
super(forEachClass, nextNodesTupleLifecycle, outputStoreSize); | ||
this.solutionExtractor = Objects.requireNonNull(solutionExtractor); | ||
} | ||
|
||
public void read(SolutionView<Solution_> solutionView, Solution_ solution) { | ||
var seenFactSet = Collections.newSetFromMap(new IdentityHashMap<A, Boolean>()); | ||
solutionExtractor.apply(solutionView, solution).forEach(a -> { | ||
if (seenFactSet.contains(a)) { // Eliminate duplicates in the source data. | ||
return; | ||
} | ||
seenFactSet.add(a); | ||
var tuple = tupleMap.get(a); | ||
if (tuple == null) { | ||
super.insert(a); | ||
} else { | ||
updateExisting(a, tuple); | ||
} | ||
}); | ||
// Retract all tuples that were not seen in the source data. | ||
var iterator = tupleMap.entrySet().iterator(); | ||
while (iterator.hasNext()) { | ||
var entry = iterator.next(); | ||
var fact = entry.getKey(); | ||
if (!seenFactSet.contains(fact)) { | ||
iterator.remove(); | ||
retractExisting(fact, entry.getValue()); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void insert(A a) { | ||
throw new IllegalStateException("Impossible state: solution-based node cannot insert."); | ||
} | ||
|
||
@Override | ||
public void update(A a) { | ||
throw new IllegalStateException("Impossible state: solution-based node cannot update."); | ||
} | ||
|
||
@Override | ||
public void retract(A a) { | ||
throw new IllegalStateException("Impossible state: solution-based node cannot retract."); | ||
} | ||
|
||
@Override | ||
public boolean supportsIndividualUpdates() { | ||
return false; | ||
} | ||
} |
Oops, something went wrong.