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

发布于 2024-09-12 04:42:44 字数 43 浏览 8 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

も星光 2024-09-19 04:42:44

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

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

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.

紫竹語嫣☆ 2024-09-19 04:42:44

背景是什么? OWL 是一种由 http://protege.stanford.edu/ 读取的本体格式。

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

梦归所梦 2024-09-19 04:42:44

您有多种选择。

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

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

或者您可以使用 OWL API。您可以在以下位置下载:http://owlapi.sourceforge.net/index.html,以及http://owlapi.sourceforge.net/documentation.html 有几个示例

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

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.

红尘作伴 2024-09-19 04:42:44

以下是使用 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();
    }
}

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();
    }
}
夏の忆 2024-09-19 04:42:44

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文