package tudelft.healthpsychology.traumaontologies; import static org.junit.Assert.assertTrue; import java.io.File; import java.io.IOException; import java.util.List; import java.util.stream.Collectors; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameter; import org.junit.runners.Parameterized.Parameters; import org.semanticweb.owlapi.apibinding.OWLManager; import org.semanticweb.owlapi.model.AxiomType; import org.semanticweb.owlapi.model.HasIRI; import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom; import org.semanticweb.owlapi.model.OWLClass; import org.semanticweb.owlapi.model.OWLDataFactory; import org.semanticweb.owlapi.model.OWLDataProperty; import org.semanticweb.owlapi.model.OWLOntology; import org.semanticweb.owlapi.model.OWLOntologyCreationException; import org.semanticweb.owlapi.model.OWLOntologyManager; import tudelft.utilities.translator.BasicCsvTranslator; import tudelft.utilities.translator.TranslationFailedException; import uk.ac.manchester.cs.owl.owlapi.OWLLiteralImplString; /** * Check if the owl files are containing good data. */ @RunWith(Parameterized.class) public class OwlFilesTest { private final static String[] owlFiles = new String[] { "Child Sexual Abuse.owl", "War.owl", "War Afghanistan.owl", "War Bosnia.owl", "War Libya.owl" }; @Parameter public String owlFile; private OWLOntology ontology; private OWLDataFactory factory; final OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); private BasicCsvTranslator translator; @Parameters public static Object[] data() { return owlFiles; } @Before public void before() throws OWLOntologyCreationException, IOException { ontology = manager.loadOntologyFromOntologyDocument( new File("src/main/resources/" + owlFile)); factory = manager.getOWLDataFactory(); translator = new BasicCsvTranslator( getClass().getResourceAsStream("/NL.csv")); } /** * Check that the top node classes are present: Object, Person, * "Geografische_locatie" and "Type_Locatie". */ @Test public void testTopClasses() { assertTrue(ontology.containsClassInSignature(getIri("Persoon"))); assertTrue(ontology.containsClassInSignature(getIri("Object"))); assertTrue(ontology .containsClassInSignature(getIri("Geografische_locatie"))); assertTrue(ontology.containsClassInSignature(getIri("Type_Locatie"))); } private IRI getIri(String name) { return IRI.create(getBaseName() + "#" + name); } private String getBaseName() { return ontology.getOntologyID().getOntologyIRI().get().toString(); } /** * All nodes, fields etc must be translate-able. This is more a Translation * test but this test needs to be done for all owl files therefore this test * is here (not in CsvTranslationFilesTest) * * @throws TranslationFailedException */ @Test public void checkAllAnnotationAssertionsTranslateProperly() throws TranslationFailedException { for (OWLAnnotationAssertionAxiom dp : ontology .getAxioms(AxiomType.ANNOTATION_ASSERTION)) { String text = ((OWLLiteralImplString) dp.getValue()).getLiteral(); System.out.println(text); try { translator.forward(text); } catch (TranslationFailedException e) { throw new IllegalStateException( "There is a problem with the ontology in axiom " + dp, e); } } } @Test public void testGetAllClasses() { System.out.println("\tAnnotation Assertion"); ontology.axioms(AxiomType.ANNOTATION_ASSERTION).forEach(dp -> { System.out.println(dp.getSubject() + " " + dp.annotationValue()); }); } @Test public void testmax1Annotation() { for (OWLClass clas : ontology.classesInSignature() .collect(Collectors.toSet())) { max1property(clas.getIRI()); } } @Test public void testmax1AnnotationInProperties() { for (OWLDataProperty clas : ontology.dataPropertiesInSignature() .collect(Collectors.toSet())) { max1property(clas.getIRI()); } } /** * Check that there is at most 1 annotation property for the given IRI. * * @param iri the object IRI. Can be a class or functionalproperty iri. */ private void max1property(IRI iri) { List annotations = ontology .axioms(AxiomType.ANNOTATION_ASSERTION) .filter(dp -> dp.getSubject().equals(iri)) .collect(Collectors.toList()); System.out.println(annotations); assertTrue("Object " + iri + " has multiple annotations but can have only 1:" + annotations, annotations.size() <= 1); } @Test public void testPropertyLabelsTranslate() throws TranslationFailedException { for (OWLDataProperty clas : ontology.dataPropertiesInSignature() .collect(Collectors.toSet())) { checkIriFragmentTranslates(clas); } } @Test public void testClassIDssTranslate() throws TranslationFailedException { for (OWLClass clas : ontology.classesInSignature() .collect(Collectors.toSet())) { checkIriFragmentTranslates(clas); } } private void checkIriFragmentTranslates(HasIRI clas) { try { translator.forward(clas.getIRI().getFragment()); } catch (TranslationFailedException e) { throw new IllegalStateException("There is a problem with " + clas, e); } } }