Skip to content

Commit

Permalink
Merge pull request #265 from heinpa/extend-sparql-query-methods
Browse files Browse the repository at this point in the history
Extend methods for creating SPARQL insert queries
  • Loading branch information
anbo-de authored May 16, 2024
2 parents 81de4ee + 9c54b8f commit e666702
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 23 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.11.5</version>
<version>3.12.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
Expand Up @@ -449,7 +449,7 @@ public String getTranslatedTextualRepresentation(String language) throws Excepti

while (resultSet.hasNext()) {
QuerySolution result = resultSet.next();
String translatedQuestionString = result.get("translation").asLiteral().getString();
String translatedQuestionString = result.get("hasBody").asLiteral().getString();
// take the first best result
return translatedQuestionString;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ public abstract class QanaryTripleStoreConnector {
public abstract void update(String sparql) throws SparqlQueryFailed;

/**
* return a readable description of the triplestore endpoint
* Return a readable description of the triplestore endpoint.
*
* @return
*/
public abstract String getFullEndpointDescription();

/**
* get current time in milliseconds
* Get current time in milliseconds.
*/
protected static long getTime() {
return System.currentTimeMillis();
Expand Down Expand Up @@ -76,7 +76,7 @@ protected static Logger getLogger() {
}

/**
* read SPARQL query from files in resources folder
* Read SPARQL query from files in resources folder.
*
* @param filenameWithRelativePath
* @return
Expand All @@ -95,7 +95,7 @@ public static String readFileFromResources(String filenameWithRelativePath) thro
}

/**
* get SELECT query to count the number of triples in a graph
* Get SELECT query to count the number of triples in a graph.
*
* @param graph
* @return
Expand All @@ -115,7 +115,7 @@ public static String getCountAllTriplesInGraph(URI graph) throws IOException {
}

/**
* get SELECT query to count the number AnnotationOfAnswer in a graph
* Get SELECT query to count the number AnnotationOfAnswer in a graph.
*
* @param graph
* @return
Expand All @@ -135,7 +135,7 @@ public static String getAllAnnotationOfAnswerInGraph(URI graph) throws IOExcepti
}

/**
* get SELECT query to get the answer with the highest score in a graph
* Get SELECT query to get the answer SPARQL with the highest score in a graph.
*
* @param graph
* @return
Expand All @@ -155,22 +155,80 @@ public static String getHighestScoreAnnotationOfAnswerInGraph(URI graph) throws
}

/**
* add AnnotationAnswer and AnnotationOfAnswerType to allow annotating typed literals
* as it may be required by Qanary QueryBuilder components
* Get SELECT query to get the answer SPARQL with the lowest index in a graph </br>
* (for implementations where order of created query candidates matters).
*
* @param graph
* @return
* @throws IOException
*/
public static String getLowestIndexAnnotationOfAnswerInGraph(URI graph) throws IOException {
String sparqlQueryString = readFileFromResources("/queries/select_lowestIndex_AnnotationOfAnswerSPARQL.rq");

QuerySolutionMap bindings = new QuerySolutionMap();
bindings.add("graph", ResourceFactory.createResource(graph.toASCIIString()));

ParameterizedSparqlString pq = new ParameterizedSparqlString(sparqlQueryString, bindings);
Query query = QueryFactory.create(pq.toString());
logger.info("generated query:\n{}", query.toString());

return query.toString();
}

/**
* Get SELECT query to get all answer SPARQL in a graph.
*
* @param graph
* @return
* @throws IOException
*/
public static String getAllAnnotationOfAnswerSPARQL(URI graph) throws IOException {
String sparqlQueryString = readFileFromResources("/queries/select_all_AnnotationOfAnswerSPARQL.rq");

QuerySolutionMap bindings = new QuerySolutionMap();
bindings.add("graph", ResourceFactory.createResource(graph.toASCIIString()));

ParameterizedSparqlString pq = new ParameterizedSparqlString(sparqlQueryString, bindings);
Query query = QueryFactory.create(pq.toString());
logger.info("generated query:\n{}", query.toString());

return query.toString();
}

/**
* Get INSERT query for AnnotationAnswer and AnnotationOfAnswerType to annotate typed literals.
*
* @param bindings
* @deprecated
* Annotation of literal values using AnnotationAnswer, as it is done in this method, is
* discouraged. Use {@link #insertAnnotationOfAnswerJson()} instead, to annotate the
* result JSON of a SPARQL query.
* @return
* @throws IOException
*/
@Deprecated
public static String insertAnnotationOfTypedLiteral(QuerySolutionMap bindings) throws IOException {
return readFileFromResourcesWithMap("/queries/insert_one_AnnotationOfTypedLiteral.rq", bindings);
}

/**
* add AnnotationOfAnswerSPARQL as it is done typically in Qanary QueryBuilder
* components
* Get INSERT query to annotate the answer data type.
*
* @param bindings
* @param bindings required bindings for the query: </br>
* ?graph, ?targetQuestion, ?answerDataType, ?confidence, ?application
* @return
* @throws IOException
*/
public static String insertAnnotationOfAnswerDataType(QuerySolutionMap bindings) throws IOException {
return readFileFromResourcesWithMap("/queries/insert_one_AnnotationOfAnswerDataType.rq", bindings);
}

/**
* get INSERT query to annotate SPARQL query that should compute the answer.
*
* @param bindings required bindings for the query: </br>
* ?graph, ?targetQuestion, ?selectQueryThatShouldComputeTheAnswer, </br>
* ?confidence, ?index, ?application
* @return
* @throws IOException
*/
Expand All @@ -179,23 +237,31 @@ public static String insertAnnotationOfAnswerSPARQL(QuerySolutionMap bindings) t
}

/**
* add AnnotationOfAnswerJson as it is done typically in Qanary QueryExecutor
* components
* Get INSERT query to annotate answer JSON.
*
* @param bindings
* @param bindings required bindings for the query: </br>
* ?graph, ?targetQuestion, ?jsonAnswer, ?confidence, ?application
* @return
* @throws IOException
*/
public static String insertAnnotationOfAnswerJson(QuerySolutionMap bindings) throws IOException {
return readFileFromResourcesWithMap("/queries/insert_one_AnnotationOfAnswerJson.rq", bindings);
}

public static String getAnnotationOfAnswerSPARQL(QuerySolutionMap bindings) throws IOException {
return readFileFromResourcesWithMap("/queries/select_all_AnnotationOfAnswerSPARQL.rq", bindings);
/**
* Get INSERT query to annotate an improved question.
*
* @param bindings required bindings for the query: </br>
* ?graph, ?targetQuestion, ?improvedQuestionText, ?confidence, ?application
* @return
* @throws IOException
*/
public static String insertAnnotationOfImprovedQuestion(QuerySolutionMap bindings) throws IOException {
return readFileFromResourcesWithMap("/queries/insert_one_AnnotationOfImprovedQuestion.rq", bindings);
}

/**
* read query from file and apply bindings
* Read query from file and apply bindings.
*
* @param filenameWithRelativePath
* @param bindings
Expand Down Expand Up @@ -237,10 +303,10 @@ public static String readFileFromResourcesWithMap(String filenameWithRelativePat
}

/**
* ensures that files exists in the resources and are non-empty
* Ensures that files exists in the resources and are non-empty.
*
* e.g., useful for component constructors to ensure that SPRARQL query template
* files (*.rq) are valid
* Useful for component constructors to ensure that SPRARQL query template
* files (*.rq) are valid.
*
* @param filenameInResources
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
PREFIX dbr: <http://dbpedia.org/resource/>
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 xsd: <http://www.w3.org/2001/XMLSchema#>

INSERT {
GRAPH ?graph {
?newTypeAnnotation rdf:type qa:AnnotationOfAnswerDataType .
?newTypeAnnotation oa:hasTarget ?targetQuestion .
?newTypeAnnotation oa:hasBody ?annotationOfAnswerDataType .
?newTypeAnnotation qa:score ?confidence .
?newTypeAnnotation oa:annotatedAt ?time .
?newTypeAnnotation oa:annotatedBy ?application .

?annotationOfAnswerDataType rdf:type qa:AnswerDataType .
?annotationOfAnswerDataType rdf:value ?answerDataType .
}
}
WHERE {
BIND (IRI(CONCAT("urn:qanary:annotation:answer:type:", STR(RAND()))) AS ?newTypeAnnotation) .
BIND (IRI(CONCAT("urn:qanary:answer:type:", STR(RAND()))) AS ?annotationOfAnswerDataType) .
BIND (now() as ?time) .
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ GRAPH ?graph {
?newAnnotation oa:hasTarget ?targetQuestion .
?newAnnotation oa:hasBody ?selectQueryThatShouldComputeTheAnswer .
?newAnnotation qa:score ?confidence .
?newAnnotation qa:index ?index .
?newAnnotation oa:annotatedAt ?time .
?newAnnotation oa:annotatedBy ?application .
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
PREFIX qa: <http://www.wdaqua.eu/qa#>
PREFIX oa: <http://www.w3.org/ns/openannotation/core/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
INSERT {
GRAPH ?graph {
?annotationImprovedQuestion a qa:AnnotationOfImprovedQuestion ;
oa:hasTarget ?question ;
oa:hasBody ?improvedQuestion ;
oa:annotatedBy ?application ;
oa:annotatedAt ?time ;
qa:score ?score .

?improvedQuestion a qa:ImprovedQuestion ;
rdf:value ?improvedQuestionText .
}
}
WHERE {
BIND (IRI(str(RAND())) AS ?annotationImprovedQuestion) .
BIND (IRI(str(RAND())) AS ?improvedQuestion) .
BIND (now() AS ?time) .
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ WHERE {
?annotationId oa:hasTarget ?hasTarget ;
oa:hasBody ?hasBody .
OPTIONAL {
?annotationId qa:score ?score .
?annotationId qa:score ?score .
}
OPTIONAL {
?annotationId qa:index ?index .
}
?annotationId oa:annotatedAt ?annotatedAt ;
oa:annotatedBy ?annotatedBy .
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
PREFIX dbr: <http://dbpedia.org/resource/>
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 xsd: <http://www.w3.org/2001/XMLSchema#>

SELECT *
FROM ?graph
WHERE {
?newAnnotation rdf:type qa:AnnotationOfAnswerSPARQL .
?newAnnotation oa:hasTarget ?targetQuestion .
?newAnnotation oa:hasBody ?selectQueryThatShouldComputeTheAnswer .
?newAnnotation qa:index ?index .
?newAnnotation oa:annotatedAt ?time .

{
SELECT ?time
WHERE {
?newAnnotation rdf:type qa:AnnotationOfAnswerSPARQL .
?newAnnotation oa:annotatedAt ?time .
}
ORDER BY DESC(?time)
LIMIT 2
}

?newAnnotation oa:annotatedBy ?application .
}
ORDER BY ASC(?index)
LIMIT 1

0 comments on commit e666702

Please sign in to comment.