OWL disjoint X Y minimalistic

OWL disjoint X Y minimalistic#

import rdflib # https://rdflib.readthedocs.io/en/stable/ 
import owlrl # https://pypi.org/project/owlrl/

# utility fn: only show ttl paragraphs with specific strings
def focus(focus_string_list, ttl):
    return "\n\n".join( [ paragraph for paragraph in ttl.split("\n\n") \
        if any( [ focus_string in paragraph for focus_string in focus_string_list ] ) ] )
ttl = """
@prefix : <http://example.org/#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .

:X a owl:Class .
:Y a owl:Class ; owl:disjointWith :X .

:x a :X .
:y a :Y .
:xy a :X, :Y . 
"""
g = rdflib.Graph().parse(data=ttl)
# before inferencing
interesting = ["xy"]
print(focus(interesting, g.serialize()))
:xy a :X,
        :Y .
# 
owlrl.DeductiveClosure(owlrl.OWLRL_Semantics,
    axiomatic_triples = False).expand(g)
# after inferencing
print(focus(interesting, g.serialize()))
:xy a :X,
        :Y,
        owl:Thing ;
    owl:sameAs :xy .

[] a err:ErrorMessage ;
    err:error "Disjoint classes http://example.org/#Y and http://example.org/#X have a common individual http://example.org/#xy" .