package tudelft.healthpsychology.traumaontologies; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.io.File; import java.io.IOException; import java.util.Collection; import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.stream.Collectors; import org.junit.Before; import org.junit.Test; import org.semanticweb.owlapi.apibinding.OWLManager; import org.semanticweb.owlapi.model.AxiomType; import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom; import org.semanticweb.owlapi.model.OWLAxiom; import org.semanticweb.owlapi.model.OWLClass; import org.semanticweb.owlapi.model.OWLDataFactory; import org.semanticweb.owlapi.model.OWLDataProperty; import org.semanticweb.owlapi.model.OWLDataPropertyAssertionAxiom; import org.semanticweb.owlapi.model.OWLDataPropertyDomainAxiom; import org.semanticweb.owlapi.model.OWLDataPropertyRangeAxiom; import org.semanticweb.owlapi.model.OWLException; import org.semanticweb.owlapi.model.OWLLiteral; import org.semanticweb.owlapi.model.OWLNamedIndividual; import org.semanticweb.owlapi.model.OWLObjectProperty; import org.semanticweb.owlapi.model.OWLObjectPropertyDomainAxiom; import org.semanticweb.owlapi.model.OWLOntology; import org.semanticweb.owlapi.model.OWLOntologyCreationException; import org.semanticweb.owlapi.model.OWLOntologyManager; import org.semanticweb.owlapi.model.OWLOntologyStorageException; import org.semanticweb.owlapi.model.OWLSubClassOfAxiom; import org.semanticweb.owlapi.reasoner.InferenceType; import org.semanticweb.owlapi.reasoner.NodeSet; import org.semanticweb.owlapi.reasoner.OWLReasoner; import org.semanticweb.owlapi.reasoner.OWLReasonerConfiguration; import org.semanticweb.owlapi.reasoner.OWLReasonerFactory; import org.semanticweb.owlapi.reasoner.SimpleConfiguration; import org.semanticweb.owlapi.reasoner.structural.StructuralReasonerFactory; import tudelft.utilities.translator.BasicCsvTranslator; /** * Tests basically to check what OWL is really doing and which functions are * actually implemented and returning useful data. * */ public class OwlTest { private final String base = "http://www.owl-ontologies.com/Ontology1444730995.owl"; private OWLOntology ontology; private OWLDataFactory factory; final OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); private BasicCsvTranslator translator; // https://github.com/apache/jena/blob/master/jena-arq/src-examples/arq/examples/riot/ExRIOT1_ReadModel.java @Before public void before() throws OWLOntologyCreationException, IOException { ontology = manager.loadOntologyFromOntologyDocument( new File("src/main/resources/Child Sexual Abuse.owl")); factory = manager.getOWLDataFactory(); translator = new BasicCsvTranslator( getClass().getResourceAsStream("/NL.csv")); } @Test public void smokeTest() { } @Test public void addAnnotationTest() throws OWLOntologyStorageException { OWLClass stringDocuClass = factory.getOWLClass( IRI.create("http://mysite.com/my_ontology.owl#StringDocu")); System.out.println("got " + stringDocuClass); // add a annotation OWLAnnotationAssertionAxiom axiom = factory .getOWLAnnotationAssertionAxiom(factory.getRDFSLabel(), IRI.create("http://barf"), factory.getOWLLiteral(true)); ontology.add(axiom); // ontology.saveOntology(); } @Test public void addIndividual() throws OWLOntologyStorageException { // add a annotation OWLDataPropertyAssertionAxiom axiom = factory .getOWLDataPropertyAssertionAxiom( factory.getOWLDataProperty(base + "hasName"), factory.getOWLNamedIndividual(base + "PersonJohn"), factory.getOWLLiteral("John")); ontology.add(axiom); // ontology.saveOntology(); } @Test public void testGetApparaatStatusProperties() { OWLClass apparaatStatus = factory .getOWLClass(IRI.create(base + "#Apparaat")); ontology.axioms(AxiomType.DATA_PROPERTY_DOMAIN).forEach(dp -> { if (dp.getDomain().equals(apparaatStatus)) { dp.dataPropertiesInSignature().forEach(odp -> { System.out.println( "found property " + odp.getIRI().getShortForm() + " with signature " + odp.getSignature()); }); } }); } @Test public void testAssertedSuperclasses() throws OWLException { // keukenapparaat subclassof apparaat OWLClass quokkaCls = factory .getOWLClass(IRI.create(base + "#Keukenapparaat")); Collection classes = ontology .getSubClassAxiomsForSubClass(quokkaCls); // for each superclass there will be a corresponding axiom // the ontology indexes axioms in a variety of ways assertEquals(1, classes.size()); System.out .println(classes.iterator().next().getSuperClass().toString()); assertTrue(classes.iterator().next().getSuperClass().toString() .endsWith("#Apparaat>")); } @Test public void testGetAllClasses() { Set classes = ontology.classesInSignature() .collect(Collectors.toSet()); System.out.println("Classes"); System.out.println("--------------------------------"); for (OWLClass cls : classes) { System.out.println("+: " + cls.getIRI().getShortForm()); System.out.println("\tObject Property Domain"); ontology.axioms(AxiomType.OBJECT_PROPERTY_DOMAIN).forEach(op -> { if (op.getDomain().equals(cls)) { System.out.println("\tComments:" + op.getAnnotations(factory.getRDFSComment())); op.objectPropertiesInSignature().forEach(oop -> { System.out.println( "\t\t-: " + oop.getIRI().getShortForm()); }); } }); System.out.println("\tData Property Domain"); ontology.axioms(AxiomType.DATA_PROPERTY_DOMAIN).forEach(dp -> { if (dp.getDomain().equals(cls)) { dp.dataPropertiesInSignature().forEach(odp -> { System.out .println("\t\t-: " + odp.getIRI().getShortForm() + " " + odp.getSignature()); }); } System.out.println(dp); }); } System.out.println("\tAnnotation Property Range"); ontology.axioms(AxiomType.ANNOTATION_PROPERTY_RANGE).forEach(dp -> { System.out.println("\t\t" + dp); }); System.out.println("\tAnnotation Assertion"); ontology.axioms(AxiomType.ANNOTATION_ASSERTION).forEach(dp -> { System.out.println("\t\t" + dp); }); System.out.println("\tAnnotation Property Range"); ontology.axioms(AxiomType.ANNOTATION_PROPERTY_RANGE).forEach(dp -> { System.out.println("\t\t" + dp); }); System.out.println("\tRDFS Comments(subset of annotations)"); ontology.axioms(AxiomType.ANNOTATION_ASSERTION).forEach(aa -> { if (aa.getSubject().equals(factory.getRDFSComment())) { System.out.println(aa); } }); System.out.println("\tSubclass of"); ontology.axioms(AxiomType.SUBCLASS_OF).forEach(aa -> { System.out.println("\t\t" + aa); }); System.out.println("-------------"); } @Test public void testGetIndividuals() { System.out.println("Individuals"); System.out.println("--------------------------------"); ontology.individualsInSignature().forEach( indiv -> System.out.println(indiv.getIRI().getShortForm())); } @Test public void getAllSubclasses() { ontology.axioms(AxiomType.SUBCLASS_OF).forEach(dp -> { System.out.println("\t\t" + dp); }); } @Test public void getAllSuperClasses() { List superClasses = getSuperclasses("Fornuis"); // fornuis ISA keukenapparaat ISA apparaat ISA object ISA Thing System.out.println("superclases of fornuis:" + superClasses); // fornuis ISA keukenapparaat ISA apparaat ISA object ISA Thing assertEquals(5, superClasses.size()); } List getSuperclasses(String classname) { IRI iri = IRI .create("http://www.owl-ontologies.com/Ontology1444730995.owl#" + classname); OWLClass fornuis = manager.getOWLDataFactory().getOWLClass(iri); // use reasoner, to do class hierarchy the inference stuff OWLReasonerFactory reasonerFactory = new StructuralReasonerFactory(); OWLReasonerConfiguration config = new SimpleConfiguration(); OWLReasoner reasoner = reasonerFactory.createReasoner(ontology, config); reasoner.precomputeInferences(InferenceType.CLASS_HIERARCHY); assertTrue(reasoner.isConsistent()); NodeSet superClasses = reasoner.getSuperClasses(fornuis, false); LinkedList all = new LinkedList( superClasses.getFlattened()); all.add(fornuis); return all; } @Test public void getAllPropertiesOfFornuis() { System.out.println( "Found the following properties for fornuis and superclasses"); for (OWLClass clas : getSuperclasses("Fornuis")) { // System.out.println(clas); for (OWLDataPropertyDomainAxiom dp : ontology .getAxioms(AxiomType.DATA_PROPERTY_DOMAIN)) { if (dp.getDomain().equals(clas)) { System.out.println(dp); dp.dataPropertiesInSignature().forEach(odp -> { System.out.println("odp=" + odp); System.out.println(odp.getIRI() + "\n comment=" + getCommentAnnotation(odp)); }); } } } } @Test public void getAllPropertiesOfBed() { System.out.println( "Found the following properties for bed and superclasses"); for (OWLClass clas : getSuperclasses("Bed")) { System.out.println("superclass: " + clas); for (OWLDataPropertyDomainAxiom dp : ontology .getAxioms(AxiomType.DATA_PROPERTY_DOMAIN)) { if (dp.getDomain().equals(clas)) { System.out.println("data property " + dp); System.out.println("\n comments=" + getCommentAnnotation( dp.dataPropertiesInSignature().findFirst().get())); // why do we need a cast here? And, isIri returns FALSE??? IRI property = (IRI) dp.getProperty().components() .findFirst().get(); List varf = ontology .axioms(AxiomType.DATA_PROPERTY_RANGE) .filter(p -> property.equals(p.getProperty() .components().findFirst().get())) .collect(Collectors.toList()); System.out.println(" range:" + varf.get(0).getRange()); } } } } @Test public void rangeOfAllDataPropRangeObjects() { for (OWLDataPropertyRangeAxiom range : ontology .getAxioms(AxiomType.DATA_PROPERTY_RANGE)) { System.out.println("property2=" + range.getProperty().components().findFirst().get()); System.out.println("range=" + range.getRange()); } } @Test public void rangeOfLocatieMeubel() { IRI iri = IRI.create(getBaseName() + "#Locatie_meubel"); ontology.axioms(AxiomType.DATA_PROPERTY_RANGE) .filter(range -> iri.equals( range.getProperty().components().findFirst().get())) .forEach(range -> System.out.println(range.getRange())); } private String getCommentAnnotation(OWLDataProperty odp) { return ontology.annotationAssertionAxioms(odp.getIRI()).findFirst() .get().getAnnotation().annotationValue().toString(); } @Test public void getAllPropertiesOfAllClasses() { Set classes; Set prop; Set dataProp; Set individuals; classes = ontology.getClassesInSignature(); prop = ontology.getObjectPropertiesInSignature(); dataProp = ontology.getDataPropertiesInSignature(); individuals = ontology.getIndividualsInSignature(); System.out.println("Classes"); System.out.println("--------------------------------"); for (OWLClass cls : classes) { System.out.println("+: " + cls.getIRI().getShortForm()); System.out.println(" \tObject Property Domain"); for (OWLObjectPropertyDomainAxiom op : ontology .getAxioms(AxiomType.OBJECT_PROPERTY_DOMAIN)) { if (op.getDomain().equals(cls)) { for (OWLObjectProperty oop : op .getObjectPropertiesInSignature()) { System.out.println( "\t\t +: " + oop.getIRI().getShortForm()); } } } System.out.println(" \tData Property Domain"); for (OWLDataPropertyDomainAxiom dp : ontology .getAxioms(AxiomType.DATA_PROPERTY_DOMAIN)) { if (dp.getDomain().equals(cls)) { for (OWLDataProperty odp : dp .getDataPropertiesInSignature()) { System.out.println( "\t\t +: " + odp.getIRI().getShortForm()); } } } } } @Test public void getSubclassesOfApparaatNotWorking() { IRI iri = IRI.create( " axioms = ontology.axioms(AxiomType.SUBCLASS_OF) .filter(aa -> aa.getSuperClass().equals(apparaat)) .collect(Collectors.toList()); System.out.println(axioms); assertFalse(axioms.isEmpty()); } @Test public void getInstancesOfShortApparaat() { IRI iri = IRI.create(getBaseName() + "#Apparaat"); OWLClass apparaat = manager.getOWLDataFactory().getOWLClass(iri); System.out.println("Subclasses of " + apparaat); boolean hasSubclasses = false; List axioms = ontology.axioms(AxiomType.SUBCLASS_OF) .filter(aa -> aa.getSuperClass().equals(apparaat)) .collect(Collectors.toList()); System.out.println(axioms); assertFalse(axioms.isEmpty()); } private String getBaseName() { return ontology.getOntologyID().getOntologyIRI().get().toString(); } @Test public void getAllFormulasTest() { System.out.println("Total axioms:" + ontology.getAxiomCount()); System.out.println( "axioms:" + ontology.axioms().collect(Collectors.toList())); assertTrue(ontology.getAxiomCount() > 100); } @Test public void addAxiomTest() { int naxioms = ontology.getAxiomCount(); OWLDataFactory df = manager.getOWLDataFactory(); IRI geneSetIRI = IRI.create("prefix:fdsfds"); OWLLiteral literal = df.getOWLLiteral(12); OWLAxiom ax = df.getOWLAnnotationAssertionAxiom(df.getRDFSLabel(), geneSetIRI, literal); ontology.add(ax); assertEquals(naxioms + 1, ontology.getAxiomCount()); } @Test public void toStringIdTest() { OWLClass apparaatStatus = factory .getOWLClass(IRI.create(base + "#Apparaat")); System.out.println(apparaatStatus.toStringID()); System.out.println(apparaatStatus.getIRI().getShortForm()); } }