SPARQL查找孙子的数量

时间:2013-08-12 10:34:02

标签: sparql schema.org

我正在使用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>的每个子类都包含它拥有的子项数。

1 个答案:

答案 0 :(得分:2)

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

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

我认为这个数字是正确的。如果您预计不同的计数,请更新问题以说明您预期的计数,以及原因。根据{{​​3}}页面显示此层次结构,Event的唯一孙子类是UserInteraction的子项,其中有九个。

type hierarchy for Event from 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              |
-------------------------------------------