如何使用SPARQL

时间:2015-12-01 09:15:40

标签: aggregate sparql

我正在使用sesame存储库和一些三重数据发布,如下所示:

<http://example.org/doc2> a qb:Observation;
   foaf:Organization "Inst. of Technol.";
   ps:sumPaper 3 .

<http://example.org/doc3> a qb:Observation;
   foaf:Organization "Institute of Technology";
   ps:sumPaper 5 .

<http://example.org/doc4> a qb:Observation;
   foaf:Organization "Dong C Univ.";
   ps:sumPaper 4 .

<http://example.org/doc5> a qb:Observation;
   foaf:Organization "University of Dong C";
   ps:sumPaper 2 .

doc2和doc3,实际上有相同的组织。除了doc 4和doc 5之外,它还有同义词组织。 我想用sparql聚合数据,我希望得到这样的结果:

   Organization              sumPaper 
   -----------------------------------
    Insitute of Technology      8  
    University of Dong C        6

所以,我在存储库中添加了同义词本体到描述。

 :org2 a foaf:Organization;
     ps:organizationName "Inst. of Technol";   
     owl:sameAs :org3.

:org3 a foaf:Organization;
     ps:organizationName "Institute of Technology".

:org4 a foaf:Organization;
     ps:organizationName "Dong C Univ.";

:org5 a foaf:Organization;
    ps:organizationName "University of Dong C";
    owl:sameAs :org4.

请帮助我......我很难搞定sparql语句来获得我期望的结果。

1 个答案:

答案 0 :(得分:3)

您使用owl:sameAs使事情变得复杂,请尝试以下方法:

:org1 a foaf:Organization ;
     ps:organizationName "Inst. of Technol", "Institute of Technology" .

:org2 a foaf:Organization ;
     ps:organizationName "Dong C Univ.", "University of Dong C" .

然后您可以执行以下操作:

select ?org (SUM(?sumP) as ?sum)
{
    ?ob a qb:Observation ;
        ps:sumPaper ?sumP ;
        foaf:Organization ?orgName .

    # Lookup org based on synonyms
    ?org ps:organizationName ?orgName .
}
group by ?org

虽然这会给你组织标识符。如果这困扰你:

select (SAMPLE(?orgName) as ?name) (SUM(?sumP) as ?sum)
...

甚至为同义词文件中的每个组织添加rdsf:labelskos:prefLabel