From fb4d1de344c70e9a789431f466c228b7ecb05b24 Mon Sep 17 00:00:00 2001 From: dschiese Date: Thu, 7 Nov 2024 20:43:53 +0100 Subject: [PATCH] Refactored workflow for method explanation --- .../controller/ExplanationController.java | 11 ++++----- .../services/ExplanationService.java | 24 ++++++++++--------- .../GenerativeExplanationsService.java | 8 +++++++ .../services/TemplateExplanationsService.java | 8 +++---- src/main/resources/explanations/methods/en | 2 +- .../queries/fetch_all_logged_methods.rq | 2 +- 6 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/wse/qanaryexplanationservice/controller/ExplanationController.java b/src/main/java/com/wse/qanaryexplanationservice/controller/ExplanationController.java index dc1e7b3..192a8c1 100644 --- a/src/main/java/com/wse/qanaryexplanationservice/controller/ExplanationController.java +++ b/src/main/java/com/wse/qanaryexplanationservice/controller/ExplanationController.java @@ -4,6 +4,7 @@ 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.ExplanationMetaData; import com.wse.qanaryexplanationservice.helper.pojos.QanaryComponent; import com.wse.qanaryexplanationservice.exceptions.ExplanationExceptionComponent; import com.wse.qanaryexplanationservice.exceptions.ExplanationExceptionPipeline; @@ -220,12 +221,10 @@ public ResponseEntity getComposedExplanation(@RequestBody QanaryExplanationDa } } - @GetMapping(value = "/explainmethods/{graph}/{component}") - @Operation( - summary = "Explains all methods logged for the passed component" - ) - public ResponseEntity getMethodExplanation(@PathVariable String graph, @PathVariable QanaryComponent component) throws IOException { - return new ResponseEntity<>(explanationService.explainComponentMethods(graph, component), HttpStatus.OK); + @GetMapping(value = "/explainmethods") + @Operation() + public ResponseEntity getMethodExplanations(@RequestBody ExplanationMetaData explanationMetaData) throws IOException { + return new ResponseEntity<>(explanationService.explainComponentMethods(explanationMetaData), HttpStatus.OK); } @GetMapping(value = {"/explain/{graph}", "/explain/{graph}/{component}"}) diff --git a/src/main/java/com/wse/qanaryexplanationservice/services/ExplanationService.java b/src/main/java/com/wse/qanaryexplanationservice/services/ExplanationService.java index 9b2363e..1eca04e 100644 --- a/src/main/java/com/wse/qanaryexplanationservice/services/ExplanationService.java +++ b/src/main/java/com/wse/qanaryexplanationservice/services/ExplanationService.java @@ -2,10 +2,7 @@ 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.helper.pojos.*; import com.wse.qanaryexplanationservice.repositories.QanaryRepository; import eu.wdaqua.qanary.commons.triplestoreconnectors.QanaryTripleStoreConnector; import org.apache.commons.lang3.StringUtils; @@ -48,15 +45,20 @@ public String getTemplateComponentInputExplanation(String graphUri, QanaryCompon return tmplExpService.createInputExplanation(graphUri, component); } - public List explainComponentMethods(String graph, QanaryComponent component) throws IOException { + public List explainComponentMethods(ExplanationMetaData explanationMetaData) throws IOException { QuerySolutionMap qsm = new QuerySolutionMap(); - qsm.add("graph", ResourceFactory.createResource(graph)); - ResultSet loggedMethods = qanaryRepository.selectWithResultSet(QanaryTripleStoreConnector.readFileFromResourcesWithMap(SELECT_ALL_LOGGED_METHODS, qsm)); List explanations = new ArrayList<>(); - // Here, decide whether to explain with GenAI or rule-based - loggedMethods.forEachRemaining(qs -> { - explanations.add(tmplExpService.explainMethod(qs)); - }); + qsm.add("graph", ResourceFactory.createResource(explanationMetaData.getGraph().toASCIIString())); + qsm.add("annotatedBy", ResourceFactory.createResource(explanationMetaData.getQanaryComponent().getPrefixedComponentName())); + ResultSet loggedMethodsResultSet = qanaryRepository.selectWithResultSet(QanaryTripleStoreConnector.readFileFromResourcesWithMap(SELECT_ALL_LOGGED_METHODS, qsm)); + + if (explanationMetaData.isDoGenerative()) { + loggedMethodsResultSet.forEachRemaining(querySolution -> { + explanations.add(tmplExpService.explainMethod(querySolution, explanationMetaData.getTemplate())); + }); + } else { + explanations.add(genExpService.explainMethod()); + } return explanations; } diff --git a/src/main/java/com/wse/qanaryexplanationservice/services/GenerativeExplanationsService.java b/src/main/java/com/wse/qanaryexplanationservice/services/GenerativeExplanationsService.java index 9af53ae..518275e 100644 --- a/src/main/java/com/wse/qanaryexplanationservice/services/GenerativeExplanationsService.java +++ b/src/main/java/com/wse/qanaryexplanationservice/services/GenerativeExplanationsService.java @@ -208,6 +208,14 @@ public String getInputDataExplanationPrompt(QanaryComponent component, String bo return prompt; } + /** + * TODO + * @return + */ + public String explainMethod() { + return ""; + } + public String getTemplateExplanation(String graphUri, QanaryComponent component, String lang) throws IOException { return tmplExpService.createOutputExplanation(graphUri, component, lang); } diff --git a/src/main/java/com/wse/qanaryexplanationservice/services/TemplateExplanationsService.java b/src/main/java/com/wse/qanaryexplanationservice/services/TemplateExplanationsService.java index 3aa5721..dc7a1f4 100644 --- a/src/main/java/com/wse/qanaryexplanationservice/services/TemplateExplanationsService.java +++ b/src/main/java/com/wse/qanaryexplanationservice/services/TemplateExplanationsService.java @@ -72,7 +72,7 @@ public class TemplateExplanationsService { private static final String EXPLANATION_NAMESPACE = "urn:qanary:explanations#"; private final String COMPOSED_EXPLANATION_TEMPLATE = "/explanations/input_output_explanation/en"; private final String METHOD_EXPLANATION_TEMPLATE = "/explanations/methods/"; - Logger logger = LoggerFactory.getLogger(TemplateExplanationsService.class); + static Logger logger = LoggerFactory.getLogger(TemplateExplanationsService.class); @Autowired private QanaryRepository qanaryRepository; @Value("${explanations.dataset.limit}") @@ -466,7 +466,7 @@ public Map convertRdfNodeToStringValue(Map map) * @param path Given path * @return String with the file's content */ - public String getStringFromFile(String path) throws RuntimeException { + public static String getStringFromFile(String path) throws RuntimeException { ClassPathResource cpr = new ClassPathResource(path); try { byte[] bdata = FileCopyUtils.copyToByteArray(cpr.getInputStream()); @@ -631,8 +631,8 @@ public String composeInputAndOutputExplanations(String inputExplanation, String .replace("${outputExplanation}", outputExplanation); } - public String explainMethod(QuerySolution qs) { - return METHOD_EXPLANATION_TEMPLATE + public String explainMethod(QuerySolution qs, String template) { + return template .replace("${annotatedBy}", qs.get("annotatedBy").toString()) .replace("${method}", qs.get("method").toString()) .replace("${annotatedAt}", qs.get("annotatedAt").toString()) diff --git a/src/main/resources/explanations/methods/en b/src/main/resources/explanations/methods/en index 4c37396..ccca0f5 100644 --- a/src/main/resources/explanations/methods/en +++ b/src/main/resources/explanations/methods/en @@ -1 +1 @@ -The component ${annotatedBy} executed the method ${method} at ${annotatedAt} on behalf of ${caller} with input variables ${inputVars} and returned ${outputVar}. \ No newline at end of file +The component ${annotatedBy} executed the method ${method} at ${annotatedAt} on behalf of ${caller} with input variables ${inputDataValues} (${inputDataTypes}}) and returned ${outputDataValue} (${outputDataType}}). \ No newline at end of file diff --git a/src/main/resources/queries/fetch_all_logged_methods.rq b/src/main/resources/queries/fetch_all_logged_methods.rq index 4090f5b..0576537 100644 --- a/src/main/resources/queries/fetch_all_logged_methods.rq +++ b/src/main/resources/queries/fetch_all_logged_methods.rq @@ -9,7 +9,7 @@ SELECT ?id ?caller ?method ?outputDataType ?outputDataValue ?annotatedAt ?annot FROM ?graph WHERE { ?id rdf:type qa:AnnotationOfLogMethod ; - prov:actedOnBehalfOf ?caller ; + prov:actedOnBehalfOf ?caller ; # Fetch method name possible, too! qa:methodName ?method ; x:output [ rdf:type ?outputDataType ; rdf:value ?outputDataValue ] ; x:input ?input .