0

我可以执行相当简单的 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>
4

1 回答 1

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'shasPlayer属性的值必须是?player

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

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

我认为您的数据或多或少如下。我在这里用 Turtle 展示它,因为它更易于人类阅读。

@prefix :      <http://www.semanticweb.org/id117167/ontologies/2013/9/stackont#> .
@prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl:   <http://www.w3.org/2002/07/owl#> .
@prefix xsd:   <http://www.w3.org/2001/XMLSchema#> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

:Team2  a              owl:NamedIndividual ;
        :TeamCategory  "division" ;
        :TeamColor     "blue" ;
        :hasPlayer     :Player2a , :Player2b .

:Player1b  a          owl:NamedIndividual ;
        :PlayerName   "Dave" ;
        :PlayerScore  "100" .

:PlayerName  a              owl:DatatypeProperty ;
        rdfs:domain         :Player ;
        rdfs:range          xsd:string ;
        rdfs:subPropertyOf  owl:topDataProperty .

:hasPlayer  a        owl:ObjectProperty ;
        rdfs:domain  :Team ;
        rdfs:range   :Player .

:Player2a  a          owl:NamedIndividual ;
        :PlayerName   "Steve" ;
        :PlayerScore  "75" .

:PlayerScore  a             owl:DatatypeProperty ;
        rdfs:domain         :Player ;
        rdfs:range          xsd:double ;
        rdfs:subPropertyOf  owl:topDataProperty .

<http://example.org/icecream>
        a       owl:Ontology .

:Team3  a              owl:NamedIndividual ;
        :TeamCategory  "league" ;
        :TeamColor     "green" ;
        :hasPlayer     :Player3a .

:TeamCategory  a     owl:DatatypeProperty ;
        rdfs:domain  :Team ;
        rdfs:range   xsd:string .

:Team   a       owl:Class .

:Player2b  a          owl:NamedIndividual ;
        :PlayerName   "Jack" ;
        :PlayerScore  "200" .

:Team1  a              owl:NamedIndividual ;
        :TeamCategory  "league" ;
        :TeamColor     "red" ;
        :hasPlayer     :Player1a , :Player1b .

:Player1a  a          owl:NamedIndividual ;
        :PlayerName   "Bob" ;
        :PlayerScore  "500" .

:Player3a  a          owl:NamedIndividual ;
        :PlayerName   "Dave" ;
        :PlayerScore  "125" .

:TeamColor  a        owl:DatatypeProperty ;
        rdfs:domain  :Team ;
        rdfs:range   xsd:string .

:Player  a                owl:Class ;
        owl:disjointWith  :Team .
于 2013-10-01T15:53:56.497 回答