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).
Related
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")));
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();
InfModel infmodel = ModelFactory.createInfModel(reasoner, m);
Resource vegetarian = infmodel.getResource(source + "Vegetarian");
Resource margherita = infmodel.getResource(source + "Example-Margherita");
if (infmodel.contains(margherita, RDF., vegetarian)) {
System.out.println("Margherita is a memberOf Vegetarian pizza");
}
The example given above is formed by formal pizza.owl. In this owl, Example-Margherita is an individual of Margherita class. So, it is already written in owl file. However, the problem is that the reasoner should infer that margherita-example should be also an vegetarian pizza.
Could anyone please give an example that shows how to find an individual's possible inferred classes like in Protege ?(Protege correctly infers that Example-Margherita is a Vegetarian Pizza. However, I can't infer programmatically)
I solved my question. I think there was a problem with my ontology. Therefore, I created another ontology to infer individuals. The ontology that I created contains Person and subclasses of Person : MalePerson, FemalePerson and MarriedPerson. And, there are two object properties(hasSpouse, hasSibling) and one data type property(hasAge).
And, I created 3 individuals.
John - MalePerson - hasAge(20) - hasSibling(Jane)
Jane - FemalePerson - hasSibling(John) - hasSpouse(Bob)
Bob - MalePerson - hasSpouse(Jane)
And, I put two restrictions for MalePerson and FemalePerson classes.
For MalePerson :
hasSpouse max 1
hasSpouse only MalePerson
For FemalePerson :
hasSpouse max 1
hasSpouse only FemalePerson
Lastly, I made MarriedPerson to be a defined class. Before reasoning, MarriedPerson has no individual. However, the model should infer that Jane and Bob are married. Therefore, at the end, MarriedPerson class should have 2 individuals.
When I ran this code in Java using Jena, I got 2 inferred individuals.
OntModel ontModel = ModelFactory.createOntologyModel();
InputStream in = FileManager.get().open(inputFileName);
if (in == null) {
throw new IllegalArgumentException( "File: " + inputFileName + " not found");
}
ontModel.read(in, "");
Reasoner reasoner = ReasonerRegistry.getOWLReasoner();
reasoner = reasoner.bindSchema(ontModel);
// Obtain standard OWL-DL spec and attach the Pellet reasoner
OntModelSpec ontModelSpec = OntModelSpec.OWL_DL_MEM;
ontModelSpec.setReasoner(reasoner);
// Create ontology model with reasoner support
OntModel model = ModelFactory.createOntologyModel(ontModelSpec, ontModel);
// MarriedPerson has no asserted instances
// However, if an inference engine is used, two of the three
// individuals in the example presented here will be
// recognized as MarriedPersons
//ns is the uri
OntClass marPerson = model.getOntClass(ns + "OWLClass_00000003866036241880"); // this is the uri for MarriedPerson class
ExtendedIterator married = marPerson.listInstances();
while(married.hasNext()) {
OntResource mp = (OntResource)married.next();
System.out.println(mp.getURI());
} // this code returns 2 individuals with the help of reasoner