作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
耶拿文档在这里 https://jena.apache.org/documentation/javadoc/jena/index.html声明 createOntologyModel 包含一个弱推理器,用于子类和子属性层次结构上的传递闭包,这就是我所寻找的。从一个简单的模型开始:
x:A rdf:type rdfs:Class .
x:A x:prop1 x:whatever .
x:B rdf:type rdfs:Class .
x:B rdfs:subClassOf x:A .
x:B x:prop2 x:other .
x:myInstance rdf:type x:B .
我寻求查询rdf:type x:B
并生成B
和父类(super class)A
的所有属性,例如
( ?all = <x:whatever> ) ( ?props = <x:prop1> ) -> ( ?type = <x:A> ) -> [Root]
( ?all = <x:A> ) ( ?props = <rdfs:subClassOf> ) -> ( ?type = <x:B> ) -> [Root]
( ?all = <x:other> ) ( ?props = <x:prop2> ) -> ( ?type = <x:B> ) -> [Root]
( ?all = <rdfs:Class> ) ( ?props = <rdf:type> ) -> ( ?type = <x:B> ) -> [Root]
我已经对这个例子进行了一些实验。它编译并运行,但仅生成 B 的属性,并且不遍历 subClassOf
树。我相信我错过了 RDF 模式的基本设置或使用,使推理机能够完成其任务。有什么线索吗?
public class jena2 {
private static void addRaw(OntModel m, String s, String p, String o) {
m.add(ResourceFactory.createStatement(
new ResourceImpl(s),new PropertyImpl(p),new ResourceImpl(o))
);
}
public static void main(String[] args) {
OntModel model = ModelFactory.createOntologyModel();
addRaw(model, "x:A", "rdf:type", "rdfs:Class");
addRaw(model, "x:A", "x:prop1", "x:whatever");
addRaw(model, "x:B", "rdf:type", "rdfs:Class");
addRaw(model, "x:B", "x:prop2", "x:other");
addRaw(model, "x:B", "rdfs:subClassOf", "x:A");
addRaw(model, "x:widget", "rdf:type", "x:B");
StringBuffer sb = new StringBuffer();
sb.append("PREFIX x: <x:>");
sb.append("PREFIX rdf: <rdf:>");
sb.append("PREFIX rdfs: <rdfs:>");
sb.append("SELECT *");
sb.append("WHERE {");
sb.append(" x:widget rdf:type ?type .");
sb.append(" ?type ?props ?all .");
sb.append("}");
Query query = QueryFactory.create(sb.toString());
try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
ResultSet results = qexec.execSelect() ;
for ( ; results.hasNext() ; ) {
QuerySolution soln = results.nextSolution() ;
System.out.println(soln);
}
} catch(Exception e) {
System.out.println("epic fail: " + e);
}
}
最佳答案
(这不是完整的答案!)
根据上面的评论,一些工作代码:
import org.apache.jena.ontology.OntModel;
import org.apache.jena.query.*;
import org.apache.jena.rdf.model.*;
import org.apache.jena.vocabulary.RDF;
import org.apache.jena.vocabulary.RDFS;
public class jena2 {
private static void addRaw(OntModel m, Resource s, Property p, Resource o) {
m.add(ResourceFactory.createStatement(s, p, o));
}
public static void main(String[] args) {
OntModel model = ModelFactory.createOntologyModel();
Resource A = ResourceFactory.createResource("x:A");
Resource B = ResourceFactory.createResource("x:B");
Property prop1 = ResourceFactory.createProperty("x:prop1");
Property prop2 = ResourceFactory.createProperty("x:prop2");
Resource whatever = ResourceFactory.createResource("x:whatever");
Resource other = ResourceFactory.createResource("x:other");
Resource widget = ResourceFactory.createResource("x:widget");
addRaw(model, A, RDF.type, RDFS.Class);
addRaw(model, A, prop1, whatever);
addRaw(model, B, RDF.type, RDFS.Class);
addRaw(model, B, prop2, other);
addRaw(model, B, RDFS.subClassOf, A);
addRaw(model, widget, RDF.type, B);
StringBuffer sb = new StringBuffer();
sb.append("PREFIX x: <x:>");
sb.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>");
sb.append("SELECT * {");
sb.append(" x:widget rdf:type ?type .");
sb.append(" ?type ?props ?all .");
sb.append("}");
Query query = QueryFactory.create(sb.toString());
try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
ResultSet results = qexec.execSelect();
for (; results.hasNext(); ) {
QuerySolution soln = results.nextSolution();
System.out.println(soln);
}
} catch (Exception e) {
System.out.println("epic fail: " + e);
}
}
}
输出:
( ?all = <x:A> ) ( ?props = rdfs:subClassOf ) -> ( ?type = <x:B> ) -> [Root]
( ?all = <x:other> ) ( ?props = <x:prop2> ) -> ( ?type = <x:B> ) -> [Root]
( ?all = rdfs:Class ) ( ?props = rdf:type ) -> ( ?type = <x:B> ) -> [Root]
( ?all = <x:B> ) ( ?props = rdfs:subClassOf ) -> ( ?type = <x:B> ) -> [Root]
( ?all = rdfs:Resource ) ( ?props = rdfs:subClassOf ) -> ( ?type = <x:B> ) -> [Root]
( ?all = rdfs:Resource ) ( ?props = rdf:type ) -> ( ?type = <x:B> ) -> [Root]
( ?all = rdfs:Class ) ( ?props = rdf:type ) -> ( ?type = rdfs:Resource ) -> [Root]
( ?all = rdfs:Resource ) ( ?props = rdfs:subClassOf ) -> ( ?type = rdfs:Resource ) -> [Root]
( ?all = rdfs:Resource ) ( ?props = rdf:type ) -> ( ?type = rdfs:Resource ) -> [Root]
( ?all = rdfs:Resource ) ( ?props = rdfs:subClassOf ) -> ( ?type = <x:A> ) -> [Root]
( ?all = <x:whatever> ) ( ?props = <x:prop1> ) -> ( ?type = <x:A> ) -> [Root]
( ?all = rdfs:Class ) ( ?props = rdf:type ) -> ( ?type = <x:A> ) -> [Root]
( ?all = <x:A> ) ( ?props = rdfs:subClassOf ) -> ( ?type = <x:A> ) -> [Root]
( ?all = rdfs:Resource ) ( ?props = rdf:type ) -> ( ?type = <x:A> ) -> [Root]
它还返回 RDFS 类 A
的属性
关于java - 在耶拿使用 ARQ (SPARQL) 进行 RDFS 推理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46492275/
另请参阅:Is this a good substr() for C? strtok() 和 friend 跳过空字段,我不知道如何告诉它在这种情况下不要跳过而是返回空。 我能看到的大多数分词器都有
这个问题在这里已经有了答案: What is a NullPointerException, and how do I fix it? (12 个答案) 关闭 6 年前。 编辑:排序。解决方案见底部
我是一名优秀的程序员,十分优秀!