3

I am using the schema.org ontology. I want to return the subclasses of <http://schema.org/Event> with the number of children for each returned subclass. My query is currently returning one subclass.

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

I want every subclass of <http://schema.org/Event> with the number of children it has.

rumplestilskin
  • 272
  • 2
  • 13
  • What results _are_ you getting from that query? I get the one class `UserInteraction` and a count of `9`. I can see (and have explained in my answer) why you're not seeing more classes, but I don't see what's incorrect about this one count. What value were you expecting? It's hard to fix something if we don't know whether you'd consider the results we get are correct or incorrect. – Joshua Taylor Aug 12 '13 at 11:39
  • Yeah, my mistake I miscounted the grandchildren, will edit question. – rumplestilskin Aug 12 '13 at 13:03

1 Answers1

3

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.

type hierarchy for Event from schema.org

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              |
-------------------------------------------
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353