Skip to content

Commit

Permalink
Fix violations in declarations not reported #1094 #1046
Browse files Browse the repository at this point in the history
  • Loading branch information
ignazio1977 committed Feb 28, 2023
1 parent 18bad20 commit b683b03
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.semanticweb.owlapi.model.OWLDatatype;
import org.semanticweb.owlapi.model.OWLDatatypeDefinitionAxiom;
import org.semanticweb.owlapi.model.OWLDatatypeRestriction;
import org.semanticweb.owlapi.model.OWLDeclarationAxiom;
import org.semanticweb.owlapi.model.OWLDifferentIndividualsAxiom;
import org.semanticweb.owlapi.model.OWLDisjointClassesAxiom;
import org.semanticweb.owlapi.model.OWLDisjointDataPropertiesAxiom;
Expand Down Expand Up @@ -105,6 +106,11 @@ public void visit(IRI iri) {
relativeIRI(iri);
}

@Override
public void visit(OWLDeclarationAxiom declaration) {
declaration.getEntity().accept(this);
}

@Override
public void visit(OWLDatatypeDefinitionAxiom axiom) {
// The datatype MUST be declared
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ private static String[] strings(String name) {
public static final String wrong =
"rdf:datatype=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral\"";

public static final String manShortOnto = "Prefix: : <urn:shortened#>\nOntology: Role1\nClass: Role";
public static final String manShortOnto =
"Prefix: : <urn:shortened#>\nOntology: Role1\nClass: Role";
public static final String NO_INPUT =
"ontology: uberon\n" + "[Term]\nid: X:1\nname: x1\nrelationship: part_of X:2\n\n"
+ "[Typedef]\nid: part_of\nxref: BFO:0000050";
Expand Down Expand Up @@ -688,6 +689,7 @@ private static String[] strings(String name) {
public static final String ontology = string("ontology.txt");
public static final String galenFragment = string("galenFragment.txt");
public static final String violation = string("violation.txt");
public static final String violationDeclaration = string("violationDeclaration.txt");
public static final String[] profileFullTestCases = strings("profileFullTestCases.txt");
public static final String[] profileDLTestCases = strings("profileDLTestCases.txt");
public static final String[] profileAllTestCases = strings("profileAllTestCases.txt");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ void shouldFindViolation() {
assertTrue(violation instanceof UseOfReservedVocabularyForAnnotationPropertyIRI);
}

@Test
void shouldFindViolationOnDeclaration() {
OWLOntology o = loadFrom(TestFiles.violationDeclaration, new RDFXMLDocumentFormat());
OWL2DLProfile p = new OWL2DLProfile();
OWLProfileReport checkOntology = p.checkOntology(o);
assertEquals(1, checkOntology.getViolations().size());
OWLProfileViolation violation = checkOntology.getViolations().get(0);
assertTrue(violation instanceof UseOfReservedVocabularyForAnnotationPropertyIRI);
}

@Test
void testGenIdGalenFragment() {
OWLOntology o = loadFrom(TestFiles.galenFragment, new RDFXMLDocumentFormat());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.semanticweb.owlapi.api.test.baseclasses.TestBase;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLAnnotationProperty;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLDataFactory;
Expand Down Expand Up @@ -76,6 +78,8 @@
import org.semanticweb.owlapi.profiles.violations.UseOfObjectPropertyInverse;
import org.semanticweb.owlapi.profiles.violations.UseOfPropertyInChainCausesCycle;
import org.semanticweb.owlapi.profiles.violations.UseOfReservedVocabularyForAnnotationPropertyIRI;
import org.semanticweb.owlapi.profiles.violations.UseOfReservedVocabularyForClassIRI;
import org.semanticweb.owlapi.profiles.violations.UseOfReservedVocabularyForDataPropertyIRI;
import org.semanticweb.owlapi.profiles.violations.UseOfReservedVocabularyForIndividualIRI;
import org.semanticweb.owlapi.profiles.violations.UseOfReservedVocabularyForObjectPropertyIRI;
import org.semanticweb.owlapi.profiles.violations.UseOfReservedVocabularyForOntologyIRI;
Expand Down Expand Up @@ -141,7 +145,13 @@ protected void checkInCollection(List<OWLProfileViolation> violations, Class<?>[

protected void runAssert(OWLProfile profile, Class<?>... expectedViolations) {
List<OWLProfileViolation> violations = profile.checkOntology(o).getViolations();
assertEquals(expectedViolations.length, violations.size(), violations.toString());
String types = violations.stream().map(violation -> violation.getClass().getSimpleName())
.collect(Collectors.joining(", "));
if (expectedViolations.length != violations.size()) {
System.out.println(types);
}
assertEquals(expectedViolations.length, violations.size(),
types + " " + violations.toString());
checkInCollection(violations, expectedViolations);
for (OWLProfileViolation violation : violations) {
o.getOWLOntologyManager().applyChanges(violation.repair());
Expand All @@ -162,7 +172,9 @@ public Optional<String> doDefault(OWLProfileViolation viol) {
void shouldCreateViolationForOWLDatatypeInOWL2DLProfile() {
declare(UNKNOWNFAKEDATATYPE, FAKEDATATYPE, Class(FAKEDATATYPE.getIRI()), DATAP);
add(DataPropertyRange(DATAP, FAKEUNDECLAREDDATATYPE));
runAssert(Profiles.OWL2_DL, UseOfUndeclaredDatatype.class);
runAssert(Profiles.OWL2_DL, UseOfUndeclaredDatatype.class,
DatatypeIRIAlsoUsedAsClassIRI.class, UseOfUnknownDatatype.class,
DatatypeIRIAlsoUsedAsClassIRI.class);
}

@Test
Expand All @@ -187,7 +199,7 @@ void shouldCreateViolationForOWLDatatypeDefinitionAxiomInOWL2DLProfileCycles() {
CycleInDatatypeDefinition.class, UseOfBuiltInDatatypeInDatatypeDefinition.class,
UseOfBuiltInDatatypeInDatatypeDefinition.class,
UseOfBuiltInDatatypeInDatatypeDefinition.class, UseOfUnknownDatatype.class,
UseOfUnknownDatatype.class);
UseOfUnknownDatatype.class, UseOfUnknownDatatype.class);
}

@Test
Expand All @@ -196,13 +208,18 @@ void shouldCreateViolationForOWLObjectPropertyInOWL2DLProfile() {
declare(ObjectProperty(iri), DataProperty(iri), AnnotationProperty(iri));
add(SubObjectPropertyOf(OPFAKE, ObjectProperty(iri)));
runAssert(Profiles.OWL2_DL, UseOfReservedVocabularyForObjectPropertyIRI.class,
UseOfUndeclaredObjectProperty.class, IllegalPunning.class, IllegalPunning.class);
UseOfReservedVocabularyForObjectPropertyIRI.class,
UseOfReservedVocabularyForDataPropertyIRI.class,
UseOfReservedVocabularyForAnnotationPropertyIRI.class,
UseOfUndeclaredObjectProperty.class, IllegalPunning.class, IllegalPunning.class,
IllegalPunning.class, IllegalPunning.class, IllegalPunning.class, IllegalPunning.class,
IllegalPunning.class, IllegalPunning.class);
}

@Test
void shouldCreateViolationForOWLDataPropertyInOWL2DLProfile1() {
declare(DataProperty(iri(START, "fail")));
runAssert(Profiles.OWL2_DL);
runAssert(Profiles.OWL2_DL, UseOfReservedVocabularyForDataPropertyIRI.class);
}

@Test
Expand All @@ -214,13 +231,13 @@ void shouldCreateViolationForOWLDataPropertyInOWL2DLProfile2() {
@Test
void shouldCreateViolationForOWLDataPropertyInOWL2DLProfile3() {
declare(DATAP, AnnotationProperty(DATAP.getIRI()));
runAssert(Profiles.OWL2_DL);
runAssert(Profiles.OWL2_DL, IllegalPunning.class, IllegalPunning.class);
}

@Test
void shouldCreateViolationForOWLDataPropertyInOWL2DLProfile4() {
declare(DATAP, ObjectProperty(DATAP.getIRI()));
runAssert(Profiles.OWL2_DL);
runAssert(Profiles.OWL2_DL, IllegalPunning.class, IllegalPunning.class);
}

@Test
Expand All @@ -230,7 +247,21 @@ void shouldCreateViolationForOWLAnnotationPropertyInOWL2DLProfile() {
add(SubAnnotationPropertyOf(AnnotationProperty(iri(URN_TEST, "t")),
AnnotationProperty(iri)));
runAssert(Profiles.OWL2_DL, UseOfReservedVocabularyForAnnotationPropertyIRI.class,
UseOfUndeclaredAnnotationProperty.class, IllegalPunning.class, IllegalPunning.class);
UseOfReservedVocabularyForAnnotationPropertyIRI.class,
UseOfReservedVocabularyForDataPropertyIRI.class,
UseOfReservedVocabularyForObjectPropertyIRI.class,
UseOfUndeclaredAnnotationProperty.class, IllegalPunning.class, IllegalPunning.class,
IllegalPunning.class, IllegalPunning.class, IllegalPunning.class, IllegalPunning.class,
IllegalPunning.class, IllegalPunning.class);
}

@Test
void shouldCreateViolationForOWLAnnotationPropertyPunnedWithObjectPropertyDeclarationInOWL2DLProfile() {
OWLAnnotationProperty a = AnnotationProperty(iriTest);
declare(ObjectProperty(iriTest), a);
add(AnnotationAssertion(a, I.getIRI(), lit1));
runAssert(Profiles.OWL2_DL, IllegalPunning.class, IllegalPunning.class,
IllegalPunning.class);
}

@Test
Expand All @@ -244,8 +275,8 @@ void shouldCreateViolationForOWLOntologyInOWL2DLProfile() {
void shouldCreateViolationForOWLClassInOWL2DLProfile() {
declare(Class(iri(START, TEST)), FAKEDATATYPE);
add(ClassAssertion(Class(FAKEDATATYPE.getIRI()), AnonymousIndividual()));
runAssert(Profiles.OWL2_DL, UseOfUndeclaredClass.class,
DatatypeIRIAlsoUsedAsClassIRI.class);
runAssert(Profiles.OWL2_DL, UseOfUndeclaredClass.class, DatatypeIRIAlsoUsedAsClassIRI.class,
UseOfReservedVocabularyForClassIRI.class, DatatypeIRIAlsoUsedAsClassIRI.class);
}

@Test
Expand Down Expand Up @@ -498,7 +529,7 @@ void shouldCreateViolationForOWLDatatypeDefinitionAxiomInOWL2Profile() {
@Test
void shouldCreateViolationForOWLDatatypeInOWL2ELProfile() {
declare(Boolean());
runAssert(Profiles.OWL2_EL);
runAssert(Profiles.OWL2_EL, UseOfIllegalDataRange.class);
}

@Test
Expand Down Expand Up @@ -710,7 +741,7 @@ void shouldCreateViolationForOWLSubPropertyChainOfAxiomInOWL2ELProfile() {
@Test
void shouldCreateViolationForOWLDatatypeInOWL2QLProfile() {
declare(FAKEDATATYPE);
runAssert(Profiles.OWL2_QL);
runAssert(Profiles.OWL2_QL, UseOfIllegalDataRange.class);
}

@Test
Expand Down Expand Up @@ -1001,7 +1032,7 @@ void shouldCreateViolationForOWLDataOneOfInOWL2RLProfile() {
@Test
void shouldCreateViolationForOWLDatatypeInOWL2RLProfile() {
declare(Datatype(iri(URN_TEST, TEST)));
runAssert(Profiles.OWL2_RL);
runAssert(Profiles.OWL2_RL, UseOfIllegalDataRange.class);
}

@Test
Expand All @@ -1023,6 +1054,7 @@ void shouldCreateViolationForOWLDatatypeDefinitionAxiomInOWL2RLProfile() {
OWLDatatype datatype = Datatype(iri(URN_TEST, "datatype"));
declare(datatype);
add(DatatypeDefinition(datatype, Boolean()));
runAssert(Profiles.OWL2_RL, UseOfIllegalAxiom.class, UseOfIllegalDataRange.class);
runAssert(Profiles.OWL2_RL, UseOfIllegalAxiom.class, UseOfIllegalDataRange.class,
UseOfIllegalDataRange.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:owl="http://www.w3.org/2002/07/owl#">
<owl:Ontology rdf:about=""/>
<owl:AnnotationProperty rdf:about="http://www.w3.org/1999/02/22-rdf-syntax-ns#type"/>
</rdf:RDF>

0 comments on commit b683b03

Please sign in to comment.