Re: How to store rule-related data in RDF and query with Sparql?

Hi Joylix,

You could try something like:

PREFIX : <http://example.org/#>
SELECT ?student ?test ?grade
WHERE {
   ?student :tookTest ?test .
   ?test :score ?score .
   ?range a :range .
   ?range :grade ?grade .
   ?range :lowerLimit ?lower .
   ?range :upperLimit ?upper .
   FILTER(?score >= ?lower && ?score <= ?upper)
}

Not tested, but I think this should work.

Best,
Aidan

P.S., if you want to express rules over RDF (in RDF) and like to use 
SPARQL for that, you might be interested in SPIN:

https://www.w3.org/Submission/spin-sparql/


On 2021-08-06 4:23, joylix wrote:
> Dear all, I have some data on students' test scores as follows:
> 
> 
>     /@prefix : <http://example.org/#> ./
> 
>     /:Bob a :Student;/
> 
>     /     :tookTest :Test0,:Test1,:Test2 ,:Test3 ./
> 
>     /:Test0 :score 90 ./
> 
>     /:Test1 :score 81 ./
> 
>     /:Test2 :score 62 ./
> 
>     /:Test3 :score 32 ./
> 
> The rules for grading based on scores are as follows:
> 
>      [90-100]  --> A
> 
>      [75-90)   --> B
> 
>      [60-75)   --> C
> 
>      [0-60)    --> D
> 
> So I used the following SPARQL query to get the grade of the student's Test:
> 
> 
>     /prefix : <http://example.org/#>/
> 
>     /select ?student ?test ?grade/
> 
>     /    WHERE { /
> 
>     /      ?student :tookTest ?test ./
> 
>     /      ?test :score ?score ./
> 
>     /      BIND ( IF ( ?score >= 90 , "A", IF ( ?score>=75, "B", IF (
>     ?score>=60, "C", "D" ) ) ) AS ?grade )/
> 
>     /      }/
> 
> And I got the right result as such:
> 
> 
>     student,                  test,                       grade
> 
>     http://example.org/#Bob,  http://example.org/#Test3,  D
> 
>     http://example.org/#Bob,  http://example.org/#Test2,  C
> 
>     http://example.org/#Bob,  http://example.org/#Test1,  B
> 
>     http://example.org/#Bob,  http://example.org/#Test0,  A
> 
>      Now my question is how to store this rule data in RDF (along with 
> the score) rather than in SPARQL, so that the rule data can be defined 
> and modified by the user, The server side reads the rule data to 
> generate the appropriate SPARQL query.
> 
> My initial thoughts are as follows:
> 
> 
>     / :range1 a :range ;/
> 
>     /         :grade "A" ;/
> 
>     /         :lowerLimit 90 ;/
> 
>     /         :upperLimit 100./
> 
>     /:range2 a :range ;/
> 
>     /         :grade "B" ;/
> 
>     /         :lowerLimit 75 ;/
> 
>     /         :upperLimit 89 ./
> 
>     /:range3 a :range ;/
> 
>     /         :grade "C" ;/
> 
>     /         :lowerLimit 60 ;/
> 
>     /         :upperLimit 74 ./
> 
>     /:range4 a :range ;/
> 
>     /         :grade "D" ;/
> 
>     /         :lowerLimit 0 ;/
> 
>     /         :upperLimit 59 ./
> 
>     /:rule1 :hasRange :range1,:range2, :range3, :range4 ./
> 
> 
> But how do I now write the right SPARQL to read the rule and complete 
> the grade transformation?
> 
> And how to handle with inclusive and exclusive of the boundary of limit?
> Thank you for any suggestion.
> 
> Kind regards,
> Joylix
> 
> 
> 
> 

Received on Friday, 6 August 2021 22:11:36 UTC