How to store simple rule in RDF and query with Sparql

Dear all, I have some data about 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 .


 Is this idea feasible? How do I write this data file and SPARQL query correctly to read the rule data and complete the grade transformation?

And how to handle with the inclusive and exclusive of boundary limit?
Thank you for any suggestion.

Kind regards,
Joylix

Received on Friday, 6 August 2021 10:10:21 UTC