/** * Test that two {@code OWLClass}es that are equal have a same hashcode, * because the OWLGraphEdge bug get me paranoid. */ @Test public void testOWLClassHashCode() { OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); OWLDataFactory factory = manager.getOWLDataFactory(); IRI iri = IRI.create("http://www.foo.org/#A"); OWLClass class1 = factory.getOWLClass(iri); //get the class by another way, even if if I suspect the two references //will point to the same object PrefixManager pm = new DefaultPrefixManager("http://www.foo.org/#"); OWLClass class2 = factory.getOWLClass(":A", pm); assertTrue("The two references point to different OWLClass objects", class1 == class2); //then of course the hashcodes will be the same... assertTrue("Two OWLClasses are equal but have different hashcode", class1.equals(class2) && class1.hashCode() == class2.hashCode()); }
@Bean @Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.INTERFACES) public OWLMutableOntology owlOntology(PrefixManager prefixManager) throws OWLOntologyCreationException { if (prefixManager.getDefaultPrefix() == null) { throw new IllegalStateException("Default ontology prefix must not be null."); } OWLOntologyManager ontologyManager = OWLManager.createOWLOntologyManager(); // Cast to a mutable ontology to pass OWLApi's strange checks return (OWLMutableOntology) ontologyManager.createOntology(IRI.create(prefixManager.getDefaultPrefix())); }
/** * Creates the owl axioms from concept version bi. * * @param setOfAxioms the set of axioms * @param factory the factory * @param pm the pm * @param conceptVersionBI the concept version bi * @return the OWL class * @throws Exception the exception */ private OWLClass createOWLAxiomsFromConceptVersionBI( Set<OWLAxiom> setOfAxioms, OWLDataFactory factory, PrefixManager pm, ConceptVersionBI conceptVersionBI) throws Exception { // Declaration OWLClass owlClass = factory.getOWLClass(":" + getSnomedConceptID(conceptVersionBI), pm); OWLDeclarationAxiom declarationAxiom = factory.getOWLDeclarationAxiom(owlClass); setOfAxioms.add(declarationAxiom); // Create annotation type axiom // Fully specified description OWLAnnotation labelAnnotation = factory.getOWLAnnotation(factory.getRDFSLabel(), factory.getOWLLiteral( conceptVersionBI.getFullySpecifiedDescription().getText(), conceptVersionBI.getFullySpecifiedDescription().getLang())); OWLAxiom labelAnnotationAxiom = factory.getOWLAnnotationAssertionAxiom(owlClass.getIRI(), labelAnnotation); setOfAxioms.add(labelAnnotationAxiom); // Preferred name OWLAnnotation preferredTerm = factory.getOWLAnnotation(factory.getOWLAnnotationProperty(IRI .create(snomedEnUsPreferredAnnotation)), factory.getOWLLiteral( conceptVersionBI.getPreferredDescription().getText(), conceptVersionBI.getPreferredDescription().getLang())); OWLAxiom preferredTermAx = factory .getOWLAnnotationAssertionAxiom(owlClass.getIRI(), preferredTerm); setOfAxioms.add(preferredTermAx); return owlClass; }
/** * Print out RelEx relations. All relations shown * in a binary form. * * Example: * _subj(throw, John) * _obj(throw, ball) * tense(throw, past) * definite-FLAG(ball, T) * noun_number(ball, singular) */ public void printRelations(ParsedSentence parse, String sentence, int sentence_id, String ontologyname) { try { sent = sentence; //Add the sentence to Sentence Class this.sentence_id = sentence_id; sentenceInd = factory.getOWLNamedIndividual(IRI.create(ontologyURI + "#" + "sentence_" + sentence_id)); //OWLAnnotationProperty p = new OWLAnnotationPropertyImpl(IRI.create(sentence)); //OWLAnnotation label = factory.getOWLAnnotation(sentence); OWLOntologyFormat ontologyFormat = manager.getOntologyFormat(ontology); OWLAnnotation label = (OWLAnnotation) factory.getOWLAnnotationProperty(sentence, (PrefixManager) ontologyFormat); OWLClassAssertionAxiom sentClass = factory.getOWLClassAssertionAxiom(this.sentence,sentenceInd); OWLAnnotationAssertionAxiom labelClass = factory.getOWLAnnotationAssertionAxiom((OWLAnnotationSubject) sentClass, label); manager.applyChange(new AddAxiom(ontology, sentClass)); manager.applyChange(new AddAxiom(ontology, labelClass)); printRelations(parse, null); } catch (OWLOntologyChangeException ex) { Logger.getLogger(OWLView.class.getName()).log(Level.SEVERE, null, ex); } }
/** * @param pm * the prefix manager to copy * @param c * comparator to sort prefixes * @param defaultPrefix * default prefix */ public DefaultPrefixManager(@Nullable PrefixManager pm, @Nullable StringComparator c, @Nullable String defaultPrefix) { comparator = c == null ? new StringLengthComparator() : c; prefix2NamespaceMap = new TreeMap<>(comparator); setupDefaultPrefixes(); if (pm != null) { copyPrefixesFrom(pm); } if (defaultPrefix != null) { setDefaultPrefix(defaultPrefix); } }
@Autowired public ClassPool(OWLDataFactory factory, PrefixManager prefixManager) { super(MAX_REUSES); this.factory = factory; this.prefixManager = prefixManager; }
@Autowired public PropertyPool(OWLDataFactory factory, PrefixManager prefixManager) { this.factory = factory; this.prefixManager = prefixManager; }
@Bean @Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.INTERFACES) public PrefixManager prefixManager(RequestInformation requestInformation) { return new DefaultPrefixManager(null, null, requestInformation.getOntologyIri()); }
/** * @return the protegePrefixmanager */ public PrefixManager getProtegePrefixmanager() { return protegePrefixmanager; }
/** * @param protegePrefixmanager the protegePrefixmanager to set */ public void setProtegePrefixmanager(PrefixManager protegePrefixmanager) { this.protegePrefixmanager = protegePrefixmanager; }
public static void main(String[] args) throws OWLOntologyCreationException { OWLOntologyManager man = OWLManager.createOWLOntologyManager(); OWLDataFactory dataFactory = man.getOWLDataFactory(); // Load your ontology. OWLOntology ont = man.loadOntologyFromOntologyDocument(new File( "c:/ontologies/ontology.owl")); // Create an ELK reasoner. OWLReasonerFactory reasonerFactory = new ElkReasonerFactory(); OWLReasoner reasoner = reasonerFactory.createReasoner(ont); // Create your desired query class expression. In this example we // will query ObjectIntersectionOf(A ObjectSomeValuesFrom(R B)). PrefixManager pm = new DefaultPrefixManager("http://example.org/"); OWLClass A = dataFactory.getOWLClass(":A", pm); OWLObjectProperty R = dataFactory.getOWLObjectProperty(":R", pm); OWLClass B = dataFactory.getOWLClass(":B", pm); OWLClassExpression query = dataFactory.getOWLObjectIntersectionOf(A, dataFactory.getOWLObjectSomeValuesFrom(R, B)); // Create a fresh name for the query. OWLClass newName = dataFactory.getOWLClass(IRI.create("temp001")); // Make the query equivalent to the fresh class OWLAxiom definition = dataFactory.getOWLEquivalentClassesAxiom(newName, query); man.addAxiom(ont, definition); // Remember to either flush the reasoner after the ontology change // or create the reasoner in non-buffering mode. Note that querying // a reasoner after an ontology change triggers re-classification of // the whole ontology which might be costly. Therefore, if you plan // to query for multiple complex class expressions, it will be more // efficient to add the corresponding definitions to the ontology at // once before asking any queries to the reasoner. reasoner.flush(); // You can now retrieve subclasses, superclasses, and instances of // the query class by using its new name instead. reasoner.getSubClasses(newName, true); reasoner.getSuperClasses(newName, true); reasoner.getInstances(newName, false); // After you are done with the query, you should remove the definition man.removeAxiom(ont, definition); // You can now add new definitions for new queries in the same way // After you are done with all queries, do not forget to free the // resources occupied by the reasoner reasoner.dispose(); }
/** * Testing correctness of the reasoner with respect to ontology changes * */ @Test public void testNoChanges() throws Exception { OWLOntologyManager man = TestOWLManager.createOWLOntologyManager(); OWLDataFactory dataFactory = man.getOWLDataFactory(); // set up resolution of prefixes PrefixManager pm = new DefaultPrefixManager(); pm.setDefaultPrefix("http://www.example.com/main#"); pm.setPrefix("A:", "http://www.example.com/A#"); pm.setPrefix("B:", "http://www.example.com/B#"); // define query classes OWLClass mainX = dataFactory.getOWLClass(":X", pm); OWLClass mainY = dataFactory.getOWLClass(":Y", pm); OWLClass extA = dataFactory.getOWLClass("A:A", pm); OWLClass extB = dataFactory.getOWLClass("B:B", pm); OWLClass extC = dataFactory.getOWLClass("B:C", pm); // loading the root ontology OWLOntology root = loadOntology(man, "root.owl"); // Create an ELK reasoner. OWLReasonerFactory reasonerFactory = new ElkReasonerFactory(); OWLReasoner reasoner = reasonerFactory.createReasoner(root); try { // statistics about the root ontology assertEquals(root.getAxiomCount(), 3); // all three ontologies should be in the closure assertEquals(root.getImportsClosure().size(), 3); // all axioms from three ontologies should be in the closure assertEquals(getAxioms(root).size(), 6); // reasoner queries -- all subclasses are there assertTrue(reasoner.getSuperClasses(mainX, true).containsEntity( mainY)); assertTrue(reasoner.getSuperClasses(mainX, true).containsEntity( extA)); assertTrue(reasoner.getSuperClasses(mainY, true).containsEntity( extB)); assertTrue(reasoner.getSuperClasses(extA, true) .containsEntity(extB)); assertTrue(reasoner.getSuperClasses(extB, true) .containsEntity(extC)); } finally { reasoner.dispose(); } }
/** * Testing correctness of the reasoner with respect to ontology changes * * removing an axiom ":X is-a :Y" */ @Test public void testRemovingXY() throws Exception { OWLOntologyManager man = TestOWLManager.createOWLOntologyManager(); OWLDataFactory dataFactory = man.getOWLDataFactory(); // set up resolution of prefixes PrefixManager pm = new DefaultPrefixManager(); pm.setDefaultPrefix("http://www.example.com/main#"); pm.setPrefix("A:", "http://www.example.com/A#"); pm.setPrefix("B:", "http://www.example.com/B#"); // define query classes OWLClass mainX = dataFactory.getOWLClass(":X", pm); OWLClass mainY = dataFactory.getOWLClass(":Y", pm); OWLClass extA = dataFactory.getOWLClass("A:A", pm); OWLClass extB = dataFactory.getOWLClass("B:B", pm); OWLClass extC = dataFactory.getOWLClass("B:C", pm); // loading the root ontology OWLOntology root = loadOntology(man, "root.owl"); // Create an ELK reasoner. OWLReasonerFactory reasonerFactory = new ElkReasonerFactory(); OWLReasoner reasoner = reasonerFactory.createReasoner(root); try { // ************************************ // ** removing an axiom ":X is-a :Y" // ************************************ OWLAxiom axiom = dataFactory.getOWLSubClassOfAxiom(mainX, mainY); man.removeAxiom(root, axiom); reasoner.flush(); // the root ontology contains one fewer axioms assertEquals(root.getAxiomCount(), 2); // the number of ontologies in the import closure does not change assertEquals(root.getImportsClosure().size(), 3); // the total number of axioms reduces assertEquals(getAxioms(root).size(), 5); // reasoner queries -- first subsumption is gone assertFalse(reasoner.getSuperClasses(mainX, true).containsEntity( mainY)); assertTrue(reasoner.getSuperClasses(mainX, true).containsEntity( extA)); assertTrue(reasoner.getSuperClasses(mainY, true).containsEntity( extB)); assertTrue(reasoner.getSuperClasses(extA, true) .containsEntity(extB)); assertTrue(reasoner.getSuperClasses(extB, true) .containsEntity(extC)); } finally { reasoner.dispose(); } }
/** * Testing correctness of the reasoner with respect to ontology changes * <p> * trying to remove "A:A is-a B:B" * <p> * Because the removed axiom belongs to the imported ontology and * not main ontology, the remove does not make any effect. So, we * should end up with the ontology we have started with. * <p> * This test is ignored, because as of OWL API 4.1.3 the removal * of the axiom is broadcasted even though the axiom is not removed. */ @Test public void testRemovingAB() throws Exception { OWLOntologyManager man = TestOWLManager.createOWLOntologyManager(); OWLDataFactory dataFactory = man.getOWLDataFactory(); // set up resolution of prefixes PrefixManager pm = new DefaultPrefixManager(); pm.setDefaultPrefix("http://www.example.com/main#"); pm.setPrefix("A:", "http://www.example.com/A#"); pm.setPrefix("B:", "http://www.example.com/B#"); // define query classes OWLClass mainX = dataFactory.getOWLClass(":X", pm); OWLClass mainY = dataFactory.getOWLClass(":Y", pm); OWLClass extA = dataFactory.getOWLClass("A:A", pm); OWLClass extB = dataFactory.getOWLClass("B:B", pm); OWLClass extC = dataFactory.getOWLClass("B:C", pm); // loading the root ontology OWLOntology root = loadOntology(man, "root.owl"); // Create an ELK reasoner. OWLReasonerFactory reasonerFactory = new ElkReasonerFactory(); OWLReasoner reasoner = reasonerFactory.createReasoner(root); try { // ************************************ // ** trying to remove "A:A is-a B:B" // ************************************ OWLSubClassOfAxiom axiom = dataFactory.getOWLSubClassOfAxiom(extA, extB); man.removeAxiom(root, axiom); reasoner.flush(); // Because the removed axiom belongs to the imported ontology and // not main ontology, the remove does not make any effect. So, we // should end up with the ontology we have started with assertEquals(root.getAxiomCount(), 3); // all three ontologies should be in the closure assertEquals(root.getImportsClosure().size(), 3); // all axioms from three ontologies should be in the closure assertEquals(getAxioms(root).size(), 6); // reasoner queries -- all subsumptions are there assertTrue(reasoner.getSuperClasses(mainX, true).containsEntity( mainY)); assertTrue(reasoner.getSuperClasses(mainX, true).containsEntity( extA)); assertTrue(reasoner.getSuperClasses(mainY, true).containsEntity( extB)); assertTrue(reasoner.getSuperClasses(extA, true) .containsEntity(extB)); assertTrue(reasoner.getSuperClasses(extB, true) .containsEntity(extC)); } finally { reasoner.dispose(); } }
/** * Testing correctness of the reasoner with respect to ontology changes * <p> * removing the import declaration for </impA> */ @Test public void testRemovingImpA() throws Exception { OWLOntologyManager man = TestOWLManager.createOWLOntologyManager(); OWLDataFactory dataFactory = man.getOWLDataFactory(); // set up resolution of prefixes PrefixManager pm = new DefaultPrefixManager(); pm.setDefaultPrefix("http://www.example.com/main#"); pm.setPrefix("A:", "http://www.example.com/A#"); pm.setPrefix("B:", "http://www.example.com/B#"); // define query classes OWLClass mainX = dataFactory.getOWLClass(":X", pm); OWLClass mainY = dataFactory.getOWLClass(":Y", pm); OWLClass extA = dataFactory.getOWLClass("A:A", pm); OWLClass extB = dataFactory.getOWLClass("B:B", pm); OWLClass extC = dataFactory.getOWLClass("B:C", pm); // loading the root ontology OWLOntology root = loadOntology(man, "root.owl"); // Create an ELK reasoner. OWLReasonerFactory reasonerFactory = new ElkReasonerFactory(); OWLReasoner reasoner = reasonerFactory.createReasoner(root); try { // ************************************ // ** removing the import declaration for </impA> // ************************************ OWLImportsDeclaration importA = new OWLImportsDeclarationImpl( IRI.create("http://www.example.com#impA")); OWLOntologyChange change = new RemoveImport(root, importA); man.applyChange(change); reasoner.flush(); // Now the root ontology should not import anything assertEquals(root.getAxiomCount(), 3); assertEquals(root.getImportsClosure().size(), 1); assertEquals(getAxioms(root).size(), 3); // reasoner queries -- only subsumptions of the root ontology are // there assertTrue(reasoner.getSuperClasses(mainX, true).containsEntity( mainY)); assertTrue(reasoner.getSuperClasses(mainX, true).containsEntity( extA)); assertTrue(reasoner.getSuperClasses(mainY, true).containsEntity( extB)); assertFalse(reasoner.getSuperClasses(extA, true).containsEntity( extB)); assertFalse(reasoner.getSuperClasses(extB, true).containsEntity( extC)); } finally { reasoner.dispose(); } }
/** * Testing correctness of the reasoner when changes are made to other, imported or not, ontologies * */ @Test public void testChangesToOtherOntologies() throws Exception { OWLOntologyManager man = TestOWLManager.createOWLOntologyManager(); OWLDataFactory dataFactory = man.getOWLDataFactory(); // set up resolution of prefixes PrefixManager pm = new DefaultPrefixManager(); pm.setDefaultPrefix("http://www.example.com/main#"); pm.setPrefix("A:", "http://www.example.com/A#"); pm.setPrefix("B:", "http://www.example.com/B#"); // define query classes OWLClass mainY = dataFactory.getOWLClass(":Y", pm); OWLClass extA = dataFactory.getOWLClass("A:A", pm); OWLClass extB = dataFactory.getOWLClass("B:B", pm); OWLClass extC = dataFactory.getOWLClass("B:C", pm); // loading the root ontology OWLOntology root = loadOntology(man, "root.owl"); // the imported ontologies must be loaded OWLOntology ontoA = man.getOntology(IRI.create("http://www.example.com/A")); OWLOntology ontoB = man.getOntology(IRI.create("http://www.example.com/B")); // Create an ELK reasoner. OWLReasonerFactory reasonerFactory = new ElkReasonerFactory(); OWLReasoner reasoner = reasonerFactory.createReasoner(root); try { assertTrue(reasoner.getSuperClasses(extA, false).containsEntity( extC)); assertTrue(reasoner.getSuperClasses(mainY, false).containsEntity( extC)); // ************************************ // ** removing an axiom "A:A is-a B:B" from impA // ************************************ OWLAxiom axiom = dataFactory.getOWLSubClassOfAxiom(extA, extB); man.removeAxiom(ontoA, axiom); reasoner.flush(); assertFalse(reasoner.getSuperClasses(extA, false).containsEntity( extC)); // put it back man.addAxiom(ontoA, axiom); reasoner.flush(); assertTrue(reasoner.getSuperClasses(extA, false).containsEntity( extC)); // ************************************ // ** removing an axiom "B:B is-a B:C" from impB // ************************************ axiom = dataFactory.getOWLSubClassOfAxiom(extB, extC); man.removeAxiom(ontoB, axiom); reasoner.flush(); assertFalse(reasoner.getSuperClasses(mainY, false).containsEntity( extC)); } finally { reasoner.dispose(); } }
/** * Load the root ontology and all imports and apply normalization. */ private void loadOntology() { clearState(); axioms = new OWLAxioms(); Collection<OWLOntology> importClosure = rootOntology.getImportsClosure(); if(configuration.getDomainIndividuals() == null) { configuration.setDomainIndividuals(rootOntology.getIndividualsInSignature(Imports.INCLUDED)); } normalization = new OWLNormalization(rootOntology.getOWLOntologyManager().getOWLDataFactory(), axioms, 0, configuration.getDomainIndividuals()); for (OWLOntology ontology : importClosure) { normalization.processOntology(ontology); } axioms.m_namedIndividuals.clear(); axioms.m_namedIndividuals.addAll(configuration.getDomainIndividuals()); try { tmpFile = File.createTempFile("wolpertinger-base-program", ".lp"); tmpFile.deleteOnExit(); output = new PrintWriter(tmpFile); naiveTranslation = new NaiveTranslation(configuration, output); naiveTranslation.translateOntology(axioms); } catch (IOException e) { e.printStackTrace(); } OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); OWLDataFactory factory = manager.getOWLDataFactory(); PrefixManager pManager = new DefaultPrefixManager(); OWLClassExpression thing = factory.getOWLClass("owl:Thing", pManager); for (OWLNamedIndividual individual : axioms.m_namedIndividuals) { OWLClassAssertionAxiom assertion = factory.getOWLClassAssertionAxiom(thing, individual); manager.addAxiom(rootOntology, assertion); } enumerator = new ClingoModelEnumerator(new String[] {tmpFile.getAbsolutePath()}); }
/** * Creates the owl property axioms from concept version bi. * * @param setOfAxioms the set of axioms * @param factory the factory * @param pm the pm * @param conceptVersionBI the concept version bi * @return the OWL object property * @throws Exception the exception */ private OWLObjectProperty createOWLPropertyAxiomsFromConceptVersionBI( Set<OWLAxiom> setOfAxioms, OWLDataFactory factory, PrefixManager pm, ConceptVersionBI conceptVersionBI) throws Exception { // Declaration OWLObjectProperty owlPropertyClass = factory.getOWLObjectProperty( ":" + getSnomedConceptID(conceptVersionBI), pm); OWLDeclarationAxiom declarationAxiom = factory.getOWLDeclarationAxiom(owlPropertyClass); setOfAxioms.add(declarationAxiom); // Create annotation type axiom // Fully specified description OWLAnnotation labelAnnotation = factory.getOWLAnnotation(factory.getRDFSLabel(), factory.getOWLLiteral( conceptVersionBI.getFullySpecifiedDescription().getText(), conceptVersionBI.getFullySpecifiedDescription().getLang())); OWLAxiom labelAnnotationAxiom = factory.getOWLAnnotationAssertionAxiom(owlPropertyClass.getIRI(), labelAnnotation); setOfAxioms.add(labelAnnotationAxiom); // Preferred name OWLAnnotation preferredTerm = factory.getOWLAnnotation(factory.getOWLAnnotationProperty(IRI .create(snomedEnUsPreferredAnnotation)), factory.getOWLLiteral( conceptVersionBI.getPreferredDescription().getText(), conceptVersionBI.getPreferredDescription().getLang())); OWLAxiom preferredTermAx = factory.getOWLAnnotationAssertionAxiom(owlPropertyClass.getIRI(), preferredTerm); setOfAxioms.add(preferredTermAx); // SubObjectPropertyOf // SubObjectPropertyOf(ObjectPropertyChain( // <http://snomed.info/id/363701004> <http://snomed.info/id/127489000> ) // <http://snomed.info/id/363701004>) for (RelationshipVersionBI<?> rel : conceptVersionBI .getRelationshipsOutgoingActiveIsa()) { if (rel.isStated()) { String sctid = getSnomedConceptID(OTFUtility.getConceptVersion(rel .getDestinationNid())); // Skip the root of the attributes tree if (!sctid.equals(snomedConceptAttributeModelConcept)) { OWLObjectProperty parent = factory.getOWLObjectProperty(":" + sctid, pm); setOfAxioms.add(factory.getOWLSubObjectPropertyOfAxiom( owlPropertyClass, parent)); } } } return owlPropertyClass; }
@Override public void copyPrefixesFrom(PrefixManager from) { copyPrefixesFrom(from.getPrefixName2PrefixMap()); }