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

7

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

6个回答

5
OWL API在source forge (http://owlapi.sourceforge.net/)中具备所有基本功能,但文档仅仅够用。如果你需要使用示例中未显示的复杂功能,可能会浪费时间去弄清楚如何操作。
我建议选择Protege API来处理OWL文件(http://protegewiki.stanford.edu/wiki/ProtegeOWL_API_Programmers_Guide)。该API有良好的文档,并且维基易于查阅。由于其语义性质,OWL文件并不容易处理,而构建自己的API可能并不容易。如果需要处理公理和规则,则Protege还具有SWRL API。

2

1

我想在Java中读取.owl文件并显示其概念和子概念。 - nagender

0

这里是使用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();
    }
}

0

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


0

您有几个选项。

.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的子类。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接