【发布时间】:2014-11-05 19:54:01
【问题描述】:
我正在尝试使用 Jena 提供的 API 以编程方式构建 SPARQL 查询。 为了简单起见,我想要这样的东西:
SELECT *
WHERE {
?typ rdfs:subClassOf+ myns:SomeType .
}
问题是rdfs:subClassOf+,它使用了属性路径。我尝试像这样构建查询:
query = new Query();
query.setQuerySelectType();
query.addResultVar("typ");
ElementTriplesBlock triplePattern = new ElementTriplesBlock();
Node typ = NodeFactory.createVariable("typ");
Node sometype = ...; // Assume OK
triplePattern.addTriple(new Triple(typ,
ResourceFactory.createProperty(RDFS.subClassOf.getURI() + "+").asNode(),
sometype));
query.setQueryPattern(triplePattern);
如果我没有属性路径,即在 SPARQL 中它只说 rdfs:subClassOf,我可以选择:
triplePattern.addTriple(new Triple(typ,
RDFS.subClassOf.asNode(),
sometype));
哪个有效。但是我无法在 URI 中指定路径修饰符来构建属性,因为我得到了:
Caused by: com.hp.hpl.jena.shared.InvalidPropertyURIException: http://www.w3.org/2000/01/rdf-schema#subClassOf+
at com.hp.hpl.jena.rdf.model.impl.PropertyImpl.checkLocalName(PropertyImpl.java:67)
at com.hp.hpl.jena.rdf.model.impl.PropertyImpl.<init>(PropertyImpl.java:56)
at com.hp.hpl.jena.rdf.model.ResourceFactory$Impl.createProperty(ResourceFactory.java:296)
at com.hp.hpl.jena.rdf.model.ResourceFactory.createProperty(ResourceFactory.java:144)
... (test class)
所以问题是,如何通过 Java API 在 Jena 中指定这样的查询。我知道我可以使用属性路径语法从字符串中进行查询,但不能在以编程方式构建查询时。
【问题讨论】: