diff --git a/src/main/java/com/wse/qanaryexplanationservice/controller/ExplanationController.java b/src/main/java/com/wse/qanaryexplanationservice/controller/ExplanationController.java index 5e08b56..5e43fd9 100644 --- a/src/main/java/com/wse/qanaryexplanationservice/controller/ExplanationController.java +++ b/src/main/java/com/wse/qanaryexplanationservice/controller/ExplanationController.java @@ -17,6 +17,8 @@ import org.springframework.web.bind.annotation.*; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; @RestController public class ExplanationController { @@ -218,6 +220,18 @@ public ResponseEntity getComposedExplanation(@RequestBody QanaryExplanationDa } } + @PostMapping(value = "/explainmethods/{graph}/{component}") + @Operation( + summary = "Explains all methods logged for the passed component" + ) + public ResponseEntity getMethodExplanation(@PathVariable String graph, @PathVariable QanaryComponent component) { + try { + return new ResponseEntity<>(explanationService.explainComponentMethods(graph, component), HttpStatus.OK); + } catch(Exception e) { + + } + } + @GetMapping(value = {"/explain/{graph}", "/explain/{graph}/{component}"}) @Operation() public ResponseEntity getComposedExplanation( diff --git a/src/main/java/com/wse/qanaryexplanationservice/services/ExplanationService.java b/src/main/java/com/wse/qanaryexplanationservice/services/ExplanationService.java index e813d99..4067c62 100644 --- a/src/main/java/com/wse/qanaryexplanationservice/services/ExplanationService.java +++ b/src/main/java/com/wse/qanaryexplanationservice/services/ExplanationService.java @@ -1,7 +1,5 @@ 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; @@ -21,6 +19,7 @@ import org.springframework.stereotype.Service; import java.io.IOException; +import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -29,6 +28,7 @@ public class ExplanationService { private final Logger logger = LoggerFactory.getLogger(ExplanationService.class); private final String SELECT_PIPELINE_INFORMATION = "/queries/select_pipeline_information.rq"; + private final String SELECT_ALL_LOGGED_METHODS = "/queries/fetch_all_logged_methods.rq"; @Autowired private TemplateExplanationsService tmplExpService; @Autowired @@ -48,6 +48,16 @@ public String getTemplateComponentInputExplanation(String graphUri, QanaryCompon return tmplExpService.createInputExplanation(graphUri, component); } + public List explainComponentMethods(String graph, QanaryComponent component) { + ResultSet loggedMethods = qanaryRepository.selectWithResultSet(SELECT_ALL_LOGGED_METHODS); + List explanations = new ArrayList<>(); + // Here, decide whether to explain with GenAI or rule-based + loggedMethods.forEachRemaining(qs -> { + explanations.add(tmplExpService.explainMethod(qs)); + }); + return explanations; + } + /** * Controller called method to start the process explaining several components with both approaches; * the rulebased and the generative one. diff --git a/src/main/java/com/wse/qanaryexplanationservice/services/TemplateExplanationsService.java b/src/main/java/com/wse/qanaryexplanationservice/services/TemplateExplanationsService.java index 7f60b8d..3aa5721 100644 --- a/src/main/java/com/wse/qanaryexplanationservice/services/TemplateExplanationsService.java +++ b/src/main/java/com/wse/qanaryexplanationservice/services/TemplateExplanationsService.java @@ -71,6 +71,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); @Autowired private QanaryRepository qanaryRepository; @@ -630,4 +631,14 @@ public String composeInputAndOutputExplanations(String inputExplanation, String .replace("${outputExplanation}", outputExplanation); } + public String explainMethod(QuerySolution qs) { + return METHOD_EXPLANATION_TEMPLATE + .replace("${annotatedBy}", qs.get("annotatedBy").toString()) + .replace("${method}", qs.get("method").toString()) + .replace("${annotatedAt}", qs.get("annotatedAt").toString()) + .replace("${caller}", qs.get("caller").toString()) + .replace("${inputVars}", null) // TODO + .replace("${outputVar}", null); // TODO + } + }