Skip to content

Commit

Permalink
Refactored workflow for method explanation
Browse files Browse the repository at this point in the history
  • Loading branch information
dschiese committed Nov 7, 2024
1 parent 353228d commit fb4d1de
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}"})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,15 +45,20 @@ public String getTemplateComponentInputExplanation(String graphUri, QanaryCompon
return tmplExpService.createInputExplanation(graphUri, component);
}

public List<String> explainComponentMethods(String graph, QanaryComponent component) throws IOException {
public List<String> 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<String> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down Expand Up @@ -466,7 +466,7 @@ public Map<String, String> convertRdfNodeToStringValue(Map<String, RDFNode> 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());
Expand Down Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/explanations/methods/en
Original file line number Diff line number Diff line change
@@ -1 +1 @@
The component ${annotatedBy} executed the method ${method} at ${annotatedAt} on behalf of ${caller} with input variables ${inputVars} and returned ${outputVar}.
The component ${annotatedBy} executed the method ${method} at ${annotatedAt} on behalf of ${caller} with input variables ${inputDataValues} (${inputDataTypes}}) and returned ${outputDataValue} (${outputDataType}}).
2 changes: 1 addition & 1 deletion src/main/resources/queries/fetch_all_logged_methods.rq
Original file line number Diff line number Diff line change
Expand Up @@ -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 .
Expand Down

0 comments on commit fb4d1de

Please sign in to comment.