计算节点之间的路径长度?
如何检索两个节点之间的路径长度?例如,给定一个组织层次结构,我如何确定父组织和子组织之间的距离有多远?考虑以下场景:
OrgA -hasSubOrganization->组织B,组织C
这是一个非常简单的情况,我想获取实体的所有直接子组织。因此路径长度为1。
OrgA ->组织B->组织C
或者一般情况
OrgA ->组织B - - - - - - - - 组织Z
我想递归地遍历图表并通过 hasSubOrganization
属性找到属于另一个组织的每个组织。为了让所有子组织递归,我可以使用 属性路径,例如 +
运算符:
OrgA hasSubOrganization+ ?subOrg
这将为我提供一直到叶节点的所有子组织。但我的最终目标是构建组织层次结构,但有关“子组织的节点数/步骤/级别/跳数”的信息丢失了。这意味着我无法重新创建可视化的组织结构。
除了子组织的名称之外,如何捕获“离开的节点数”信息?
How can I retrieve the length of a path between two nodes? For instance, given an organizational hierarchy, how can I determine how far separated are a parent and an descendant organization? Consider the following scenarios:
OrgA -hasSubOrganization-> OrgB, OrgC
This is the very simplistic case where I want to get all the immediate suborganizations of an entity. Hence the path length is 1.
OrgA -> OrgB -> OrgC
or the general case
OrgA -> OrgB - - - - - - - - OrgZ
I want to recursively traverse down the graph and find each organization belonging to another organization through the hasSubOrganization
property. To get all the sub-organizations recursive I can use property paths, e.g., the +
operator:
OrgA hasSubOrganization+ ?subOrg
This will give me all the suborganizations right down to the leaf nodes. But my ultimate goal is to build the organization hierarchy, but the information about the "Number of nodes/steps/levels/hops away a suborganization is" is lost. This means that I cannot recreate the org structure for a visualization.
How can I capture the "number of nodes away" information in addition to the name of the suborganization?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这基于使用 SPARQL 计算 RDF 列表中元素位置的相同技术,如下所述: Is是否可以在 SPARQL 中获取 RDF 集合中元素的位置?
如果您有这样的数据:
它描述了这样的层次结构:
然后您可以使用如下查询:
得到如下结果:
这里的技巧是认识到从 X 到 Y 的任何路径都可以被视为(可能是空的) )从 X 到某个中间节点 Z 的路径(非空意味着您可以选择 X 作为 Z)与从 Z 到 Y 的(非空)路径连接。选择 Z 的可能方式的数量表示路径的长度。
This is based on the same technique used to compute the position of an element in an RDF list using SPARQL that is described in: Is it possible to get the position of an element in an RDF Collection in SPARQL?
If you have data like this:
which describes a hierarchy like this:
then you can use a query like this:
to get results like these:
The trick here is to recognize that any path from X to Y can be viewed as a (possibly empty) path from X to some intermediate node Z (nonempty means that you can choose X as Z) concatenated with a (non empty) path from Z to Y. The number of possible ways of picking Z indicates the length of the path.
您无法使用正确的路径来执行此操作,因为工作组专门选择不提供此信息,因为它使实现变得更加复杂。
如果您想生成层次结构,那么进行一系列 SPARQL 查询可能同样高效,其中每个查询扩展层次结构的一个叶子,并且根本不使用属性路径(如果您的目标只是可视化层次结构
)使用 Jena Ontology API 的其他方法 - 我建议在他们的邮件列表上询问 [email受保护]获取更多专家帮助
You can't do this using propery paths since the working group specifically chose not to make this information available as it makes implementation much more complex.
If you want to generate a hierarchy it will probably be just as efficient to make a whole series of SPARQL queries where each query expands one leaf of the hierarchy and not use property paths at all if your goal is just to visualise the hierarchy
There may be other approaches using the Jena Ontology API - I'd recommend asking on their mailing list [email protected] for more expert help