Skip to content

Commit

Permalink
Comfort changes, WIP support for own SPARQL query passing
Browse files Browse the repository at this point in the history
  • Loading branch information
dschiese committed Nov 7, 2024
1 parent fb4d1de commit f3095f5
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,19 @@ public String getTemplateComponentInputExplanation(String graphUri, QanaryCompon
public List<String> explainComponentMethods(ExplanationMetaData explanationMetaData) throws IOException {
QuerySolutionMap qsm = new QuerySolutionMap();
List<String> 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<String> 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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<String> variables, String template) {
for (String variable : variables) {
template = template.replace("${" + variable + "}", querySolution.get(variable).toString());
}
return template;
}

}
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 ${inputDataValues} (${inputDataTypes}}) and returned ${outputDataValue} (${outputDataType}}).
The component ${annotatedBy} executed the method ${method} at ${annotatedAt} on behalf of ${caller} with input variables ${inputDataValues} (${inputDataTypes}) and returned ${outputDataValue} (${outputDataType}).

0 comments on commit f3095f5

Please sign in to comment.