Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid dropping axioms or misplacing annotations when malformed axiom annotations are present #1062

Merged
merged 3 commits into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.semanticweb.owlapi.rio;


import org.junit.jupiter.api.Test;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.OWLAnnotation;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;

import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;

public class TestRDFParsingAmongMalformedAxiomAnnotations {

@Test
public void testAxiomParsing() throws OWLOntologyCreationException, IOException {
try (InputStream ttlStream = this.getClass().getResourceAsStream("/rdfParserReservedPropertiesAnnotations.ttl")) {
OWLOntology ontTTL = OWLManager.createOWLOntologyManager().loadOntologyFromOntologyDocument(ttlStream);
assertEquals(14, ontTTL.getAxiomCount(), "Test file should have 14 axioms");
Optional<OWLAnnotation> annotationOpt = ontTTL.getAxioms().stream()
.flatMap(ax -> ax.getAnnotations().stream())
.filter(ann -> ann.getValue().isLiteral())
.filter(ann -> ann.getValue().asLiteral().get().getLiteral().equals("This assertion is annotated"))
.findAny();
assertFalse(annotationOpt.isPresent(), "Annotation should not have been added to any axiom.");
}
}

}
115 changes: 115 additions & 0 deletions contract/src/test/resources/rdfParserReservedPropertiesAnnotations.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
@prefix : <http://example.org/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@base <http://example.org/> .

<http://example.org/> rdf:type owl:Ontology .

#################################################################
# Object Properties
#################################################################

### http://example.org/contains
:contains rdf:type owl:ObjectProperty .


### http://example.org/has_book
:has_book rdf:type owl:ObjectProperty ;
owl:propertyChainAxiom ( :has_books
:contains
) .


### http://example.org/has_books
:has_books rdf:type owl:ObjectProperty .


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

### http://example.org/Book
:Book rdf:type owl:Class .


### http://example.org/BookOrLibrary
:BookOrLibrary rdf:type owl:Class ;
owl:equivalentClass [ rdf:type owl:Class ;
owl:unionOf ( :Book
:Library
)
] .

[ rdf:type owl:Axiom ;
owl:annotatedSource :BookOrLibrary ;
owl:annotatedProperty owl:equivalentClass ;
owl:annotatedTarget [ rdf:type owl:Class ;
owl:unionOf ( :Book
:Library
)
] ;
rdfs:comment "This equivalence axiom is annotated."
] .


### http://example.org/Library
:Library rdf:type owl:Class ;
rdfs:subClassOf [ rdf:type owl:Restriction ;
owl:onProperty :has_books ;
owl:allValuesFrom [ rdf:type owl:Restriction ;
owl:onProperty :contains ;
owl:allValuesFrom :Book
]
] .


#################################################################
# Individuals
#################################################################

### http://example.org/moby_dick
:moby_dick rdf:type owl:NamedIndividual .


### http://example.org/the_library
:the_library rdf:type owl:NamedIndividual ,
:Library .

[ rdf:type owl:Axiom ;
owl:annotatedSource :the_library ;
owl:annotatedProperty rdf:type ;
owl:annotatedTarget :Library ;
rdfs:comment "This class assertion is annotated."
] .


### http://example.org/the_republic
:the_republic rdf:type owl:NamedIndividual .

_:genid42 rdf:type owl:Axiom ;
owl:annotatedSource _:genid8 ;
owl:annotatedProperty rdf:first ;
owl:annotatedTarget :moby_dick ;
rdfs:comment "This assertion is annotated" .

# These triples should be ignored
_:genid43 rdf:type owl:Axiom ;
owl:annotatedSource _:genid8 ;
owl:annotatedProperty rdfs:first ; #Not a real property but triggers bug
owl:annotatedTarget :moby_dick ;
rdfs:comment "This assertion is annotated" .

# These triples should be ignored
_:genid44 rdf:type owl:Axiom ;
owl:annotatedSource _:genid8 ;
owl:annotatedProperty rdf:subject ;
owl:annotatedTarget :moby_dick ;
rdfs:comment "This assertion is annotated" .

_:genid8 :contains :moby_dick .


### Generated by the OWL API (version 4.5.9.2019-02-01T07:24:44Z) https://github.com/owlcs/owlapi
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,10 @@ protected void handle(IRI s, IRI p, IRI o) {
}
}
}
// Clears pending annotations; if this triple represents an annotated axiom, then if an axiom was created to annotate,
// pending will already be cleared. However, if an axiom was not created (e.g. invalid triple such as misuse of reserved
// vocabulary) if annotations are left they will be placed on an incorrect axiom.
consumer.getPendingAnnotations();
}

protected void handle(IRI s, IRI p, OWLLiteral o) {
Expand Down Expand Up @@ -2385,8 +2389,10 @@ public void handleTriple(IRI s, IRI p, IRI o) {
}
if (!annotations.isEmpty()) {
OWLAxiom ax = consumer.getLastAddedAxiom();
consumer.removeAxiom(verifyNotNull(ax, "no axiom added yet by the consumer")
.getAxiomWithoutAnnotations());
// Only remove unannotated axiom if it received annotations
if (!verifyNotNull(ax, "no axiom added yet by the consumer").getAnnotations().isEmpty()) {
consumer.removeAxiom(ax.getAxiomWithoutAnnotations());
}
}
consume(s, p, o);
}
Expand Down