Jena查询组合个人的数据属性及其对象属性

时间:2013-10-01 14:38:21

标签: rdf sparql jena

我可以做相当简单的SPARQL SELECT和CONSTRUCT查询,并且我还使用Jena的SimpleSelector来从Jena模型中检索语句。但是我不确定查询Jena模型的最佳方法,而不必对结果进行大量的后期处理,这让我想知道我是不是在咆哮错误的树。

我想找到联盟球队中名为Dave的球员的球队颜色和球员得分。

使用SPARQL SELECT查询我可以单独找到联盟球队的颜色,以及称为Dave的球员的得分,但是如何在同一个查询中指定它们并获得明确的结果呢?

我正在使用以下本体:

<?xml version="1.0"?>


<!DOCTYPE rdf:RDF [
    <!ENTITY owl "http://www.w3.org/2002/07/owl#" >
    <!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
    <!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
    <!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
    <!ENTITY stackont "http://www.semanticweb.org/id117167/ontologies/2013/9/stackont#" >
]>


<rdf:RDF xmlns="http://www.semanticweb.org/id117167/ontologies/2013/9/stackont#"
     xml:base="http://www.semanticweb.org/id117167/ontologies/2013/9/stackont"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
     xmlns:stackont="http://www.semanticweb.org/id117167/ontologies/2013/9/stackont#"
     xmlns:owl="http://www.w3.org/2002/07/owl#"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
    <owl:Ontology rdf:about="http://www.semanticweb.org/id117167/ontologies/2013/9/stackont"/>



    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Object Properties
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->




    <!-- http://www.semanticweb.org/id117167/ontologies/2013/9/stackont#hasPlayer -->

    <owl:ObjectProperty rdf:about="&stackont;hasPlayer">
        <rdfs:range rdf:resource="&stackont;Player"/>
        <rdfs:domain rdf:resource="&stackont;Team"/>
    </owl:ObjectProperty>



    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Data properties
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->




    <!-- http://www.semanticweb.org/id117167/ontologies/2013/9/stackont#PlayerName -->

    <owl:DatatypeProperty rdf:about="&stackont;PlayerName">
        <rdfs:domain rdf:resource="&stackont;Player"/>
        <rdfs:range rdf:resource="&xsd;string"/>
        <rdfs:subPropertyOf rdf:resource="&owl;topDataProperty"/>
    </owl:DatatypeProperty>



    <!-- http://www.semanticweb.org/id117167/ontologies/2013/9/stackont#PlayerScore -->

    <owl:DatatypeProperty rdf:about="&stackont;PlayerScore">
        <rdfs:domain rdf:resource="&stackont;Player"/>
        <rdfs:range rdf:resource="&xsd;double"/>
        <rdfs:subPropertyOf rdf:resource="&owl;topDataProperty"/>
    </owl:DatatypeProperty>



    <!-- http://www.semanticweb.org/id117167/ontologies/2013/9/stackont#TeamCategory -->

    <owl:DatatypeProperty rdf:about="&stackont;TeamCategory">
        <rdfs:domain rdf:resource="&stackont;Team"/>
        <rdfs:range rdf:resource="&xsd;string"/>
    </owl:DatatypeProperty>



    <!-- http://www.semanticweb.org/id117167/ontologies/2013/9/stackont#TeamColor -->

    <owl:DatatypeProperty rdf:about="&stackont;TeamColor">
        <rdfs:domain rdf:resource="&stackont;Team"/>
        <rdfs:range rdf:resource="&xsd;string"/>
    </owl:DatatypeProperty>



    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Classes
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->




    <!-- http://www.semanticweb.org/id117167/ontologies/2013/9/stackont#Player -->

    <owl:Class rdf:about="&stackont;Player">
        <owl:disjointWith rdf:resource="&stackont;Team"/>
    </owl:Class>



    <!-- http://www.semanticweb.org/id117167/ontologies/2013/9/stackont#Team -->

    <owl:Class rdf:about="&stackont;Team"/>



    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Individuals
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->




    <!-- http://www.semanticweb.org/id117167/ontologies/2013/9/stackont#Player1a -->

    <owl:NamedIndividual rdf:about="&stackont;Player1a">
        <PlayerScore>500</PlayerScore>
        <PlayerName>Bob</PlayerName>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/id117167/ontologies/2013/9/stackont#Player1b -->

    <owl:NamedIndividual rdf:about="&stackont;Player1b">
        <PlayerScore>100</PlayerScore>
        <PlayerName>Dave</PlayerName>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/id117167/ontologies/2013/9/stackont#Player2a -->

    <owl:NamedIndividual rdf:about="&stackont;Player2a">
        <PlayerScore>75</PlayerScore>
        <PlayerName>Steve</PlayerName>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/id117167/ontologies/2013/9/stackont#Player2b -->

    <owl:NamedIndividual rdf:about="&stackont;Player2b">
        <PlayerName>Jack</PlayerName>
        <PlayerScore>200</PlayerScore>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/id117167/ontologies/2013/9/stackont#Team1 -->

    <owl:NamedIndividual rdf:about="&stackont;Team1">
        <TeamColor>red</TeamColor>
        <TeamCategory>league</TeamCategory>
        <hasPlayer rdf:resource="&stackont;Player1a"/>
        <hasPlayer rdf:resource="&stackont;Player1b"/>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/id117167/ontologies/2013/9/stackont#Team2 -->

    <owl:NamedIndividual rdf:about="&stackont;Team2">
        <TeamCategory>division</TeamCategory>
        <TeamColor>blue</TeamColor>
        <hasPlayer rdf:resource="&stackont;Player2a"/>
        <hasPlayer rdf:resource="&stackont;Player2b"/>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/id117167/ontologies/2013/9/stackont#Team3 -->

    <owl:NamedIndividual rdf:about="&stackont;Team3">
        <TeamColor>green</TeamColor>
        <TeamCategory>league</TeamCategory>
        <hasPlayer rdf:resource="&stackont;Player3a"/>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/id117167/ontologies/2013/9/stackont#Player3a -->

    <owl:NamedIndividual rdf:about="&stackont;Player3a">
        <PlayerName>Dave</PlayerName>
        <PlayerScore>125</PlayerScore>
    </owl:NamedIndividual>
</rdf:RDF>

1 个答案:

答案 0 :(得分:0)

SPARQL是一种RDF图形查询语言。您只需要编写一个与您要查找的数据类型相匹配的图形模式。基于您的数据的暂时完成(以便我可以对它运行查询),看起来像这样的查询应该适合您:

prefix :      <http://www.semanticweb.org/id117167/ontologies/2013/9/stackont#> 

select * where { 
  ?player :PlayerName "Dave" ;
          :PlayerScore ?score .
  ?team :hasPlayer ?player ;
        :TeamCategory "league" ;
        :TeamColor ?color .
}

使用Jena的命令行工具对数据运行此查询会产生如下结果:

$ sparql --data leagues.n3  --query query.rq
----------------------------------------
| player    | score | team   | color   |
========================================
| :Player3a | "125" | :Team3 | "green" |
| :Player1b | "100" | :Team1 | "red"   |
----------------------------------------

值得更深入地研究三重模式。关于玩家的部分:

  ?player :PlayerName "Dave" ;
          :PlayerScore ?score .

表示?player必须绑定到属性"Dave"的值为PlayerName的某个资源,并且具有PlayerScore属性的值,该属性受绑定到?score。团队的描述类似,但与其他模式“连接”,因为?team的{​​{1}}属性的值必须为hasPlayer

?player

附录A:您的数据暂定完成

我认为您的数据或多或少如下。我在Turtle中展示它,因为它更具人性化。

  ?team :hasPlayer ?player ;
        :TeamCategory "league" ;
        :TeamColor ?color .