当我执行插入查询 RDF4J 时如何获取 UpdateExpr
How get UpdateExpr when i execute insert query RDF4J
插入查询有问题,无法解析简单语句
示例:
查询:
PREFIX dc: <http://purl.org/dc/elements/1.1/>
INSERT DATA
{
<http://example/book1> dc:title "A new book";
dc:creator "A.N.Other" .
}
结果:
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX sesame: <http://www.openrdf.org/schema/sesame#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX fn: <http://www.w3.org/2005/xpath-functions#>
<http://example/book1> dc:title "A new book" ; dc:creator "A.N.Other" .
但如果我解析了一些更难的东西,一切都很好。
查询:
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
INSERT
{ GRAPH <http://example/bookStore2> { ?book ?p ?v } }
WHERE
{ GRAPH <http://example/bookStore>
{ ?book dc:date ?date .
FILTER ( ?date > "1970-01-01T00:00:00-02:00"^^xsd:dateTime )
?book ?p ?v
} }
结果:
StatementPattern FROM NAMED CONTEXT
Var (name=book)
Var (name=p)
Var (name=v)
Var (name=_const_f1165b11_uri, value=http://example/bookStore2, anonymous)
Modify
StatementPattern FROM NAMED CONTEXT
Var (name=book)
Var (name=p)
Var (name=v)
Var (name=_const_f1165b11_uri, value=http://example/bookStore2, anonymous)
Filter
Compare (>)
Var (name=date)
ValueConstant (value="1970-01-01T00:00:00-02:00"^^<http://www.w3.org/2001/XMLSchema#dateTime>)
Join
StatementPattern FROM NAMED CONTEXT
Var (name=book)
Var (name=_const_9b277d39_uri, value=http://purl.org/dc/elements/1.1/date, anonymous)
Var (name=date)
Var (name=_const_39534d41_uri, value=http://example/bookStore, anonymous)
StatementPattern FROM NAMED CONTEXT
Var (name=book)
Var (name=p)
Var (name=v)
Var (name=_const_39534d41_uri, value=http://example/bookStore, anonymous)
我需要在第一个案例中得到类似的东西。我做错了什么?
Java 执行这些查询的代码:
String query = ...
SPARQLParser parser = new SPARQLParser();
ParsedUpdate q2 = parser.parseUpdate(query, null);
Iterator var2 = q2.getUpdateExprs().iterator();
UpdateExpr updateExpr = (UpdateExpr)var2.next();
System.out.println(updateExpr);
如评论中所示,INSERT DATA
SPARQL 更新的数据块在内部存储为(未解析的)字符串。如果你想进一步处理它,你有几个选择。
一个选项是实际上只执行更新,例如针对 temporary/empty 内存存储,然后只从该存储中检索语句:
String update = "INSERT DATA { .... }";
Repository tempRep = new SailRepository(new MemoryStore());
tempRep.init();
try(RepositoryConnection conn = tempRep.getConnection()) {
conn.prepareUpdate(update).execute());
Model statements = QueryResults.asModel(conn.getStatements(null, null, null));
}
另一种可能更具可扩展性的选择是将数据块提取为字符串并将其传递给解析器。 SPARQLUpdateDataBlockParser
专门为此目的而存在:
InsertData insertDataExpr = (InsertData)updateExpr;
RDFParser parser = new SPARQLUpdateDataBlockParser();
StatementCollector handler = new StatementCollector();
parser.setRDFHandler(handler);
parser.parse(new StringReader(insertDataExpr.getDataBlock()), "");
Collection<Statement> stmts = handler.getStatements();
插入查询有问题,无法解析简单语句
示例: 查询:
PREFIX dc: <http://purl.org/dc/elements/1.1/>
INSERT DATA
{
<http://example/book1> dc:title "A new book";
dc:creator "A.N.Other" .
}
结果:
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX sesame: <http://www.openrdf.org/schema/sesame#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX fn: <http://www.w3.org/2005/xpath-functions#>
<http://example/book1> dc:title "A new book" ; dc:creator "A.N.Other" .
但如果我解析了一些更难的东西,一切都很好。
查询:
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
INSERT
{ GRAPH <http://example/bookStore2> { ?book ?p ?v } }
WHERE
{ GRAPH <http://example/bookStore>
{ ?book dc:date ?date .
FILTER ( ?date > "1970-01-01T00:00:00-02:00"^^xsd:dateTime )
?book ?p ?v
} }
结果:
StatementPattern FROM NAMED CONTEXT
Var (name=book)
Var (name=p)
Var (name=v)
Var (name=_const_f1165b11_uri, value=http://example/bookStore2, anonymous)
Modify
StatementPattern FROM NAMED CONTEXT
Var (name=book)
Var (name=p)
Var (name=v)
Var (name=_const_f1165b11_uri, value=http://example/bookStore2, anonymous)
Filter
Compare (>)
Var (name=date)
ValueConstant (value="1970-01-01T00:00:00-02:00"^^<http://www.w3.org/2001/XMLSchema#dateTime>)
Join
StatementPattern FROM NAMED CONTEXT
Var (name=book)
Var (name=_const_9b277d39_uri, value=http://purl.org/dc/elements/1.1/date, anonymous)
Var (name=date)
Var (name=_const_39534d41_uri, value=http://example/bookStore, anonymous)
StatementPattern FROM NAMED CONTEXT
Var (name=book)
Var (name=p)
Var (name=v)
Var (name=_const_39534d41_uri, value=http://example/bookStore, anonymous)
我需要在第一个案例中得到类似的东西。我做错了什么?
Java 执行这些查询的代码:
String query = ...
SPARQLParser parser = new SPARQLParser();
ParsedUpdate q2 = parser.parseUpdate(query, null);
Iterator var2 = q2.getUpdateExprs().iterator();
UpdateExpr updateExpr = (UpdateExpr)var2.next();
System.out.println(updateExpr);
如评论中所示,INSERT DATA
SPARQL 更新的数据块在内部存储为(未解析的)字符串。如果你想进一步处理它,你有几个选择。
一个选项是实际上只执行更新,例如针对 temporary/empty 内存存储,然后只从该存储中检索语句:
String update = "INSERT DATA { .... }";
Repository tempRep = new SailRepository(new MemoryStore());
tempRep.init();
try(RepositoryConnection conn = tempRep.getConnection()) {
conn.prepareUpdate(update).execute());
Model statements = QueryResults.asModel(conn.getStatements(null, null, null));
}
另一种可能更具可扩展性的选择是将数据块提取为字符串并将其传递给解析器。 SPARQLUpdateDataBlockParser
专门为此目的而存在:
InsertData insertDataExpr = (InsertData)updateExpr;
RDFParser parser = new SPARQLUpdateDataBlockParser();
StatementCollector handler = new StatementCollector();
parser.setRDFHandler(handler);
parser.parse(new StringReader(insertDataExpr.getDataBlock()), "");
Collection<Statement> stmts = handler.getStatements();