Skip to content

Commit

Permalink
core: exporters: owl2: refactor export of items' annotations
Browse files Browse the repository at this point in the history
Previous to this all project's annotations (except for axiom annotations)
where exported by iterating over all project IRIs at the end of
the export procedure, and generating an annotation assertion for each of them.
This has the drawback that there is no control for filtering
annotations of entities that are part of diagrams excluded from
the exported OWL ontology.

This commit refactors the way annotations associated to items are exported
by moving their generation at the time where an item is exported (similarly
to what happended with the meta system), so that the diagram filtering
is applied. Remaining non-item annotations (such as ontology annotations)
are still dealt with separately.

Closes #155
  • Loading branch information
mnamici committed Sep 29, 2021
1 parent a690dae commit 11ca7b5
Showing 1 changed file with 49 additions and 48 deletions.
97 changes: 49 additions & 48 deletions eddy/core/exporters/owl2.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ def __init__(self, project, path=None, **kwargs):
if not self.vm.isRunning():
self.vm.initialize()
self.vm.attachThreadToJVM()
self.AddOntologyAnnotation = self.vm.getJavaClass('org.semanticweb.owlapi.model.AddOntologyAnnotation')
self.DefaultPrefixManager = self.vm.getJavaClass('org.semanticweb.owlapi.util.DefaultPrefixManager')
self.FunctionalSyntaxDocumentFormat = self.vm.getJavaClass('org.semanticweb.owlapi.formats.FunctionalSyntaxDocumentFormat')
self.HashSet = self.vm.getJavaClass('java.util.HashSet')
Expand Down Expand Up @@ -675,6 +676,27 @@ def step(self, num, increase=0):
# AUXILIARY METHODS
#################################

def getOWLApiAnnotation(self, annotation):
"""
Returns an OWLAnnotation corresponding to the given annotation object.
:type annotation: Annotation
:rtype: OWLAnnotation
"""
prop = self.df.getOWLAnnotationProperty(self.IRI.create(str(annotation.assertionProperty)))
if annotation.isIRIValued():
value = self.IRI.create(str(annotation.value))
else:
lexicalForm = annotation.value.replace('\n', ' ')
if annotation.language:
value = self.df.getOWLLiteral(lexicalForm, annotation.language)
else:
if annotation.datatype:
datatype = self.df.getOWLDatatype(self.IRI.create(str(annotation.datatype)))
else:
datatype = self.OWL2Datatype.RDF_PLAIN_LITERAL
value = self.df.getOWLLiteral(lexicalForm, datatype)
return self.df.getOWLAnnotation(prop, value)

def getOWLApiDatatype(self, datatype):
"""
Returns the OWLDatatype matching the given Datatype.
Expand Down Expand Up @@ -807,25 +829,9 @@ def getAxiomAnnotationSet(self, edge):
:rtype: Set
"""
collection = self.HashSet()
for annotation in edge.annotations:
annProp = annotation.assertionProperty
owlApiProp = self.df.getOWLAnnotationProperty(self.IRI.create(str(annProp)))
if annotation.isIRIValued():
obj = annotation.value
owlApiObj = self.IRI.create(str(obj))
else:
obj = annotation.value.replace('\n', ' ')
datatype = annotation.datatype
lang = annotation.language
if lang:
owlApiObj = self.df.getOWLLiteral(obj, lang)
else:
if datatype:
owlApiDatatype = self.df.getOWLDatatype(self.IRI.create(str(datatype)))
else:
owlApiDatatype = self.df.getOWLDatatype(self.IRI.create(str(OWL2Datatype.PlainLiteral.value)))
owlApiObj = self.df.getOWLLiteral(obj, owlApiDatatype)
collection.add(self.df.getOWLAnnotation(owlApiProp, owlApiObj))
if OWLAxiom.Annotation in self.axiomsList:
for annotation in edge.annotations:
collection.add(self.getOWLApiAnnotation(annotation))
return collection

def getComplement(self, node):
Expand Down Expand Up @@ -1316,6 +1322,17 @@ def getValueDomain(self, node):
# AXIOMS GENERATION
#################################

def createAnnotationAssertionAxioms(self, node):
"""
Generate a set of OWL 2 annotation assertion axioms for the given node annotations.
:type node: AbstractNode
"""
if OWLAxiom.Annotation in self.axiomsList:
for annotation in node.iri.annotationAssertions:
subject = self.IRI.create(str(annotation.subject))
value = self.getOWLApiAnnotation(annotation)
self.addAxiom(self.df.getOWLAnnotationAssertionAxiom(subject, value))

def createClassAssertionAxiom(self, edge):
"""
Generate a OWL 2 ClassAssertion axiom.
Expand Down Expand Up @@ -1805,6 +1822,15 @@ def run(self):
for prefix, ns in self.project.prefixDictItems():
self.pm.setPrefix(prefix, ns)

#############################################
# ONTOLOGY ANNOTATIONS
#################################

if OWLAxiom.Annotation in self.axiomsList:
for annotation in self.project.ontologyIRI.annotationAssertions:
value = self.getOWLApiAnnotation(annotation)
self.ontology.applyChange(self.AddOntologyAnnotation(self.ontology, value))

LOGGER.debug('Initialized OWL 2 Ontology: %s', ontologyIRI)

#############################################
Expand Down Expand Up @@ -1837,6 +1863,10 @@ def run(self):
self.createPropertyRangeAxiom(node)
elif node.type() is Item.HasKeyNode:
self.createHasKeyAxiom(node)

if node.isPredicate():
self.createAnnotationAssertionAxioms(node)

self.step(+1)

LOGGER.debug('Generated OWL 2 axioms from nodes (axioms = %s)', len(self.axioms()))
Expand Down Expand Up @@ -1953,35 +1983,6 @@ def run(self):

LOGGER.debug('Generated OWL 2 axioms from edges (axioms = %s)', len(self.axioms()))

#############################################
# ANNOTATION ASSERTIONS
#################################

if OWLAxiom.Annotation in self.axiomsList:
for ann in [x for iri in self.project.iris for x in iri.annotationAssertions]:
subj = ann.subject
owlApiSubj = self.IRI.create(str(subj))
annProp = ann.assertionProperty
owlApiProp = self.df.getOWLAnnotationProperty(self.IRI.create(str(annProp)))
if ann.isIRIValued():
obj = ann.value
owlApiObj = self.IRI.create(str(obj))
else:
obj = ann.value.replace('\n', ' ')
datatype = ann.datatype
lang = ann.language
if lang:
owlApiObj = self.df.getOWLLiteral(obj, lang)
else:
if datatype:
owlApiDatatype = self.df.getOWLDatatype(self.IRI.create(str(datatype)))
else:
owlApiDatatype = self.df.getOWLDatatype(self.IRI.create(str(OWL2Datatype.PlainLiteral.value)))
owlApiObj = self.df.getOWLLiteral(obj, owlApiDatatype)
self.addAxiom(self.df.getOWLAnnotationAssertionAxiom(owlApiProp, owlApiSubj, owlApiObj))

LOGGER.debug('Generated OWL 2 annotation assertion axioms from edges (axioms = %s)', len(self.axioms()))

#############################################
# APPLY GENERATED AXIOMS
#################################
Expand Down

0 comments on commit 11ca7b5

Please sign in to comment.