如何.owl
在 java 中读取文件并显示其内容?
6 回答
source forge ( http://owlapi.sourceforge.net/ ) 中的 OWL API具有所有基本功能,尽管文档还不够。您最终可能会浪费时间来弄清楚示例中未显示的复杂功能是如何实现的。
我建议使用 Protege API for OWL 文件。(http://protegewiki.stanford.edu/wiki/ProtegeOWL_API_Programmers_Guide/)。这个 API 有很好的文档并且 wiki 很容易浏览。OWL 文件因其语义特性而不易处理,而且构建自己的 API 可能并不容易。如果你想处理公理和规则,Protege 也有 SWRL API。
使用OWL API。
上下文是什么?OWL 是一种由http://protege.stanford.edu/读取的本体格式。
还有另一种在 JAVA 中使用 jena api 的方法,但您必须为给定的 OWL 文件创建 SDB 或 TDB 文件。然后您可以使用 SPARQL 进行查询。耶拿 API
下面是一个使用 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();
}
}
你有几个选择。
.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 的子类。