I try to print the Superclasses of standard Pizza ontology downloaded from here . I am using OWL API 3.4.3 and Hermit 1.3.8.1 (reasoner).
The following code snippet is used to print the required Superclasses of class "Food".
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
IRI ontologyIRI =IRI.create("file:/D:/pizza.owl.xml");
//IRI ontologyIRI =IRI.create("http://www.co-ode.org/ontologies/pizza/pizza.owl");
OWLOntology ontology = manager.loadOntology(ontologyIRI);
OWLReasoner reasoner = new Reasoner.ReasonerFactory().createReasoner(ontology);
OWLDataFactory df = manager.getOWLDataFactory();
try{
reasoner.precomputeInferences(InferenceType.CLASS_ASSERTIONS);
//following Lines are to see super classes of Container
OWLClass clsA = df.getOWLClass(IRI.create(ontologyIRI + "#Food"));
Set<OWLClassExpression> superClasses = clsA.getSuperClasses(ontology);
System.out.println("in TRY 1");
//System.out.println("Hello World\n"+superClasses.iterator().toString());
for (OWLClassExpression g : superClasses) {
System.out.println("The superclasses are:"+g);
}
}
catch (Exception e) {
e.printStackTrace();
}
I do not get any compilation error. The result is in TRY 1. The content inside for loop has not been printed.
In protege 5.0, I have seen two Superclasses of Food class; namely DomainConcept and owl:Thing. Why these two names has not been printed by the program?
Where am I doing wrong?
Thanks for any kind of help.
Your ontology IRI is a local file name. When you use it to obtain a class IRI, you're getting a different IRI from the one actually used in the ontology. Check what IRI the class actually has and that should fix your issue.
Related
I am reading the attached university-bench ontology file (which I generated from UBA1.7 lubm) in java using owlapi. But it is not reading any axiom like subclass etc. And it is also not giving me any error. Can anyone tell me what I am doing wrong. The below code I used to retrieve the subclass axioms from this ontology but it return me nothing/ blank output. I wanted to output subclass, disjoint class, sub property, disjoint property, anonymous superclass axioms. but currently I am unable to get anything out from the ontology.
When I use the ontology which is created by me using protege. The below code works fine. But when i try to execute the ontology generated from UBA1.7 it gives me nothing.
public static void axioms(File ontology) throws OWLOntologyCreationException {
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLOntology ontology = manager.loadOntologyFromOntologyDocument(ontology);
OWLDataFactory df = manager.getOWLDataFactory();
for (final OWLSubClassOfAxiom subClasse : ontology.getAxioms(AxiomType.SUBCLASS_OF))
{
if (subClasse.getSuperClass() instanceof OWLClass && subClasse.getSubClass() instanceof OWLClass)
{
System.out.println(subClasse.getSubClass() + " extends " + subClasse.getSuperClass());
}
}
}
I am trying to create and store an ontology file in functional format using OWL API:
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLOntology ontology = manager.createOntology();
OWLDataFactory factory = manager.getOWLDataFactory();
PrefixManager pm = new FunctionalSyntaxDocumentFormat();
pm.setDefaultPrefix(" :");
OWLClass item = factory.getOWLClass(IRI.create("item"), pm);
manager.addAxiom(ontology, factory.getOWLDeclarationAxiom(item));
manager.saveOntology(ontology, new FunctionalSyntaxDocumentFormat(), new FileOutputStream("FileName"))
The result in the saved file for this axiom is this:
Declaration(Class(< :item>))
How do I get rid of the < > brackets around entities? It happens to all entities that I create, and it is preventing my file from being parsed correctly.
Two issues: there should not be a space in the default prefix, and the prefix manager you are setting the prefix on must be the same used in the call to saveOntology(). You can just pass the first functional document format to the last method in your code.
Edit: After trying to run the code, I think there's a bit of a bug in the OWL API. It is necessary to set the format on the manager for the prefixes to be picked up properly. That should not be necessary. However, there's a workaround.
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLOntology ontology = manager.createOntology();
OWLDataFactory factory = manager.getOWLDataFactory();
FunctionalSyntaxDocumentFormat pm = new FunctionalSyntaxDocumentFormat();
pm.setPrefix(":", "http://test.owl/test#");
manager.setOntologyFormat(ontology, pm);
OWLClass item = factory.getOWLClass("item", pm);
manager.addAxiom(ontology, factory.getOWLDeclarationAxiom(item));
manager.saveOntology(ontology, System.out);
I am trying to remove some literal Annotations from an ontology using OWLAPI version 4.0.2 (from Maven)
In this purpose I am using the RemoveOntologyAnnotation class and the manager applyChange() method.
Here is the (simplified) code I use:
OWLOntologyManager m = OWLManager.createOWLOntologyManager();
OWLOntology ontology = null;
File ontologyFile = new File(ontologyFileName);
try {
ontology = m.loadOntologyFromOntologyDocument(ontologyFile);
} catch (OWLOntologyCreationException e) {
e.printStackTrace();
}
for (OWLClass cls : ontology.getClassesInSignature()) {
for (OWLAnnotation annotation : EntitySearcher.getAnnotations(cls.getIRI(), ontology)) {
if (annotation.getValue() instanceof OWLLiteral) {
RemoveOntologyAnnotation rm = new RemoveOntologyAnnotation(ontology, annotation);
System.out.println(m.applyChange(rm));
}
}
}
The applyChange() method is always returning "UNSUCCESSFULLY"
And I could not find any documentation about why the annotation removing don't work.
N.B.: found some indications here http://sourceforge.net/p/owlapi/mailman/message/28203984/
Where it seems to work
As also noted in the mailing list thread linked in your question, annotations on ontologies and annotations on ontology elements are two different things.
RemoveOntologyAnnotation only removes annotations on the ontology itself.
Annotations on elements are represented using axioms, specifically OWLAnnotationAssertionAxioms: Consequently, they have to be removed using OWLOntologyManager.removeAxiom() or similar means:
for (OWLClass cls : ontology.getClassesInSignature()) {
for (OWLAnnotationAssertionAxiom annAx : EntitySearcher.getAnnotationAssertionAxioms(cls.getIRI(), ontology)) {
if (annAx.getValue().getValue() instanceof OWLLiteral) {
m.removeAxiom(annAx);
}
}
}
Given an arbitrary IRI, such as the main ontology or one of the ontologies it imports, I would like to extract the title but the code yields no annotations.
Here's an example of what I'm talking about, from the SKOS ontology:
<owl:Ontology rdf:about="http://www.w3.org/2004/02/skos/core">
<dct:title xml:lang="en">SKOS Vocabulary</dct:title>
How exactly would I extract, "SKOS Vocabulary".
Here is some code I am currently using from an OWL-API tutorial.
public void testingOWL() throws OWLOntologyCreationException, OWLOntologyStorageException
{
// Get hold of an ontology manager
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
// Load an ontology from the Web. We load the ontology from a document IRI
IRI docIRI = IRI.create("http://www.w3.org/2009/08/skos-reference/skos.rdf");
OWLOntology skos = manager.loadOntologyFromOntologyDocument(docIRI);
System.out.println("Loaded ontology: " + skos);
System.out.println();
// Save a local copy of the ontology. (Specify a path appropriate to your setup)
File file = new File("e:/downloadAndSaveOWLFile.owl");
manager.saveOntology(skos, IRI.create(file.toURI()));
// Ontologies are saved in the format from which they were loaded.
// We can get information about the format of an ontology from its manager
OWLOntologyFormat format = manager.getOntologyFormat(skos);
System.out.println(" format: " + format);
System.out.println();
// Save the ontology in owl/xml format
OWLXMLOntologyFormat owlxmlFormat = new OWLXMLOntologyFormat();
// Some ontology formats support prefix names and prefix IRIs.
// In our case we loaded the pizza ontology from an rdf/xml format, which supports prefixes.
// When we save the ontology in the new format we will copy the prefixes over
// so that we have nicely abbreviated IRIs in the new ontology document
if(format.isPrefixOWLOntologyFormat())
{
owlxmlFormat.copyPrefixesFrom(format.asPrefixOWLOntologyFormat());
}
manager.saveOntology(skos, owlxmlFormat, IRI.create(file.toURI()));
// Dump an ontology to System.out by specifying a different OWLOntologyOutputTarget
// Note that we can write an ontology to a stream in a similar way
// using the StreamOutputTarget class
OWLOntologyDocumentTarget documentTarget = new SystemOutDocumentTarget();
// Try another format - The Manchester OWL Syntax
ManchesterOWLSyntaxOntologyFormat manSyntaxFormat = new ManchesterOWLSyntaxOntologyFormat();
if(format.isPrefixOWLOntologyFormat())
{
manSyntaxFormat.copyPrefixesFrom(format.asPrefixOWLOntologyFormat());
}
manager.saveOntology(skos, manSyntaxFormat, documentTarget);
}
EDIT: Update the code based on the suggestion below but only returns 1 object for rdfs:seeAlso.
public void getData() throws OWLOntologyCreationException
{
// Get hold of an ontology manager
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
// Load an ontology from the Web. We load the ontology from a document IRI
IRI docIRI = IRI.create("http://www.w3.org/2009/08/skos-reference/skos.rdf");
OWLOntology skos = manager.loadOntologyFromOntologyDocument(docIRI);
for (OWLAnnotation ann: skos.getAnnotations())
{
System.out.println("ann: " + ann.getProperty());
System.out.println();
}
}
The annotation you're looking for is an ontology annotation, meaning the IRI that is its subject is the ontology IRI itself. This is accessed differently from standard annotations.
OWLOntology o= ... // init the ontology as usual
for (OWLAnnotation ann: o.getAnnotations()){
if(ann.getProperty().equals(dataFactory.getRDFSLabel()){
// here you have found a rdfs:label annotation, so you can use the value for your purposes
}
}
Edit: Example of use
public static void main(String[] args) throws OWLOntologyCreationException {
OWLOntologyManager m = OWLManager.createOWLOntologyManager();
OWLOntology o = m.loadOntology(IRI
.create("http://www.w3.org/2009/08/skos-reference/skos.rdf"));
for (OWLAnnotation a : o.getAnnotations()) {
System.out.println("TestSkos.main() " + a);
}
}
Output:
TestSkos.main() Annotation(rdfs:seeAlso <http://www.w3.org/TR/skos-reference/>)
TestSkos.main() Annotation(<http://purl.org/dc/terms/creator> "Alistair Miles")
TestSkos.main() Annotation(<http://purl.org/dc/terms/description> "An RDF vocabulary for describing the basic structure and content of concept schemes such as thesauri, classification schemes, subject heading lists, taxonomies, 'folksonomies', other types of controlled vocabulary, and also concept schemes embedded in glossaries and terminologies."#en)
TestSkos.main() Annotation(<http://purl.org/dc/terms/contributor> "Participants in W3C's Semantic Web Deployment Working Group.")
TestSkos.main() Annotation(<http://purl.org/dc/terms/creator> "Sean Bechhofer")
TestSkos.main() Annotation(<http://purl.org/dc/terms/contributor> "Nikki Rogers")
TestSkos.main() Annotation(<http://purl.org/dc/terms/title> "SKOS Vocabulary"#en)
TestSkos.main() Annotation(<http://purl.org/dc/terms/contributor> "Dave Beckett")
I'm trying to write an OWLObjectPropertyExpression on OWL Ontology object. If I had an OWL Class I use something like the following:
OWLOntologyManager managerWriter = OWLManager.createOWLOntologyManager();
OWLOntology ontoWrite=managerWriter.createOntology();
OWLDataFactory factory = manager.getOWLDataFactory();
managerWriter.addAxiom(ontoWrite,factory.getOWLDeclarationAxiom(factory.getOWLClass((cl.getIRI()))));
But what should I write if I want to write an OWLObjectPropertyExpression ?
Thanks in advance !.
The following code snippet illustrate an example of usage and creation of an OWL expression using the OWL API (taken and adapted from there):
//OWL Expression we would like to create:
//in OWL Functional syntax: ObjectIntersectionOf(A ObjectSomeValuesFrom(R B))
//in Manchester syntax: A and R some B
PrefixManager pm = new DefaultPrefixManager("http://example.org/");
OWLClass A = factory.getOWLClass(":A", pm);
OWLObjectProperty R = factory.getOWLObjectProperty(":R", pm);
OWLClass B = factory.getOWLClass(":B", pm);
//The expression
OWLClassExpression expression =
factory.getOWLObjectIntersectionOf(A, factory.getOWLObjectSomeValuesFrom(R, B));
//Create a class in order to use the expression
OWLClass C = factory.getOWLClass(":C", pm);
// Declare an equivalentClass axiom
//Just there to show how an example on how to use the expression
OWLAxiom definition = factory.getOWLEquivalentClassesAxiom(C, expression);
manager.addAxiom(ontology, definition);