SPARQL组concat union

Jen*_*Jin 1 sparql

我想GROUP_CONCATUNION两套三元组.这是不允许的?

PREFIX bo: <https://webfiles.uci.edu/jenniyk2/businessontology#>  
SELECT (GROUP_CONCAT(DISTINCT ?m2;SEPARATOR = ", ") AS ?comp)
WHERE 
{
{{SELECT  ?m2 ?c ?p
WHERE { ?c rdfs:label ?m. ?c2 rdfs:label ?m2. ?so bo:owner ?p. 
?so bo:sharesIn ?c. ?so2 bo:owner ?p. ?so2 bo:sharesIn ?c2. }
}
UNION
{SELECT  ?m2 ?c ?p
WHERE { ?c rdfs:label ?m. ?c2 rdfs:label ?m2. ?dir  bo:isPartOf ?c. 
?dir bo:isDirectedBy ?p. ?dir2 bo:isPartOf ?c2. ?dir2 bo:isDirectedBy ?p.}
}}
GROUP BY ?c
HAVING (COUNT(?m2) >1)}
Run Code Online (Sandbox Code Playgroud)

它说最后一行有一个错误.

Found group. Was expecting one of : BIND, BLANK_NODE_LABEL, DECIMAL, DOUBLE, FALSE, FILTER, GEO, GRAPH, INTEGER, MINUS, NIL-SYMBOL, OPTIONAL, Q_IRI_REF, QNAME, QNAME_NS, SERVICE, STRING_LITERAL1, STRING_LITERAL2, STRING_LITERAL_LONG1, STRING_LITERAL_LONG2, TEXTINDEX, TRUE, UNION, VALUES, VARNAME or punctuation '(', '+', '-', '.', '[', '[]', '{', '}'.

Jee*_*tra 6

GROUP BYHAVING条款应该是外面WHERE条款,而不是在它里面,因为他们现在是.要使查询在语法上正确,请}从最后一行中删除结束并将其添加到您的上方的行上GROUP BY:

PREFIX bo: <https://webfiles.uci.edu/jenniyk2/businessontology#>  
SELECT (GROUP_CONCAT(DISTINCT ?m2;SEPARATOR = ", ") AS ?comp)
WHERE 
{
{{SELECT  ?m2 ?c ?p
WHERE { ?c rdfs:label ?m. ?c2 rdfs:label ?m2. ?so bo:owner ?p. 
?so bo:sharesIn ?c. ?so2 bo:owner ?p. ?so2 bo:sharesIn ?c2. }
}
UNION
{SELECT  ?m2 ?c ?p
WHERE { ?c rdfs:label ?m. ?c2 rdfs:label ?m2. ?dir  bo:isPartOf ?c. 
?dir bo:isDirectedBy ?p. ?dir2 bo:isPartOf ?c2. ?dir2 bo:isDirectedBy ?p.}
}}}
GROUP BY ?c
HAVING (COUNT(?m2) >1)
Run Code Online (Sandbox Code Playgroud)

正如约书亚所指出的那样,查询可以写得更简单,但这应该可以解决你的问题.


Jos*_*lor 6

Jeen Broekstra的答案确定了导致语法错误的排版问题,但我认为值得一提的是这个查询可以简化一下,因为从一开始就更简单的代码可以更容易地发现拼写错误和语法错误.如果我正确读取它,看起来查询的主体简化为:

  ?c  rdfs:label ?m .
  ?c2 rdfs:label ?m2 .
  { ?so  bo:owner ?p ;
         bo:sharesIn ?c .
    ?so2 bo:owner ?p ;
         bo:sharesIn ?c2 . }
  union
  { ?dir  bo:isPartOf ?c ;
          bo:isDirectedBy ?p .
    ?dir2 bo:isPartOf ?c2 ;
          bo:isDirectedBy ?p }
Run Code Online (Sandbox Code Playgroud)

您正在寻找一个2 C多数民众赞成由两个特定类型的路径中的一个连接到?C2,和您要组2 C和串联的不同的标签?C2.我认为有两种方法可以进一步简化.第一个是使用属性路径,因为您实际上并未使用许多这些变量的值.通过财产路径,身体可能变成:

  ?c  rdfs:label ?m .
  ?c2 rdfs:label ?m2 .
  { ?c ^bo:sharesIn/bo:owner/^bo:owner/bo:sharesIn ?c2 }
  union
  { ?c ^bo:isPartOf/bo:isDirectedBy/^bo:isDirectedBy/bo:isPartOf ?c2 }
Run Code Online (Sandbox Code Playgroud)

作为另一种选择,由于每个联合替代中的模式结构类似,您可以使用抽象一点:

  ?c  rdfs:label ?m .
  ?c2 rdfs:label ?m2 .
  values (?sharesIn_isPartOf ?owner_isDirectedBy) {
    (bo:sharesIn bo:owner)
    (bo:isPartOf bo:isDirectedBy)
  }
  ?x  ?owner_isDirectedBy ?y  ;
      ?sharesIn_isPartOf  ?c  .
  ?x2 ?owner_isDirectedBy ?y  ;
      ?sharesIn_isPartOf  ?c2 .
Run Code Online (Sandbox Code Playgroud)