Reputation: 29
I am looking for an easy way to get the relations from all relations of a concept. For example: You search for a concept with the name "Abc". It has some relation to other objects, such as "D", "Ef", "Ghi". The result looks like:
------------------------------------------------------------------------------- | concept | relation | value | ===================================== | uri:Abc | skos:narrower | uri:D | | uri:Abc | skos:narrower | uri:Ef | | uri:Abc | skos:broader | uri:Ghi | -------------------------------------
So now you know the relations of "Abc". If you want to know the relations from the relations of "Abc" you can use a subselect. Such as the query below. But what I want is both of the results. I will know the relations of "Abc", but also from "D", "Ef", "Ghi".
SELECT (?v1 as ?concept) ?relation ?value WHERE
{
?v1 ?relation ?value .
{
SELECT ?c1 ?r1 ?v1 WHERE
{
?c1 rdf:label "Abc" .
?c1 ?r1 ?v1 .
}
}
}
This is my current query. In production I'm use a filter in it, but for this example it is not necessary.
Upvotes: 0
Views: 839
Reputation: 85853
There's no need for a subquery here, and my initial attempt was much more complicated than what you actually needed. If you want all the subject, predicate, object triples where subject is either the thing with label "Abc" or something that it's connected to, you can use this query:
select ?s ?p ?o {
?c rdfs:label "Abc" .
?c (<>|!<>)? ?s .
?s ?p ?o .
}
?c rdfs:label "Abc" binds ?c to the Abc object, as in your query. Then ?c (<>|!<>)? ?s makes ?s be either ?c or something one relation away from it. This works because <>|!<> is a wildcard (since every property is either <> or it isn't, and ? says zero or one occurrences).
For instance, with this data and query, you get the following results:
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
@prefix skos: <http://www.w3.org/2004/02/skos/core#>.
@prefix : <urn:ex:>.
:Abc rdfs:label "Abc" ;
skos:narrower :D, :Ef ;
skos:broader :Ghi .
:D skos:narrower :Da, :De, :Do .
:Ghi skos:narrower :Ginormous, :General .
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix skos: <http://www.w3.org/2004/02/skos/core#>
prefix : <urn:ex:>
select ?s ?p ?o {
?c rdfs:label "Abc" .
?c (<>|!<>)? ?s .
?s ?p ?o
}
-------------------------------------
| s | p | o |
=====================================
| :Ghi | skos:narrower | :General |
| :Ghi | skos:narrower | :Ginormous |
| :D | skos:narrower | :Do |
| :D | skos:narrower | :De |
| :D | skos:narrower | :Da |
| :Abc | skos:broader | :Ghi |
| :Abc | skos:narrower | :Ef |
| :Abc | skos:narrower | :D |
| :Abc | rdfs:label | "Abc" |
-------------------------------------
The construct version of this (since you're essentially getting triples back) would be:
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix skos: <http://www.w3.org/2004/02/skos/core#>
prefix : <urn:ex:>
construct { ?s ?p ?o }
where {
?c rdfs:label "Abc" .
?c (<>|!<>)? ?s .
?s ?p ?o
}
Upvotes: 2