lambdamusic / Ontospy

Python library and command-line interface for inspecting and visualizing RDF models aka ontologies.
http://lambdamusic.github.io/Ontospy/
MIT License
218 stars 52 forks source link

Add non-redundant-tree logic for ontodocs #87

Open floatingpurr opened 4 years ago

floatingpurr commented 4 years ago

Hello Michele (a.k.a. @lambdamusic). Please, consider the integration of this PR into Ontospy.

In a nutshell

This PR allows the generation of ontologyClassTrees, ontologyPropTrees, ontologyConceptTrees without repeating redundant sub-entities along the hierarchy. It display a "distinct" taxonomy without repetitions. Multi-inheritance (i.e., a class inheriting from diverse classes) is preserved.

Application

Useful when you need to represent trees of full reasoned ontologies (e.g., when you load the ontology from the SPARQL endpoint of Ontotext's GraphDB with reasoning enabled). In those cases new implicit triples are inferred and added to the knowledge graph (e.g., due to the transitivity of properties likerdfs:subClassOf) but you might not want to visualize all implicit sub-*-classes relations in the ontodocs trees.

Rationale

Rule introduced:

Full story

Let's start from this ontology

@prefix : <http://theRed-beardedOntology#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

#################################################################
#    Classes
#################################################################

:Mammal rdf:type owl:Class .

:Human rdf:type owl:Class ;
       rdfs:subClassOf :Mammal .

:Red-beardedGuy rdf:type owl:Class ;
                rdfs:subClassOf :Human .

:Mammal is subclassed by :Human that is subclassed by :Red-beardedGuy. If this ontology is reasoned with common rulesets, the following triple is materialized:

:Red-beardedGuy rdfs:subClassOf :Mammal .

Now, if we call the legacy version of ontologyClassTrees, we would get this tree in the ontology doc:

http://theRed-beardedOntology#Mammal
|       http://theRed-beardedOntology#Human
|       |        http://theRed-beardedOntology#Red-beardedGuy
|       http://theRed-beardedOntology#Red-beardedGuy

...and it's perfectly fine. Indeed, :Red-beardedGuy is a sub-class both of :Mammal and of :Human. However, this representation could be pretty redundant for some uses. A kind of "distinct" taxonomy like this one is desirable:

http://theRed-beardedOntology#Mammal
|       http://theRed-beardedOntology#Human
|       |        http://theRed-beardedOntology#Red-beardedGuy

and it makes much more sense to the reader. : )

To get this, I introduced the arg repeatChildren in the tree methods of the class Ontospy (it defaults to True for backward compatibility). It repeatChildren is set to False, you get the non-redundant tree version.

Let me know your reactions as you go through it.

Ciao Michele e grazie per questo bel lavoro che metti a disposizione qui su GitHub!

lambdamusic commented 2 years ago

Hello Andrea this looks very useful. I will review a bit more before merging. PS Massive apologies for the abysmal delay in getting back to you!

floatingpurr commented 2 years ago

Hey Michele, no worries at all. This is just a little contribution. I really enjoyed working with this library!

jonassmedegaard commented 9 months ago

Any progress in getting this nice looking PR accepted?