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);
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 new to OWL API hence I am facing some issues for retrieving data.
Suppose I have the following data:
<rdf:Description rdf:about="http://purl.obolibrary.org/obo/GO_0044297">
<oboInOwl:creation_date>"2010-02-05T10:37:16Z"</oboInOwl:creation_date>
<obo:IAO_0000115>"The portion of a cell bearing surface projections such as axons, dendrites, cilia, or flagella that includes the nucleus, but excludes all cell projections."</obo:IAO_0000115>
<oboInOwl:hasOBONamespace>"cellular_component"</oboInOwl:hasOBONamespace>
<oboInOwl:hasDbXref>"Wikipedia:Cell_body"</oboInOwl:hasDbXref>
<oboInOwl:hasDbXref>"FMA:67301"</oboInOwl:hasDbXref>
<oboInOwl:hasExactSynonym>"cell soma"</oboInOwl:hasExactSynonym>
<rdfs:label>"cell body"</rdfs:label>
<rdfs:subClassOf>http://purl.obolibrary.org/obo/GO_0044464</rdfs:subClassOf>
<oboInOwl:hasDbXref>"FBbt:00005107"</oboInOwl:hasDbXref>
<rdf:type>http://www.w3.org/2002/07/owl#Class</rdf:type>
<oboInOwl:id>"GO:0044297"</oboInOwl:id>
<rdfs:comment>"Note that 'cell body' and 'cell soma' are not used in the literature for cells that lack projections, nor for some cells (e.g. yeast with mating projections) that do have projections."</rdfs:comment>
<oboInOwl:created_by>"xyz"</oboInOwl:created_by>
<oboInOwl:inSubset>http://purl.obolibrary.org/obo/go#goslim_pir</oboInOwl:inSubset>
</rdf:Description>
<rdf:Description rdf:about="http://purl.obolibrary.org/obo/GO_0071509">
<oboInOwl:hasRelatedSynonym>"activation of MAPKK activity involved in mating response"</oboInOwl:hasRelatedSynonym>
<rdfs:subClassOf>http://purl.obolibrary.org/obo/GO_0090028</rdfs:subClassOf>
<oboInOwl:hasOBONamespace>"biological_process"</oboInOwl:hasOBONamespace>
<oboInOwl:hasExactSynonym>"activation of MAP kinase kinase activity during conjugation with cellular fusion"</oboInOwl:hasExactSynonym>
<oboInOwl:hasExactSynonym>"conjugation with cellular fusion, activation of MAPKK activity"</oboInOwl:hasExactSynonym>
<rdfs:label>"activation of MAPKK activity involved in conjugation with cellular fusion"</rdfs:label>
<rdf:type>http://www.w3.org/2002/07/owl#Class</rdf:type>
<oboInOwl:id>"GO:0071509"</oboInOwl:id>
<oboInOwl:creation_date>"2010-01-05T02:09:58Z"</oboInOwl:creation_date>
<oboInOwl:hasExactSynonym>"conjugation with cellular fusion, activation of MAP kinase kinase activity"</oboInOwl:hasExactSynonym>
<oboInOwl:created_by>"midori"</oboInOwl:created_by>
<obo:IAO_0000115>"Any process that initiates the activity of the inactive enzyme MAP kinase kinase in the context of conjugation with cellular fusion."</obo:IAO_0000115>
<rdfs:subClassOf>http://purl.obolibrary.org/obo/GO_0000186</rdfs:subClassOf>
</rdf:Description>
For each of the "rdf:description" I want to retrieve its corresponding "rdf:label", "oboInOwl:hasExactSynonym" and "rdfs:subClassOf" using OWL API in java.
So far I can get all labels but not the linkages as to which label is for which description.
Currently my code looks like:
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLOntology pizzaOntology = manager.loadOntologyFromOntologyDocument(f);
Set<OWLOntology> allOntologies = manager.getImportsClosure(pizzaOntology);
//System.out.println(allOntologies);
OWLReasonerFactory reasonerFactory = new ElkReasonerFactory();
OWLReasoner reasoner = reasonerFactory.createReasoner(pizzaOntology);
//pizzaOntology
OWLDataFactory factory = manager.getOWLDataFactory();
Set<OWLAxiom> axiom = pizzaOntology.getAxioms();
for (OWLAxiom o : axiom) {
AxiomType<?> at = o.getAxiomType();
//System.out.println("Annotation type is "+at+" for "+o);
if (at == AxiomType.ANNOTATION_ASSERTION) {
OWLAnnotationAssertionAxiom ax = (OWLAnnotationAssertionAxiom) o;
//Check if the axiom is a label and write to file
if(ax.getProperty().toString().contains("hasExactSynonym"))
System.out.println("Data is "+ax.getValue().toString());
if (ax.getProperty().equals(factory.getRDFSLabel())) {
String label = ax.getValue().toString();
label = label.toLowerCase();
label = label.replaceAll("[^\\p{L}\\p{Nd}]+", " ");
allLabels.add(label);
}
}
}
Can someone help me with some ideas about this?
This should help:
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLOntology pizzaOntology = manager.loadOntologyFromOntologyDocument(f);
Set<OWLOntology> allOntologies = manager.getImportsClosure(pizzaOntology);
// System.out.println(allOntologies);
OWLReasonerFactory reasonerFactory = new ElkReasonerFactory();
OWLReasoner reasoner = reasonerFactory.createReasoner(pizzaOntology);
// pizzaOntology
OWLDataFactory factory = manager.getOWLDataFactory();
Map<OWLAnnotationSubject, String> allLabels = new HashMap<>();
Map<OWLAnnotationSubject, String> allExactSynonyms = new HashMap<>();
for (OWLAnnotationAssertionAxiom ax : pizzaOntology
.getAxioms(AxiomType.ANNOTATION_ASSERTION)) {
// Check if the axiom is a label and write to file
OWLAnnotationSubject subject = ax.getSubject();
if (ax.getProperty().toString().contains("hasExactSynonym")) {
allExactSynonyms.put(subject, ax.getValue().toString());
}
if (ax.getProperty().equals(factory.getRDFSLabel())) {
String label = ax.getValue().toString();
label = label.toLowerCase();
label = label.replaceAll("[^\\p{L}\\p{Nd}]+", " ");
allLabels.put(subject, label);
}
}
The two maps hold the relation between an IRI (the annotation subject in this case is always an IRI - annotation are attached to IRIs of classes, not to classes themselves) and the value of a property. If you match the label value with the value of a property for an IRI, you can find the other value through the IRI in the map.
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.
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);