SPARQL查询以获取节点的所有父节点

时间:2013-07-31 11:22:44

标签: php mysql treeview sparql

我的mySQL数据库中有表格,如:

+-------+------------+-------------+-----------+
|  ID   |  subject   |  Predicate  |  object   |
+-------+------------+-------------+-----------+
|  1    | ATM        |  subClassof | Network   |
+-------+------------+-------------+-----------+
|  2    | ARPANET    |  subClassof | Network   |
+-------+------------+-------------+-----------+
|  3    | Network    |  subClassof | Main      |
+-------+------------+-------------+-----------+
|  5    | Software   |  subclassof | Main      |
+-------+------------+-------------+-----------+
|  7    | Linux      |  subClassof | Software  |
+-------+------------+-------------+-----------+
|  8    | Windows    |  subClassof | Software  |
+-------+------------+-------------+-----------+
|  12   | XP         |  subClassof | Windows   |
+-------+------------+-------------+-----------+
|  13   | Win7       |  subClassof | Windows   |
+-------+------------+-------------+-----------+
|  14   | Win8       |  subClassof | Windows   |
+-------+------------+-------------+-----------+

对于谓词subClassof,它将具有如下树视图:

Main
   |__ Network
   |         |__ ATM
   |         |__ ARPANET
   |
   |__ Software
              |__ Linux
              |__ Windows
                        |__ XP
                        |__ Win7
                        |__ Win8

我想创建一个表单,可以选择起始节点并获取所有父节点。例如,选择我想要的Win7

main, Software, Windows, Win7


第2步:有没有办法用这样的简单文字打印这个节点:

Main
   |__ Software
              |__ Windows
                        |__ Win7

1 个答案:

答案 0 :(得分:11)

您的数据可以在RDF中表示为data.n3

@prefix : <http://example.org/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

:Network rdfs:subClassOf :Main .

:ATM rdfs:subClassOf :Network .
:ARPANET rdfs:subClassOf :Network .

:Software rdfs:subClassOf :Main .

:Linux rdfs:subClassOf :Software .
:Windows rdfs:subClassOf :Software .

:XP rdfs:subClassOf :Windows .
:Win7 rdfs:subClassOf :Windows .
:Win8 rdfs:subClassOf :Windows .

从这里开始,您只需要一个SPARQL查询,该查询通过rdfs:subClassOf属性的路径(包括空路径)查找连接到特定类的所有内容。

prefix : <http://example.org/>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

select ?superclass where { 
  :Win7 rdfs:subClassOf* ?superclass
}
--------------
| superclass |
==============
| :Win7      |
| :Windows   |
| :Software  |
| :Main      |
--------------

该查询中的结果不一定按其在路径中的位置排序(尽管在这种情况下它们恰好是)。如果您确实需要它们,则可以执行此操作(基于this answer about computing the position of elements in an RDF list):

prefix : <http://example.org/>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

select ?class where { 
  :Win7 rdfs:subClassOf* ?mid .
  ?mid rdfs:subClassOf* ?class .
}
group by ?class
order by count(?mid)

这会找到?class的每个祖先:Win7以及每个?mid中间祖先。对于祖先?class,距离计算为两者之间的中间关系数(count(?mid))。它根据该距离对结果进行排序,因此:Win7是最近的祖先,之后是:Windows,依此类推。

你甚至可以做一些你想要的花哨格式:

prefix : <http://example.org/>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

select (group_concat( ?name ; separator="--" )  as ?path) where {
  {
    select ?name where { 
      :Win7 rdfs:subClassOf* ?mid .
      ?mid rdfs:subClassOf* ?class .
      bind( strAfter( str(?class), "http://example.org/") as ?name )
    }
    group by ?class ?name
    order by count(?mid)
  }
}
-----------------------------------
| path                            |
===================================
| "Win7--Windows--Software--Main" |
-----------------------------------

可能可以进行一些更高级的字符串处理并获取多行字符串。您可以查看this answer的后半部分,其中有一些花哨的格式,可以很好地对齐想法的矩阵。