I have an ontology IRI without version. How to add or update version IRI of a particular OWL ontology ?
You need to create a change object that sets the ontology id.
So, if you have an ontology o with an IRI but no version IRI:
OWLOntology o = ...
IRI versionIRI=IRI.create("version");
SetOntologyID change=new SetOntologyID(o,
new OWLOntologyID(o.getOntologyID().getIR(), Optional.of(versionIRI)));
o.getOWLOntologyManager().applyChange(change);
Related
Suppose an OWL ontology contains the following SWRL rule:
has_part(?x, ?y) ^ participates(?x, ?z) -> participates(?y, ?z)
and following object property assertions:
o1 has_part o2
o1 participates p1
When I run the Hermit reasoner in Protege, I obtain the following inferred object property assertion:
o2 participates p1
However, when I load the same ontology with owlready2 and run its Hermit reasoner, I do not get any inferred object property assertion. For example:
from owlready2 import *
onto = get_ontology("test.owl").load()
sync_reasoner()
onto.o2.get_properties()
does not return any properties.
I did save the ontology in the RDFXML format and I can retrieve the SWRL rule with owlready2.
Does owlready2 reasoner not support this kind of inference?
Solution: sync_reasoner(infer_property_values = True)
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).
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)
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.
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();