SPARQL

时间:2015-09-17 11:22:19

标签: rdf sparql virtuoso

我正在尝试运行以下SPARQL查询,但它不断返回SR171: Transaction timed out

SELECT ?isBusAvailable WHERE {
  SELECT DISTINCT IF (
  (
  SELECT ?value2 WHERE {
      GRAPH data: { ?obsValueID2 ontology:value ?value2 }
      GRAPH data: { ?obsValueID2 rdf:label "Availability" }
      GRAPH data: { ?obsValueID2 ontology:isObservedValueOf ?obsID2}
      GRAPH data: { ?obsID2 ssn:observationResultTime ?time2 }
      GRAPH data: { ?obsID2 ssn:observedBy ?id2 }
      GRAPH meta: { ?id2 rdf:label "MyBusService" } 
  } ORDER BY DESC (?time2) LIMIT 1) > 1, "Take Bus", (
                       SELECT ?isBikeAvailable WHERE {
                       SELECT DISTINCT IF (
                       (
                       SELECT ?value3 WHERE {
                            GRAPH data: { ?obsValueID3 ontology:value ?value3 }
                            GRAPH data: { ?obsValueID3 rdf:label "Availability" }
                            GRAPH data: { ?obsValueID3 ontology:isObservedValueOf ?obsID3}
                            GRAPH data: { ?obsID3 ssn:observationResultTime ?time3 }
                            GRAPH data: { ?obsID3 ssn:observedBy ?id3 }
                            GRAPH meta: { ?id3 rdf:label "MyBikeService" } 
                       } ORDER BY DESC (?time3) LIMIT 1
                       ) > 0, "Take Bike", "Take Taxi") as ?isBikeAvailable WHERE { ?1 ?2 ?3}})) as ?isBusAvailable WHERE { ?4 ?5 ?6}}

如果我单独运行它们,它会在1秒内运行。以下示例有效。

SELECT ?isBusAvailable WHERE {
  SELECT DISTINCT IF (
  (
  SELECT ?value2 WHERE {
      GRAPH data: { ?obsValueID2 ontology:value ?value2 }
      GRAPH data: { ?obsValueID2 rdf:label "Availability" }
      GRAPH data: { ?obsValueID2 ontology:isObservedValueOf ?obsID2}
      GRAPH data: { ?obsID2 ssn:observationResultTime ?time2 }
      GRAPH data: { ?obsID2 ssn:observedBy ?id2 }
      GRAPH meta: { ?id2 rdf:label "MyBusService" } 
  } ORDER BY DESC (?time2) LIMIT 1) > 1, "Take Bus", 'Take Bike') as ?isBusAvailable WHERE { ?4 ?5 ?6}} 

如果第一个查询的结果为真,则返回'Take Bus';否则,运行第二个查询,然后返回'Take Bike'或'Take Taxi'。

显然问题在于第二个查询(来自第一个查询的错误条件)。在“Take Bus”之后,如果我将第二个查询更改为“Take Bike”,它就可以了。

2 个答案:

答案 0 :(得分:5)

@andrei - 我认为你的?isBusAvailable让人感到困惑 - 考虑到你最后返回的文字值,这可能会更好?travelMethod

在我阅读内容时,这里的问题实际上是如何正确构建嵌套的IFs - 问题的标题应该是“在SPARQL中嵌套IF语句的正确语法是什么?”

简单来说,我认为你正在做的是检查一辆可用的公共汽车,然后返回“乘坐公共汽车”(如果存在的话);如果没有,你检查一辆可用的自行车,并返回“骑自行车”,如果存在的话;如果没有,你返回“乘坐出租车”。在伪代码中,IF AVAIL bus, THEN take bus, ELSE ( IF AVAIL bike, THEN take bike, ELSE take taxi )

@ joshua-taylor关于一件事是正确的 - SELECT DISTINCT IF (…)并非严格有效,但修复只是一个额外的paren包装 - 例如,以下是有效的(和验证) -

SELECT DISTINCT ( IF ( ( 1 = 1 ) 
                     , "true" 
                     , "false" 
                     )           AS ?test ) 
WHERE { ?x ?y ?z } 
LIMIT 1

但是在查询中修复此问题并不会从验证程序获得更好的输出。事实上,用(SELECT ?value … LIMIT 1)替换内部1查询会使整个大型查询验证 -

SELECT ?travelMethod 
   WHERE 
     { 
        SELECT DISTINCT 
                         ( IF 
                               (
                                  (
                                     1  > 0
                                  )
                               ,  "Take Bus"
                               ,  (
                                        IF (
                                              (
                                                 1  > 0
                                              )
                                           ,  "Take Bike"
                                           ,  "Take Taxi"
                                           )
                                  )
                               )  AS  ?travelMethod
                         ) 
        FROM         data: 
        WHERE        { ?1  ?2  ?3 }
     }

- 那些内部(SELECT ?value … LIMIT 1)查询也会自行验证 -

SELECT    ?value 
WHERE     
          {
             GRAPH data: { ?obsValueID  ontology:value              ?value          }
             GRAPH data: { ?obsValueID  rdf:label                   "Availability"  }
             GRAPH data: { ?obsValueID  ontology:isObservedValueOf  ?obsID          }
             GRAPH data: { ?obsID       ssn:observationResultTime   ?time           }
             GRAPH data: { ?obsID       ssn:observedBy              ?id             }
             GRAPH meta: { ?id          rdf:label                   "MyBusService"  } 
          }  
ORDER BY  DESC (?time) 
LIMIT     1

我相信以下是有效的语法 - 即使sparql.org验证器扼杀它 - 但我无法确定它是否执行因为我没有时间重新调整查询以针对随机端点运行和数据集。如果你重新编写查询来运行DBpedia,那么你可以帮助下一个人!

SELECT ?travelMethod 
   WHERE 
     { 
        SELECT DISTINCT 
                         ( IF 
                               (
                                  (
                                     (  SELECT    ?value 
                                        WHERE     
                                                  {
                                                     GRAPH data: { ?obsValueID  ontology:value              ?value          }
                                                     GRAPH data: { ?obsValueID  rdf:label                   "Availability"  }
                                                     GRAPH data: { ?obsValueID  ontology:isObservedValueOf  ?obsID          }
                                                     GRAPH data: { ?obsID       ssn:observationResultTime   ?time           }
                                                     GRAPH data: { ?obsID       ssn:observedBy              ?id             }
                                                     GRAPH meta: { ?id          rdf:label                   "MyBusService"  } 
                                                  }  
                                        ORDER BY  DESC (?time) 
                                        LIMIT     1
                                     )  > 0
                                  )
                               ,  "Take Bus"
                               ,  (
                                        IF (
                                              (
                                                 (  SELECT    ?value 
                                                    WHERE
                                                              {
                                                                 GRAPH data: { ?obsValueID  ontology:value              ?value           }
                                                                 GRAPH data: { ?obsValueID  rdf:label                   "Availability"   }
                                                                 GRAPH data: { ?obsValueID  ontology:isObservedValueOf  ?obsID           }
                                                                 GRAPH data: { ?obsID       ssn:observationResultTime   ?time            }
                                                                 GRAPH data: { ?obsID       ssn:observedBy              ?id              }
                                                                 GRAPH meta: { ?id          rdf:label                   "MyBikeService"  } 
                                                              } 
                                                    ORDER BY  DESC (?time) 
                                                    LIMIT     1
                                                 )  > 0
                                              )
                                           ,  "Take Bike"
                                           ,  "Take Taxi"
                                           )
                                  )
                               )  AS  ?travelMethod
                         ) 
        FROM         data: 
        WHERE        { ?1  ?2  ?3 }
     }

花了一点时间远离它,并在上面的评论中重新阅读你对数据所说的内容,我可以进一步优化这个查询,这也可以让它更快地执行 -

SELECT ?travelMethod 
   WHERE 
     { 
        SELECT DISTINCT 
                         ( IF 
                               (
                                  (
                                     (  SELECT    ?value 
                                        WHERE     
                                                  {
                                                     GRAPH data: { ?obsValueID  ontology:value              ?value
                                                                             ;  rdf:label                   "Availability"
                                                                             ;  ontology:isObservedValueOf  ?obsID
                                                                 . ?obsID       ssn:observationResultTime   ?time
                                                                             ;  ssn:observedBy              ?id
                                                                 }
                                                     GRAPH meta: { ?id          rdf:label                   "MyBusService"  } 
                                                  }  
                                        ORDER BY  DESC (?time) 
                                        LIMIT     1
                                     )  > 0
                                  )
                               ,  "Take Bus"
                               ,  (
                                        IF (
                                              (
                                                 (  SELECT    ?value 
                                                    WHERE
                                                              {
                                                                 GRAPH data: { ?obsValueID  ontology:value              ?value
                                                                                         ;  rdf:label                   "Availability"
                                                                                         ;  ontology:isObservedValueOf  ?obsID
                                                                             . ?obsID       ssn:observationResultTime   ?time
                                                                                         ;  ssn:observedBy              ?id
                                                                             }
                                                                 GRAPH meta: { ?id          rdf:label                   "MyBikeService"  } 
                                                              } 
                                                    ORDER BY  DESC (?time) 
                                                    LIMIT     1
                                                 )  > 0
                                              )
                                           ,  "Take Bike"
                                           ,  "Take Taxi"
                                           )
                                  )
                               )  AS  ?travelMethod
                         ) 
        FROM         data: 
        WHERE        { ?1  ?2  ?3 }
     }

答案 1 :(得分:0)

处于false状态的查询需要遵循SPARQL的IF条件语句的格式,因为它返回一个值。删除“SELECT DISTINCT”

SELECT ?isBusAvailable WHERE { 
  SELECT DISTINCT (IF(
        (
         SELECT ?value WHERE {
                GRAPH data: { ?obsValueID ontology:value ?value }
                GRAPH data: { ?obsValueID rdf:label "Availability" }
                GRAPH data: { ?obsValueID ontology:isObservedValueOf ?obsID}
                GRAPH data: { ?obsID ssn:observationResultTime ?time }
                GRAPH data: { ?obsID ssn:observedBy ?id }
                GRAPH meta: { ?id rdf:label "MyBusService" } 
        } ORDER BY DESC (?time) LIMIT 1) > 0,'Take Bus', (

                       IF (
                       (
                       SELECT ?value WHERE {
                            GRAPH data: { ?obsValueID ontology:value ?value }
                            GRAPH data: { ?obsValueID rdf:label "Availability" }
                            GRAPH data: { ?obsValueID ontology:isObservedValueOf ?obsID}
                            GRAPH data: { ?obsID ssn:observationResultTime ?time }
                            GRAPH data: { ?obsID ssn:observedBy ?id }
                            GRAPH meta: { ?id rdf:label "MyBikeService" } 
                       } ORDER BY DESC (?time) LIMIT 1
                       ) > 0, "Take Bike", "Take Taxi")
                       )) AS ?isBusAvailable) FROM data: WHERE { ?1 ?2 ?3 }}

编辑: select distinct if( ... )不合法的SPARQL,因此if语句需要包装在括号中。查询变为:select distinct (if( ... ))