1

Is there a way to know in my query how many steps were followed by the inferencer to connect a subclass to a super class?

Theo Stefou
  • 389
  • 2
  • 16
  • no, that's not possible. That's nowhere in the SPARQL specs and anything beyond is research topic. I mean not that I know what you mean by "inferencer" ... but SPARQL is nothing more than a pattern matching based query engine with - since SPARQL 1.1 - entailment support if implemented by the triple store. Even getting length of path is mostly impossible with SPARQL, that's the wrong query language compared to graph traversal languages like Cypher, Gremlin, etc. – UninformedUser Oct 16 '19 at 03:10
  • Ok, but what if I want to find a subclass which is at most two subclass of paths away? Is there something to be done if the inferencer is on? – Theo Stefou Oct 16 '19 at 09:04
  • you can use SPARQL property path `?cls rdfs:subClassOf/rdfs:subClassOf? ` for distance 1 or 2. If you want to have the intermediate nodes, you can use `UNION`, e.g. `{?cls rdfs:subClassOf } UNION {?cls rdfs:subClassOf ?mid . ?mid rdfs:subClassOf }` – UninformedUser Oct 16 '19 at 12:07
  • I have done this and it works fine. But if the inferencer is on, it's a different story – Theo Stefou Oct 16 '19 at 14:05
  • with inference enabled this won't work, correct. any triple pattern `?cls rdfs:subClassOf ` would bind all inferred subclasses to the variable `?cls`. but then the question would be, why not disable inference (e.g. GraphDB allows this via a query param ad-hoc) and compute the steps on the client side? but honestly, without knowing the whole use-case, it's hard to give any meaningful advice. – UninformedUser Oct 16 '19 at 16:47

1 Answers1

1

You could use the length of the rdfs:subClass path between the classes.

Try to adapt the answer from Calculate length of path between nodes?

e.g. something like:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select ?sub ?super (count(?mid) as ?distance) { 
  ?sub rdfs:subClassOf* ?mid .
  filter(!sameTerm(?sub, ?mid))
  ?mid rdfs:subClassOf+ ?super .
}
group by ?sub ?super 
order by ?sub ?super
Damyan Ognyanov
  • 791
  • 3
  • 7