使用IF将变量绑定到两个值之一? [英] Binding a variable to one of two values with IF?

查看:107
本文介绍了使用IF将变量绑定到两个值之一?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的SPARQL查询中,我不知道如何使用 if 将两个字符串中的一个绑定到变量?result 。我听说有范围和超出范围的概念,但我并没有真正看到差异。我也尝试在 select 行中放置 if 子句,但它也不起作用。如何修复此查询以根据条件将?result 绑定到两个字符串之一?

In the following SPARQL query, I'm not sure how to use if to bind one of two strings to the variable ?result. I heard that there are concepts of "in scope" and "out of scope," but I don't really see the difference. I've also tried putting the if clause in the select line, but it didn't work either. How can I fix this query to bind ?result to one of the two strings based on the condition?

SELECT ?result
WHERE{
    ?chain rdf:type rdfs:Property .
    ?chain rdfs:domain <http://www.vs.cs.hs-rm.de/ontostor/SVC#MDiskGroup> .
    ?chain rdfs:range <http://www.vs.cs.hs-rm.de/ontostor/SVC#IOgroup> .
    ?this ?chain ?arg .
    ?arg io:id ?var .
    IF(?var = "0"^^xsd:integer,
       BIND("    *"^^xsd:string AS ?result),
       BIND(""^^xsd:string AS ?result)) .
}


推荐答案

如果 SPARQL中的运算符不是语句,因为它有时是编程语言,而是函数形式。表达。 if(test,a,b) a if test 为true, b 如果 test 为false。正如文档所说:

The if operator in SPARQL isn't a statement as it sometimes is in a programming language, but a functional form. expression. The value of if(test,a,b) is a if test is true, and b if test is false. As the documentation says:


17.4.1.2 IF



17.4.1.2 IF

rdfTerm  IF (expression1, expression2, expression3)

IF 函数表单评估第一个参数,将其解释为
有效布尔值,然后返回 expression2 <的值/ code>如果
EBV为真,否则返回 expression3 的值。只计算一个
的expression2和expression3。如果评估第一个
参数会引发错误,那么IF表达式的评估
会引发错误。

The IF function form evaluates the first argument, interprets it as a effective boolean value, then returns the value of expression2 if the EBV is true, otherwise it returns the value of expression3. Only one of expression2 and expression3 is evaluated. If evaluating the first argument raises an error, then an error is raised for the evaluation of the IF expression.

示例:假设?x = 2,?z = 0且?y未绑定在某些查询
解决方案中:

Examples: Suppose ?x = 2, ?z = 0 and ?y is not bound in some query solution:

IF(?x = 2, "yes", "no")     returns "yes"
IF(bound(?y), "yes", "no")  returns "no"
IF(?x=2, "yes", 1/?z)       returns "yes", the expression 1/?z is not evaluated
IF(?x=1, "yes", 1/?z)       raises an error
IF("2" > 1, "yes", "no")    raises an error


所以,如果不是一个类似于编程语言的语句,但它只是一个带有三个参数并返回一个值的函数。 SPARQL是一种查询语言,没有执行的语句;它是一种查询语言,用于匹配图形中的模式并将变量绑定到值。所以如果是一个函数,如果第一个参数为true,则返回第二个参数,否则返回第三个参数。通常,您将函数的值绑定到变量

So, if isn't a statement like it might be in a programming language, but it's simply a function that takes three arguments and returns a value. SPARQL is a query language, and doesn't have statements that get executed; it's a query language for matching patterns in a graph and binding variables to values. So if is a function, and it just so happens that if the first argument is true, then it returns the second argument, otherwise it returns the third. In general, you'd bind the value of a function to a variable with

bind( function(args...) as ?variable )

这种情况也不例外。您将调用 if 函数并将其结果绑定到变量

and this case is no different. You'd call the if function and bind its result to a variable with

bind( if(condition,then,else) as ?result )

在你的情况下,这个意味着您将使用以下查询。我添加了一些新行来帮助提高可读性,但它们并不是必需的。 SPARQL查询中的整数是类型 xsd:integer 的文字的简写,所以我也使用过(感谢RobV的评论) 0 而不是0^^ xsd:integer 。 (参见 2.3.2使用数字类型匹配文字。)

In your case, this means that you would use the following query. I've added some newlines to help the readability, but they're not necessary. Integers in a SPARQL query are shorthand for a literal with type xsd:integer, so I've also used (thanks to RobV's comment) 0 instead of "0"^^xsd:integer. (See 2.3.2 Matching Literals with Numeric Types.)

bind(if(?var = 0,
        "    *"^^xsd:string,
        ""^^xsd:string )
     as ?result)

如果我们真的想缩短这个,那么我们可以使用 xsd:string 作为构造函数,然后执行(参见 17.5 XPath构造函数):

If we actually want to shorten this even more, then we can use xsd:string as a constructor, and do (see 17.5 XPath Constructor Functions):

bind(xsd:string(if(?var = 0,"    *", "")) as ?result)

如果你是的话,这可能看起来有点奇怪习惯做像

This might seem a little bit odd at first if you're used to doing things like

String result;
if ( var == 0 ) {
  result = "case 1";
}
else {
  result = "case 2";
}

但是很多语言实际上提供了一个三元运算符,可以让你做得更短

but many language actually provide a ternary operator that lets you do the much shorter

String result = (var == 0) ? "case 1" : "case 2";

。这是您使用SPARQL获得的功能。

instead. This is the functionality you're getting with SPARQL.

这篇关于使用IF将变量绑定到两个值之一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆