-
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 branch 'develop' into qrisp-sat-solver
- Loading branch information
Showing
31 changed files
with
481 additions
and
72 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
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()); | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
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,114 @@ | ||
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 = RouterUtility.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 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
118 changes: 118 additions & 0 deletions
118
src/main/java/edu/kit/provideq/toolbox/api/ProblemExampleRouter.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,118 @@ | ||
package edu.kit.provideq.toolbox.api; | ||
|
||
import static org.springdoc.core.fn.builders.apiresponse.Builder.responseBuilder; | ||
import static org.springdoc.core.fn.builders.arrayschema.Builder.arraySchemaBuilder; | ||
import static org.springdoc.core.fn.builders.content.Builder.contentBuilder; | ||
import static org.springdoc.core.fn.builders.exampleobject.Builder.exampleOjectBuilder; | ||
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.web.reactive.function.server.RequestPredicates.accept; | ||
import static org.springframework.web.reactive.function.server.ServerResponse.ok; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import edu.kit.provideq.toolbox.exception.MissingExampleException; | ||
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 java.util.List; | ||
import java.util.Optional; | ||
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.ServerResponse; | ||
import reactor.core.publisher.Mono; | ||
|
||
/** | ||
* This router provides example problems for each problem type. | ||
*/ | ||
@Configuration | ||
@EnableWebFlux | ||
public class ProblemExampleRouter { | ||
private ProblemManagerProvider managerProvider; | ||
|
||
@Bean | ||
RouterFunction<ServerResponse> getProblemExampleRoutes() { | ||
var managers = this.managerProvider.getProblemManagers(); | ||
return managers.stream().map(this::defineReadRoute) | ||
.reduce(RouterFunction::and) | ||
.orElseThrow(); | ||
} | ||
|
||
/** | ||
* GET /problems/TYPE/example. | ||
*/ | ||
private RouterFunction<ServerResponse> defineReadRoute(ProblemManager<?, ?> manager) { | ||
return route().GET( | ||
getPath(manager.getType()), | ||
accept(APPLICATION_JSON), | ||
req -> handleRead(manager), | ||
ops -> configureReadDocs(manager, ops) | ||
).build(); | ||
} | ||
|
||
private <InputT, ResultT> Mono<ServerResponse> handleRead( | ||
ProblemManager<InputT, ResultT> manager | ||
) { | ||
var exampleProblems = getExampleInput(manager); | ||
|
||
return ok().body(Mono.just(exampleProblems), new ParameterizedTypeReference<>() { | ||
}); | ||
} | ||
|
||
private static <InputT, ResultT> List<InputT> getExampleInput( | ||
ProblemManager<InputT, ResultT> manager | ||
) { | ||
return manager.getExampleInstances() | ||
.stream() | ||
.map(Problem::getInput) | ||
.filter(Optional::isPresent) | ||
.map(Optional::get) | ||
.toList(); | ||
} | ||
|
||
private static void configureReadDocs(ProblemManager<?, ?> manager, Builder ops) { | ||
var type = manager.getType(); | ||
|
||
String requestString; | ||
try { | ||
requestString = new ObjectMapper().writeValueAsString(getExampleInput(manager)); | ||
} catch (JsonProcessingException exception) { | ||
throw new MissingExampleException(manager.getType(), exception); | ||
} | ||
|
||
ops | ||
.operationId(getPath(manager.getType())) | ||
.description("This endpoint can be used to view example problems for '" | ||
+ type.getId() + "'.") | ||
.tag(type.getId()) | ||
.response(responseBuilder() | ||
.responseCode(String.valueOf(HttpStatus.OK.value())) | ||
.content(contentBuilder() | ||
.array(arraySchemaBuilder() | ||
.schema(schemaBuilder().implementation(String.class)) | ||
) | ||
.example(exampleOjectBuilder() | ||
.value(requestString) | ||
) | ||
) | ||
.content(contentBuilder())); | ||
} | ||
|
||
private static String getPath(ProblemType<?, ?> type) { | ||
return "/problems/%s/examples".formatted(type.getId()); | ||
} | ||
|
||
@Autowired | ||
void setManagerProvider(ProblemManagerProvider managerProvider) { | ||
this.managerProvider = managerProvider; | ||
} | ||
|
||
} |
Oops, something went wrong.