diff --git a/src/main/java/hbp/mip/models/DTOs/AlgorithmSpecificationDTO.java b/src/main/java/hbp/mip/models/DTOs/AlgorithmSpecificationDTO.java index 3286a626..c69acd46 100644 --- a/src/main/java/hbp/mip/models/DTOs/AlgorithmSpecificationDTO.java +++ b/src/main/java/hbp/mip/models/DTOs/AlgorithmSpecificationDTO.java @@ -19,7 +19,7 @@ public record TransformerSpecificationDTO(String name, String label, String desc List parameters) { public TransformerSpecificationDTO(Exareme2AlgorithmSpecificationDTO.Exareme2TransformerSpecificationDTO transformerDTO) { this( - transformerDTO.name().toUpperCase(), + transformerDTO.name(), transformerDTO.label(), transformerDTO.desc(), getAlgorithmParameterSpecifications(transformerDTO.parameters()) @@ -128,7 +128,7 @@ private static String getParameterColumnValuesIsCategorical(String inputDataDeta public AlgorithmSpecificationDTO(Exareme2AlgorithmSpecificationDTO exareme2Algorithm){ this( - exareme2Algorithm.name().toUpperCase(), + exareme2Algorithm.name(), exareme2Algorithm.label(), exareme2Algorithm.desc(), getAlgorithmParameters(exareme2Algorithm), diff --git a/src/main/java/hbp/mip/models/DTOs/exareme2/Exareme2AlgorithmRequestDTO.java b/src/main/java/hbp/mip/models/DTOs/exareme2/Exareme2AlgorithmRequestDTO.java index 38a84da2..311f4074 100644 --- a/src/main/java/hbp/mip/models/DTOs/exareme2/Exareme2AlgorithmRequestDTO.java +++ b/src/main/java/hbp/mip/models/DTOs/exareme2/Exareme2AlgorithmRequestDTO.java @@ -2,6 +2,7 @@ import com.google.gson.JsonSyntaxException; import hbp.mip.models.DTOs.ExperimentExecutionDTO; +import hbp.mip.utils.Exceptions.InternalServerError; import hbp.mip.utils.JsonConverters; import java.util.*; @@ -16,13 +17,13 @@ public record Exareme2AlgorithmRequestDTO( public Exareme2AlgorithmRequestDTO( UUID experimentUUID, List exaremeAlgorithmRequestParamDTOs, - List exaremeTransformers - ) { + List exaremeTransformers, + Exareme2AlgorithmSpecificationDTO exareme2AlgorithmSpecificationDTO) { this( experimentUUID.toString(), getInputData(exaremeAlgorithmRequestParamDTOs), - getParameters(exaremeAlgorithmRequestParamDTOs), - getPreprocessing(exaremeTransformers) + getParameters(exaremeAlgorithmRequestParamDTOs, exareme2AlgorithmSpecificationDTO), + getPreprocessing(exaremeTransformers, exareme2AlgorithmSpecificationDTO) ); } @@ -65,7 +66,7 @@ private static Exareme2InputDataRequestDTO getInputData(List getParameters(List exaremeAlgorithmRequestParamDTOs) { + private static Map getParameters(List exaremeAlgorithmRequestParamDTOs, Exareme2AlgorithmSpecificationDTO exareme2AlgorithmSpecificationDTO) { if (exaremeAlgorithmRequestParamDTOs == null) { return null; } @@ -75,28 +76,46 @@ private static Map getParameters(List exareme2Parameters = new HashMap<>(); exaremeAlgorithmRequestParamDTOs.forEach(parameter -> { - if (!inputDataFields.contains(parameter.name())) - exareme2Parameters.put(parameter.name(), convertStringToProperExareme2ParameterType(parameter.value())); + if (!inputDataFields.contains(parameter.name())){ + Exareme2AlgorithmSpecificationDTO.Exareme2AlgorithmParameterSpecificationDTO paramSpecDto = exareme2AlgorithmSpecificationDTO.parameters().get(parameter.name()); + exareme2Parameters.put(parameter.name(), convertStringToProperExareme2ParameterTypeAccordingToSpecs(parameter.value(), paramSpecDto)); + } }); return exareme2Parameters; } - private static Map getPreprocessing(List exaremeTransformers) { + private static Map getPreprocessing(List exaremeTransformers, Exareme2AlgorithmSpecificationDTO exareme2AlgorithmSpecificationDTO) { if (exaremeTransformers == null) { return null; } HashMap exareme2Preprocessing = new HashMap<>(); exaremeTransformers.forEach(transformer -> { + String transformer_name = transformer.name(); HashMap transformerParameterDTOs = new HashMap<>(); - for (ExperimentExecutionDTO.AlgorithmExecutionDTO.AlgorithmParameterExecutionDTO parameter : transformer.parameters()) - transformerParameterDTOs.put(parameter.name(), convertStringToProperExareme2ParameterType(parameter.value())); - exareme2Preprocessing.put(transformer.name(), transformerParameterDTOs); + for (ExperimentExecutionDTO.AlgorithmExecutionDTO.AlgorithmParameterExecutionDTO parameter : transformer.parameters()){ + String param_name = parameter.name(); + Optional transformerSpecificationDTO = exareme2AlgorithmSpecificationDTO.preprocessing().stream() + .filter(transformerSpec-> transformerSpec.name().equals(transformer_name)) + .findFirst(); + if (transformerSpecificationDTO.isEmpty()) throw new InternalServerError("Missing the transformer: " + transformer_name); + + Exareme2AlgorithmSpecificationDTO.Exareme2AlgorithmParameterSpecificationDTO paramSpecDto = transformerSpecificationDTO.get().parameters().get(param_name); + transformerParameterDTOs.put(param_name, convertStringToProperExareme2ParameterTypeAccordingToSpecs(parameter.value(), paramSpecDto)); + } + exareme2Preprocessing.put(transformer_name, transformerParameterDTOs); }); return exareme2Preprocessing; } + private static Object convertStringToProperExareme2ParameterTypeAccordingToSpecs(String value, Exareme2AlgorithmSpecificationDTO.Exareme2AlgorithmParameterSpecificationDTO paramSpecDto) { + if (paramSpecDto.enums() != null){ + return value; + } + return convertStringToProperExareme2ParameterType(value); + } + private static Object convertStringToProperExareme2ParameterType(String str) { if (isMap(str)) return JsonConverters.convertJsonStringToObject(str, Map.class); diff --git a/src/main/java/hbp/mip/models/DTOs/exareme2/Exareme2AlgorithmSpecificationDTO.java b/src/main/java/hbp/mip/models/DTOs/exareme2/Exareme2AlgorithmSpecificationDTO.java index 776546cf..4196bb99 100644 --- a/src/main/java/hbp/mip/models/DTOs/exareme2/Exareme2AlgorithmSpecificationDTO.java +++ b/src/main/java/hbp/mip/models/DTOs/exareme2/Exareme2AlgorithmSpecificationDTO.java @@ -1,5 +1,7 @@ package hbp.mip.models.DTOs.exareme2; +import com.google.gson.annotations.SerializedName; + import java.util.Collections; import java.util.List; import java.util.Map; @@ -9,7 +11,6 @@ public record Exareme2AlgorithmSpecificationDTO( String name, String label, String desc, - String type, Exareme2AlgorithmInputdataSpecificationDTO inputdata, Map parameters, List preprocessing @@ -24,7 +25,6 @@ public List preprocessing() { return Objects.requireNonNullElse(preprocessing, Collections.EMPTY_LIST); } - public record Exareme2AlgorithmParameterSpecificationDTO( String label, String desc, @@ -33,8 +33,9 @@ public record Exareme2AlgorithmParameterSpecificationDTO( String multiple, String min, String max, + @SerializedName("default") String default_value, - Exareme2AlgorithmParameterSpecificationDTO.Exareme2AlgorithmEnumDTO enums, + Exareme2AlgorithmEnumDTO enums, Exareme2AlgorithmEnumDTO dict_keys_enums, Exareme2AlgorithmEnumDTO dict_values_enums diff --git a/src/main/java/hbp/mip/services/AlgorithmService.java b/src/main/java/hbp/mip/services/AlgorithmService.java index c337f1f6..2c922f7e 100644 --- a/src/main/java/hbp/mip/services/AlgorithmService.java +++ b/src/main/java/hbp/mip/services/AlgorithmService.java @@ -5,6 +5,7 @@ import hbp.mip.models.DTOs.AlgorithmSpecificationDTO; import hbp.mip.models.DTOs.exareme2.Exareme2AlgorithmSpecificationDTO; import hbp.mip.utils.CustomResourceLoader; +import hbp.mip.utils.Exareme2AlgorithmsSpecs; import hbp.mip.utils.HTTPUtil; import hbp.mip.utils.Logger; import org.springframework.beans.factory.annotation.Value; @@ -23,6 +24,7 @@ public class AlgorithmService { private static final Gson gson = new Gson(); + private final Exareme2AlgorithmsSpecs exareme2AlgorithmsSpecs; private final CustomResourceLoader resourceLoader; @Value("${files.disabledAlgorithms_json}") @@ -31,7 +33,8 @@ public class AlgorithmService { @Value("${services.exareme2.algorithmsUrl}") private String exareme2AlgorithmsUrl; - public AlgorithmService(CustomResourceLoader resourceLoader) { + public AlgorithmService(Exareme2AlgorithmsSpecs exareme2AlgorithmsSpecs, CustomResourceLoader resourceLoader) { + this.exareme2AlgorithmsSpecs = exareme2AlgorithmsSpecs; this.resourceLoader = resourceLoader; } @@ -77,6 +80,7 @@ private List getExareme2Algorithms(Logger log } logger.debug("Fetched " + algorithms.size() + " exareme2 algorithms."); + exareme2AlgorithmsSpecs.setAlgorithms(algorithms); return algorithms; } diff --git a/src/main/java/hbp/mip/services/ExperimentService.java b/src/main/java/hbp/mip/services/ExperimentService.java index 5c6e7f3b..2036c60e 100644 --- a/src/main/java/hbp/mip/services/ExperimentService.java +++ b/src/main/java/hbp/mip/services/ExperimentService.java @@ -1,21 +1,16 @@ package hbp.mip.services; import hbp.mip.models.DAOs.ExperimentDAO; -import hbp.mip.models.DTOs.ExperimentDTO; -import hbp.mip.models.DTOs.ExperimentExecutionDTO; -import hbp.mip.models.DTOs.ExperimentsDTO; -import hbp.mip.models.DTOs.UserDTO; +import hbp.mip.models.DTOs.*; import hbp.mip.models.DTOs.exareme2.Exareme2AlgorithmRequestDTO; +import hbp.mip.models.DTOs.exareme2.Exareme2AlgorithmSpecificationDTO; import hbp.mip.repositories.ExperimentRepository; import hbp.mip.repositories.ExperimentSpecifications; -import hbp.mip.utils.ClaimUtils; +import hbp.mip.utils.*; import hbp.mip.utils.Exceptions.BadRequestException; import hbp.mip.utils.Exceptions.InternalServerError; import hbp.mip.utils.Exceptions.NoContent; import hbp.mip.utils.Exceptions.UnauthorizedException; -import hbp.mip.utils.HTTPUtil; -import hbp.mip.utils.JsonConverters; -import hbp.mip.utils.Logger; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; @@ -36,6 +31,7 @@ public class ExperimentService { private final ActiveUserService activeUserService; private final ClaimUtils claimUtils; + private final Exareme2AlgorithmsSpecs exareme2AlgorithmsSpecs; private final ExperimentRepository experimentRepository; @@ -48,10 +44,12 @@ public class ExperimentService { public ExperimentService( ActiveUserService activeUserService, ClaimUtils claimUtils, + Exareme2AlgorithmsSpecs exareme2AlgorithmsSpecs, ExperimentRepository experimentRepository ) { this.activeUserService = activeUserService; this.claimUtils = claimUtils; + this.exareme2AlgorithmsSpecs = exareme2AlgorithmsSpecs; this.experimentRepository = experimentRepository; } @@ -342,8 +340,9 @@ private String getExperimentDatasets(ExperimentExecutionDTO experimentExecutionD private ExperimentAlgorithmResultDTO runExaremeAlgorithm(UUID uuid, ExperimentExecutionDTO experimentExecutionDTO, Logger logger) { String algorithmName = experimentExecutionDTO.algorithm().name(); - String algorithmEndpoint = exareme2AlgorithmsUrl + "/" + algorithmName.toLowerCase(); - var exareme2AlgorithmRequestDTO = new Exareme2AlgorithmRequestDTO(uuid, experimentExecutionDTO.algorithm().parameters(), experimentExecutionDTO.algorithm().preprocessing()); + String algorithmEndpoint = exareme2AlgorithmsUrl + "/" + algorithmName; + Exareme2AlgorithmSpecificationDTO exareme2AlgorithmSpecificationDTO = getAlgorithmSpec(algorithmName); + var exareme2AlgorithmRequestDTO = new Exareme2AlgorithmRequestDTO(uuid, experimentExecutionDTO.algorithm().parameters(), experimentExecutionDTO.algorithm().preprocessing(), exareme2AlgorithmSpecificationDTO); String algorithmBody = convertObjectToJsonString(exareme2AlgorithmRequestDTO); logger.debug("Exareme2 algorithm request, endpoint: " + algorithmEndpoint); logger.debug("Exareme2 algorithm request, body: " + algorithmBody); @@ -362,6 +361,14 @@ private ExperimentAlgorithmResultDTO runExaremeAlgorithm(UUID uuid, ExperimentEx return new ExperimentAlgorithmResultDTO(requestResponseCode, result); } + private Exareme2AlgorithmSpecificationDTO getAlgorithmSpec(String algorithmName){ + Optional algorithmSpecification = exareme2AlgorithmsSpecs.getAlgorithms().stream() + .filter(algorithmSpec-> algorithmSpec.name().equals(algorithmName)) + .findFirst(); + if (algorithmSpecification.isEmpty()) throw new InternalServerError("Missing the algorithm: " + algorithmName); + return algorithmSpecification.get(); + } + record ExperimentAlgorithmResultDTO(int code, List result) { } } diff --git a/src/main/java/hbp/mip/utils/Exareme2AlgorithmsSpecs.java b/src/main/java/hbp/mip/utils/Exareme2AlgorithmsSpecs.java new file mode 100644 index 00000000..9b87ca25 --- /dev/null +++ b/src/main/java/hbp/mip/utils/Exareme2AlgorithmsSpecs.java @@ -0,0 +1,21 @@ +package hbp.mip.utils; + +import hbp.mip.models.DTOs.exareme2.Exareme2AlgorithmSpecificationDTO; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.List; + +@Component +public class Exareme2AlgorithmsSpecs { + private List algorithms = new ArrayList<>(); + + + public List getAlgorithms() { + return algorithms; + } + + public void setAlgorithms(List algorithms) { + this.algorithms = algorithms; + } +}