How to get `filter not exists` query working with ARC2? - sparql

I have an ARC2 based RDF store setup and filled with some data. The following query returns the expected results (all URIs of things of type vcard:Individual or foaf:Person) when I run it without the FILTER NOT EXISTS part:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX app: <http://example.com#>
SELECT ?person
WHERE {
?person rdf:type ?type .
FILTER (?type = vcard:Individual || ?type = foaf:Person)
FILTER NOT EXISTS {
?person app:id ?id
}
}
When I try to exclude those things having the app:id property by running the full query, it fails with the following message:
Incomplete FILTER in ARC2_SPARQLPlusParser
Incomplete or invalid Group Graph pattern. Could not handle " FILTER NOT EXISTS { " in ARC2_SPARQLPlusParser
When testing the query in the query validator on sparql.org, there are no issues. Is there something wrong with my query that I am missing or is this a shortcoming of ARC2?
Can you think of an alternative query that I can try with ARC2 to get the desired result?

ARC2 does not support SPARQL 1.1, thus, you have to use the common OPTIONAL-FILTER(!BOUND()) pattern:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX app: <http://example.com#>
SELECT ?person
WHERE {
?person rdf:type ?type .
FILTER (?type = vcard:Individual || ?type = foaf:Person)
OPTIONAL {
?person app:id ?id
}
FILTER ( !BOUND(?id) )
}

Related

Can't get actor names for a given film title when multiple films have the same name in DBpedia

I am trying to get actor names for a given film title (I also have the release date in hand) with my sparql query, but given the situation that multiple films have the same name, I'm trying to differentiate them with the release date. Some films don't have the release date specified, some films don't have the label specified.
I'm trying to get results when either the release date is specified and is matching, or when it is in the label of the film and is also matching.
If I can't match the date with one of these attributes, I want no results in return
Here is my current query:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT ?film ?aname
WHERE {
?film a dbo:Film ;
foaf:name "A Nightmare on Elm Street"#en ;
dbo:starring ?a .
?a foaf:name ?aname
OPTIONAL
{ ?film dbo:releaseDate ?rd
BIND(year(xsd:date(?rd)) AS ?rrd)
FILTER ( ( ?rd = "1984-04-30"^^xsd:date ) || ( ?rrd = 1984) )
}
OPTIONAL
{ ?film rdfs:label ?lab
FILTER regex(?lab, "1984", "i")
FILTER ( lang(?lab) = "en" )
}
}
I think you are misusing OPTIONAL here. Instead you should be looking at UNION.
What's the difference?
SELECT ?person ?child
WHERE {
?person a :Person .
OPTIONAL {?person :hasSon ?child}
OPTIONAL {?person :hasDaughter ?child}
}
Will return every person, and optionally their sons/daughters. However this will return also people without any children at all.
Instead, something like:
SELECT ?person ?child
WHERE {
?person a :Person .
{?person :hasSon ?child}
UNION
{?person :hasDaughter ?child}
}
Will only return people who have at least one son or daughter.
Now, in your example, I have a query working like this:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT ?film ?aname
WHERE {
?film a dbo:Film ;
foaf:name "A Nightmare on Elm Street"#en ;
dbo:starring ?a .
?a foaf:name ?aname
{ ?film dbp:released 1984}
UNION
{ ?film rdfs:label ?lab
FILTER regex(?lab, "1984", "i")
FILTER ( lang(?lab) = "en" )
}
}
Notice that I used the dbp:released property which seems to be working.
It seems that the property used for releases is inconsistent across films, i.e. some use dbp:released , others dbo:releaseDate.
If that's an issue, you can of course add another UNION statement in the query to deal with the different case.
One more thing:
there are many triplestores out there that have reasoning, and reasoning is something that can help deal with a variety of such situations (disambiguation, multiple properties for the same thing, etc)

Error handling of SPARQL query with given RDF graph

I have the following RDF graph with prefixes
PREFIX r: <http://dbpedia.org/resources/>
PREFIX o: <http://dbpedia.org/ontology/>
And the query
PREFIX r: <http://dbpedia.org/resources/>
PREFIX o: <http://dbpedia.org/ontology/>
SELECT ?s ?author
WHERE {
?s o:type o:Book .
?s o:author ?author .
?author ?incategory r:Category:American_atheists.
}
I am now wondering what the output would look like. I have tried using https://dbpedia.org/sparql but this results in a parsing error.
Is this a proper query anyway ?
The graph has the prefix r for Book and the query has o:Book in the triple.
The parsing error is due to the colon after r:Category. Colon in abbreviated IRIs can only be used as part of the prefix. This query should work:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX r: <http://dbpedia.org/resource/>
PREFIX o: <http://dbpedia.org/ontology/>
SELECT ?s ?author
WHERE {
?s rdf:type o:Book .
?s o:author ?author .
?author ?incategory <http://dbpedia.org/resource/Category:American_atheists> .
}
Or, if you want a more concise WHERE clause:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX r: <http://dbpedia.org/resource/>
PREFIX o: <http://dbpedia.org/ontology/>
PREFIX c: <http://dbpedia.org/resource/Category:>
SELECT ?s ?author
WHERE {
?s rdf:type o:Book .
?s o:author ?author .
?author ?incategory c:American_atheists .
}

Sparql - Conditional Output

I am very new to the semantic web and sparql. I have an internal ontology that uses SmartLogic in order to manage the data.
I am writing some simple queries to get started.
PREFIX skos: <http://www.w3.org/2004/02/skos/core#> # Simple Knowledge Organization System - https://www.w3.org/2004/02/skos/
PREFIX skosxl: <http://www.w3.org/2008/05/skos-xl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <https://www.w3.org/TR/rdf-schema/>
prefix owl: <http://www.w3.org/2002/07/owl#>
prefix ap: <http://cv.ap.org/ns>
SELECT DISTINCT
?subjectPrefLabel ?p ?o ?oL
WHERE {
{
?subject skosxl:prefLabel ?subjectLabel .
?subjectLabel skosxl:literalForm ?subjectPrefLabel .
?subject ?p ?o .
OPTIONAL {?o skos:prefLabel ?oL}
}
FILTER regex(?subjectPrefLabel, "Trump", 'i')
} order by ?subjectPrefLabel
This query returns results that look like :
I am trying to merge the ?o and ?oL fields, so that it will replace the ?o field, if and only if there is a valid ?oL Field
I haven't been able to figure it out quite yet. If there is any suggestions please let me know.
A bit difficult without data for testing the query, but in SPARQL 1.1 you can use BIND(IF(condition,then,else) as ?result ):
PREFIX skosxl: <http://www.w3.org/2008/05/skos-xl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX rdfs: <https://www.w3.org/TR/rdf-schema/>
PREFIX ap: <http://cv.ap.org/ns>
SELECT DISTINCT ?subjectPrefLabel ?p ?o
WHERE
{ ?subject skosxl:prefLabel ?subjectLabel .
?subjectLabel
skosxl:literalForm ?subjectPrefLabel .
?subject ?p ?o_tmp
OPTIONAL
{ ?o_tmp skos:prefLabel ?oL }
BIND(if(bound(?oL), ?oL, ?o_tmp) AS ?o)
FILTER regex(?subjectPrefLabel, "Trump", "i")
}
ORDER BY ?subjectPrefLabel

How to query DBpedia SPARQL by resource uri?

I'm querying DBpedia types in SPARQL (http://dbpedia.org/sparql) by resource's label
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX : <http://dbpedia.org/resource/>
PREFIX ru: <http://ru.dbpedia.org/resource/>
PREFIX dbpedia2: <http://dbpedia.org/property/>
PREFIX dbpedia: <http://dbpedia.org/>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?type ?superType WHERE { {
?res rdfs:label "HarryPotter"#en.
} UNION {
?redir dbo:wikiPageRedirects ?res .
?redir rdfs:label "HarryPotter"#en .
}
?res rdf:type ?type .
OPTIONAL {
?type rdfs:subClassOf ?superType .
}
}
It works fine.
But what if I know the exact resource - http://dbpedia.org/page/Harry_Potter? I tried something like:
?res a :Harry_Potter.
But it does not work.
How to query DBpedia types and supertypes if I know the resource URI? I can't figure out which property or operator I should use (e.g., rdfs:Resource, a, etc., which do not work)
When you write
?res a :Harry_Potter.
It doesn't work, because this means "a resource, which is of type :Harry_Potter". It is equivalent to
?res rdf:type :Harry_Potter.
:Harry_Potter identifies a resource and not the type, thus it should be used in place of ?res.
Also I think you mean Harry_Potter_(character), because that is the actual identifier and not redirect.
You query would be as simple as
SELECT ?type ?superType WHERE
{
# give me ?type of the resource
<http://dbpedia.org/resource/Harry_Potter_(character)> rdf:type ?type .
# give me ?superTypes of ?type
OPTIONAL {
?type rdfs:subClassOf ?superType .
}
}
You can just put the URI as the subject in there WHERE conditions.
SELECT ?title, ?releaseDate
WHERE {
<http://dbpedia.org/resource/Super_Mario_Bros._3> dbp:title ?title .
<http://dbpedia.org/resource/Super_Mario_Bros._3> dbo:releaseDate ?releaseDate .
}

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)