Reputation: 1692
I want to have the following query in Sparql: "social triads involving Barack Obama and two other people who have some connection to the state of Illinois." Where a social triad is a group of three people where there is a link between every pair.
# Find social triads involving Barack Obama and two other people who
# have some connection to the state of Illinois. A social triad is a
# group of three people where there is a link between every pair.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpr: <http://dbpedia.org/resource/>
PREFIX dbpo: <http://dbpedia.org/ontology/>
PREFIX dbpprop: <http://dbpedia.org/property/>
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
SELECT distinct ?P1 ?P2
WHERE {
?P1 a dbpo:Person.
?P2 a dbpo:Person.
?P1 dbpprop:state dbpr:Illinois.
?P2 dbpprop:state dbpr:Illinois.
?prop a rdf:Property.
{?P1 ?prop ?P2} UNION {?P2 ?prop ?P1}.
{dbpedia:Barack_Obama ?prop ?P1} UNION {?P1 ?prop dbpedia:Barack_Obama}.
{dbpedia:Barack_Obama ?prop ?P2} UNION {?P2 ?prop dbpedia:Barack_Obama}.
}
However I get no result out of it. How can I get it to work? Moreover I want ?P1 and ?P2 to be different persons. How is it that possible?
Upvotes: 2
Views: 564
Reputation: 2193
If I understand you correctly, then this may be what you want:
SELECT distinct ?P1 ?P2
WHERE {
?P1 a dbpo:Person.
?P2 a dbpo:Person.
?P1 dbpprop:state dbpr:Illinois.
?P2 dbpprop:state dbpr:Illinois.
# all three are connected via prop
?P1 ?prop ?P2, dbpedia:Barack_Obama .
?P2 ?prop ?P1, dbpedia:Barack_Obama .
dbpedia:Barack_Obama ?prop ?P1, ?P2 .
# P1 and P2 are not the same, and not Barack Obama
FILTER (?P1 != ?P2)
FILTER (?P1 != dbpedia:Barack_Obama)
FILTER (?P2 != dbpedia:Barack_Obama)
}
Note that this queries only for "triads" of people who have the same property ?prop
linking them, not triads connected by any property.
Upvotes: 4