简体   繁体   English

SPARQL查询中的“if”

[英]“if” in a SPARQL query

my problem is very simple: I have a source my:g1 that contains: 我的问题很简单:我有一个源码my:g1包含:

my:a1 my:b "literal 1"

Then I have a second source my:g2 that contains: 然后我有第二个来源my:g2包含:

my:a2 my:b my:c.
my:c rdfs:label "literal 2"

how can I set a SPARQL query that produces something like: 如何设置生成类似以下内容的SPARQL查询:

| ?a    | ?b    | ?literal    |
|-------|-------|-------------|
| my:a1 | my:b  | "literal 1" |
| my:a2 | my:b  | "literal 2" |

ie. 即。 how can i tell sparql to use the same variable for both "literal 1" and "literal 2" : I'm looking for something like 我如何告诉sparql对"literal 1""literal 2"使用相同的变量:我正在寻找像

Select ?a ?b ?literal 
where {
 if (?g = my:g1) then
  GRAPH ?g { ?a ?b ?literal}
 else if (?g = my:g2) then
  GRAPH ?g { ?a ?b ?c. ?c rdfs:label ?literal}
}

NOTE: I know that this query is horribly wrong, but is just to clarify my intention 注意:我知道这个查询非常错误,但只是为了澄清我的意图

EDIT: 编辑:

in this specific case a "union" statement like 在这个特定情况下,“联合”声明就像

select ?a ?b ?literal 
where {
{
 GRAPH my:g1 { ?a ?b ?literal}
}
union
{
  GRAPH my:g2 { ?a ?b ?c. ?c rdfs:label ?literal}
}
}

would work, but is not my "real" case. 会起作用,但不是我的“真实”案例。 There are any other solutions? 还有其他解决方案吗?

You can use a property path and a filter. 您可以使用属性路径和过滤器。 The trick here is that the property path with a question mark means a path of length 0 or 1. If the path is of length 0, then ?literal is the same as ?c, which covers the case when ?a is related directly to a literal. 这里的技巧是带有问号的属性路径表示长度为0或1的路径。如果路径长度为0,那么?literal与?c相同,它涵盖了?a直接与文字。 If the path is length 1, then ?literal is the value of rdfs:label for ?c. 如果路径长度为1,那么?literal是?c的rdfs:label的值。

Here's an example with real data: 这是一个真实数据的例子:

@prefix : <urn:ex:>

:a :b "literal 1" .
:a :b :c .
:c :label "literal 2" .prefix : <urn:ex:>
select distinct ?a ?b ?literal where {
  ?a ?b ?c .
  ?c :label? ?literal
  filter isLiteral(?literal)
}
-----------------------------
| a  | b      | literal     |
=============================
| :a | :b     | "literal 1" |
| :a | :b     | "literal 2" |
| :c | :label | "literal 2" |
-----------------------------

You might not have been expecting that last row in the results, but if ?a and ?b are variables, then it makes sense, because there's nothing saying that the variable ?b has to be bound to the specific property :b . 你可能没想到结果中的最后一行,但如果?a和?b是变量,那么它是有意义的,因为没有什么说变量?b必须绑定到特定属性:b

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

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