我使用org.openrdf.rio. trigr . trigwriter来创建SPARQL更新查询。我的查询需要是以下格式:
PREFIX ....
PREFIX ....
insert data{
graph <....../id>{}
graph <....../id>{}
graph <....../id>{}
}
但是我现在得到的是:
PREFIX ....
PREFIX ....
insert data{
graph <....../id>
@prefix ...
@prefix ...
{....}
graph <....../id>
@prefix ...
@prefix ...
{....}
graph <....../id>
@prefix ...
@prefix ...
{....}
}
前缀与TriGWriter.startRDF()和TriGWriter.endRDF()调用有关。我想要抑制所有的@prefix行。我希望有一种干净的方法来做到这一点,而不是在他们已经在那里的时候把他们赶出去。我正在看TriGWriter.closeActiveContext()调用,但我没有得到好的结果。如有任何帮助,我将不胜感激。
下面是生成sparql的代码:def rdfWriter = new org.openrdf.rio.trig.TriGWriter(updateWriter);
rdfWriter.startRDF();
rdfWriter.handleNamespace("dc","http://purl.org/dc/terms/");
rdfWriter.handleNamespace("mrss","http://search.yahoo.com/mrss/");
rdfWriter.handleNamespace("xsd","http://www.w3.org/2001/XMLSchema#");
rdfWriter.handleNamespace("owl","http://www.w3.org/2002/07/owl#");
rdfWriter.handleNamespace("sesame","http://www.openrdf.org/schema/sesame#");
rdfWriter.endRDF();
updateWriter.getBuffer().setLength(resetLength);
it = map.entrySet().iterator();
while (it.hasNext())
{
Map.Entry pair = (Map.Entry)it.next();
rdfWriter.startRDF();
"""GRAPH <${camelContext.getRegistry().lookup('context')}content/${pair.getKey()}>""".writeTo(updateWriter);
def graph = pair.getValue();
graph.each{
rdfWriter.handleStatement(it);
}
rdfWriter.endRDF();
}
updateWriter.append("${lsp} }; ${lsp}");
TriGWriter真的不是为这种事情设计的。它编写TriG文档,而不是SPARQL更新语句,虽然它们表面上在语法方面相似,但并不相同(正如您所发现的)。我认为您最好推出自己的简单RDFHandler
实现,其中您实际上只需要实现handleStatement
。
然而,如果你坚持使用TriGWriter,你可以做的是创建一个TriGWriter的子类,在这个子类中你重写handleNamespace
方法,使它成为一个简单的无操作。这样,编写器将永远不会编写前缀映射,也永远不会使用前缀名称而不是IRIs。