diff --git a/qanary_commons/pom.xml b/qanary_commons/pom.xml index 9f8aeeee..a096ec6f 100644 --- a/qanary_commons/pom.xml +++ b/qanary_commons/pom.xml @@ -2,7 +2,7 @@ 4.0.0 eu.wdaqua.qanary qa.commons - 3.11.5 + 3.12.0 org.springframework.boot spring-boot-starter-parent diff --git a/qanary_commons/src/main/java/eu/wdaqua/qanary/commons/QanaryQuestion.java b/qanary_commons/src/main/java/eu/wdaqua/qanary/commons/QanaryQuestion.java index cc993c1b..a352a713 100644 --- a/qanary_commons/src/main/java/eu/wdaqua/qanary/commons/QanaryQuestion.java +++ b/qanary_commons/src/main/java/eu/wdaqua/qanary/commons/QanaryQuestion.java @@ -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; } diff --git a/qanary_commons/src/main/java/eu/wdaqua/qanary/commons/triplestoreconnectors/QanaryTripleStoreConnector.java b/qanary_commons/src/main/java/eu/wdaqua/qanary/commons/triplestoreconnectors/QanaryTripleStoreConnector.java index da4588ab..b9a212a6 100644 --- a/qanary_commons/src/main/java/eu/wdaqua/qanary/commons/triplestoreconnectors/QanaryTripleStoreConnector.java +++ b/qanary_commons/src/main/java/eu/wdaqua/qanary/commons/triplestoreconnectors/QanaryTripleStoreConnector.java @@ -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(); @@ -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 @@ -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 @@ -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 @@ -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 @@ -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
+ * (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:
+ * ?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:
+ * ?graph, ?targetQuestion, ?selectQueryThatShouldComputeTheAnswer,
+ * ?confidence, ?index, ?application * @return * @throws IOException */ @@ -179,10 +237,10 @@ 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:
+ * ?graph, ?targetQuestion, ?jsonAnswer, ?confidence, ?application * @return * @throws IOException */ @@ -190,12 +248,20 @@ public static String insertAnnotationOfAnswerJson(QuerySolutionMap bindings) thr 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:
+ * ?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 @@ -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 */ diff --git a/qanary_commons/src/main/resources/queries/insert_one_AnnotationOfAnswerDataType.rq b/qanary_commons/src/main/resources/queries/insert_one_AnnotationOfAnswerDataType.rq new file mode 100644 index 00000000..dd6c4615 --- /dev/null +++ b/qanary_commons/src/main/resources/queries/insert_one_AnnotationOfAnswerDataType.rq @@ -0,0 +1,24 @@ +PREFIX dbr: +PREFIX oa: +PREFIX qa: +PREFIX rdf: +PREFIX xsd: + +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) . +} diff --git a/qanary_commons/src/main/resources/queries/insert_one_AnnotationOfAnswerSPARQL.rq b/qanary_commons/src/main/resources/queries/insert_one_AnnotationOfAnswerSPARQL.rq index db10fc2a..ab9c5b5d 100644 --- a/qanary_commons/src/main/resources/queries/insert_one_AnnotationOfAnswerSPARQL.rq +++ b/qanary_commons/src/main/resources/queries/insert_one_AnnotationOfAnswerSPARQL.rq @@ -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 . } diff --git a/qanary_commons/src/main/resources/queries/insert_one_AnnotationOfImprovedQuestion.rq b/qanary_commons/src/main/resources/queries/insert_one_AnnotationOfImprovedQuestion.rq new file mode 100644 index 00000000..72c5c6f7 --- /dev/null +++ b/qanary_commons/src/main/resources/queries/insert_one_AnnotationOfImprovedQuestion.rq @@ -0,0 +1,22 @@ +PREFIX qa: +PREFIX oa: +PREFIX xsd: +PREFIX rdf: +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) . +} diff --git a/qanary_commons/src/main/resources/queries/select_all_AnnotationOfAnswerSPARQL.rq b/qanary_commons/src/main/resources/queries/select_all_AnnotationOfAnswerSPARQL.rq index f21fdbdf..6dc64bcc 100644 --- a/qanary_commons/src/main/resources/queries/select_all_AnnotationOfAnswerSPARQL.rq +++ b/qanary_commons/src/main/resources/queries/select_all_AnnotationOfAnswerSPARQL.rq @@ -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 . diff --git a/qanary_commons/src/main/resources/queries/select_lowestIndex_AnnotationOfAnswerSPARQL.rq b/qanary_commons/src/main/resources/queries/select_lowestIndex_AnnotationOfAnswerSPARQL.rq new file mode 100644 index 00000000..701efb16 --- /dev/null +++ b/qanary_commons/src/main/resources/queries/select_lowestIndex_AnnotationOfAnswerSPARQL.rq @@ -0,0 +1,29 @@ +PREFIX dbr: +PREFIX oa: +PREFIX qa: +PREFIX rdf: +PREFIX xsd: + +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