如何在 java 中读取 .owl 文件并显示其内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3364588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:30:48  来源:igfitidea点击:

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

javasemantic-webowl

提问by nagender

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

如何.owl在java中读取文件并显示其内容?

回答by Guru

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.

source forge ( http://owlapi.sourceforge.net/) 中的 OWL API具有所有基本功能,尽管文档还不够。您最终可能会浪费时间弄清楚示例中未显示的复杂函数是如何工作的。

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.

我建议使用用于 OWL 文件的 Protege API。(http://protegewiki.stanford.edu/wiki/ProtegeOWL_API_Programmers_Guide/)。这个 API 有很好的文档,而且 wiki 很容易导航。OWL 文件由于其语义性质而不易处理,并且构建您自己的 API 可能并不容易。如果你想处理公理和规则,Protege 也有 SWRL API。

回答by Kaarel

Use The OWL API.

使用OWL API

回答by Sam Coles

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

上下文是什么?OWL 是一种本体格式,由http://protege.stanford.edu/读取。

回答by Jay Elston

You have several options.

您有多种选择。

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

.owl 文件是文本文件,您可以通过这种方式显示它们。

.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.

.owl 使用 XML,因此您可以将其视为 XML 文档。请参阅http://www.w3.org/TR/owl-xmlsyntax/http://www.w3.org/TR/owl2-overview/以获取标签列表及其代表的内容。

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

或者您可以使用 OWL API。你可以在这里下载:http://owlapi.sourceforge.net/index.html,有几个例子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.

显示和 OWL 本体有些挑战性,因为您要显示的信息可以高度链接,因此它的结构是图形而不是顺序或表格的结构。类可能是许多其他子类的子类,并且循环分类是可能的。也就是说,A可以是B的子类,B可以是C的子类,C可以是A的子类。

回答by Renaud

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

下面是一个使用 OWL API 库解析 OWL 本体的示例:

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();
    }
}

回答by Renaud

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

在 JAVA 中还有另一种使用 jena api 的方法,但您必须为给定的 OWL 文件创建 SDB 或 TDB 文件。然后您可以使用 SPARQL 进行查询。耶拿API