简体   繁体   English

解析 SPARQL 的部分并在 RDF4j SparqlBuilder 中重用它们

[英]Parse portions of SPARQL and reuse them in RDF4j SparqlBuilder

I use some configuration logic to generate Sparql queries with RDF4j and the SparqlBuilder.我使用一些配置逻辑通过 RDF4j 和 SparqlBuilder 生成 Sparql 查询。

// prepare selectVariables, prefixes and whereCondition according to configuration

SelectQuery mainQuery = Queries.SELECT(selectVariables)
            .prefix(prefixes)
            .where(whereCondition)

Now I wish to allow for users to configure custom WHERE conditions to be used as SubSelects and composed with the rest of the query logic.现在我希望允许用户配置自定义 WHERE 条件以用作 SubSelects 并与查询逻辑的 rest 组合。

Since the configuration is YAML and the users are trained in Sparql, I wished to let users specify custom patterns as YAML multiline strings like this example由于配置是 YAML 并且用户在 Sparql 中接受过培训,我希望让用户将自定义模式指定为 YAML 多行字符串,例如这个例子

customQuery: |
  ?_ wdt:P31 wd:Q5;       
     wdt:P19/wdt:P131* wd:Q60.

This way I can let the users customize freely the different queries that I will generate based on the configured condition.这样我可以让用户根据配置的条件自由定制我将生成的不同查询。

The problem问题

I already managed to parse the query fragment using RDFj SparqlParser:我已经设法使用 RDFj SparqlParser 解析查询片段:

SPARQLParserFactory PARSER_FACTORY = new SPARQLParserFactory();
QueryParser parser = PARSER_FACTORY.getParser();
ParsedQuery parsed = parser.parseQuery(query, null);
ProjectionVisitor projectionVisitor = new ProjectionVisitor();
parsed.getTupleExpr().visit(projectionVisitor);

TupleExpr parsedExpression = projectionVisitor.getProjectionArg();

but I can't use the parsedExpression into the SparqlBuilder methods, the nodes representation for the parser looks incompatible with the ones for the fluent builder.但我不能在 SparqlBuilder 方法中使用parsedExpression ,解析器的节点表示看起来与流利的构建器的节点表示不兼容。

Is there any way to use parsed expressions inside the SparqlBuilder?有没有办法在 SparqlBuilder 中使用已解析的表达式?

No, it is not possible to use parsed expressions in the SparqlBuilder.不,不能在 SparqlBuilder 中使用已解析的表达式。 What you could probably do instead though (freewheeling here) is use the SparqlBuilder to generate a query with a placeholder pattern of some sort, parse that, and then use a parse tree visitor to find that placeholder pattern and replace it with the custom parsed expression you got from the user.不过,您可能做的(在这里随心所欲)是使用 SparqlBuilder 生成具有某种占位符模式的查询,对其进行解析,然后使用解析树访问者找到该占位符模式并将其替换为自定义解析表达式你从用户那里得到的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM