-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #273 from WDAqua/explanationAdjustments
Explanation adjustments
- Loading branch information
Showing
6 changed files
with
421 additions
and
377 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 74 additions & 52 deletions
126
...wdaqua/qanary/commons/triplestoreconnectors/QanaryTripleStoreConnectorQanaryInternal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
qanary_commons/src/main/resources/queries/insert_explanation_data_sparql_query.rq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) . | ||
} |
Oops, something went wrong.