OWLAPI: converting OWLOntology object into .owl files - owl

anyone could help me solve this task?
I need to get an .owl file (possibly specifying the syntax) from an OWLOntology object.
Thanks.
Edit: after using the code below, I got this exception:
"org.semanticweb.owlapi.model.OWLStorerNotFoundException: Could not find an ontology storer which can handle the format: OWL Functional Syntax". I have tried to search for a solution. So far I have found none.

Javadoc and/or examples are always a good source ...
OWLOntologyManager::saveOntology(OWLOntology ontology, OWLDocumentFormat ontologyFormat, OutputStream outputStream)
OWLOntologyManager manager = ...
...
OWLOntology ontology = ...
...
manager.saveOntology(ontology,
new RDFXMLDocumentFormat(),
new FileOutputStream(new File("/PATH/TO/FILE")));

Related

Apache Jena: If I have the model and the data in a single file generated by Protegé: how do I use the ModelFactory.createInfModel method

I just started using Apache Jena.
I am trying to use the Apache Jena reasoner on a .owl file generated by Protegé. I saved the file using the RDF / XML format. In that file is the schema and the individuals. I would like to make inferences. My problem is that I don't know how to create the inference model because in the examples I find, the schema is in a file and the data is in another file, that is, for example:
Model schema = RDFDataMgr.loadModel ("file: data / owlDemoSchema.owl");
Model data = RDFDataMgr.loadModel ("file: data / owlDemoData.rdf");
Reasoner reasoner = ReasonerRegistry.getOWLReasoner ();
reasoner = reasoner.bindSchema (schema);
InfModel infmodel = ModelFactory.createInfModel (reasoner, data);
If I have the model and the data in a single file generated by Protegé: how do I use the ModelFactory.createInfModel method?
Thanks in advance

OWL API: Traverse imported ontology

I'm trying to import an ontology to the primary ontology, and traverse over all classes:
manager = OWLManager.createOWLOntologyManager();
ontology = manager.loadOntologyFromOntologyDocument(new File("data/prim.owl"));
factory = manager.getOWLDataFactory();
OWLImportsDeclaration im = factory.getOWLImportsDeclaration(IRI.create("https://protege.stanford.edu/ontologies/pizza/pizza.owl"));
manager.applyChange(new AddImport(ontology,im));
reasoner = OpenlletReasonerFactory.getInstance().createReasoner(ontology);
I’m running this code to get all classes:
//*********************
Set<OWLClass> allCls = ontology.getClassesInSignature();
allCls.forEach(System.out::println);
Classes belonging to prim.owl are returned, but classes in the imported ontology (pizza.owl) are not returned.
The code in the question contains a mistake: it does not load the desired imported ontology (pizza) into the manager.
OWLImportsDeclaration im = factory.getOWLImportsDeclaration(IRI.create("https://protege.stanford.edu/ontologies/pizza/pizza.owl"));
manager.applyChange(new AddImport(ontology,im));
These lines just add the owl:imports declaration into the ontology header (_:x a owl:Ontology) for the pizza-iri.
To make the code work, you need to load the pizza-ontology separately:
OWLOntology pizza = manager.loadOntology(IRI.create("https://protege.stanford.edu/ontologies/pizza/pizza.owl"));
OWLImportsDeclaration im = factory.getOWLImportsDeclaration(pizza.getOntologyID().getOntologyIRI().orElseThrow(AssertionError::new));
manager.applyChange(new AddImport(ontology, im));
Now you can check that all imports and references are really present and correct, and, therefore, your ontology has a reference to the pizza ontology:
Assert.assertEquals(1, ontology.importsDeclarations().count());
Assert.assertEquals(1, ontology.imports().count());
Assert.assertEquals(2, manager.ontologies().count());
Then you can get all OWL-classes from both ontologies as a single collection or java-Stream:
ontology.classesInSignature(Imports.INCLUDED).forEach(System.err::println);
Also please note: the method Set<OWLClass> getClassesInSignature(boolean includeImportsClosure) is deprecated (in OWL-API v5).

Get an ontology IRI

When we wanna create or load an ontology we use this line of code IRI ontologyIRI = IRI.create("http://owl.man.ac.uk/2005/07/sssw/ontologyName"); So, what should I use to get it as an output?
I tried with this function IRI documentIRI = manager.getOntologyDocumentIRI(ontology); but it retunrs the location of the ontology file, something like this file:/Users/.../Desktop/ontologyname.owl.
Instead of it, I need the one written like this :
http://owl.man.ac.uk/2005/07/sssw/ontologyName
Please, If you have any ideas... Thank you
OWLOntology o = ...
IRI iri = o.getOWLOntologyID().getOntologyIRI().get();
This returns the IRI that the ontology is identified with; note - this might not be the same IRI that you loaded the ontology from. The resolvable IRI in your example might point at an ontology declaring a different IRI for itself (it is, in that sense, a document IRI).
With OWLAPI-5, you can get the ontology IRI using the method:
ont.getOntologyID().getOntologyIRI().get()
Here you are an example:
OWLOntologyManager man = OWLManager.createOWLOntologyManager();
OWLOntology ont = man.loadOntologyFromOntologyDocument(IRI.create("file:/Users/.../Desktop/ontologyname.owl"));
IRI yourIRI = ont.getOntologyID().getOntologyIRI().get();
String yourOntoURI = yourIRI.toString();
Example source from owlapi: owlapi-github-examples (line 1115)

How to set the Ontology ID of an anonymous Ontology using the OWL API

I have a file containing an ontology without an ontology id (the ontology tag <Ontology/> is empty). The used serialization format is RDF/XML. My goal is to serialize the file, set an ontology id and write the file back using the OWLAPI. Unfortunatly I don't know how to do this. I tried the following:
ontology = ontologyManager.loadOntologyFromOntologyDocument(new File("filename"));
ontologyManager.setOntologyDocumentIRI(ontology, IRI.create("http://www.mydesiredIri.com/abc"));
ontologyManager.saveOntology(ontology,new FileOutputStream(new File("outputfile")));
By running the code, the Ontology-ID is not added to the ontology. Instead of <Ontology rdf:about="http://www.mydesiredIri.com/abc"/> the tag is still emtpy. What I am doing wrong?
Thank you!
Kind regards
OWLOntologyManager.setOntologyDocumentIRI() is for setting the document IRI of the ontology, not the ontology IRI itself. The difference between the two is that the document IRI is a resolvable URL or a file path (i.e., int can be used to parse the ontology), while the ontology IRI is the symbolic name of the ontology (it does not need to be resolvable and it can even be missing - which is the case for anonymous ontologies).
To set the ontology IRI, use:
//versionIRI can be null
OWLOntologyID newOntologyID = new OWLOntologyID(ontologyIRI, versionIRI);
// Create the change that will set our version IRI
SetOntologyID setOntologyID = new SetOntologyID(ontology, newOntologyID);
// Apply the change
manager.applyChange(setOntologyID);
After this, save the ontology as usual.

How to retrieve values from OWL Ontology header values

Can someone help in how to fetch the header values from the below sample OWL header using JenaOWLModel or OWLOntology.
<owl:Ontology rdf:about="">
<owl:versionInfo>v 1.17 2003/02/26 12:56:51 mdean</owl:versionInfo>
<rdfs:comment>An example ontology</rdfs:comment>
<owl:imports rdf:resource="http://www.example.org/foo"/>
</owl:Ontology>
You can refer http://www.w3.org/TR/owl-ref/#Annotations (2.2. Ontology Headers) for more reference.
I tried using the below code to fetch the values, but not able to fetch the values. I can just see the keys. Any help would be appreciated.
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLDataFactory dataFactory = manager.getOWLDataFactory();
OWLOntology owlLOntology = manager.loadOntologyFromOntologyDocument(new File(dataPath));
Set owlAnnotationPropertySet = owlLOntology.getAnnotationPropertiesInSignature();
Iterator<OWLAnnotationProperty> iter = owlAnnotationPropertySet.iterator();while(iter.hasNext()){
OWLAnnotationProperty owlAnnotationProperty = (OWLAnnotationProperty) iter.next();
}
Thanks
Anurag
If you are using OWL API you can get the ontology IRI (the contents of the rdf:about tag) and version info by using
owlLOntology.getOntologyID().getOntologyIRI();
and
owlLOntology.getOntologyID().getVersionInfo();
For the annotations you can try
for (OWLAnnotation annontation: o.getAnnotations() {
annotation.getValue();
}
to get the values for each annotation property. As for imports try owlLOntology.getImports();

Resources