diff --git a/src/main/java/com/wse/qanaryexplanationservice/helper/pojos/ExplanationMetaData.java b/src/main/java/com/wse/qanaryexplanationservice/helper/pojos/ExplanationMetaData.java new file mode 100644 index 0000000..9ec9c4d --- /dev/null +++ b/src/main/java/com/wse/qanaryexplanationservice/helper/pojos/ExplanationMetaData.java @@ -0,0 +1,81 @@ +package com.wse.qanaryexplanationservice.helper.pojos; + +import com.wse.qanaryexplanationservice.services.TemplateExplanationsService; +import org.apache.jena.query.Query; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.URI; +import java.net.URISyntaxException; + +/** + * Contains relevant data for the creation of (template-based) explanations + */ +public class ExplanationMetaData { + + private QanaryComponent qanaryComponent; + private URI graph; + private String template; + private boolean doGenerative; + private final Logger logger = LoggerFactory.getLogger(ExplanationMetaData.class); + private static String DEFAULT_METHOD_TEMPLATE_PATH = "/explanations/methods/en"; // TODO: Set lang? + private String requestQuery; + + public ExplanationMetaData(String qanaryComponent, String graph, String template, boolean doGenerative, String query) throws URISyntaxException { + this.qanaryComponent = new QanaryComponent(qanaryComponent); + this.graph = new URI(graph); + this.doGenerative = doGenerative; + this.template = checkTemplateValidity(template); + } + + // TODO: Relevant to implement? How to do it, if we want to use it for different templates? + public String checkTemplateValidity(String template) { + if(template == null) { + logger.warn("Using default template as no template was passed."); + return TemplateExplanationsService.getStringFromFile(DEFAULT_METHOD_TEMPLATE_PATH); + } else { + // TODO: Validity check, see method comment + return template; + } + } + + public boolean isDoGenerative() { + return doGenerative; + } + + public void setDoGenerative(boolean doGenerative) { + this.doGenerative = doGenerative; + } + + public QanaryComponent getQanaryComponent() { + return qanaryComponent; + } + + public String getTemplate() { + return template; + } + + public URI getGraph() { + return graph; + } + + public void setGraph(URI graph) { + this.graph = graph; + } + + public void setQanaryComponent(QanaryComponent qanaryComponent) { + this.qanaryComponent = qanaryComponent; + } + + public void setTemplate(String template) { + this.template = template; + } + + public String getRequestQuery() { + return requestQuery; + } + + public void setRequestQuery(String requestQuery) { + this.requestQuery = requestQuery; + } +} diff --git a/src/main/java/com/wse/qanaryexplanationservice/services/ExplanationService.java b/src/main/java/com/wse/qanaryexplanationservice/services/ExplanationService.java index 1eca04e..1f955e7 100644 --- a/src/main/java/com/wse/qanaryexplanationservice/services/ExplanationService.java +++ b/src/main/java/com/wse/qanaryexplanationservice/services/ExplanationService.java @@ -48,17 +48,19 @@ public String getTemplateComponentInputExplanation(String graphUri, QanaryCompon public List explainComponentMethods(ExplanationMetaData explanationMetaData) throws IOException { QuerySolutionMap qsm = new QuerySolutionMap(); List explanations = new ArrayList<>(); + 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)); + String query = QanaryTripleStoreConnector.readFileFromResourcesWithMap(SELECT_ALL_LOGGED_METHODS, qsm); + logger.debug("Query: {}", query); + ResultSet loggedMethodsResultSet = qanaryRepository.selectWithResultSet(query); + List variables = loggedMethodsResultSet.getResultVars(); - if (explanationMetaData.isDoGenerative()) { + if (!explanationMetaData.isDoGenerative()) { loggedMethodsResultSet.forEachRemaining(querySolution -> { - explanations.add(tmplExpService.explainMethod(querySolution, explanationMetaData.getTemplate())); + explanations.add(tmplExpService.replacePlaceholdersWithVarsFromQuerySolution(querySolution, variables, explanationMetaData.getTemplate())); }); - } else { - explanations.add(genExpService.explainMethod()); - } + } else explanations.add(genExpService.explainMethod()); + return explanations; } diff --git a/src/main/java/com/wse/qanaryexplanationservice/services/TemplateExplanationsService.java b/src/main/java/com/wse/qanaryexplanationservice/services/TemplateExplanationsService.java index dc7a1f4..e55dc81 100644 --- a/src/main/java/com/wse/qanaryexplanationservice/services/TemplateExplanationsService.java +++ b/src/main/java/com/wse/qanaryexplanationservice/services/TemplateExplanationsService.java @@ -1,5 +1,6 @@ package com.wse.qanaryexplanationservice.services; +import com.wse.qanaryexplanationservice.helper.pojos.ExplanationMetaData; import com.wse.qanaryexplanationservice.helper.pojos.QanaryComponent; import com.wse.qanaryexplanationservice.repositories.QanaryRepository; import eu.wdaqua.qanary.commons.triplestoreconnectors.QanaryTripleStoreConnector; @@ -631,14 +632,12 @@ public String composeInputAndOutputExplanations(String inputExplanation, String .replace("${outputExplanation}", outputExplanation); } - 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()) - .replace("${caller}", qs.get("caller").toString()) - .replace("${inputVars}", null) // TODO - .replace("${outputVar}", null); // TODO + // TODO: Make the same as for the template for SPARQL queries, too. Hence, this would provide a great and flexible playground for both researcher and user. + public String replacePlaceholdersWithVarsFromQuerySolution(QuerySolution querySolution, List variables, String template) { + for (String variable : variables) { + template = template.replace("${" + variable + "}", querySolution.get(variable).toString()); + } + return template; } } diff --git a/src/main/resources/explanations/methods/en b/src/main/resources/explanations/methods/en index ccca0f5..611750c 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 ${inputDataValues} (${inputDataTypes}}) and returned ${outputDataValue} (${outputDataType}}). \ 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