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