Rewrite SPARQL DESCRIBE query as CONSTRUCT - sparql

For some reason I can't issue DESCRIBE queries using Redland ( librdf.org ), is it possible to rewrite DESCRIBE as a CONSTRUCT QUERY for a given URI?
DESCRIBE <urn:my-uri>
I was thinking about writting it into something like this but I don't think this is valid in SPARQL
CONSTRUCT { ?subject ?predicate ?object }
WHERE {
{ ?subject ?predicate ?object }
AND {
{ <urn:my-uri> ?predicate ?object }
OR { ?subject <urn:my-uri> ?object }
OR { ?subject ?predicate <urn:my-uri> }
}
}

Your are right that is not a valid SPARQL. The closest thing to your OR is UNION. And, there is no need for the AND operator, every triple pattern is by default a join not a union.
For what you are trying is better to use a FILTER, like this example:
CONSTRUCT { ?subject ?predicate ?object }
WHERE { ?subject ?predicate ?object .
FILTER ( ?subject = <urn:your_uri> || ?object = <urn:your_uri>)
}
In some systems, for large knowledge bases, this query can be very expensive. And also if your database contains bNodes this query won't get the description of those nodes, it will get just the internal code. For most cases, running a DESCRIBE manually can't be accomplished with a single query and you'll have to implement some recursive logic in order to get all the information that describes a URI.

After trying something like the FILTER ( A || B ) method, I got the impression that it is pretty slow.
I think you can do the same thing, basically, but using VALUES and UNION
I tried it on DBPedia (~2.46 billion triples) with a movie, and it seemed to perform well.
CONSTRUCT {
?subject ?predicate ?object
}
WHERE {
{ ?subject ?predicate ?object .
VALUES ?subject { dbpedia:The_Matrix }
}
UNION
{ ?subject ?predicate ?object .
VALUES ?object { dbpedia:The_Matrix }
}
}
sparql result on dbpedia
Edit: Just for the sake of additional info, I think you could technically also write the following:
CONSTRUCT { ?subject ?predicate ?object }
WHERE {
?subject ?predicate ?object .
OPTIONAL { dbpedia:The_Matrix ?predicate ?object . }
OPTIONAL { ?subject ?predicate dbpedia:The_Matrix . }
}
but some popular RDF databases really can't handle OPTIONAL very performantly yet, and will die.

Related

Nested SPARQL Query

I have the following structure an Object and each Object has multiple Children as Changelog to the Parent Object and each Change Log has an Author.
Now each Changelog has status and Timestamp I would now like to write a SPARQL query where I need to get the list of Objects which has me as the author and of particular status say "to be approved" and it has to be the latest changelog.
This query is what I have tried so far :
I try to get first the changelog then the object related to it after that get all the other changelog in it and compare the latest changelog by timestamp and filter it if it is greater.
select ?Object WHERE{ ?author myPrefix:userId "myuserid".
?changelog myPrefix:hasApprover ?author .
?changelog myPrefix:timestamp ?dv
FILTER( ?dv >= ?latestdate )
{
SELECT (max(?dv1) AS ?latestdate) WHERE {
?Object myPrefix:hasChangeLog ?changelog .
?Object myPrefix:hasChangeLog ?changelog1 .
?changelog1 myPrefix:timestamp ?dv1 .
}
}
?changelog myPrefix:status "To be approved" .
?Object myPrefix:hasChangeLog ?changelog .}
What I am doing wrong here is that it compares it to all the Objects changelog and not Objectwise comparison how can I achieve that?
As per the suggestion from UninformedUser:
select ?Object WHERE{ ?author myPrefix:userId "myuserid".
?changelog myPrefix:hasApprover ?author .
?changelog myPrefix:timestamp ?dv
FILTER( ?dv >= ?latestdate )
{
SELECT (max(?dv1) AS ?latestdate) WHERE {
?Object myPrefix:hasChangeLog ?changelog1 .
?changelog1 myPrefix:timestamp ?dv1 .
}group by ?Object
}
?changelog myPrefix:status "To be approved" .
?Object myPrefix:hasChangeLog ?changelog .}

Retrieve properties and their descriptions from instances of the same class

I want to retrieve all distinct object properties of instances with the same type (class), starting with two initial seeds (wd:Q963 and wd:Q42320). First, I ask for the type (and maybe subtype) of such seeds. Second, all instances of the same class of the seeds are retrieved. Third, properties of the instances are retrieved. Finally, I want to retrieve descriptions of such properties and if possible alternative labels. My query is as follows:
select distinct ?property ?description ?label where{
{
wd:Q963 wdt:P31 ?typesSubject .
?instancesS (wdt:P31|wdt:P279) ?typesSubject .
?instancesS ?property ?unknown .
}
UNION
{
wd:Q42320 wdt:P31 ?typesObject .
?instancesO (wdt:P31|wdt:P279) ?typesObject .
?unknown ?property ?instancesO .
}
?claimPredicate wikibase:directClaim ?property .
?claimPredicate schema:description ?description .
?claimPredicate rdfs:label ?label .
FILTER(strstarts(str(?property),str(wdt:)))
FILTER(strstarts(str(?unknown),str(wd:)))
FILTER(LANG(?description) = "en").
FILTER(LANG(?label) = "en").
}
The problem is that my actual query takes a lot of time and it fails in the public Wikidata endpoint. Does anyone can provide me some hints to optimize such a query?
To be honest, I can't understand the aim of your query. I suppose you are interested in semantic similarity or something like.
Basically, you could reduce the number of joins, retrieving only unique wdt-predicates with nested SELECT DITINCT.
SELECT ?property ?claimPredicateLabel ?claimPredicateDescription
WHERE {
hint:Query hint:optimizer "None" .
{
SELECT DISTINCT ?property {
VALUES (?s) {(wd:Q963) (wd:Q42320)}
?s wdt:P31/^(wdt:P31|wdt:P279) ?instances .
?instances ?property ?unknown .
}
}
?claimPredicate wikibase:directClaim ?property .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
Try it!
This is fast enough (~ 3s) even with SERVICE wikibase:label.
Also, you don't need FILTER(strstarts(str(?property),str(wdt:))) after ?claimPredicate wikibase:directClaim ?property.
As for hint:Query hint:optimizer "None", this hint forces Blazegraph to follow standard bottom-up evaluation order. In this particular query, hint:Query hint:optimizer "Runtime" or hint:SubQuery hint:runOnce true should also work.

Federated SPARQL Query on a subgraph of a SPARQL endpoint

I would like to ask, how to do a federated SPARQL query on a subgraph of a SPARQL endpoint (not the entire remote SPARQL endpoint).
I got my data in Virtuoso v7 while the SPARQL endpoint is "http://localhost:8890/sparql", I'd like to do a remote query on a subgraph of this endpoint which is "http://localhost:8890/TC", and I tried
SELECT *
WHERE
{ SERVICE <http://localhost:8890/sparql>
{ SELECT ?subject ?predicate ?object
FROM <http://localhost:8890/TC>
WHERE
{ ?subject ?predicate ?object }
}
} LIMIT 50
And I got the error that "FROM" is not correctly used, so I have two questions:
1) can I do a remote query on a subgraph of a SPARQL endpoint?
2) can I have a SPARQL endpoint for each graph in Virtuoso v7?
Thanks a lot for your help.
You can use graph instead of from.
In your example:
SELECT *
WHERE
{
SERVICE <http://localhost:8890/sparql>
{
SELECT ?subject ?predicate ?object
WHERE
{ graph <http://localhost:8890/TC> { ?subject ?predicate ?object } }
}
} LIMIT 50
I tested this syntax with the following query in the Uniprot SPARQL endpoint (Virtuoso) while federating with dbpedia (Virtuoso):
SELECT *
WHERE
{ SERVICE <http://dbpedia.org/sparql>
{select distinct ?activity where { graph <http://dbpedia.org> {?activity a <http://www.ontologydesignpatterns.org/ont/d0.owl#Activity>} } LIMIT 10
}
} LIMIT 50

Query for resources using their URIs

I have a bunch of resource URIs, and I need the property values related to each of them. For a single resource, say <http://my.url/res#resourceUri>, I can write this query:
PREFIX v: <http://my.url/res#>
SELECT ?name
WHERE {
<http://my.url/res#resourceUri> a v:t;
rdfs:label ?name .
}
For multiple resources, I can use UNION, like this:
PREFIX v: <http://my.url/res#>
SELECT ?name
WHERE {
{ <http://my.url/res#resourceUri> a v:t; rdfs:label ?name } UNION
{ <http://my.url/res#anotherResource> a v:t; rdfs:label ?name }
}
Is there a way to write a shorter, leaner version of this second query?
You can use values for this. Your example would be written as
PREFIX v: <http://my.url/res#>
SELECT ?resource ?name WHERE {
values ?resource { <http://my.url/res#resourceUri>
<http://my.url/res#anotherResource> }
?resource a v:t;
rdfs:label ?name
}
The question is different, but the answer to how to use Union/or in sparql path with arbitrary length? is similar.

extract city data from dbpedia or LinkedGeoData

I'm trying now for a couple of hours to figure out how to get various informations out of dbpedia or LinkedGeoData.
I used this interface (http://dbpedia.org/snorql) and tried a different approaches, but I never got the result that I need.
If I use something lik this:
SELECT * WHERE {
?subject rdf:type <http://dbpedia.org/ontology/City>.
OPTIONAL {
?subject <http://dbpedia.org/ontology/populationTotal> ?populationTotal.
}
OPTIONAL {
?subject <http://dbpedia.org/ontology/populationUrban> ?populationUrban.
}
OPTIONAL {
?subject <http://dbpedia.org/ontology/areaTotal> ?areaTotal.
}
OPTIONAL {
?subject <http://dbpedia.org/ontology/populationUrbanDensity> ?populationUrbanDensity.
}
OPTIONAL {
?subject <http://dbpedia.org/ontology/isPartOf> ?isPartOf.
}
OPTIONAL {
?subject <http://dbpedia.org/ontology/country> ?country.
}
OPTIONAL {
?subject <http://dbpedia.org/ontology/utcOffset> ?utcOffset.
}
OPTIONAL {
?subject <http://dbpedia.org/property/janHighC> ?utcOffset.
}
OPTIONAL {
?subject <http://dbpedia.org/property/janLowC> ?utcOffset.
}
}
LIMIT 20
I run out of limits.
I also tried this:
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/>
SELECT * WHERE {
?subject rdf:type <http://dbpedia.org/ontology/City>.
?subject rdfs:label ?label.
FILTER ( lang(?label) = 'en'
}
LIMIT 100
But that give me en error, which I don't understand. If I remove the FILTER, it works but give me the labels in all languages...
What I'm looking for is something like this http://dbpedia.org/page/Vancouver
But not all the data, but some of it like population, area, coutry, elevation, lat, long, timezone, label#en, abstract#en etc.
Can someone help me to get working syntax?
Thanks for y'all help.
UPDATE:
I got it to work so far with:
SELECT DISTINCT *
WHERE {
?city rdf:type dbpedia-owl:Settlement ;
rdfs:label ?label;
dbpedia-owl:abstract ?abstract ;
dbpedia-owl:populationTotal ?pop ;
dbpedia-owl:country ?country ;
dbpprop:website ?website .
FILTER ( lang(?abstract) = 'en' && lang(?label) = 'en')
}
LIMIT 20
But still running out of limits if I want to get all settlements. Btw. is there a way to get all cities and settlements in one table?
By "run out of limits", do you mean the error "Bandwidth Limit Exceeded URI = '/!sparql/'"? I guess this is a limit set by dbpedia to make sure that it is not flooded with queries that take "forever" to run, and if so, then there is probably not so much you can do. You can ask for data in chunks, using OFFSET, LIMIT and ORDER BY, see http://www.w3.org/TR/rdf-sparql-query/#modOffset.
UPDATE: Yes, this seems to be the way to go: http://www.mail-archive.com/dbpedia-discussion#lists.sourceforge.net/msg03368.html
In the second query the error is a missing parenthesis. This
FILTER ( lang(?label) = 'en'
should be
FILTER ( lang(?label) = 'en')
For your last question, a natural way to collect multiple things/(similiar queries) in one query/table is using UNION, e.g.,
SELECT ?x
WHERE {
{ ?x rdf:type dbpedia-owl:City }
UNION
{ ?x rdf:type dbpedia-owl:Settlement }
}