Rewrite SPARQL DESCRIBE query as CONSTRUCT
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 UR开发者_开发知识库I?
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.
精彩评论