SPARQL:找出高数据 属性 值

SPARQL: Figure out high data property values

我有一个问答游戏,学生必须解决化学、英语、物理等三类问题。学生将在这些类别中得分,例如学生 1 的化学 50 分、英语 70 分和物理 65 分。

我可以算出学生在哪个类别中得分最高。但是我怎样才能得到任何学生得分最高的类别?我的意思是,如果一个学生英语考了90分(没有其他学生拿到这个分数),那么我们怎么能算出英语最高分是90分。

记住:英语成绩、化学成绩、物理成绩是存储在 rdf 文件中的数据属性。 我想是否可以使用 Jena 规则或 SPARQL 或纯 Java 代码。

如果我没理解错的话,你是在要求找到每个类别的最高分,然后为每个类别找到在该类别中得分最高的学生。使用数据更容易(将来,请尽量提供我们可以使用的最少数据),所以这里有一些示例数据:

@prefix : <urn:ex:>

:student1 :hasScore [ :inCategory :category1 ; :value 90 ] ,
                    [ :inCategory :category2 ; :value 75 ] ,
                    [ :inCategory :category3 ; :value 85 ] .

:student2 :hasScore [ :inCategory :category2 ; :value 75 ] ,
                    [ :inCategory :category3 ; :value 90 ] ,
                    [ :inCategory :category4 ; :value 90 ] .

:student3 :hasScore [ :inCategory :category1 ; :value 85 ] ,
                    [ :inCategory :category2 ; :value 80 ] ,
                    [ :inCategory :category4 ; :value 95 ] .

有四个类别,student1在类别1中得分最高,student3在类别2和4中得分最高,student2在类别3中得分最高。我们可以这样写查询:

prefix : <urn:ex:>

select ?category ?student ?highScore where {

  #-- Find the high score in each category
  { select ?category (max(?score) as ?highScore) {
      ?student :hasScore [ :inCategory ?category ; :value ?score ] .
    }
    group by ?category
  }

  #-- Then find the student that had that high
  #-- score in the category.
  ?student :hasScore [ :inCategory ?category ; :value ?highScore ] .
}
--------------------------------------
| category   | student   | highScore |
======================================
| :category1 | :student1 | 90        |
| :category2 | :student3 | 80        |
| :category3 | :student2 | 90        |
| :category4 | :student3 | 95        |
--------------------------------------

如果您不关心哪个学生得分最高,那么您只需要内部子查询:

prefix : <urn:ex:>

select ?category (max(?score) as ?highScore) {
  ?student :hasScore [ :inCategory ?category ; :value ?score ] .
}
group by ?category
--------------------------
| category   | highScore |
==========================
| :category1 | 90        |
| :category2 | 80        |
| :category3 | 90        |
| :category4 | 95        |
--------------------------

如果您使用不同的属性

在评论中,您问,

I have my ontology like this: Student1 :Englishscore 90; PhyscicsScore 67; ChemScore 78. Similarly for other students. Should I introduce a blank node like hasScore which reference to Englishscore, PhyscicsScore [sic], and ChemScore?

首先,我建议您标准化您的命名约定。首先,一定要使用正确的拼写(例如 Physics)。然后,要么缩写,要么不缩写。您将 Chemistry 缩写为 Chem,而不是将 English 缩写为 Eng。最后,保持大小写一致(例如 EnglishScore,而不是 Englishscore)。

没有必要使用我使用的那种表示。您没有提供示例数据(请以后提供),所以我使用了我认为相当容易使用的数据。您的表示似乎不太灵活,但您仍然可以获得所需的信息。这是一些新的示例数据:

@prefix : <urn:ex:>

:student1 :hasCat1Score 90 ;
          :hasCat2Score 75 ;
          :hasCat3Score 85 .

:student2 :hasCat2Score 75 ;
          :hasCat3Score 90 ;
          :hasCat4Score 90 .

:student3 :hasCat1Score 85 ;
          :hasCat2Score 80 ;
          :hasCat4Score 95 .

然后查询只需要为 属性 使用一个变量,该变量同时将学生与分数相关联,并指示类别。所以你仍然只是按 属性 分组并要求最高分:

prefix : <urn:ex:>

select ?hasScore (max(?score) as ?highScore) {
  ?student ?hasScore ?score
}
group by ?hasScore
-----------------------------
| hasScore      | highScore |
=============================
| :hasCat1Score | 90        |
| :hasCat2Score | 80        |
| :hasCat3Score | 90        |
| :hasCat4Score | 95        |
-----------------------------