2

我想在 Sparql 中进行以下查询:“涉及巴拉克奥巴马和其他两个与伊利诺伊州有某种联系的人的社会三合会。” 社会三合会是由三个人组成的一组,每对之间都有联系。

# 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}. 

  }

但是我没有得到任何结果。我怎样才能让它工作?此外,我希望 ?P1 和 ?P2 是不同的人。这怎么可能?

4

1 回答 1

4

如果我对您的理解正确,那么这可能就是您想要的:

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)
}

请注意,这仅查询具有相同属性的人“三合会” ,而不是任何属性连接的三合会。 ?prop

于 2013-03-26T10:13:50.433 回答