Java 如何在Jena';什么是SPARQLAPI?

Java 如何在Jena';什么是SPARQLAPI?,java,sparql,rdf,jena,arq,Java,Sparql,Rdf,Jena,Arq,我希望避免将SPARQL查询作为字符串传递。因此,我使用Jena的API创建查询。现在我的查询中需要一个PropertyPath,但是我找不到任何支持这个的Java类。你能给我一个提示吗 下面是我想插入的一些示例代码(Jena 3.0.1): 可以使用PathFactory创建属性路径 考虑下图: @prefix dc: <http://purl.org/dc/elements/1.1/>. @prefix ex: <http://example.com/>.

我希望避免将SPARQL查询作为字符串传递。因此,我使用Jena的API创建查询。现在我的查询中需要一个PropertyPath,但是我找不到任何支持这个的Java类。你能给我一个提示吗

下面是我想插入的一些示例代码(Jena 3.0.1):


可以使用PathFactory创建属性路径

考虑下图:

@prefix dc: <http://purl.org/dc/elements/1.1/>.
@prefix ex: <http://example.com/>.

    ex:Manager ex:homeOffice ex:HomeOffice
    ex:HomeOffice dc:title "Home Office Title"
?x ex:homeOffice/dc:title ?title
 //create the path
Path exhomeOffice = PathFactory.pathLink(NodeFactory.createURI("http://example.com/homeOffice"));
 Path dcTitle = PathFactory.pathLink(NodeFactory.createURI("http://purl.org/dc/elements/1.1/title"));
Path fullPath = PathFactory.pathSeq(exhomeOffice,dcTitle);
TriplePath t = new TriplePath(Var.alloc("x"),fullPath,Var.alloc("title"));
下面的代码实现了它:

@prefix dc: <http://purl.org/dc/elements/1.1/>.
@prefix ex: <http://example.com/>.

    ex:Manager ex:homeOffice ex:HomeOffice
    ex:HomeOffice dc:title "Home Office Title"
?x ex:homeOffice/dc:title ?title
 //create the path
Path exhomeOffice = PathFactory.pathLink(NodeFactory.createURI("http://example.com/homeOffice"));
 Path dcTitle = PathFactory.pathLink(NodeFactory.createURI("http://purl.org/dc/elements/1.1/title"));
Path fullPath = PathFactory.pathSeq(exhomeOffice,dcTitle);
TriplePath t = new TriplePath(Var.alloc("x"),fullPath,Var.alloc("title"));

可以使用PathFactory创建属性路径

考虑下图:

@prefix dc: <http://purl.org/dc/elements/1.1/>.
@prefix ex: <http://example.com/>.

    ex:Manager ex:homeOffice ex:HomeOffice
    ex:HomeOffice dc:title "Home Office Title"
?x ex:homeOffice/dc:title ?title
 //create the path
Path exhomeOffice = PathFactory.pathLink(NodeFactory.createURI("http://example.com/homeOffice"));
 Path dcTitle = PathFactory.pathLink(NodeFactory.createURI("http://purl.org/dc/elements/1.1/title"));
Path fullPath = PathFactory.pathSeq(exhomeOffice,dcTitle);
TriplePath t = new TriplePath(Var.alloc("x"),fullPath,Var.alloc("title"));
下面的代码实现了它:

@prefix dc: <http://purl.org/dc/elements/1.1/>.
@prefix ex: <http://example.com/>.

    ex:Manager ex:homeOffice ex:HomeOffice
    ex:HomeOffice dc:title "Home Office Title"
?x ex:homeOffice/dc:title ?title
 //create the path
Path exhomeOffice = PathFactory.pathLink(NodeFactory.createURI("http://example.com/homeOffice"));
 Path dcTitle = PathFactory.pathLink(NodeFactory.createURI("http://purl.org/dc/elements/1.1/title"));
Path fullPath = PathFactory.pathSeq(exhomeOffice,dcTitle);
TriplePath t = new TriplePath(Var.alloc("x"),fullPath,Var.alloc("title"));

只是一个问题,更重要的是为了代码的清晰性:将查询作为字符串编写一次,对其进行解析,然后将结果查询对象传递给其他对象,是否会更容易、更清晰?您仍然可以传递对象,而不是字符串,但将来调试和修改似乎要容易得多。@Joshua Taylor,是的,我认为这是可能的。但是由于propertyPath是我唯一的变量输入,我必须将它插入到字符串中,这显然是可能的。我正在寻找一种更干净的解决方案。与此同时,我发现我可能应该使用这段代码来解析PropertyPath:
Path p=SSE.parsePath(“dct:creator/gndo:preferredNameForThePerson”,PMAP);addTriplePath(新的TriplePath(…,parsedPropertyPath,…)
但是SSE.parsePath此时遇到了一个异常。提问和回答:摘要:使用
PathParser
。谢谢你的帮助,Andy。如果你在这里发布你的解决方案,我可以接受这是一个答案,并希望明天发布一个固定的代码示例。只是一个问题,对于代码的清晰性来说比任何其他问题都重要:这可能更简单、更简单吗将查询作为字符串编写一次,对其进行解析,然后将结果查询对象传递给其他人会更清楚吗?您仍然可以传递对象,而不是字符串,但看起来以后调试和修改要容易得多。@Joshua Taylor,是的,我认为这是可能的。但是由于propertyPath是我唯一的变量输入,我会我想把它插入到字符串中,这显然是可能的。我正在寻找一个更干净的解决方案。与此同时,我发现我可能应该使用这段代码来解析属性路径:
Path p=SSE.parsePath(“dct:creator/gndo:preferredNameForThePerson”,PMAP);triplesBlock.addTriplePath(new TriplePath(…,parsedPropertyPath,…)
但是SSE.parsePath此时遇到了一个异常。询问和回答:摘要:使用
PathParser
。谢谢你的帮助,Andy。如果你在这里发布你的解决方案,我可以接受这是一个答案,希望明天发布一个固定的代码示例。