Java源码示例:org.semanticweb.owlapi.model.PrefixManager
示例1
@Override
protected void storeOntology(@Nonnull OWLOntology ontology, @Nonnull Writer writer, OWLDocumentFormat format) throws OWLOntologyStorageException {
try {
FunctionalSyntaxObjectRenderer renderer = new FunctionalSyntaxObjectRenderer(ontology, writer);
// Force ontology prefix manager into renderer
if (format instanceof PrefixManager) {
renderer.setPrefixManager((PrefixManager) format);
}
ontology.accept(renderer);
writer.flush();
} catch (IOException e) {
throw new OWLOntologyStorageException(e);
}
}
示例2
/**
* 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());
}
示例3
/**
* 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);
}
}
示例4
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();
}
示例5
/**
* 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();
}
}
示例6
/**
* 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();
}
}
示例7
/**
* 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();
}
}
示例8
/**
* 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();
}
}
示例9
/**
* 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();
}
}
示例10
/**
* 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();
}
}