我正在玩芝麻的 queryparser-sparql 库,但我似乎无法OPTIONAL
从解析的查询中获取语句。
给定查询:
PREFIX ex: <http://example.com/#>
SELECT * WHERE {
?s a ex:Foo .
OPTIONAL { ?s ex:someProperty ?property }
} LIMIT 10
使用以下代码解析它(使用 Sesame 2.7.2):
SPARQLParserFactory factory = new SPARQLParserFactory();
QueryParser parser = factory.getParser();
ParsedQuery parsedQuery = parser.parseQuery(sparqlQuery, null);
StatementPatternCollector collector = new StatementPatternCollector();
TupleExpr tupleExpr = parsedQuery.getTupleExpr();
tupleExpr.visit(collector);
for (StatementPattern pattern : collector.getStatementPatterns()) {
System.out.println(pattern);
}
印刷parsedQuery
给出:
Slice ( limit=10 )
Projection
ProjectionElemList
ProjectionElem "s"
ProjectionElem "property"
LeftJoin
StatementPattern
Var (name=s)
Var (name=-const-1, value=http://www.w3.org/1999/02/22-rdf-syntax-ns#type, anonymous)
Var (name=-const-2, value=http://example.com/#Foo, anonymous)
StatementPattern
Var (name=s)
Var (name=-const-3, value=http://example.com/#someProperty, anonymous)
Var (name=property)
打印每个pattern
给出:
StatementPattern
Var (name=s)
Var (name=-const-1, value=http://www.w3.org/1999/02/22-rdf-syntax-ns#type, anonymous)
Var (name=-const-2, value=http://example.com/#Foo, anonymous)
StatementPattern
Var (name=s)
Var (name=-const-3, value=http://example.com/#someProperty, anonymous)
Var (name=property)
如何从 a 获取有关StatementPattern
它是否是的信息OPTIONAL
?