SPARQL to find count of grandchildren

半城伤御伤魂 提交于 2019-12-04 14:47:11

You don't mention what “the incorrect count of its children“ is. When I run your query on the schema.org OWL document, I get these results:

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

I think that this count is correct. If you expected a different count, please update the question to state what count you expected, and why. According to the schema.org Full Hierarchy page shows this hierarchy, under which the only grandchildren classes of Event are children of UserInteraction, and there are nine of them.

At any rate, the reason you're not seeing more results in this list is that the query now only finds children that actually have their own children, and this restricts your results significantly. You can select the grandchildren of Event inside of an optional pattern if you want to select the children of Event that themselves have no children. To illustrate:

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
  }
}

produces:

$ 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 |
--------------------------------------------------

Most subclasses of Event don't have any subclasses; only UserInteraction does. That said, now that we see how to find these classes, even if they don't have subclasses, the counting should fall into place with a query very much like your original:

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

The results, of course, show a count of zero for most of the classes, and nine for UserInteraction.

$ 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              |
-------------------------------------------
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!