繁体   English   中英

SPARQL查找孙子的数量

[英]SPARQL to find count of grandchildren

我正在使用schema.org本体。 我想返回<http://schema.org/Event>的子类,其子类数为每个返回的子类。 我的查询当前正在返回一个子类。

PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> 
SELECT ?uri (count(?children) as ?childrenCount) WHERE 
{
  ?uri rdfs:subClassOf <http://schema.org/Event>.
  ?children rdfs:subClassOf ?uri
} 
GROUP BY ?uri

我希望<http://schema.org/Event>每个子类都包含它拥有的子节点数。

你没有提到“孩子的错误数量”是什么。 当我在schema.org OWL文档上运行您的查询时,我得到以下结果:

-------------------------------------------------------
| uri                                 | childrenCount |
=======================================================
| <http://schema.org/UserInteraction> | 9             |
-------------------------------------------------------

我认为这个数字是正确的。 如果你预期不同的数,请更新问题说明什么算你的预期,以及为什么。 根据schema.org Full Hierarchy页面显示了这个层次结构,其中Event的唯一孙子类是UserInteraction子类,其中有9个。

来自schema.org的事件的类型层次结构

无论如何,您在此列表中没有看到更多结果的原因是查询现在只查找实际拥有自己子项的子项,这会显着限制您的结果。 如果要选择自己没有孩子的Event子项,可以在optional模式中选择Event的孙子。 为了显示:

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

select * where {
  ?child rdfs:subClassOf schema:Event .
  optional { 
    ?grandchild rdfs:subClassOf ?child
  }
}

生产:

$ arq --data schemaorg.owl --query query.sparql
--------------------------------------------------
| child                  | grandchild            |
==================================================
| schema:FoodEvent       |                       |
| schema:Festival        |                       |
| schema:SportsEvent     |                       |
| schema:SaleEvent       |                       |
| schema:EducationEvent  |                       |
| schema:ChildrensEvent  |                       |
| schema:DanceEvent      |                       |
| schema:BusinessEvent   |                       |
| schema:TheaterEvent    |                       |
| schema:SocialEvent     |                       |
| schema:VisualArtsEvent |                       |
| schema:MusicEvent      |                       |
| schema:ComedyEvent     |                       |
| schema:LiteraryEvent   |                       |
| schema:UserInteraction | schema:UserCheckins   |
| schema:UserInteraction | schema:UserTweets     |
| schema:UserInteraction | schema:UserLikes      |
| schema:UserInteraction | schema:UserPlays      |
| schema:UserInteraction | schema:UserDownloads  |
| schema:UserInteraction | schema:UserPlusOnes   |
| schema:UserInteraction | schema:UserComments   |
| schema:UserInteraction | schema:UserBlocks     |
| schema:UserInteraction | schema:UserPageVisits |
--------------------------------------------------

Event大多数子类都没有任何子类; 只有UserInteraction才有。 也就是说,既然我们已经看到了如何找到这些类,即使它们没有子类,计数也应该与查询非常相似:

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

select ?child (count(?grandchild) as ?nGrandchildren) where {
  ?child rdfs:subClassOf schema:Event .
  optional { 
    ?grandchild rdfs:subClassOf ?child
  }
}
group by ?child

当然,结果显示大多数类的计数为零,而UserInteraction的计数为9。

$ arq --data schemaorg.owl --query query.sparql
-------------------------------------------
| child                  | nGrandchildren |
===========================================
| schema:ComedyEvent     | 0              |
| schema:ChildrensEvent  | 0              |
| schema:SportsEvent     | 0              |
| schema:FoodEvent       | 0              |
| schema:BusinessEvent   | 0              |
| schema:Festival        | 0              |
| schema:EducationEvent  | 0              |
| schema:LiteraryEvent   | 0              |
| schema:SaleEvent       | 0              |
| schema:TheaterEvent    | 0              |
| schema:SocialEvent     | 0              |
| schema:UserInteraction | 9              |
| schema:MusicEvent      | 0              |
| schema:DanceEvent      | 0              |
| schema:VisualArtsEvent | 0              |
-------------------------------------------

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM