SPARQL: Dividing a BGP into multiple Groups - sparql

In a previous question, a comment suggests that
select ?x ?y where {
{?x rdf:type ex:someType}
{?x ex:someProperty ?y}
}
is like (not identical) to:
select ?x ?y where {
?x rdf:type ex:someType.
?x ex:someProperty ?y.
}
Same triple patterns are used. However, the first query contains two BGPs (each one in a group pattern), while the second contains one BGP (no group patterns).
The algebra for the first query is a JOIN between two BGPs while the algebra of the second is only a BGP.
First query algebra (Apache Jena)
(project (?x ?y)
(join
(bgp (triple ?x <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.example.com/someType>))
(bgp (triple ?x <http://www.example.com/someProperty> ?y))))
Second query algebra (Apache Jena):
(project (?x ?y)
(bgp
(triple ?x <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.example.com/someType>)
(triple ?x <http://www.example.com/someProperty> ?y)
))
In the original question, the answer suggest that they are not identical and argues
If the SPARQL entailment is supported, then the separate {} are evaluated for entailment separately.
if you use labels bnodes in queries there restrictions cross {}
if you add FILTERS, then they do not apply outside the {} they are written in.
its a different abstract syntax tree. All minor points but which may come into play later.
Now let's put the blank nodes (2) and the different syntax trees aside (4), and ask the following: Would the two queries, in any case, yield different results because of filter (3) or entailment(1)? I do not see any possibility for that. Well, those who have a different opinion, may you, please show with some example?

Scope of FILTER is affected.
In:
select ?x ?y where {
{?x rdf:type ex:someType FILTER (?y > 0 ) }
{?x ex:someProperty ?y}
}
The FILTER is false always because ?y is unbound so the expression is error and the FILTER is false regardless of the ?x ex:someProperty ?y.
(3) and (4) are related.

Related

How to catch correct resource among disambiguations / interrogate subresources in DBPedia using SPARQL?

I have the title of a Youtube music video and I need to gather additional information from DBPedia like album, artist, release date, etc. I'd also like to get the 'abstract' of all these entities from DBPedia. I already use Musicbrainz to differentiate between song and artist in the title and it works rather well, except a few cases.
However my main problem is: when I pass the song to ask DBPedia (using a query with resource/{song}), sometimes I get no answer because {song} causes disambiguation. Example: resource/It's_My_Life has 11 disambiguates, 6 of which are songs. I need the resource "It's_My_Life_(Bon_Jovi_song)". How can I tell DBPedia that I need a resource of 'MusicalWork' type of a certain artist?
I tried in many ways to do this with SPARQL, but I always get an empty result and I don't know what I'm doing wrong. So far I was able to only get the abstract of every disambiguation, but I cannot get a specific property (like abstract) of a subresource of a disambiguation.
SELECT ?x ?y WHERE {
<http://dbpedia.org/resource/It's_My_Life> dbo:wikiPageDisambiguates ?x .
?x dbo:abstract ?y .
}
I can't seem to go father than this. I tried with:
SELECT ?x ?y WHERE {
<http://dbpedia.org/resource/It's_My_Life> dbo:wikiPageDisambiguates ?x .
?x dbo:MusicalArtist ?y .
{ SELECT ?z WHERE {
?y dbo:abstract ?z}
}
}
and
SELECT ?x ?y WHERE {
<http://dbpedia.org/resource/It's_My_Life> dbo:wikiPageDisambiguates ?x
?x rdfs:type ?y .
}
But results are always empty. How am I supposed to interrogate a subresource of a resource? Can anyone help me?
Solution, pulled from comments by #AKSW --
SELECT ?x ?y
WHERE
{
<http://dbpedia.org/resource/It's_My_Life> dbo:wikiPageDisambiguates ?x .
?x rdf:type dbo:MusicalWork .
?x dbo:abstract ?y .
FILTER ( LANGMATCHES ( LANG (?y), 'en' ) )
}

DBpedia Sparql by page template

I am trying to run a query on dbpedia using SPARQL syntax, to look for all pages of a certain template. Doesn't seem to be work, I am looking for all pages with dbpprop:wikiPageUsesTemplate. Does anyone know how to correct this to properly look for templates?
SELECT ?name ?member_Of ?country ?lat ?lng ?link
WHERE {
?x dbpprop:wikiPageUsesTemplate "dbpedia:Template:Infobox_settlement" .
?x a <http://dbpedia.org/ontology/Settlement> .
?x foaf:name ?name .
?x dbpedia-owl:isPartOf ?member_Of.
?x dbpedia-owl:country ?country.
?x geo:lat ?lat .
?x geo:long ?lng .
?x foaf:isPrimaryTopicOf ?link .
}
LIMIT 2500 OFFSET 0
I've also attempted to run it just by the dbprop to no avail.
SELECT * WHERE { ?page dbpprop:wikiPageUsesTemplate <http://dbpedia.org/resource/Template:Infobox_settlement> . ?page dbpedia:name ?name .}
If anyone is trying to do a similar thing, it is also possible via the wiki api, where you can pagananate over all results. http://en.wikipedia.org/w/api.php?action=query&list=embeddedin&eititle=Template:Infobox_settlement
There are at least two problems: (i) you need to use IRIs in places, and not strings; and (ii) you need to use properties that DBpedia uses.
Use IRIs
In
?x a <http://dbpedia.org/ontology/Settlement> .
and
?x foaf:isPrimaryTopicOf ?link .
you've demonstrated that you know that URIs need to be written in full with < and >, or abbreviated with a prefix. However,
?x dbpprop:wikiPageUsesTemplate "dbpedia:Template:Infobox_settlement" .
certainly isn't going to work. It's legal SPARQL, because a string can be the object of a triple, but you almost certainly want an IRI.
Use DBpedia's vocabulary
A query with dbpprop:wikiPageUsesTemplate like this returns no results:
select distinct ?template where {
[] dbpprop:wikiPageUsesTemplate ?template
}
SPARQL results
That's easy enough to check, and quickly confirms that there's no data that can possibly match your query. Where did you find this property? Have you seen it used somewhere? I'm not confident that you can query DBpedia based on infobox templates. DBpedia is not the same as Wikipedia, and even if the Wikipedia API supports it, it doesn't mean that DBpedia has a counterpart. There is a note on DBpedia Data Set Properties that says:
http://xx.dbpedia.org/property/wikiPageUsesTemplate (will be changed to http://dbpedia.org/ontology/wikiPageUsesTemplate)
but that latter property doesn't seem to be in use on the endpoints either. See my answer to Syntax for Sparql query for pages with specific infobox for more details.

Extract Chemical Data from DBpedia via SPARQL

I'd like to know how to submit a SPARQL query to DDBpedia and be given back a table that includes the information found in the Wikipedia "chembox" info box template, such as molecular weight or formula.
So, the first step was just to make a query whose results should be a list of chemical substances that had the formula and molecularWeight properties included. But the following returns no results:
SELECT * WHERE {
?y rdf:type dbpedia-owl:ChemicalSubstance.
?y rdfs:label ?Name .
?y dbpedia:molecularWeight ?molecularWeight .
?y dbpedia:formula ?formula .
OPTIONAL {?y dbpedia-owl:iupacName ?iupacname} .
FILTER (langMatches(lang(?Name),"en"))
}
LIMIT 50
SPARQL Explorer at dbpedia.org
And so I'm stuck. Is something wrong with this query or does DBPedia really not collect that information from the Wikipedia chemboxes?
You caught the wrong namespace for both dbpedia:molecularWeight and dbpedia:formula. The correct namespace here would be dbpedia2.
Furthermore, there seem rarely any entries having a dbpedia-owl:iupacName, dbpedia2:molecularWeight and dbpedia2:formula.
SELECT * WHERE {
?y rdf:type dbpedia-owl:ChemicalSubstance.
?y rdfs:label ?Name .
OPTIONAL {?y dbpedia2:formula ?formula }.
OPTIONAL {?y dbpedia2:molecularWeight ?molecularWeight}.
OPTIONAL {?y dbpedia-owl:iupacName ?iupacname} .
FILTER (langMatches(lang(?Name),"en"))
}
LIMIT 50
SPARQL Explorer #dbpedia.org
To get the correct namespaces, you could either look at one example like this or get a list of all used properties for type dbpedia-owl:ChemicalSubstance using
SELECT DISTINCT ?rel WHERE {
?y rdf:type dbpedia-owl:ChemicalSubstance.
?y ?rel ?x
}
SPARQL Explorer #dbpedia.org

COUNT aggregate not working on DBPedia when issued via Jena

Below is a piece of code I am using for querying the DBPEDIA SPARQL endpoint. The query select count(?y) where {?x ?y ?z .} works fine but the below query does not run giving an error on the variable ?count?
I think the syntax is right, can anyone point out what can be the probable error here ?
int count=0;
String queryStringNew="select count(distinct ?y) where {?x ?y ?z .}";
Query query=QueryFactory.create(queryStringNew);
ARQ.getContext().setTrue(ARQ.useSAX);
QueryExecution qexec = QueryExecutionFactory.sparqlService("http://DBpedia.org/sparql", query);
ResultSet results = qexec.execSelect();
while(results.hasNext())
{
QuerySolution soln=results.nextSolution();
//System.out.println(soln.get("?x"));
count++;
}
The problem is that your query is not valid SPARQL 1.1
Virtuoso (the software used for DBPedia) is notorious for having a non-standard SPARQL implementation which supports various syntax extensions that are not valid in standard SPARQL. This is why when you enter the query at the DBPedia endpoint directly yourself it is accepted.
To be valid SPARQL all aggregates (and project expressions) need to be enclosed in brackets and aliased (have an AS clause) e.g.
SELECT ?y (COUNT(DISTINCT ?y) AS ?count) WHERE { ?x ?y ?z }
If you do want to support non-standard SPARQL via Jena you can turn on extended syntax when you parse the query:
Query q = QueryFactory.create("SELECT ?y COUNT(DISTINCT ?y) WHERE { ?x ?y ?z }", Syntax.syntaxARQ);
Note that even with this turned on there are still lots of non-standard SPARQL syntactic constructs that Virtuoso supports that ARQ will still reject.
However it is better to use standard SPARQL wherever possible as otherwise you tie your queries to a specific implementation and restrict the portability of your queries.
As you are using Jena, the correct syntax the query string should be:
String queryStringNew = "select ?y count(distinct ?y) where {?x ?y ?z .}";
or
SELECT ?y (COUNT(?y) as ?yCount)

pathLength and match functions in Allegrograph

I'm playing around with property paths and i'm using AllegroGraph Server Edition 4.9. I created a sparql query,
select ?y ?x (count(?x) as ?degree)
where{
{
?x $path ?y.
filter(match($path,foaf:knows))
filter(pathLength($path) <= 3)
}
UNION
{
?y $path ?x.
filter(match($path,foaf:knows))
filter(pathLength($path) <= 3)
}
} group by ?y ?x.
which i expected to see max 3rd degree relatedness with foaf:knows property. But it gives me match and pathLength functions are not recognised. I think this it is an allegrograph problem. Or maybe my query is wrong. Any one tried pathLengh and match functions on allegrograph?
Thanks
Çağdaş
Sparql 1.1 does not include match and pathLength functions. This is reference of property paths. So its my bad. Hope to see these features in further sparql versions.