【问题标题】:SPARQL query to get all parent of a nodeSPARQL 查询以获取节点的所有父节点
【发布时间】:2013-07-31 11:22:44
【问题描述】:

我的 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

【问题讨论】:

  • 这个问题很有用,而且很切题。不知道为什么它被关闭了。

标签: php mysql treeview sparql


【解决方案1】:

您的数据可以在 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)

这会找到:Win7 的每个祖先?class 以及每个?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 的后半部分,其中有一些精美的格式,可以很好地对齐想法矩阵。

【讨论】:

  • 对你答案的最后一部分 …in progress… 大赞。 :) 并感谢您的正确回答。投票并接受,我无能为力!
  • 好吧,毕竟它还在进行中。 :) 现在我正在考虑它可能不会很难获得格式良好的多行字符串。以后可能会更新……
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-11
  • 1970-01-01
  • 1970-01-01
  • 2018-03-13
  • 2019-07-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多