Skip to content

Commit

Permalink
Added support for passing sparql query
Browse files Browse the repository at this point in the history
  • Loading branch information
dschiese committed Nov 8, 2024
1 parent 9ce5736 commit b9dde4f
Showing 1 changed file with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
import com.wse.qanaryexplanationservice.repositories.QanaryRepository;
import eu.wdaqua.qanary.commons.triplestoreconnectors.QanaryTripleStoreConnector;
import org.apache.commons.lang3.StringUtils;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.QuerySolutionMap;
import org.apache.jena.query.ResultSet;
import org.apache.jena.query.*;
import org.apache.jena.rdf.model.ResourceFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -49,16 +47,18 @@ public String getTemplateComponentInputExplanation(String graphUri, QanaryCompon
public String explainComponentMethods(ExplanationMetaData explanationMetaData) throws IOException {
QuerySolutionMap qsm = new QuerySolutionMap();
AtomicReference<String> prefixExplanation = new AtomicReference<>();
AtomicInteger i = new AtomicInteger(1);
StringBuilder explanationItems = new StringBuilder();

qsm.add("graph", ResourceFactory.createResource(explanationMetaData.getGraph().toASCIIString()));
String query = QanaryTripleStoreConnector.readFileFromResourcesWithMap(SELECT_ALL_LOGGED_METHODS, qsm);
String query = explanationMetaData.getRequestQuery() == null ?
QanaryTripleStoreConnector.readFileFromResourcesWithMap(SELECT_ALL_LOGGED_METHODS, qsm) :
transformQueryStringToQuery(explanationMetaData.getRequestQuery(), qsm);

logger.debug("Query: {}", query);
ResultSet loggedMethodsResultSet = qanaryRepository.selectWithResultSet(query);
List<String> variables = loggedMethodsResultSet.getResultVars();

AtomicInteger i = new AtomicInteger(1);

if (!explanationMetaData.isDoGenerative()) {
loggedMethodsResultSet.forEachRemaining(querySolution -> {
if(prefixExplanation.get() == null) {
Expand All @@ -72,6 +72,12 @@ public String explainComponentMethods(ExplanationMetaData explanationMetaData) t
return prefixExplanation + explanationItems.toString();
}

public String transformQueryStringToQuery(String queryString, QuerySolutionMap querySolutionMap) {
ParameterizedSparqlString pq = new ParameterizedSparqlString(queryString, querySolutionMap);
Query query = QueryFactory.create(pq.toString());
return query.toString();
}

/**
* Controller called method to start the process explaining several components with both approaches;
* the rulebased and the generative one.
Expand Down

0 comments on commit b9dde4f

Please sign in to comment.