How do I read .owl files in java and display its contents?
Asked Answered
D

6

7

How do I read .owl files in java and display its contents?

Defecate answered 29/7, 2010 at 16:6 Comment(0)
A
5

The OWL API in source forge (http://owlapi.sourceforge.net/) has all the basic functions, although the documentation is barely enough. You may end up wasting time figuring out how do the complex functions not shown in the examples.

I would recommend going for the Protege API for OWL files. (http://protegewiki.stanford.edu/wiki/ProtegeOWL_API_Programmers_Guide/). This API has good documentation and the wiki is easy to navigate. OWL files are not easy to work around because of its semantic nature and building your own API might not be easy. Protege also has SWRL API if you want to process axioms and rules.

Absonant answered 31/1, 2012 at 13:36 Comment(0)
H
2

Use The OWL API.

Heretical answered 30/7, 2010 at 15:18 Comment(0)
A
1

What's the context? OWL is an ontology format read by http://protege.stanford.edu/.

Anglican answered 29/7, 2010 at 16:10 Comment(1)
i want to read .owl file in java and display its concepts and sub-conceptsDefecate
C
0

You have several options.

.owl files are text files, and you can display them that way.

.owl uses XML, so you can treat it like an XML document. Refer to http://www.w3.org/TR/owl-xmlsyntax/ and http://www.w3.org/TR/owl2-overview/ for a list of tags and what they represent.

Or you can use the OWL API. You can download it at: http://owlapi.sourceforge.net/index.html, and there are several examples at http://owlapi.sourceforge.net/documentation.html

Displaying and OWL ontology is somewhat challenging because the information you want to display can be highly linked, so its structure is that of a graph rather than sequential or tabular. It is possible for classes to be subclasses many other subclasses, and cyclical classification is possible. That is, A can be a subclass of B, which can be a subclass of C which can be a subclass of A.

Coaction answered 22/1, 2011 at 3:25 Comment(0)
A
0

Here is an example to parse an OWL ontology with the OWL API library:

import static org.semanticweb.owlapi.search.Searcher.annotations;
import static org.semanticweb.owlapi.vocab.OWLRDFVocabulary.RDFS_LABEL;

import java.util.ArrayList;
import java.util.List;

import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLAnnotation;
import org.semanticweb.owlapi.model.OWLAnnotationProperty;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLException;
import org.semanticweb.owlapi.model.OWLLiteral;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;

public class OwlParser {

    private final OWLOntology ontology;
    private OWLDataFactory df;

    public OwlParser(OWLOntology ontology, OWLDataFactory df) {
        this.ontology = ontology;
        this.df = df;
    }

    public void parseOntology()
            throws OWLOntologyCreationException {

        for (OWLClass cls : ontology.getClassesInSignature()) {
            String id = cls.getIRI().toString();
            String label = get(cls, RDFS_LABEL.toString()).get(0);
            System.out.println(label + " [" + id + "]");
        }
    }

    private List<String> get(OWLClass clazz, String property) {
        List<String> ret = new ArrayList<>();

        final OWLAnnotationProperty owlProperty = df
                .getOWLAnnotationProperty(IRI.create(property));
        for (OWLOntology o : ontology.getImportsClosure()) {
            for (OWLAnnotation annotation : annotations(
                    o.getAnnotationAssertionAxioms(clazz.getIRI()), owlProperty)) {
                if (annotation.getValue() instanceof OWLLiteral) {
                    OWLLiteral val = (OWLLiteral) annotation.getValue();
                    ret.add(val.getLiteral());
                }
            }
        }
        return ret;
    }

    public static void main(String[] args) throws OWLException,
            InstantiationException, IllegalAccessException,
            ClassNotFoundException {

        String x = "http://ontology.neuinfo.org/NIF/Dysfunction/NIF-Dysfunction.owl";

        IRI documentIRI = IRI.create(x);
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        OWLOntology ontology = manager
                .loadOntologyFromOntologyDocument(documentIRI);
        OwlParser parser = new OwlParser(ontology, manager.getOWLDataFactory());
        parser.parseOntology();
    }
}
Addend answered 22/4, 2015 at 14:9 Comment(0)
P
0

There is one more way using jena api in JAVA but you have to create SDB or TDB files for the given OWL file. Then you can query using SPARQL. JENA API

Pussyfoot answered 22/4, 2015 at 15:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.