解析 SPARQL 查询
我需要测试几百万个 SPARQL 查询的特定结构属性,为此我需要 WHERE
语句的结构。我目前正在尝试使用 fyzz 来执行此操作,但不幸的是它的文档不是很有用。解析查询很容易,问题是我无法恢复语句的结构。例如:
>>> from fyzz import parse
>>> a=parse("SELECT * WHERE {?x a ?y . {?x a ?z}}")
>>> b=parse("SELECT * WHERE {?x a ?y OPTIONAL {?x a ?z}}")
>>> a.where==b.where
True
>>> a.where
[(SparqlVar('x'), ('', 'a'), SparqlVar('y')), (SparqlVar('x'), ('', 'a'), SparqlVar('y'))]
有没有办法恢复 fyzz 中的实际解析树而不仅仅是三元组,或者其他一些工具可以让我做到这一点? RDFLib 过去似乎有一个 bison SPARQL 解析器,但我在 rdflib 或 rdfextras.sparql 包中找不到它。
谢谢
I need to test for a certain structural property of a couple million SPARQL queries, and for that I need the structure of the WHERE
statement. I'm currently trying to use fyzz to do this, but unfortunately its documentation is not very useful. Parsing queries is easy, the problem is that i haven't been able to recover the structure of the statement. For example:
>>> from fyzz import parse
>>> a=parse("SELECT * WHERE {?x a ?y . {?x a ?z}}")
>>> b=parse("SELECT * WHERE {?x a ?y OPTIONAL {?x a ?z}}")
>>> a.where==b.where
True
>>> a.where
[(SparqlVar('x'), ('', 'a'), SparqlVar('y')), (SparqlVar('x'), ('', 'a'), SparqlVar('y'))]
Is there a way to recover the actual parse tree in fyzz instead of just the triples, or some other tool which would let me do this? RDFLib seems to have had a bison SPARQL parser in the past, but I can't find it in the rdflib
or rdfextras.sparql
packages.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
另一个工具是
roqet
,它封装在rasqal 中。它是一个返回解析树的命令行工具。例如:roqet -i laqrs -d Structure -n -e "SELECT * WHERE {?xa ?y OPTIONAL {?xa ?z}}"
将输出 ..
查看您在另一个中的评论回答我认为这不是你所需要的。我认为您不会在 SPARQL 解析器内部找到答案。查询中的对象(或三元组模式)评估发生在查询引擎内,在设计良好的系统中,查询引擎与查询解析是隔离的。
例如,在 4store 中,您可以使用选项
-vvv
(非常详细)查看4s-query
命令,您将在其中看到查询执行方式的输出,以及如何对每个三重模式评估进行替换。Another tool is
roqet
a tool that is packaged within rasqal. It is a command line tool that returns the parsed tree. For instance:roqet -i laqrs -d structure -n -e "SELECT * WHERE {?x a ?y OPTIONAL {?x a ?z}}"
would output ..
Looking at your comment in the other answer I don't think this is what yo need. And I don't think you will find an answer looking inside SPARQL parsers. The object (or triple pattern) evaluation in a query happens inside
Query Engines
that, in well designed systems, is isolated from query parsing.For instance, in 4store you could look at the
4s-query
command with the option-vvv
(very verbose) where you would see an output of how the query was executed and how substitutions were performed for each triple pattern evaluation.ANTLR在这里有一个SPARQL语法: http://www.antlr.org/grammar/1200929755392/ index.html
ANTLR可以生成解析代码供Python运行。
ANTLR has a SPARQL grammar here: http://www.antlr.org/grammar/1200929755392/index.html
ANTLR can generate parsing code for Python to run.
尝试使用 rdflib.plugins.sparql.parser.parseQuery 。
Try using
rdflib.plugins.sparql.parser.parseQuery
.