如何让这个sparql查询工作?

时间:2013-03-26 00:36:52

标签: sparql semantic-web

我希望在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是不同的人。怎么可能呢?

1 个答案:

答案 0 :(得分: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将其链接起来的人任何属性连接的三元组。