diff --git a/qanary-component-QE-Wikidata/pom.xml b/qanary-component-QE-Wikidata/pom.xml index 1169ab28a..f9ea28f79 100644 --- a/qanary-component-QE-Wikidata/pom.xml +++ b/qanary-component-QE-Wikidata/pom.xml @@ -5,7 +5,7 @@ 4.0.0 eu.wdaqua.qanary.component qanary-component-QE-Wikidata - 3.1.8 + 3.1.9 org.springframework.boot diff --git a/qanary-component-QE-Wikidata/src/main/java/eu/wdaqua/component/wikidata/qe/QueryExecuter.java b/qanary-component-QE-Wikidata/src/main/java/eu/wdaqua/component/wikidata/qe/QueryExecuter.java index f769fda31..77afc934d 100644 --- a/qanary-component-QE-Wikidata/src/main/java/eu/wdaqua/component/wikidata/qe/QueryExecuter.java +++ b/qanary-component-QE-Wikidata/src/main/java/eu/wdaqua/component/wikidata/qe/QueryExecuter.java @@ -39,14 +39,16 @@ public class QueryExecuter extends QanaryComponent { private final String applicationName; private String FILENAME_INSERT_ANNOTATION = "/queries/insert_one_annotation.rq"; - private String FILENAME_GET_ANNOTATION = "/queries/get_annotation.rq"; + private String FILENAME_GET_ANNOTATION_TYPED_SPARQL_QUERY = "/queries/get_annotation_typed_sparql_query.rq"; + private String FILENAME_GET_ANNOTATION_UNTYPED_SPARQL_QUERY = "/queries/get_annotation_untyped_sparql_query.rq"; public QueryExecuter(@Value("${spring.application.name}") final String applicationName) { this.applicationName = applicationName; // check if files exists and are not empty QanaryTripleStoreConnector.guardNonEmptyFileFromResources(FILENAME_INSERT_ANNOTATION); - QanaryTripleStoreConnector.guardNonEmptyFileFromResources(FILENAME_GET_ANNOTATION); + QanaryTripleStoreConnector.guardNonEmptyFileFromResources(FILENAME_GET_ANNOTATION_TYPED_SPARQL_QUERY); + QanaryTripleStoreConnector.guardNonEmptyFileFromResources(FILENAME_GET_ANNOTATION_UNTYPED_SPARQL_QUERY); } /** @@ -154,17 +156,17 @@ public QanaryMessage process(QanaryMessage myQanaryMessage) throws Exception { // Here we fetch those annotations (AnnotationOfAnswerSPARQL) from the Qanary // triplestore - QuerySolutionMap bindingsForInsert = new QuerySolutionMap(); - bindingsForInsert.add("graph", ResourceFactory.createResource(myQanaryQuestion.getInGraph().toASCIIString())); - bindingsForInsert.add("targetQuestion", ResourceFactory.createResource(myQanaryQuestion.getUri().toASCIIString())); + QuerySolutionMap bindingsForSelectSparqlQuery = new QuerySolutionMap(); + bindingsForSelectSparqlQuery.add("graph", ResourceFactory.createResource(myQanaryQuestion.getInGraph().toASCIIString())); + bindingsForSelectSparqlQuery.add("targetQuestion", ResourceFactory.createResource(myQanaryQuestion.getUri().toASCIIString())); - // get the template of the INSERT query - String sparqlSelectQuery = this.loadQueryFromFile(FILENAME_GET_ANNOTATION, bindingsForInsert); + // get the template of the SELECT query --> search for well-formed (typed) SPARQL queries first + String sparqlSelectQuery = this.loadQueryFromFile(FILENAME_GET_ANNOTATION_TYPED_SPARQL_QUERY, bindingsForSelectSparqlQuery); + logger.info("Query to fetch all available typed SPARQL queries in the current Qanary triplestore graph:\n{}", sparqlSelectQuery); + List queries = new LinkedList(); // fetch data from the triplestore ResultSet resultset = myQanaryUtils.getQanaryTripleStoreConnector().select(sparqlSelectQuery); - List queries = new LinkedList(); - while (resultset.hasNext()) { QuerySolution tupel = resultset.next(); @@ -172,6 +174,23 @@ public QanaryMessage process(QanaryMessage myQanaryMessage) throws Exception { logger.info("found query {}", wikidataQuery); queries.add(wikidataQuery); } + logger.info("found {} typed SPARQL queries in triplestore", queries.size()); + + if( queries.size() == 0) { + // redo: search for untyped SPARQL queries, too + sparqlSelectQuery = this.loadQueryFromFile(FILENAME_GET_ANNOTATION_UNTYPED_SPARQL_QUERY, bindingsForSelectSparqlQuery); + logger.info("Query to fetch all available UNtyped SPARQL queries in the current Qanary triplestore graph:\n{}", sparqlSelectQuery); + // fetch data from the triplestore + resultset = myQanaryUtils.getQanaryTripleStoreConnector().select(sparqlSelectQuery); + while (resultset.hasNext()) { + QuerySolution tupel = resultset.next(); + + String wikidataQuery = tupel.get("wikidataQuery").toString(); + logger.info("found query {}", wikidataQuery); + queries.add(wikidataQuery); + } + logger.info("found {} UNtyped SPARQL queries in triplestore", queries.size()); + } // STEP 2: compute new knowledge about the given question // diff --git a/qanary-component-QE-Wikidata/src/main/resources/queries/get_annotation_typed_sparql_query.rq b/qanary-component-QE-Wikidata/src/main/resources/queries/get_annotation_typed_sparql_query.rq index 3529c574d..62a362d05 100644 --- a/qanary-component-QE-Wikidata/src/main/resources/queries/get_annotation_typed_sparql_query.rq +++ b/qanary-component-QE-Wikidata/src/main/resources/queries/get_annotation_typed_sparql_query.rq @@ -1,15 +1,19 @@ -PREFIX dbr: -PREFIX oa: -PREFIX qa: -PREFIX rdf: +PREFIX qa: +PREFIX oa: +PREFIX dbr: +PREFIX rdf: -SELECT * -FROM ?grph +# select prepared SPARQL queries that are explicitly typed as qa:SparqlQuery +SELECT ?annotation ?wikidataQuery ?annotationScore +FROM ?graph WHERE { - ?annotation a qa:AnnotationOfAnswerSPARQL . - ?annotation oa:hasBody ?wikidataQueryData . #the entity in question - ?wikidataQueryData a qa:SparqlQuery . #a SPARQL query object - ?wikidataQueryData rdf:value ?wikidataQuery . #the SPARQL query string - ?annotation qa:score ?annotationScore . - ?annotation oa:hasTarget ?targetQuestion . + ?annotation + rdf:type qa:AnnotationOfAnswerSPARQL ; + oa:hasBody ?wikidataQueryData . + ?wikidataQueryData + rdf:type qa:SparqlQuery ; + rdf:value ?wikidataQuery . + ?annotation + qa:score ?annotationScore ; + oa:hasTarget ?targetQuestion } \ No newline at end of file diff --git a/qanary-component-QE-Wikidata/src/main/resources/queries/get_annotation_untyped_sparql_query.rq b/qanary-component-QE-Wikidata/src/main/resources/queries/get_annotation_untyped_sparql_query.rq new file mode 100644 index 000000000..b3feb5dc5 --- /dev/null +++ b/qanary-component-QE-Wikidata/src/main/resources/queries/get_annotation_untyped_sparql_query.rq @@ -0,0 +1,17 @@ +PREFIX qa: +PREFIX oa: +PREFIX dbr: +PREFIX rdf: + +SELECT ?annotation ?wikidataQuery ?annotationScore +FROM ?graph +WHERE { + ?annotation + rdf:type qa:AnnotationOfAnswerSPARQL ; + oa:hasBody ?wikidataQueryData . + FILTER(isLiteral(?wikidataQueryData)). + BIND(?wikidataQueryData AS ?wikidataQuery ). + ?annotation + qa:score ?annotationScore ; + oa:hasTarget ?targetQuestion +} \ No newline at end of file