Skip to content

Commit

Permalink
Merge pull request #273 from WDAqua/explanationAdjustments
Browse files Browse the repository at this point in the history
Explanation adjustments
  • Loading branch information
dschiese authored May 22, 2024
2 parents 1b7de67 + ce3f89e commit d238d6b
Show file tree
Hide file tree
Showing 6 changed files with 421 additions and 377 deletions.
2 changes: 1 addition & 1 deletion qanary_commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>eu.wdaqua.qanary</groupId>
<artifactId>qa.commons</artifactId>
<version>3.12.1</version>
<version>3.13.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,71 +1,93 @@
package eu.wdaqua.qanary.commons.triplestoreconnectors;

import java.net.URI;
import java.net.URISyntaxException;

import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.ResultSet;
import org.apache.jena.query.ResultSetFactory;
import org.apache.jena.query.ResultSetRewindable;
import eu.wdaqua.qanary.exceptions.SparqlQueryFailed;
import org.apache.jena.query.*;
import org.apache.jena.rdf.model.ResourceFactory;
import org.apache.jena.rdfconnection.RDFConnection;
import org.apache.jena.sparql.exec.QueryExecBuilder; // QueryExecBuilder is required for RDFConnection
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import eu.wdaqua.qanary.exceptions.SparqlQueryFailed;
import java.net.URI;
import java.net.URISyntaxException;

/**
* simple connection to open endpoint without authorization using the Apache Jena library
*
* @author AnBo
*
* @author AnBo
*/
public class QanaryTripleStoreConnectorQanaryInternal extends QanaryTripleStoreConnector {
private static final Logger logger = LoggerFactory.getLogger(QanaryTripleStoreConnectorQanaryInternal.class);
private URI endpoint;
private RDFConnection connection;

public QanaryTripleStoreConnectorQanaryInternal(URI endpoint) throws URISyntaxException {
this.endpoint = endpoint;
this.connect();
}
private static final Logger logger = LoggerFactory.getLogger(QanaryTripleStoreConnectorQanaryInternal.class);
private URI endpoint;
private RDFConnection connection;
private String applicationName;

public QanaryTripleStoreConnectorQanaryInternal(URI endpoint, String applicationName) throws URISyntaxException {
this.setApplicationName(applicationName);
this.endpoint = endpoint;
this.connect();
}

public String getApplicationName() {
return applicationName;
}

private void setApplicationName(String applicationName) {
this.applicationName = "urn:qanary:" + applicationName;
}

private URI getEndpoint() {
return this.endpoint;
}

@Override
public void connect() {
org.apache.jena.query.ARQ.init(); // maybe it helps to prevent problems?
this.connection = RDFConnection.connect(this.getEndpoint().toASCIIString());
}

@Override
public ResultSet select(String sparql) throws SparqlQueryFailed {
QueryExecution queryExecution = this.connection.query(sparql);
logData(sparql);
ResultSet myResultSet = queryExecution.execSelect();
ResultSetRewindable resultSet = ResultSetFactory.makeRewindable(myResultSet);
return resultSet;
}

private URI getEndpoint() {
return this.endpoint;
}

@Override
public void connect() {
org.apache.jena.query.ARQ.init(); // maybe it helps to prevent problems?
this.connection = RDFConnection.connect(this.getEndpoint().toASCIIString());
}
private void logData(String sparql) {
QuerySolutionMap querySolutionMap = new QuerySolutionMap();
Query query = QueryFactory.create(sparql);
querySolutionMap.add("graph", ResourceFactory.createResource(query.getGraphURIs().get(0)));
querySolutionMap.add("component", ResourceFactory.createResource(this.applicationName));
querySolutionMap.add("questionID", ResourceFactory.createResource("urn:qanary:currentQuestion"));
querySolutionMap.add("body", ResourceFactory.createPlainLiteral(sparql));
try {
this.update(QanaryTripleStoreConnector.readFileFromResourcesWithMap("/queries/insert_explanation_data_sparql_query.rq", querySolutionMap));
} catch (Exception e) {
getLogger().error("Logging failed, {}", e);
}

@Override
public ResultSet select(String sparql) throws SparqlQueryFailed {
QueryExecution queryExecution = this.connection.query(sparql);
ResultSet myResultSet = queryExecution.execSelect();
ResultSetRewindable resultSet = ResultSetFactory.makeRewindable(myResultSet);
return resultSet;
}
}

@Override
public void update(String sparql, URI graph) throws SparqlQueryFailed {
this.update(sparql);
}
@Override
public void update(String sparql, URI graph) throws SparqlQueryFailed {
this.update(sparql);
}

@Override
public void update(String sparql) throws SparqlQueryFailed {
logger.debug("UPDATE on {}: {}", this.getEndpoint().toASCIIString(), sparql);
this.connection.update(sparql);
}
@Override
public void update(String sparql) throws SparqlQueryFailed {
logger.debug("UPDATE on {}: {}", this.getEndpoint().toASCIIString(), sparql);
this.connection.update(sparql);
}

@Override
public boolean ask(String sparql) throws SparqlQueryFailed {
return this.connection.queryAsk(sparql);
}
@Override
public boolean ask(String sparql) throws SparqlQueryFailed {
logData(sparql);
return this.connection.queryAsk(sparql);
}

@Override
public String getFullEndpointDescription() {
return "simple open endpoint without authorization: " + this.getEndpoint().toASCIIString();
}
@Override
public String getFullEndpointDescription() {
return "simple open endpoint without authorization: " + this.getEndpoint().toASCIIString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
PREFIX oa: <http://www.w3.org/ns/openannotation/core/>
PREFIX qa: <http://www.wdaqua.eu/qa#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

INSERT {
GRAPH ?graph {
?newAnnotation rdf:type qa:AnnotationOfLogQuery .
?newAnnotation oa:hasBody ?body .
?newAnnotation oa:annotatedAt ?time .
?newAnnotation oa:annotatedBy ?component .
?newAnnotation oa:hasTarget ?questionID .
}
}
WHERE {
BIND (IRI(CONCAT("urn:qanary:", STR(RAND()))) AS ?newAnnotation) .
BIND (now() as ?time) .
}
Loading

0 comments on commit d238d6b

Please sign in to comment.