Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose ConstraintMetaModel at build time #1343

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions quarkus-integration/quarkus/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.revapi</groupId>
<artifactId>revapi-maven-plugin</artifactId>
<configuration>
<!-- Introduction of API package to check -->
<oldArtifacts>
<artifact>${project.groupId}:timefold-solver-quarkus-deployment:1.18.0</artifact>
</oldArtifacts>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"extension": "revapi.differences",
"configuration": {
"differences": [
]
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"extension": "revapi.filter",
"configuration": {
"elements": {
"include": [
"class ai\\.timefold\\.solver\\.quarkus\\.deployment\\.api.*"
]
}
}
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public final class SolverConfigBuildItem extends SimpleBuildItem {
* Constructor for multiple solver configurations.
*/
public SolverConfigBuildItem(Map<String, SolverConfig> solverConfig, GeneratedGizmoClasses generatedGizmoClasses) {
this.solverConfigurations = solverConfig;
this.solverConfigurations = Map.copyOf(solverConfig);
rsynek marked this conversation as resolved.
Show resolved Hide resolved
this.generatedGizmoClasses = generatedGizmoClasses;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import ai.timefold.solver.core.api.domain.solution.ProblemFactCollectionProperty;
import ai.timefold.solver.core.api.score.calculator.EasyScoreCalculator;
import ai.timefold.solver.core.api.score.calculator.IncrementalScoreCalculator;
import ai.timefold.solver.core.api.score.stream.ConstraintMetaModel;
import ai.timefold.solver.core.api.score.stream.ConstraintProvider;
import ai.timefold.solver.core.api.solver.SolverFactory;
import ai.timefold.solver.core.api.solver.SolverManager;
Expand All @@ -39,11 +40,14 @@
import ai.timefold.solver.core.impl.domain.common.ReflectionHelper;
import ai.timefold.solver.core.impl.domain.solution.descriptor.SolutionDescriptor;
import ai.timefold.solver.core.impl.heuristic.selector.common.nearby.NearbyDistanceMeter;
import ai.timefold.solver.core.impl.score.stream.common.AbstractConstraintStreamScoreDirectorFactory;
import ai.timefold.solver.core.impl.solver.DefaultSolverFactory;
import ai.timefold.solver.quarkus.TimefoldRecorder;
import ai.timefold.solver.quarkus.bean.DefaultTimefoldBeanProvider;
import ai.timefold.solver.quarkus.bean.TimefoldSolverBannerBean;
import ai.timefold.solver.quarkus.bean.UnavailableTimefoldBeanProvider;
import ai.timefold.solver.quarkus.config.TimefoldRuntimeConfig;
import ai.timefold.solver.quarkus.deployment.api.ConstraintMetaModelBuildItem;
import ai.timefold.solver.quarkus.deployment.config.SolverBuildTimeConfig;
import ai.timefold.solver.quarkus.deployment.config.TimefoldBuildTimeConfig;
import ai.timefold.solver.quarkus.devui.DevUISolverConfig;
Expand Down Expand Up @@ -573,6 +577,43 @@ private SolverConfig loadSolverConfig(IndexView indexView,
return solverConfig;
}

@BuildStep
void buildConstraintMetaModel(SolverConfigBuildItem solverConfigBuildItem,
rsynek marked this conversation as resolved.
Show resolved Hide resolved
BuildProducer<ConstraintMetaModelBuildItem> constraintMetaModelBuildItemBuildProducer) {
if (solverConfigBuildItem.getSolverConfigMap().isEmpty()) {
return;
}

Map<String, ConstraintMetaModel> constraintMetaModelsBySolverNames = new HashMap<>();
solverConfigBuildItem.getSolverConfigMap().forEach((solverName, solverConfig) -> {
// Gizmo-generated member accessors are not yet available at build time.
DomainAccessType originalDomainAccessType = solverConfig.getDomainAccessType();
solverConfig.setDomainAccessType(DomainAccessType.REFLECTION);

var solverFactory = (DefaultSolverFactory<?>) SolverFactory.create(solverConfig);
ConstraintMetaModel constraintMetaModel = buildConstraintMetaModel(solverFactory);
// Avoid changing the original solver config.
solverConfig.setDomainAccessType(originalDomainAccessType);
constraintMetaModelsBySolverNames.put(solverName, constraintMetaModel);
});

constraintMetaModelBuildItemBuildProducer.produce(new ConstraintMetaModelBuildItem(constraintMetaModelsBySolverNames));
}

private static ConstraintMetaModel buildConstraintMetaModel(DefaultSolverFactory<?> solverFactory) {
var scoreDirectorFactory = solverFactory.getScoreDirectorFactory();

ConstraintMetaModel constraintMetaModel;
if (scoreDirectorFactory instanceof AbstractConstraintStreamScoreDirectorFactory<?, ?> castScoreDirectorFactory) {
constraintMetaModel = castScoreDirectorFactory.getConstraintMetaModel();
} else {
throw new IllegalStateException(
"Cannot provide %s because the score director does not use the Constraint Streams API."
.formatted(ConstraintMetaModel.class.getSimpleName()));
}
return constraintMetaModel;
}

@BuildStep
@Record(RUNTIME_INIT)
void recordAndRegisterRuntimeBeans(TimefoldRecorder recorder, RecorderContext recorderContext,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ai.timefold.solver.quarkus.deployment.api;

import java.util.Map;

import ai.timefold.solver.core.api.score.stream.ConstraintMetaModel;

import io.quarkus.builder.item.SimpleBuildItem;

/**
* Represents a {@link ai.timefold.solver.core.api.score.stream.ConstraintMetaModel} at the build time for the purpose
* of Quarkus augmentation.
*/
public final class ConstraintMetaModelBuildItem extends SimpleBuildItem {

private final Map<String, ConstraintMetaModel> constraintMetaModelsBySolverNames;

public ConstraintMetaModelBuildItem(Map<String, ConstraintMetaModel> constraintMetaModelsBySolverNames) {
this.constraintMetaModelsBySolverNames = Map.copyOf(constraintMetaModelsBySolverNames);
}

public Map<String, ConstraintMetaModel> constraintMetaModelsBySolverNames() {
return constraintMetaModelsBySolverNames;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Public {@link io.quarkus.builder.item.BuildItem}s consumable by other Quarkus extensions.
* <p>
* All classes in this package are part of the public API of this extension.
* Moreover, the extension is responsible for creating instances of these classes during the build phase.
*/
package ai.timefold.solver.quarkus.deployment.api;
Loading