From cb7d2de5474dd9dedbe82315172770d4208cb3f8 Mon Sep 17 00:00:00 2001 From: Dennis Schiese <135758800+dschiese@users.noreply.github.com> Date: Thu, 8 Aug 2024 15:19:32 +0200 Subject: [PATCH] Revert "Explanation api qanary" --- pom.xml | 2 +- .../controller/ExplanationController.java | 20 ++--- .../exceptions/ExplanationException.java | 9 -- .../ExplanationExceptionComponent.java | 7 -- .../ExplanationExceptionPipeline.java | 7 -- .../helper/dtos/QanaryExplanationData.java | 60 ------------- .../services/ExplanationService.java | 88 +------------------ .../services/TemplateExplanationsService.java | 12 --- .../explanations/pipeline_component/en_prefix | 3 - .../TemplateExplanationsServiceTest.java | 2 +- 10 files changed, 13 insertions(+), 197 deletions(-) delete mode 100644 src/main/java/com/wse/qanaryexplanationservice/exceptions/ExplanationException.java delete mode 100644 src/main/java/com/wse/qanaryexplanationservice/exceptions/ExplanationExceptionComponent.java delete mode 100644 src/main/java/com/wse/qanaryexplanationservice/exceptions/ExplanationExceptionPipeline.java delete mode 100644 src/main/java/com/wse/qanaryexplanationservice/helper/dtos/QanaryExplanationData.java delete mode 100644 src/main/resources/explanations/pipeline_component/en_prefix diff --git a/pom.xml b/pom.xml index aff667d..643fbbf 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ com.wse qanary-explanation-service - 3.6.0 + 3.5.4 Qanary explanation service Webservice for rule-based explanation of QA-Systems as well as specific components diff --git a/src/main/java/com/wse/qanaryexplanationservice/controller/ExplanationController.java b/src/main/java/com/wse/qanaryexplanationservice/controller/ExplanationController.java index 2c45ff7..2677c4c 100644 --- a/src/main/java/com/wse/qanaryexplanationservice/controller/ExplanationController.java +++ b/src/main/java/com/wse/qanaryexplanationservice/controller/ExplanationController.java @@ -1,12 +1,8 @@ package com.wse.qanaryexplanationservice.controller; -import com.wse.qanaryexplanationservice.exceptions.ExplanationException; import com.wse.qanaryexplanationservice.helper.dtos.ComposedExplanationDTO; -import com.wse.qanaryexplanationservice.helper.dtos.QanaryExplanationData; import com.wse.qanaryexplanationservice.helper.pojos.ComposedExplanation; import com.wse.qanaryexplanationservice.helper.pojos.QanaryComponent; -import com.wse.qanaryexplanationservice.exceptions.ExplanationExceptionComponent; -import com.wse.qanaryexplanationservice.exceptions.ExplanationExceptionPipeline; import com.wse.qanaryexplanationservice.services.ExplanationService; import io.swagger.v3.oas.annotations.Operation; import org.slf4j.Logger; @@ -203,19 +199,21 @@ public ResponseEntity getComposedExplanationOutputData(@RequestBody ComposedE /** * Endpoint explaining a component / pipeline input and output data - * @param body TODO + * @param graph + * @param component * @return * @throws IOException */ - @PostMapping(value = {"/explain"}) + @GetMapping(value = {"/explain/{graph}", "/explain/{graph}/{component}"}) @Operation() - public ResponseEntity getComposedExplanation(@RequestBody QanaryExplanationData body) throws ExplanationException { // TODO: Extend methods + public ResponseEntity getComposedExplanation( + @PathVariable(required = true) String graph, + @PathVariable(required = false) String component) throws IOException { try { - logger.info(body.getComponent()); - String explanation = explanationService.explain(body); + String explanation = explanationService.getComposedExplanation(graph, component); return new ResponseEntity<>(explanation, HttpStatus.OK); - } catch (Exception e) { - return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); + } catch (IOException e) { + return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST); } } diff --git a/src/main/java/com/wse/qanaryexplanationservice/exceptions/ExplanationException.java b/src/main/java/com/wse/qanaryexplanationservice/exceptions/ExplanationException.java deleted file mode 100644 index 06fbffb..0000000 --- a/src/main/java/com/wse/qanaryexplanationservice/exceptions/ExplanationException.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.wse.qanaryexplanationservice.exceptions; - -public class ExplanationException extends Exception { - - - public ExplanationException(String message) { - super(message); - } -} diff --git a/src/main/java/com/wse/qanaryexplanationservice/exceptions/ExplanationExceptionComponent.java b/src/main/java/com/wse/qanaryexplanationservice/exceptions/ExplanationExceptionComponent.java deleted file mode 100644 index 3b929cc..0000000 --- a/src/main/java/com/wse/qanaryexplanationservice/exceptions/ExplanationExceptionComponent.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.wse.qanaryexplanationservice.exceptions; - -public class ExplanationExceptionComponent extends ExplanationException { - public ExplanationExceptionComponent() { - super("Error code 1"); // - } -} diff --git a/src/main/java/com/wse/qanaryexplanationservice/exceptions/ExplanationExceptionPipeline.java b/src/main/java/com/wse/qanaryexplanationservice/exceptions/ExplanationExceptionPipeline.java deleted file mode 100644 index 898697a..0000000 --- a/src/main/java/com/wse/qanaryexplanationservice/exceptions/ExplanationExceptionPipeline.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.wse.qanaryexplanationservice.exceptions; - -public class ExplanationExceptionPipeline extends ExplanationException { - public ExplanationExceptionPipeline() { - super("Error code 2"); // Pipeline explanation creation error - } -} diff --git a/src/main/java/com/wse/qanaryexplanationservice/helper/dtos/QanaryExplanationData.java b/src/main/java/com/wse/qanaryexplanationservice/helper/dtos/QanaryExplanationData.java deleted file mode 100644 index dafdfc4..0000000 --- a/src/main/java/com/wse/qanaryexplanationservice/helper/dtos/QanaryExplanationData.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.wse.qanaryexplanationservice.helper.dtos; - -import com.fasterxml.jackson.annotation.JsonIgnore; - -import java.util.List; -import java.util.Map; - -public class QanaryExplanationData { - - private String graph; - private String questionId; - private String component; - private String serverHost; - private Map explanations; - - public QanaryExplanationData() { - - } - - public Map getExplanations() { - return explanations; - } - - public void setExplanations(Map explanations) { - this.explanations = explanations; - } - - public String getComponent() { - return component; - } - - public String getGraph() { - return graph; - } - - public String getQuestionId() { - return questionId; - } - - public void setQuestionId(String questionId) { - this.questionId = questionId; - } - - public void setComponent(String component) { - this.component = component; - } - - public void setGraph(String graph) { - this.graph = graph; - } - - public String getServerHost() { - return serverHost; - } - - public void setServerHost(String serverHost) { - this.serverHost = serverHost; - } - -} diff --git a/src/main/java/com/wse/qanaryexplanationservice/services/ExplanationService.java b/src/main/java/com/wse/qanaryexplanationservice/services/ExplanationService.java index fe604d2..40a62f9 100644 --- a/src/main/java/com/wse/qanaryexplanationservice/services/ExplanationService.java +++ b/src/main/java/com/wse/qanaryexplanationservice/services/ExplanationService.java @@ -1,16 +1,12 @@ package com.wse.qanaryexplanationservice.services; -import com.wse.qanaryexplanationservice.exceptions.ExplanationExceptionComponent; -import com.wse.qanaryexplanationservice.exceptions.ExplanationExceptionPipeline; import com.wse.qanaryexplanationservice.helper.dtos.ComposedExplanationDTO; -import com.wse.qanaryexplanationservice.helper.dtos.QanaryExplanationData; import com.wse.qanaryexplanationservice.helper.pojos.ComposedExplanation; import com.wse.qanaryexplanationservice.helper.pojos.GenerativeExplanationObject; import com.wse.qanaryexplanationservice.helper.pojos.GenerativeExplanationRequest; import com.wse.qanaryexplanationservice.helper.pojos.QanaryComponent; import com.wse.qanaryexplanationservice.repositories.QanaryRepository; import eu.wdaqua.qanary.commons.triplestoreconnectors.QanaryTripleStoreConnector; -import org.apache.commons.lang3.StringUtils; import org.apache.jena.query.QuerySolution; import org.apache.jena.query.QuerySolutionMap; import org.apache.jena.query.ResultSet; @@ -19,10 +15,10 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.web.reactive.function.client.WebClient; import java.io.IOException; import java.util.List; -import java.util.Map; @Service public class ExplanationService { @@ -130,10 +126,6 @@ public String explainPipelineOutput(String graphUri) throws IOException { return tmplExpService.getPipelineOutputExplanation(results, graphUri); } - public String explainPipelineOutput(String graphUri, Map explanations) { - return tmplExpService.getPipelineOutputExplanation(explanations, graphUri); - } - public String explainPipelineInput(String graphUri) throws IOException { ResultSet results = getPipelineInformation(graphUri); String questionId = ""; @@ -154,9 +146,7 @@ public ResultSet getPipelineInformation(String graphUri) throws IOException { return qanaryRepository.selectWithResultSet(sparql); } - public String getComposedExplanation(QanaryExplanationData body) throws IOException { - String graph = body.getGraph(); - String component = body.getComponent(); + public String getComposedExplanation(String graph, String component) throws IOException { String explanation = null; String inputExplanation = null; String outputExplanation = null; @@ -175,78 +165,4 @@ public String getTemplateComponentOutputExplanation(String graph, QanaryComponen return tmplExpService.createOutputExplanation(graph, component, lang); } - protected String getComponentExplanation(String graph, QanaryComponent qanaryComponent) throws IOException { - return tmplExpService.composeInputAndOutputExplanations( - getTemplateComponentInputExplanation(graph, qanaryComponent), - getTemplateComponentOutputExplanation(graph, qanaryComponent, "en"), - qanaryComponent.getComponentName() - ); - } - - protected String getPipelineExplanation(String graph, Map explanations) throws IOException { - return tmplExpService.composeInputAndOutputExplanations( - explainPipelineInput(graph), - explainPipelineOutput(graph, explanations), - null - ); - } - - // Deprecated, alt. approach - /* - public String explain(QanaryExplanationData explanationData) throws ExplanationExceptionComponent, ExplanationExceptionPipeline { - if(explanationData.getExplanations() != null) { // It's a pipeline (as component) -> Composes explanations - try { - return getPipelineExplanation( - explanationData.getGraph(), - explanationData.getExplanations() - ); - } catch(Exception e) { - throw new ExplanationExceptionPipeline(); - } - } - else { // It's a component - QanaryComponent qanaryComponent = new QanaryComponent(explanationData.getComponent()); - try { - return getComponentExplanation(explanationData.getGraph(), qanaryComponent); - } catch (IOException e) { - e.printStackTrace(); - throw new ExplanationExceptionComponent(); - } - } - } - */ - - public String explain(QanaryExplanationData data) { - logger.info("Explaining ..."); - if(data.getExplanations() == null || data.getExplanations().isEmpty()) { // componentName, questionId and graph provided // component-based explanation - QanaryComponent qanaryComponent = new QanaryComponent(data.getComponent()); - try { - return getComponentExplanation(data.getGraph(), qanaryComponent); // TODO: Add lang-support - } catch (IOException e) { - e.printStackTrace(); - } - } - else if (data.getComponent() != "" || data.getComponent() != null){ // componentName, componentExplanations, questionId and graph are provided // PaC-based explanation - String explanationTemplate = tmplExpService.getStringFromFile("/explanations/pipeline_component/en_prefix"); - String components = StringUtils.join(data.getExplanations().keySet().toArray(), ", "); - return explanationTemplate - .replace("${component}", data.getComponent()) - .replace("${components}", components) - .replace("${componentsAndExplanations}", composeComponentExplanations(data.getExplanations())); - } - else { // only questionId and graph are provided // System-based explanation - // TODO: Implement. Extend pipeline with /explain or handle it here? - } - return null; - } - - public String composeComponentExplanations(Map componentAndExplanation) { - StringBuilder composedExplanations = new StringBuilder(); - componentAndExplanation.forEach((k,v) -> { - composedExplanations.append (k + ": " + v + "\n\n"); - }); - return composedExplanations.toString(); - } - } - diff --git a/src/main/java/com/wse/qanaryexplanationservice/services/TemplateExplanationsService.java b/src/main/java/com/wse/qanaryexplanationservice/services/TemplateExplanationsService.java index 43271ed..7a55919 100644 --- a/src/main/java/com/wse/qanaryexplanationservice/services/TemplateExplanationsService.java +++ b/src/main/java/com/wse/qanaryexplanationservice/services/TemplateExplanationsService.java @@ -590,7 +590,6 @@ public String getPipelineInputExplanation(String question) { return explanation.replace("${question}", question); } - // Computes the explanation itself public String getPipelineOutputExplanation(ResultSet results, String graphUri) { String explanation = getStringFromFile("/explanations/pipeline/en_prefix").replace("${graph}", graphUri); String componentTemplate = getStringFromFile("/explanations/pipeline/en_list_item"); @@ -602,17 +601,6 @@ public String getPipelineOutputExplanation(ResultSet results, String graphUri) { return explanation + " " + StringUtils.join(explanations, ", "); } - // Composes the passed explanations - public String getPipelineOutputExplanation(Map explanations, String graphUri) { - String explanation = getStringFromFile("/explanations/pipeline/en_prefix").replace("${graph}", graphUri); - String componentTemplate = getStringFromFile("/explanations/pipeline/en_list_item"); - List explanationsList = new ArrayList<>(); - explanations.forEach((key,value) -> { - explanationsList.add(componentTemplate.replace("${component}", key).replace("${componentExplanation}", value)); - }); - return explanation + "\n" + StringUtils.join(explanationsList,"\n\n"); - } - public String composeInputAndOutputExplanations(String inputExplanation, String outputExplanation, String componentUri) throws IOException { String explanationTemplate = getStringFromFile(COMPOSED_EXPLANATION_TEMPLATE); String component = componentUri == null ? "pipeline" : "component " + componentUri; diff --git a/src/main/resources/explanations/pipeline_component/en_prefix b/src/main/resources/explanations/pipeline_component/en_prefix deleted file mode 100644 index 2767ce1..0000000 --- a/src/main/resources/explanations/pipeline_component/en_prefix +++ /dev/null @@ -1,3 +0,0 @@ -The pipeline component ${component} has executed the components ${components} with the following explanations: - -${componentsAndExplanations} \ No newline at end of file diff --git a/src/test/java/com/wse/qanaryexplanationservice/services/TemplateExplanationsServiceTest.java b/src/test/java/com/wse/qanaryexplanationservice/services/TemplateExplanationsServiceTest.java index e75b96f..3b220fc 100644 --- a/src/test/java/com/wse/qanaryexplanationservice/services/TemplateExplanationsServiceTest.java +++ b/src/test/java/com/wse/qanaryexplanationservice/services/TemplateExplanationsServiceTest.java @@ -247,7 +247,7 @@ public void pipelineOutputExplanationTest() throws IOException { String explanation = templateExplanationsService.getPipelineOutputExplanation(results, graph); File file = new File(Objects.requireNonNull(classLoader.getResource("expected_explanations/pipeline_output")).getFile()); String expectedOutcome = new String(Files.readAllBytes(file.toPath())); - // assertEquals(expectedOutcome, explanation); + assertEquals(expectedOutcome, explanation); } }