Problem when running query with MINUS filter in jena - sparql

I want to add the MINUS filter in a query but when I add it the query simply does not execute, but when I remove the MINUS filter the query executes normally.
This is the code when I use MINUS:
Query sparql = QueryFactory.create("PREFIX sosa: <http://www.w3.org/ns/sosa/>\r\n" +
"PREFIX owl: <http://www.w3.org/2002/07/owl#>\r\n" +
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" +
"SELECT ?s ?c ?p ?o FROM <http://147.27.60.65/sensorOntology> WHERE {?s rdf:type owl:NamedIndividual.\r\n" +
"?p rdf:type owl:ObjectProperty.\r\n" +
"?c rdf:type owl:Class.\r\n" +
"?s rdf:type ?c.\r\n" +
"?s ?p ?o\r\n" +
"MINUS {?s rdf:type owl:Thing}\r\n" +
"}");
QueryExecution vqe = QueryExecutionFactory.create(sparql, inf);
This is the code that executes correctly when I do not use MINUS:
Query sparql = QueryFactory.create("PREFIX sosa: <http://www.w3.org/ns/sosa/>\r\n" +
"PREFIX owl: <http://www.w3.org/2002/07/owl#>\r\n" +
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" +
"SELECT ?s ?c ?p ?o FROM <http://147.27.60.65/sensorOntology> WHERE {?s rdf:type owl:NamedIndividual.\r\n" +
"?p rdf:type owl:ObjectProperty.\r\n" +
"?c rdf:type owl:Class.\r\n" +
"?s rdf:type ?c.\r\n" +
"?s ?p ?o}");
QueryExecution vqe = QueryExecutionFactory.create(sparql, inf);
Is there something wrong with the syntax? Is it perhaps that I query a InfModel type ?

Related

The mechanism of "FILTER NOT EXISTS" in SPARQL

Assuming the triples are following:
#prefix : <http://example/> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#prefix foaf: <http://xmlns.com/foaf/0.1/> .
:alice rdf:type foaf:Person .
:alice foaf:name "Alice" .
:bob rdf:type foaf:Person .
and then we perform 3 queries based on SPARQL 1.1:
Q1:
SELECT ?s
WHERE
{
?s ?p ?o .
FILTER NOT EXISTS { ?s foaf:name ?y }
}
Q2:
SELECT ?s
WHERE
{
?s ?p ?o .
FILTER NOT EXISTS { ?x foaf:name ?y }
}
Q3:
SELECT ?s
WHERE
{
?s ?p ?o .
FILTER NOT EXISTS { ?x foaf:mailbox ?y }
}
These three queries return three different solutions. Could anyone help me figure out why Q2 evaluates to no query solution in contrast to Q1 and Q3? Many thanks in advance :)
Q2 returns no solution because in your data, there exists a statement that matches ?x foaf:name ?y: ?x = :alice and ?y = "Alice". You've put no further constraints on either ?x or ?y. So no matter what the other variables in your query (?s, ?p and ?o) are bound to, the NOT EXISTS condition will always fail and therefore the query returns no result.

Sparql query issue with DBpedia retrieving

I'm trying to run this Sparql statement to read data from DBpedia; However, it only return the column names and no row data.
If anybody can let me know what's the issue. I'll be more appreciated
Sparql query below:
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX dc: <http://purl.org/dc/terms/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?actor ?movie ?director ?movie_date
WHERE {
?m dc:subject <http://dbpedia.org/resource/Category:American_films> .
?m rdfs:label ?movie .
FILTER(LANG(?movie) = "en")
?m dbp:released ?movie_date .
FILTER(DATATYPE(?movie_date) = xsd:date)
?m dbp:starring ?a .
?a rdfs:label ?actor .
FILTER(LANG(?actor) = "en")
?m dbp:director ?d .
?d rdfs:label ?director .
FILTER(LANG(?director) = "en")
}
The problem is not all the queried triples exist. If at least one of the relations doesnt exist, no result will be given.
So with the OPTIONAL keyword, answers will be given only if they exist and wont block your query if they don't. It simply leaves the missing value empty.
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX dc: <http://purl.org/dc/terms/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?actor ?movie ?director ?movie_date
WHERE {
?m dc:subject <http://dbpedia.org/resource/Category:American_films> .
OPTIONAL{
?m rdfs:label ?movie .
FILTER(LANG(?movie) = "en") }
OPTIONAL {
?m dbp:released ?movie_date .
FILTER(DATATYPE(?movie_date) = xsd:date) }
OPTIONAL{
?m dbp:starring ?a .
?a rdfs:label ?actor .
FILTER(LANG(?actor) = "en") }
OPTIONAL{
?m dbp:director ?d .
?d rdfs:label ?director .
FILTER(LANG(?director) = "en") }
}

DBpedia query giving me error when using jena

When i run this query on dbpedia.org
SELECT DISTINCT ?resource ?label ?location
WHERE
{
<http://dbpedia.org/resource/New_York_City> geo:geometry ?sourcegeo .
?resource geo:geometry ?location ;
rdfs:label ?label .
FILTER( bif:st_intersects( ?location, ?sourcegeo, 20 ) ) .
FILTER( lang( ?label ) = "en" )
}
it is producing results fine but same query gives me error when using java jena api.
String s2 = "SELECT DISTINCT ?resource ?label ?location \r\n" +
"WHERE \r\n" +
" { \r\n" +
" <http://dbpedia.org/resource/New_York_City> geo:geometry ?sourcegeo . \r\n" +
" ?resource geo:geometry ?location ; \r\n" +
" rdfs:label ?label . \r\n" +
" FILTER( bif:st_intersects( ?location, ?sourcegeo, 20 ) ) . \r\n" +
" FILTER( lang( ?label ) = \"en\" ) \r\n" +
" }\r\n" +
"";
Query query = QueryFactory.create(s2); //s2 = the query above
QueryExecution qExe = QueryExecutionFactory.sparqlService( "http://dbpedia.org/sparql", query );
ResultSet results = qExe.execSelect();
ResultSetFormatter.out(System.out, results, query) ;
it gives me the following errors
Exception in thread "main" org.apache.jena.query.QueryParseException: Line 4, column 49: Unresolved prefixed name: geo:geometry
at org.apache.jena.sparql.lang.ParserBase.throwParseException(ParserBase.java:521)
at org.apache.jena.sparql.lang.ParserBase.resolvePName(ParserBase.java:286)
at org.apache.jena.sparql.lang.sparql_11.SPARQLParser11.PrefixedName(SPARQLParser11.java:4857)
at org.apache.jena.sparql.lang.sparql_11.SPARQLParser11.iri(SPARQLParser11.java:4841) and it goes on
DBPedia has a number of Predefined Namespace Prefixes, which includes
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>,
which Jena does not have. Hence you have to specify it for use Jena before your SELECT. I.e.
String s2 = "PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>\r\n" +
"SELECT DISTINCT ?resource ?label ?location \r\n" +
... etc

SPARQL execdescribe query in jena only giving prefix as output

Below SPARQL execdescribe query giving me only two prefixes as output while running with Jena query but when I run this query on virtuoso SPARQL endpoint it's giving perfect output.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX db: <http://dbpedia.org/ontology/>
PREFIX prop: <http://dbpedia.org/property/>
DESCRIBE ?movie ?author ?genre
WHERE {
?movie rdf:type db:Film ;
prop:author ?author ;
prop:genre ?genre .
}
LIMIT 2
OFFSET 0
When I run with Jena I am getting only two line output like this,
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
Below is my code which I am using but with some query its working fine,
String queryString = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" +
"PREFIX db: <http://dbpedia.org/ontology/> " +
"PREFIX prop: <http://dbpedia.org/property/>" +
"DESCRIBE ?movie ?author ?genre" +
"WHERE { " +
"?movie rdf:type db:Film ;" +
"prop:author ?author ;" +
"prop:genre ?genre ." +
"}" +
"LIMIT 2" +
"OFFSET 0";
QueryExecution qexec = QueryExecutionFactory.sparqlService("http://localhost:8890/sparql", queryString);
Model results = qexec.execDescribe();
results.write(System.out,"TTL");
Its perfectly giving me output on virtuoso SPARQL end point.
below is screen shot,
As suggested in comments by #AKSW, There is a space issue in the query. So after edited my query, it will perfectly work fine.

DBpedia query returns results with Virtuoso but not with Jena

I'm trying to create a SPARQL query with Jena to query DBpedia. The query is working when I use Virtuoso but when I plug it into the following Java code, it returns an empty set.
String sr="Christopher_Nolan";
String sparqlQueryString1 = "PREFIX dbont: <http://dbpedia.org/ontology/> "+
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"+
"PREFIX foaf: <http://xmlns.com/foaf/0.1/> "+
" SELECT distinct ?s"+
" WHERE { "+
"?s foaf:name ?label ." +
"filter(?label=\"Christpher_Nolan\"#en)." +
" }";
Query query = QueryFactory.create(sparqlQueryString1);
QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);
ResultSet results = qexec.execSelect();
ResultSetFormatter.out(System.out,results, query);
qexec.close() ;
I don't think this is a problem with Jena, but with your particular query. Your query, when run on the DBpedia SPARQL endpoint, produces no results
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT distinct ?s
WHERE {
?s foaf:name ?label .
filter(?label="Christpher_Nolan"#en)
}
SPARQL results (no results)
However, if you add an o to the name Christopher, and change the underscore to a space, you get three results:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT distinct ?s
WHERE {
?s foaf:name ?label .
filter(?label="Christopher Nolan"#en)
}
SPARQL results (3 results)
s
http://dbpedia.org/resource/Christopher_Nolan_(author)
http://dbpedia.org/resource/Christopher_Nolan
http://dbpedia.org/resource/Chris_Nolan_(musician)
I'd also point out that this is a rather unusual use of filter. If you want to select triples that use a certain value, just put that value into the triple pattern:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT distinct ?s
WHERE {
?s foaf:name "Christopher Nolan"#en
}
SPARQL results (same 3 results)