解析 SPARQL 查询

发布于 2024-11-29 00:37:38 字数 602 浏览 11 评论 0原文

我需要测试几百万个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

演出会有结束 2024-12-06 00:37:39

另一个工具是roqet,它封装在rasqal 中。它是一个返回解析树的命令行工具。例如:

roqet -i laqrs -d Structure -n -e "SELECT * WHERE {?xa ?y OPTIONAL {?xa ?z}}"

将输出 ..

Query:
query verb: SELECT
query bound variables (3): x, y, z
query Group graph pattern[0] {
  sub-graph patterns (2) {
    Basic graph pattern[1] #0 {
      triples {
        triple #0 { triple(variable(x), uri<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>, variable(y)) }
      }
    }
    Optional graph pattern[2] #1 {
      sub-graph patterns (1) {
        Basic graph pattern[3] #0 {
          triples {
            triple #0 { triple(variable(x), uri<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>, variable(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 ..

Query:
query verb: SELECT
query bound variables (3): x, y, z
query Group graph pattern[0] {
  sub-graph patterns (2) {
    Basic graph pattern[1] #0 {
      triples {
        triple #0 { triple(variable(x), uri<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>, variable(y)) }
      }
    }
    Optional graph pattern[2] #1 {
      sub-graph patterns (1) {
        Basic graph pattern[3] #0 {
          triples {
            triple #0 { triple(variable(x), uri<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>, variable(z)) }
          }
        }
      }
    }
  }
}

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.

凉薄对峙 2024-12-06 00:37:39

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.

黯然 2024-12-06 00:37:39

尝试使用 rdflib.plugins.sparql.parser.parseQuery 。

Try using rdflib.plugins.sparql.parser.parseQuery.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文