简体   繁体   English

如何在SPARQL查询中预定义变量

[英]How can you predefine a variable in a SPARQL query

I have created a SPARQL query to retrieve a lot of information about workflow elements. 我创建了一个SPARQL查询来检索有关工作流元素的大量信息。 The query talks to a database and is called from Python. 查询与数据库进行通信,并从Python调用。 The query is in a form like this: 查询的形式如下:

"""SELECT ?input ?value1 ?value2 ?value3 ....
WHERE {?input rdf:type InputVar.
?input property1 ?value1.
?input property2 ?value2.
?input property3 ?value3.
.... etc....
}"""

This query will then return to me all of the values corresponding to the property I want to know. 然后,此查询将返回与我想要知道的属性对应的所有值。

What I now want my query to be able to do is I want it to be able to both look for ALL input variables (like it does now) and I want it to be able to collect the information for ONE, specified input. 我现在希望我的查询能够做的是我希望它能够同时查找所有输入变量(就像它现在一样)并且我希望它能够收集ONE,指定输入的信息。 This would be possible doing something like this: 这可能是这样的:

%s property1 ?value1
%s property2 ?value2

Where I would let %s, depending on if there is a name specified, be ?input or inputname (the name specified). 在哪里我会让%s,取决于是否有指定的名称,是?input或inputname(指定的名称)。

However, if I do it like this I will need lots of %s's, as I am extracting quite some properties. 但是,如果我这样做,我需要很多%s,因为我提取了很多属性。

In SPARQL, would it be possible to define the name of the variable you want to look for? 在SPARQL中,是否可以定义要查找的变量的名称? So would it be possible to do something like this? 那么可以这样做吗?

    """SELECT ?input ?value1 ?value2 ?value3 ....
    WHERE {(?input=inputname)
    ?input rdf:type InputVar.
    ?input property1 ?value1.
    ?input property2 ?value2.
    ?input property3 ?value3.
    .... etc....
    }"""

So add something like (?input=inputname) at the beginning, so it only looks for this value? 所以在开头添加类似(?input = inputname)的东西,所以它只查找这个值?

In the general case you can try using the VALUES clause for this eg 在一般情况下,您可以尝试使用VALUES子句,例如

SELECT *
WHERE 
{
  VALUES ?input { <http://myConstant> }
  ?input rdf:type InputVar.
  ?input property1 ?value1.
  ?input property2 ?value2.
  ?input property3 ?value3.
  # etc.
}

However it is important to understand that this doesn't actually do what you want directly. 但重要的是要明白,这实际上并不是你想要的。 Semantically what it does is join the constant into the matches for the graph pattern thus eliminating matches where ?input does not match your constant. 它在语义上的作用是将常量连接到图形模式的匹配中,从而消除匹配,其中?input与您的常量不匹配。 Some query engines may be smart enough to simplify this to a substitution but not all of them will and that will only be applicable for relatively simple queries. 一些查询引擎可能足够聪明,可以将其简化为替换,但并非所有查询引擎都将这样,并且只适用于相对简单的查询。

It sounds like you are really after some kind of prepared/parameterized query support as found in most SQL APIs? 听起来你真的在使用大多数SQL API中的某种准备/参数化查询支持?

Take a look at What's the best way to parameterise SPARQL queries which discusses how to do this in various APIs. 看看参数化SPARQL查询的最佳方法是什么 ,讨论如何在各种API中执行此操作。

Since you mention Python the answer using Python may be of particular interest which is based off an example of prepared queries from the RDFLib docs 因为你提到Python, 使用 Python的答案可能特别有意义,它基于来自RDFLib文档的准备好的查询的例子

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

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