Java Code Examples for org.semanticweb.owlapi.model.OWLOntologyManager#getOWLDataFactory()
The following examples show how to use
org.semanticweb.owlapi.model.OWLOntologyManager#getOWLDataFactory() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AssertInferenceTool.java From owltools with BSD 3-Clause "New" or "Revised" License | 6 votes |
private static List<OWLOntologyChange> handleSupportOntologies(OWLGraphWrapper graph) { OWLOntology ontology = graph.getSourceOntology(); OWLOntologyManager manager = ontology.getOWLOntologyManager(); OWLDataFactory factory = manager.getOWLDataFactory(); List<OWLOntologyChange> removeImportChanges = new ArrayList<OWLOntologyChange>(); Set<OWLOntology> supportOntologySet = graph.getSupportOntologySet(); for (OWLOntology support : supportOntologySet) { Optional<IRI> supportIRI = support.getOntologyID().getOntologyIRI(); if(supportIRI.isPresent()) { IRI ontologyIRI = supportIRI.get(); OWLImportsDeclaration importDeclaration = factory.getOWLImportsDeclaration(ontologyIRI); ChangeApplied status = manager.applyChange(new AddImport(ontology, importDeclaration)); if (ChangeApplied.SUCCESSFULLY == status) { // the change was successful, create remove import for later removeImportChanges.add(new RemoveImport(ontology, importDeclaration)); } } } return removeImportChanges; }
Example 2
Source File: OWLGraphManipulatorTest.java From owltools with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * 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()); }
Example 3
Source File: FilterOperationTest.java From robot with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Filter for one object property from an ontology that has just one object property. Result is * identical. * * @throws IOException on file problem */ @Test public void testKeepParts() throws IOException { OWLOntology simpleParts = loadOntology("/simple_parts.owl"); OWLOntologyManager manager = simpleParts.getOWLOntologyManager(); OWLDataFactory df = manager.getOWLDataFactory(); Set<OWLObjectProperty> properties = new HashSet<>(); properties.add(df.getOWLObjectProperty(IRI.create(base + "simple.owl#part_of"))); OWLOntology filtered = loadOntology("/simple_parts.owl"); FilterOperation.filter(filtered, properties); assertIdentical("/simple_parts.owl", filtered); }
Example 4
Source File: TransformationUtils.java From owltools with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Names all inner ObjectSomeValuesFrom expressions * * @param srcOntology * @param tgtOntology * @param qmap * @param isAddLabels * @return */ public static Map<OWLClass, OWLClassExpression> nameObjectSomeValuesFrom(OWLOntology srcOntology, OWLOntology tgtOntology, Map<OWLClass,OWLClassExpression> qmap, boolean isAddLabels) { if (qmap == null) qmap = new HashMap<OWLClass, OWLClassExpression>(); OWLOntologyManager mgr = srcOntology.getOWLOntologyManager(); OWLDataFactory df = mgr.getOWLDataFactory(); for (OWLOntology ont : srcOntology.getImportsClosure()) { for (OWLAxiom ax : srcOntology.getAxioms()) { for (OWLClassExpression x : ax.getNestedClassExpressions()) { if (x instanceof OWLObjectSomeValuesFrom) { OWLObjectSomeValuesFrom svf = (OWLObjectSomeValuesFrom)x; OWLClass filler = (OWLClass) svf.getFiller(); OWLObjectProperty p = (OWLObjectProperty) svf.getProperty(); IRI iri = getSkolemIRI(filler, p); OWLClass c = df.getOWLClass(iri); mgr.addAxiom(tgtOntology, df.getOWLEquivalentClassesAxiom(c, svf)); qmap.put(c, svf); if (isAddLabels) { Set<OWLAnnotation> anns = OwlHelper.getAnnotations(filler, df.getRDFSLabel(), ont); for (OWLAnnotation ann : anns) { mgr.addAxiom(tgtOntology, df.getOWLAnnotationAssertionAxiom(c.getIRI(), ann)); } } } } } } return qmap; }
Example 5
Source File: ABoxUtils.java From owltools with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Creates a "fake" individual for every class. * * ABox IRI = TBox IRI + suffix * * if suffix == null, then we are punning * * @param srcOnt * @param iriSuffix * @throws OWLOntologyCreationException */ public static void makeDefaultIndividuals(OWLOntology srcOnt, String iriSuffix) throws OWLOntologyCreationException { OWLOntologyManager m = srcOnt.getOWLOntologyManager(); OWLDataFactory df = m.getOWLDataFactory(); for (OWLClass c : srcOnt.getClassesInSignature(Imports.INCLUDED)) { IRI iri; if (iriSuffix == null || iriSuffix.equals("")) iri = c.getIRI(); else iri = IRI.create(c.getIRI().toString()+iriSuffix); OWLNamedIndividual ind = df.getOWLNamedIndividual(iri); m.addAxiom(srcOnt, df.getOWLDeclarationAxiom(ind)); m.addAxiom(srcOnt, df.getOWLClassAssertionAxiom(c, ind)); } }
Example 6
Source File: OWLGraphWrapperEdgeTest.java From owltools with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void testEdgeCache() throws Exception { OWLGraphWrapper g = getGraph("graph/cache-test.obo"); OWLOntology o = g.getSourceOntology(); OWLOntologyManager m = o.getOWLOntologyManager(); OWLDataFactory f = m.getOWLDataFactory(); OWLClass orphan = g.getOWLClassByIdentifier("FOO:0004"); OWLClass root = g.getOWLClassByIdentifier("FOO:0001"); g.getEdgesBetween(orphan, root); //just to trigger the cache OWLSubClassOfAxiom ax = f.getOWLSubClassOfAxiom(orphan, root); AddAxiom addAx = new AddAxiom(o, ax); m.applyChange(addAx); Set<OWLGraphEdge> edges = g.getEdgesBetween(orphan, root); assertNotNull(edges); assertEquals(0, edges.size()); g.clearCachedEdges(); // test clear cache method edges = g.getEdgesBetween(orphan, root); assertNotNull(edges); assertEquals(1, edges.size()); }
Example 7
Source File: RedundantAxiomTagger.java From owltools with BSD 3-Clause "New" or "Revised" License | 5 votes |
public static void tagRedundantAxioms(OWLReasoner reasoner) { OWLOntology ont = reasoner.getRootOntology(); OWLOntologyManager mgr = ont.getOWLOntologyManager(); OWLDataFactory df = mgr.getOWLDataFactory(); OWLAnnotationProperty anProp = df.getOWLAnnotationProperty(IRI.create("http://www.geneontology.org/formats/oboInOwl#source")); for (OWLSubClassOfAxiom ax : ont.getAxioms(AxiomType.SUBCLASS_OF)) { if (!ax.getSuperClass().isAnonymous()) { OWLClass supc = (OWLClass) ax.getSuperClass(); mgr.removeAxiom(ont, ax); reasoner.flush(); NodeSet<OWLClass> ancs = reasoner.getSuperClasses(ax.getSubClass(), false); //LOG.info(ax + " ANCS="+ancs); if (ancs.containsEntity( supc)) { String direct = "indirect"; if (reasoner.getSuperClasses(ax.getSubClass(), true).containsEntity( supc)) { direct = "direct"; } LOG.info("SCA = "+ax+" D="+direct); OWLAnnotation ann = df.getOWLAnnotation(anProp, df.getOWLLiteral(direct)); OWLAxiom newAxiom = changeAxiomAnnotations(ax, Collections.singleton(ann), df); mgr.addAxiom(ont, newAxiom); } else { // put it back mgr.addAxiom(ont, ax); } } } }
Example 8
Source File: BridgeExtractor.java From owltools with BSD 3-Clause "New" or "Revised" License | 5 votes |
private void addObjectProperty(OWLObjectProperty p, OWLOntology xOnt) { if (xOnt.getDeclarationAxioms(p).size() > 0) { return; } OWLOntologyManager m = ontology.getOWLOntologyManager(); OWLDataFactory df = m.getOWLDataFactory(); m.addAxiom(xOnt, df.getOWLDeclarationAxiom(p)); for (OWLAxiom ax : ontology.getAxioms(p, Imports.EXCLUDED)) { m.addAxiom(xOnt, ax); } // TODO }
Example 9
Source File: OWLConverter.java From owltools with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Convenience method for adding an annotation assertion to the * ontology. * * @param ontology the current ontology * @param subject the subject of the annotation * @param property the annotation property * @param value the annotation value * @return the annotation axiom */ protected static OWLAnnotationAssertionAxiom annotate( OWLOntology ontology, OWLEntity subject, OWLAnnotationProperty property, OWLAnnotationValue value) { OWLOntologyManager manager = ontology.getOWLOntologyManager(); OWLDataFactory dataFactory = manager.getOWLDataFactory(); OWLAnnotationAssertionAxiom axiom = dataFactory.getOWLAnnotationAssertionAxiom( property, subject.getIRI(), value); manager.addAxiom(ontology, axiom); return axiom; }
Example 10
Source File: WEKAOntologyConnector.java From AILibs with GNU Affero General Public License v3.0 | 5 votes |
/** * Creates an ontology connector using the standard ontology. * * @throws OWLOntologyCreationException * If the ontology cannot be created */ public WEKAOntologyConnector() throws OWLOntologyCreationException { OWLOntologyManager ontologyManager = OWLManager.createOWLOntologyManager(); dataFactory = ontologyManager.getOWLDataFactory(); InputStream inputStream = Thread.currentThread().getContextClassLoader() .getResourceAsStream(ONTOLOGY_FILE_NAME); ontology = ontologyManager.loadOntologyFromOntologyDocument(inputStream); }
Example 11
Source File: AxiomAnnotationTools.java From owltools with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Append xref annotations onto an existing axiom * * @param axiom * @param xrefs * @param ontology * @return */ public static OWLAxiom appendXrefAnnotations(OWLAxiom axiom, Set<String> xrefs, OWLOntology ontology) { // filter existing Set<OWLAnnotation> newAnnotations = new HashSet<OWLAnnotation>(axiom.getAnnotations()); final OWLOntologyManager manager = ontology.getOWLOntologyManager(); final OWLDataFactory factory = manager.getOWLDataFactory(); for (String x : xrefs) { OWLAnnotationProperty p = factory.getOWLAnnotationProperty(IRI.create("http://www.geneontology.org/formats/oboInOwl#hasDbXref")); OWLLiteral v = factory.getOWLLiteral(x); newAnnotations.add(factory.getOWLAnnotation(p, v)); } final OWLAxiom newAxiom = changeAxiomAnnotations(axiom, newAnnotations, ontology); return newAxiom; }
Example 12
Source File: OWLConverter.java From owltools with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Convenience method for asserting a subClass relation between * a parent and child class in an ontology. * * @param ontology the current ontology * @param child the child class * @param parent the parent class * @return the axiom */ protected static OWLSubClassOfAxiom assertSubClass( OWLOntology ontology, OWLClass child, OWLClass parent) { OWLOntologyManager manager = ontology.getOWLOntologyManager(); OWLDataFactory dataFactory = manager.getOWLDataFactory(); OWLSubClassOfAxiom axiom = dataFactory.getOWLSubClassOfAxiom(child, parent); ontology.getOWLOntologyManager().addAxiom(ontology, axiom); return axiom; }
Example 13
Source File: AxiomAnnotationTools.java From owltools with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Mark the given axioms as inferred.<br> * <b>Side effect</b>: Removes the axiom in the given ontology and creates a * new axiom with the changed annotations. Returns the new changed axiom. * * @param axioms * @param ontology * @return changed axiom */ public static Set<OWLAxiom> markAsInferredAxiom(Set<OWLAxiom> axioms, OWLOntology ontology) { final OWLOntologyManager manager = ontology.getOWLOntologyManager(); final OWLDataFactory factory = manager.getOWLDataFactory(); // update axioms Set<OWLAxiom> newAxioms = new HashSet<OWLAxiom>(); for (OWLAxiom axiom : axioms) { newAxioms.add(updateInferredAxiom(axiom, factory, true)); } // change ontology manager.removeAxioms(ontology, axioms); manager.addAxioms(ontology, newAxioms); return newAxioms; }
Example 14
Source File: IncrementalClassification.java From elk-reasoner with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws OWLOntologyStorageException, OWLOntologyCreationException { OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); // Load your ontology OWLOntology ont = manager.loadOntologyFromOntologyDocument(new File("path-to-ontology")); // Create an ELK reasoner. OWLReasonerFactory reasonerFactory = new ElkReasonerFactory(); OWLReasoner reasoner = reasonerFactory.createReasoner(ont); // Classify the ontology. reasoner.precomputeInferences(InferenceType.CLASS_HIERARCHY); OWLDataFactory factory = manager.getOWLDataFactory(); OWLClass subClass = factory.getOWLClass(IRI.create("http://www.co-ode.org/ontologies/galen#AbsoluteShapeState")); OWLAxiom removed = factory.getOWLSubClassOfAxiom(subClass, factory.getOWLClass(IRI.create("http://www.co-ode.org/ontologies/galen#ShapeState"))); OWLAxiom added = factory.getOWLSubClassOfAxiom(subClass, factory.getOWLClass(IRI.create("http://www.co-ode.org/ontologies/galen#GeneralisedStructure"))); // Remove an existing axiom, add a new axiom manager.addAxiom(ont, added); manager.removeAxiom(ont, removed); // This is a buffering reasoner, so you need to flush the changes reasoner.flush(); // Re-classify the ontology, the changes should be accommodated // incrementally (i.e. without re-inferring all subclass relationships) // You should be able to see it from the log output reasoner.precomputeInferences(InferenceType.CLASS_HIERARCHY); // Terminate the worker threads used by the reasoner. reasoner.dispose(); }
Example 15
Source File: AssertInferenceTool.java From owltools with BSD 3-Clause "New" or "Revised" License | 4 votes |
public static void assertAllInferences(OWLGraphWrapper graph, String idsInputFile) { final OWLOntology ontology = graph.getSourceOntology(); final OWLOntologyManager manager = ontology.getOWLOntologyManager(); final OWLDataFactory factory = manager.getOWLDataFactory(); Set<String> ids = loadIdsInputFile(idsInputFile); final OWLReasonerFactory reasonerFactory = new ElkReasonerFactory(); final OWLReasoner reasoner = reasonerFactory.createReasoner(ontology); try { logger.info("Start check all"); // check all classes from the main ontology AllInferenceReport report = new AllInferenceReport(); Set<OWLClass> classes = ontology.getClassesInSignature(Imports.EXCLUDED); int count = 0; int total = ids != null ? ids.size() : classes.size(); int step = 100; for (final OWLClass owlClass : classes) { if (ids != null) { String id = graph.getIdentifier(owlClass); if (ids.contains(id) == false) { continue; } } count += 1; // get axioms for the current class Set<OWLClassAxiom> axioms = ontology.getAxioms(owlClass, Imports.EXCLUDED); handleAxioms(owlClass, axioms, ontology, manager, factory, reasoner, report); // handleAxioms2(owlClass, axioms, ontology, manager, factory, reasoner, report); if (count % step == 0) { logger.info("Current count "+count+" of "+total); } } PrintWriter writer = new PrintWriter(System.out); report.printReport(writer); writer.close(); } finally { reasoner.dispose(); } }
Example 16
Source File: CardinalityContraintsTools.java From owltools with BSD 3-Clause "New" or "Revised" License | 4 votes |
public CardinalityHandler(OWLOntology ontology) { this.ontology = ontology; OWLOntologyManager manager = ontology.getOWLOntologyManager(); factory = manager.getOWLDataFactory(); handler = new CardinalityExpressionHandler(factory); }
Example 17
Source File: ElkReasonerTest.java From elk-reasoner with Apache License 2.0 | 4 votes |
/** * Testing correctness of the reasoner with respect to ontology changes * <p> * removing the import declaration for </impA>, * adding the import declaration for </impB> and removing * ":Y is-a B:B" */ @Test public void testRemovingImpAAddingImpBRemovingYB() 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 { // ************************************ // ** adding the import declaration for </impB> and removing // ":Y is-a B:B" // ************************************ OWLImportsDeclaration importA = new OWLImportsDeclarationImpl( IRI.create("http://www.example.com#impA")); OWLOntologyChange change = new RemoveImport(root, importA); man.applyChange(change); OWLImportsDeclaration importB = new OWLImportsDeclarationImpl( IRI.create("http://www.example.com#impB")); change = new AddImport(root, importB); man.applyChange(change); OWLSubClassOfAxiom axiom = dataFactory.getOWLSubClassOfAxiom(mainY, extB); man.removeAxiom(root, axiom); reasoner.flush(); // Now ontology should import only ontology </impB> assertEquals(root.getAxiomCount(), 2); assertEquals(root.getImportsClosure().size(), 2); assertEquals(getAxioms(root).size(), 4); // reasoner queries -- only subsumptions of the root // ontology </impB> and there assertTrue(reasoner.getSuperClasses(mainX, true).containsEntity( mainY)); assertTrue(reasoner.getSuperClasses(mainX, true).containsEntity( extA)); assertFalse(reasoner.getSuperClasses(mainY, true).containsEntity( extB)); assertFalse(reasoner.getSuperClasses(extA, true).containsEntity( extB)); assertTrue(reasoner.getSuperClasses(extB, true) .containsEntity(extC)); } finally { reasoner.dispose(); } }
Example 18
Source File: ElkReasonerTest.java From elk-reasoner with Apache License 2.0 | 4 votes |
/** * 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(); } }
Example 19
Source File: ElkReasonerTest.java From elk-reasoner with Apache License 2.0 | 4 votes |
/** * 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(); } }
Example 20
Source File: QueryingUnnamedClassExpressions.java From elk-reasoner with Apache License 2.0 | 4 votes |
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(); }