org.semanticweb.owlapi.util.DefaultPrefixManager Java Examples
The following examples show how to use
org.semanticweb.owlapi.util.DefaultPrefixManager.
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: 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 #2
Source File: DiffOperation.java From robot with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Given an IOHelper to get short form of IRIs, a map from IRIs to labels, and an axiom string, * add labels next to any IRIs in the string, shorten OBO IRIs, and return the updated string. * * @param ioHelper IOHelper to use for prefixes * @param labels a map from IRIs to label strings * @param axiom a string representation of an OWLAxiom * @return a string with labels inserted next to CURIEs * @deprecated This functionality is now provided by the owl-diff library and no longer used to * format results in DiffOperation. */ @Deprecated public static String addLabels(IOHelper ioHelper, Map<IRI, String> labels, String axiom) { DefaultPrefixManager pm = ioHelper.getPrefixManager(); Matcher matcher = iriPattern.matcher(axiom); StringBuffer sb = new StringBuffer(); while (matcher.find()) { IRI iri = IRI.create(matcher.group(1)); String id = pm.getShortForm(iri); if (id.startsWith("obo:")) { id = id.substring(4).replace("_", ":"); } if (!id.startsWith("<") && !id.endsWith(">")) { id = "<" + id + ">"; } String replacement = id; if (labels.containsKey(iri)) { replacement = id + "[" + labels.get(iri) + "]"; } matcher.appendReplacement(sb, replacement); } matcher.appendTail(sb); return sb.toString(); }
Example #3
Source File: IgnoreChangesInNonImportedOntologiesTest.java From elk-reasoner with Apache License 2.0 | 5 votes |
/** * Testing correctness of the reasoner with respect to ontology changes */ @Test public void ignoreChangesInNonImportedOntologies() throws Exception { OWLOntologyManager man = TestOWLManager.createOWLOntologyManager(); OWLDataFactory dataFactory = man.getOWLDataFactory(); // set up resolution of prefixes DefaultPrefixManager 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#"); OWLClass extA = dataFactory.getOWLClass("A:A", pm); OWLClass extB = dataFactory.getOWLClass("B:B", pm); // loading the root ontology OWLOntology root = loadOntology(man, "root.owl"); // Create an ELK reasoner. ElkReasoner reasoner = (ElkReasoner) new ElkReasonerFactory() .createReasoner(root); // make sure the reasoner loads the ontology reasoner.flush(); reasoner.isConsistent(); try { OWLOntology nonImported = loadOntology(man, "nonImported.owl"); OWLAxiom axiom = dataFactory.getOWLSubClassOfAxiom(extA, extB); man.removeAxiom(nonImported, axiom); reasoner.flush(); AbstractReasonerState state = reasoner.getInternalReasoner(); assertTrue(state.stageManager.inputLoadingStage.isCompleted()); } finally { reasoner.dispose(); } }
Example #4
Source File: OWLConverter.java From owltools with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Initialize the format with standard default prefixes * (rdf, rdfs, xsd, owl) and with the prefixes we will use * (obo, oio, iao). * * @return The format with the prefixes set. */ protected static RDFXMLDocumentFormat initializeFormat() { RDFXMLDocumentFormat format = new RDFXMLDocumentFormat(); format.copyPrefixesFrom(new DefaultPrefixManager()); format.setPrefix("obo", OBO); format.setPrefix("oio", OIO); format.setPrefix("iao", IAO); format.setPrefix("ncbi", NCBI); format.setPrefix("ncbitaxon", OBO + "ncbitaxon#"); return format; }
Example #5
Source File: IOHelperTest.java From robot with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Test the default prefix manager. * * @throws IOException on file problem */ @Test public void testPrefixManager() throws IOException { IOHelper ioh = new IOHelper(); DefaultPrefixManager pm = ioh.getPrefixManager(); assertEquals( "Check GO CURIE", "http://purl.obolibrary.org/obo/GO_12345", pm.getIRI("GO:12345").toString()); }
Example #6
Source File: IOHelper.java From robot with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Make an OWLAPI DefaultPrefixManager from a map of prefixes. * * @param prefixes a map from prefix name strings to prefix IRI strings * @return a new DefaultPrefixManager */ public static DefaultPrefixManager makePrefixManager(Map<String, String> prefixes) { DefaultPrefixManager pm = new DefaultPrefixManager(); for (Map.Entry<String, String> entry : prefixes.entrySet()) { pm.setPrefix(entry.getKey() + ":", entry.getValue()); } return pm; }
Example #7
Source File: OntologyService.java From snomed-owl-toolkit with Apache License 2.0 | 5 votes |
public OntologyService(Set<Long> ungroupedAttributes) { this.ungroupedAttributes = ungroupedAttributes; manager = OWLManager.createOWLOntologyManager(); factory = new OWLDataFactoryImpl(); prefixManager = new DefaultPrefixManager(); prefixManager.setDefaultPrefix(SNOMED_CORE_COMPONENTS_URI); missingDialectWarnings = new AtomicLong(); }
Example #8
Source File: ElkReasonerTest.java From elk-reasoner with Apache License 2.0 | 4 votes |
/** * 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(); } }
Example #9
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 #10
Source File: ExtractCommand.java From robot with BSD 3-Clause "New" or "Revised" License | 4 votes |
/** * Given an IOHelper and the path to a term-to-source map, return a map of term IRI to source IRI. * * @param ioHelper IOHelper to handle prefixes * @param sourceMapPath path of the term-to-source map * @return map of term IRI to source IRI * @throws Exception on file reading issue */ private static Map<IRI, IRI> getSourceMap(IOHelper ioHelper, String sourceMapPath) throws Exception { // If no source map path is specified, just return null if (sourceMapPath == null) { return null; } // Otherwise, use the path to get a file containing the mappings File sourceMapFile = new File(sourceMapPath); if (!sourceMapFile.exists()) { throw new Exception(String.format(missingFileError, sourceMapPath, "--sources")); } char separator; if (sourceMapPath.endsWith(".tsv")) { separator = '\t'; } else if (sourceMapPath.endsWith(".csv")) { separator = ','; } else { throw new Exception(invalidSourceMapError); } DefaultPrefixManager pm = ioHelper.getPrefixManager(); Reader reader = new FileReader(sourceMapFile); CSVReader csv = new CSVReaderBuilder(reader) .withCSVParser(new CSVParserBuilder().withSeparator(separator).build()) .build(); // Skip first line csv.skip(1); Map<IRI, IRI> sourceMap = new HashMap<>(); for (String[] line : csv) { IRI entity = ioHelper.createIRI(line[0]); // Maybe create a source IRI from a prefix // Otherwise the full IRI should be provided IRI source; String sourceStr = line[1]; String namespace = pm.getPrefix(sourceStr + ":"); if (namespace != null) { if (namespace.endsWith("_") || namespace.endsWith("#") || namespace.endsWith("/")) { namespace = namespace.substring(0, namespace.length() - 1); } source = IRI.create(namespace.toLowerCase() + ".owl"); } else { source = IRI.create(sourceStr); } sourceMap.put(entity, source); } return sourceMap; }
Example #11
Source File: DiffOperation.java From robot with BSD 3-Clause "New" or "Revised" License | 4 votes |
/** * Given two ontologies, a Writer, and a set of diff options, get their differences and write then * to the writer. The ontologies are not changed. * * @param ontology1 the first ontology * @param ontology2 the second ontology * @param ioHelper IOHelper to use for prefixes * @param writer the Writer for the report, or null * @param options diff options * @return true if the ontologies are the same, false otherwise * @throws IOException on writer failure */ public static boolean compare( OWLOntology ontology1, OWLOntology ontology2, IOHelper ioHelper, Writer writer, Map<String, String> options) throws IOException { boolean useLabels = OptionsHelper.optionIsTrue(options, "labels"); String format = OptionsHelper.getOption(options, "format", "plain"); format = format.toLowerCase(); if (useLabels && format.equals("plain")) { format = "pretty"; } Differ.BasicDiff diff = Differ.diff(ontology1, ontology2); if (diff.isEmpty()) { if (writer != null) { writer.write("Ontologies are identical\n"); } return true; } if (writer == null) { return false; } OWLOntologySetProvider ontologyProvider = new DualOntologySetProvider( ontology1.getOWLOntologyManager(), ontology2.getOWLOntologyManager()); switch (format) { case "plain": writer.write(BasicDiffRenderer.renderPlain(diff)); break; case "pretty": DefaultPrefixManager pm = ioHelper.getPrefixManager(); AnnotationValueShortFormProvider labelProvider = new AnnotationValueShortFormProvider( ontologyProvider, pm, pm, Collections.singletonList(OWLManager.getOWLDataFactory().getRDFSLabel()), Collections.emptyMap()); OBOShortenerShortFormProvider iriProvider = new OBOShortenerShortFormProvider(pm); DoubleShortFormProvider doubleProvider = new DoubleShortFormProvider(iriProvider, labelProvider); writer.write(BasicDiffRenderer.render(diff, doubleProvider)); break; case "markdown": Differ.GroupedDiff groupedForMarkdown = Differ.groupedDiff(diff); writer.write(MarkdownGroupedDiffRenderer.render(groupedForMarkdown, ontologyProvider)); break; case "html": Differ.GroupedDiff groupedForHTML = Differ.groupedDiff(diff); writer.write(HTMLDiffRenderer.render(groupedForHTML, ontologyProvider)); break; default: throw new IOException("Unknown diff format: " + format); } return false; }
Example #12
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 #13
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 #14
Source File: ElkReasonerTest.java From elk-reasoner with Apache License 2.0 | 4 votes |
/** * 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(); } }
Example #15
Source File: ElkReasonerTest.java From elk-reasoner with Apache License 2.0 | 4 votes |
/** * 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(); } }
Example #16
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(); }
Example #17
Source File: IOHelper.java From robot with BSD 3-Clause "New" or "Revised" License | 2 votes |
/** * Get a prefix manager with the current prefixes. * * @return a new DefaultPrefixManager */ public DefaultPrefixManager getPrefixManager() { return makePrefixManager(context.getPrefixes(false)); }