簡體   English   中英

如何使用變量謂詞編寫 sparql 查詢?

[英]How to write a sparql query with variable predicate?

請看下面的例子:

@prefix : <#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

:Bob :score :78 .
:John :score :50 .

:rule1 :condition [
        rdf:subject :score ;
        rdf:predicate :notLessThan ;
        rdf:object 60
     ];
     :condition [
        rdf:subject :score ;
        rdf:predicate :notGreaterThan ;
        rdf:object 80 
     ];
     :rating :passed .

:rule2 :condition [
        rdf:subject :score ;
        rdf:predicate :lessThan ;
        rdf:object 60
     ];    
     :rating :failed .

我想通過 spqrql 查詢得到以下 output:

 :Bob :rating :passed.    
 :John :rating :failed.

這里 rdf:predicate 可由用戶更改,其值可能是:

 lessThan, notLessThan, greaterThan, notGreaterThan

那么如何基於動態謂詞編寫這個 SPARQL 語句呢?
我沒有任何想法。 感謝你們對我的幫助。

這看起來應該可以工作。

PREFIX : <#>
PREFIX rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs:    <http://www.w3.org/2000/01/rdf-schema#>

CONSTRUCT {
    ?student :rating ?rating .
} WHERE {
    ?student :score ?score .
    ?rule :rating ?rating .
    OPTIONAL {
       ?rule :condition ?condition .
       ?condition rdf:subject :score .
       ?condition rdf:predicate ?predicate .
       ?condition rdf:object ?object .
       # fails condition
       FILTER (
           ! ( (?predicate = :lessThan && ?score < ?object ) ||
               (?predicate = :notLessThan && ?score >= ?object ) ||
               (?predicate = :greaterThan && ?score > ?object ) ||
               (?predicate = :notGreaterThan && ?score <= ?object )
           )
       )
    }
    FILTER(!bound(?condition))
}

但我得到的只是“失敗”的結果。 讓我仔細看看出了什么問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM