-
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.
feat: expose constraint metamodel in Quarkus and Spring Boot (#1108)
- Loading branch information
Showing
43 changed files
with
655 additions
and
308 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
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
44 changes: 44 additions & 0 deletions
44
core/src/main/java/ai/timefold/solver/core/api/score/stream/ConstraintMetaModel.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,44 @@ | ||
package ai.timefold.solver.core.api.score.stream; | ||
|
||
import java.util.Collection; | ||
import java.util.Set; | ||
|
||
import ai.timefold.solver.core.api.score.constraint.ConstraintRef; | ||
|
||
/** | ||
* Provides information about the known constraints. | ||
* Works in combination with {@link ConstraintProvider}. | ||
*/ | ||
public interface ConstraintMetaModel { | ||
|
||
/** | ||
* Returns the constraint for the given reference. | ||
* | ||
* @param constraintRef never null | ||
* @return null if such constraint does not exist | ||
*/ | ||
Constraint getConstraint(ConstraintRef constraintRef); | ||
|
||
/** | ||
* Returns all constraints defined in the {@link ConstraintProvider}. | ||
* | ||
* @return never null, iteration order is undefined | ||
*/ | ||
Collection<Constraint> getConstraints(); | ||
|
||
/** | ||
* Returns all constraints from {@link #getConstraints()} that belong to the given group. | ||
* | ||
* @param constraintGroup never null | ||
* @return never null, iteration order is undefined | ||
*/ | ||
Collection<Constraint> getConstraintsPerGroup(String constraintGroup); | ||
|
||
/** | ||
* Returns constraint groups with at least one constraint in it. | ||
* | ||
* @return never null, iteration order is undefined | ||
*/ | ||
Set<String> getConstraintGroups(); | ||
|
||
} |
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
58 changes: 58 additions & 0 deletions
58
...n/java/ai/timefold/solver/core/impl/score/director/stream/DefaultConstraintMetaModel.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,58 @@ | ||
package ai.timefold.solver.core.impl.score.director.stream; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.TreeMap; | ||
|
||
import ai.timefold.solver.core.api.score.constraint.ConstraintRef; | ||
import ai.timefold.solver.core.api.score.stream.Constraint; | ||
import ai.timefold.solver.core.api.score.stream.ConstraintMetaModel; | ||
import ai.timefold.solver.core.impl.util.CollectionUtils; | ||
|
||
record DefaultConstraintMetaModel( | ||
Map<ConstraintRef, Constraint> constraintPerRefMap, | ||
Map<String, List<Constraint>> constraintPerGroupMap) implements ConstraintMetaModel { | ||
|
||
public static ConstraintMetaModel of(List<? extends Constraint> constraints) { | ||
var constraintCount = constraints.size(); | ||
// Preserve iteration order by using LinkedHashMap. | ||
var perRefMap = CollectionUtils.<ConstraintRef, Constraint> newLinkedHashMap(constraintCount); | ||
var perGroupMap = new TreeMap<String, List<Constraint>>(); | ||
for (var constraint : constraints) { | ||
perRefMap.put(constraint.getConstraintRef(), constraint); | ||
// The list is used to preserve iteration order of the constraints. | ||
// Constraint groups are an optional feature, therefore most people won't use them, | ||
// therefore sizing the list assuming all constraints end up in the default group. | ||
perGroupMap.computeIfAbsent(constraint.getConstraintGroup(), k -> new ArrayList<>(constraintCount)) | ||
.add(constraint); | ||
} | ||
return new DefaultConstraintMetaModel( | ||
Collections.unmodifiableMap(perRefMap), | ||
Collections.unmodifiableMap(perGroupMap)); | ||
} | ||
|
||
@Override | ||
public Constraint getConstraint(ConstraintRef constraintRef) { | ||
return constraintPerRefMap.get(constraintRef); | ||
} | ||
|
||
@Override | ||
public Collection<Constraint> getConstraintsPerGroup(String constraintGroup) { | ||
return constraintPerGroupMap.getOrDefault(constraintGroup, Collections.emptyList()); | ||
} | ||
|
||
@Override | ||
public Set<String> getConstraintGroups() { | ||
return constraintPerGroupMap.keySet(); | ||
} | ||
|
||
@Override | ||
public Collection<Constraint> getConstraints() { | ||
return constraintPerRefMap.values(); | ||
} | ||
|
||
} |
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
Oops, something went wrong.