-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #106 from ProvideQ/feature/solution-quality-evaluator
Solution Quality Evaluator
- Loading branch information
Showing
18 changed files
with
252 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package edu.kit.provideq.toolbox; | ||
|
||
/** | ||
* Represents a bound value with its type. | ||
* | ||
* @param value the estimated value | ||
* @param boundType the type of the bound | ||
*/ | ||
public record Bound(float value, BoundType boundType) { | ||
} |
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,15 @@ | ||
package edu.kit.provideq.toolbox; | ||
|
||
/** | ||
* Represents the bound type of bounds. | ||
*/ | ||
public enum BoundType { | ||
/** | ||
* An upper bound. | ||
*/ | ||
UPPER, | ||
/** | ||
* A lower bound. | ||
*/ | ||
LOWER | ||
} |
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,10 @@ | ||
package edu.kit.provideq.toolbox; | ||
|
||
/** | ||
* Represents a bound estimation for the solution of a problem. | ||
* | ||
* @param bound the value | ||
* @param executionTime the time it took to estimate the value | ||
*/ | ||
public record BoundWithInfo(Bound bound, long executionTime) { | ||
} |
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,13 @@ | ||
package edu.kit.provideq.toolbox.api; | ||
|
||
import edu.kit.provideq.toolbox.BoundType; | ||
import edu.kit.provideq.toolbox.BoundWithInfo; | ||
|
||
public record BoundDto(float bound, BoundType boundType, long executionTime) { | ||
public BoundDto(BoundWithInfo boundWithInfo) { | ||
this( | ||
boundWithInfo.bound().value(), | ||
boundWithInfo.bound().boundType(), | ||
boundWithInfo.executionTime()); | ||
} | ||
} |
130 changes: 130 additions & 0 deletions
130
src/main/java/edu/kit/provideq/toolbox/api/EstimationRouter.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,130 @@ | ||
package edu.kit.provideq.toolbox.api; | ||
|
||
import static org.springdoc.core.fn.builders.apiresponse.Builder.responseBuilder; | ||
import static org.springdoc.core.fn.builders.content.Builder.contentBuilder; | ||
import static org.springdoc.core.fn.builders.parameter.Builder.parameterBuilder; | ||
import static org.springdoc.core.fn.builders.schema.Builder.schemaBuilder; | ||
import static org.springdoc.webflux.core.fn.SpringdocRouteBuilder.route; | ||
import static org.springframework.http.MediaType.APPLICATION_JSON; | ||
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; | ||
import static org.springframework.web.reactive.function.server.RequestPredicates.accept; | ||
import static org.springframework.web.reactive.function.server.ServerResponse.ok; | ||
|
||
import edu.kit.provideq.toolbox.meta.Problem; | ||
import edu.kit.provideq.toolbox.meta.ProblemManager; | ||
import edu.kit.provideq.toolbox.meta.ProblemManagerProvider; | ||
import edu.kit.provideq.toolbox.meta.ProblemType; | ||
import io.swagger.v3.oas.annotations.enums.ParameterIn; | ||
import java.util.NoSuchElementException; | ||
import java.util.UUID; | ||
import org.springdoc.core.fn.builders.operation.Builder; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.ParameterizedTypeReference; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.reactive.config.EnableWebFlux; | ||
import org.springframework.web.reactive.function.server.RouterFunction; | ||
import org.springframework.web.reactive.function.server.ServerRequest; | ||
import org.springframework.web.reactive.function.server.ServerResponse; | ||
import org.springframework.web.server.ResponseStatusException; | ||
import reactor.core.publisher.Mono; | ||
|
||
/** | ||
* This router handles requests regarding {@link Problem} instance solution bound estimations. | ||
* The /bound endpoint is only available for problem types that have an estimator. | ||
*/ | ||
@Configuration | ||
@EnableWebFlux | ||
public class EstimationRouter { | ||
public static final String PROBLEM_ID_PARAM_NAME = "problemId"; | ||
private ProblemManagerProvider managerProvider; | ||
|
||
@Bean | ||
RouterFunction<ServerResponse> getEstimationRoutes() { | ||
return managerProvider.getProblemManagers().stream() | ||
.filter(manager -> manager.getType().getEstimator().isPresent()) | ||
.map(this::defineGetRoute) | ||
.reduce(RouterFunction::and) | ||
.orElse(null); | ||
} | ||
|
||
/** | ||
* Estimate Operation: GET /problems/TYPE/{problemId}/bound. | ||
*/ | ||
private RouterFunction<ServerResponse> defineGetRoute(ProblemManager<?, ?> manager) { | ||
return route().GET( | ||
getEstimationRouteForProblemType(manager.getType()), | ||
accept(APPLICATION_JSON), | ||
req -> handleGet(manager, req), | ||
ops -> handleGetDocumentation(manager, ops) | ||
).build(); | ||
} | ||
|
||
private <InputT, ResultT> Mono<ServerResponse> handleGet( | ||
ProblemManager<InputT, ResultT> manager, | ||
ServerRequest req | ||
) { | ||
var problemId = req.pathVariable(PROBLEM_ID_PARAM_NAME); | ||
var problem = findProblemOrThrow(manager, problemId); | ||
|
||
Mono<BoundDto> bound; | ||
try { | ||
problem.estimateBound(); | ||
bound = Mono.just(new BoundDto(problem.getBound().orElseThrow())); | ||
} catch (IllegalStateException | NoSuchElementException e) { | ||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage()); | ||
} | ||
|
||
return ok().body(bound, new ParameterizedTypeReference<>() { | ||
}); | ||
} | ||
|
||
private void handleGetDocumentation( | ||
ProblemManager<?, ?> manager, | ||
Builder ops | ||
) { | ||
ProblemType<?, ?> problemType = manager.getType(); | ||
ops | ||
.operationId(getEstimationRouteForProblemType(problemType)) | ||
.tag(problemType.getId()) | ||
.description("Estimates the solution bound for the problem with the given ID.") | ||
.parameter(parameterBuilder().in(ParameterIn.PATH).name(PROBLEM_ID_PARAM_NAME)) | ||
.response(responseBuilder() | ||
.responseCode(String.valueOf(HttpStatus.OK.value())) | ||
.content(getOkResponseContent()) | ||
); | ||
} | ||
|
||
private static org.springdoc.core.fn.builders.content.Builder getOkResponseContent() { | ||
return contentBuilder() | ||
.mediaType(APPLICATION_JSON_VALUE) | ||
.schema(schemaBuilder().implementation(BoundDto.class)); | ||
} | ||
|
||
private <InputT, ResultT> Problem<InputT, ResultT> findProblemOrThrow( | ||
ProblemManager<InputT, ResultT> manager, | ||
String id | ||
) { | ||
UUID uuid; | ||
try { | ||
uuid = UUID.fromString(id); | ||
} catch (IllegalArgumentException e) { | ||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid problem ID"); | ||
} | ||
|
||
return manager.findInstanceById(uuid) | ||
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, | ||
"Could not find a problem for this type with this problem ID!")); | ||
} | ||
|
||
private String getEstimationRouteForProblemType(ProblemType<?, ?> type) { | ||
return "/problems/%s/{%s}/bound".formatted(type.getId(), PROBLEM_ID_PARAM_NAME); | ||
} | ||
|
||
@Autowired | ||
void setManagerProvider(ProblemManagerProvider managerProvider) { | ||
this.managerProvider = managerProvider; | ||
} | ||
|
||
} |
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
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.