Rdf 如何通过Sparql查询在Jena中进行推理
我正在使用Jena和Sparql查询本体文件 我有Rdf 如何通过Sparql查询在Jena中进行推理,rdf,sparql,jena,owl,ontology,Rdf,Sparql,Jena,Owl,Ontology,我正在使用Jena和Sparql查询本体文件 我有 类标记有两个子类:C++和java。李> 课程主题包含几个子类,代表特定的大学主题:“C++编程”、“系统编程”、“Java编程”等 ObjectProperty“hasTags”域:主题范围:标记。每个类主题都有一些标记,如“Java”、“C++” 执行查询时 SELECT ?subject WHERE { ?subject owl:equivalentClass ?restriction . ?restriction owl:onP
SELECT ?subject
WHERE
{ ?subject owl:equivalentClass ?restriction .
?restriction owl:onProperty ont:hasTags .
?restriction ?restrictType ont:Java
}
它表示接收带有标记“Java”I success的所有主题
因此,目标是通过查询“标记”来接收所有标记为“Java”和“C++”的主题,如下所示:
SELECT ?subject
WHERE
{ ?subject owl:equivalentClass ?restriction .
?restriction owl:onProperty ont:hasTags .
?restriction ?restrictType ont:Tag
}
我以为这个查询将返回所有标记为“Java”或“C++”的实体,但它什么也不返回
我想接收带有标记“Java”或“C++”的对象,在查询中只写“Tag”。为了实现这一点,我必须做些什么,使用Jena API是否可行
UPD:这是我的RDF/XML语法的本体文件
<?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#" >
]>
<rdf:RDF xmlns="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#"
xml:base="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
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/man/ontologies/2014/5/untitled-ontology-11">
<rdfs:label>University subjects ontology
</rdfs:label>
</owl:Ontology>
<!--
///////////////////////////////////////////////////////////////////////////////////////
//
// Object Properties
//
///////////////////////////////////////////////////////////////////////////////////////
-->
<!-- http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#hasTags -->
<owl:ObjectProperty rdf:about="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#hasTags">
<rdf:type rdf:resource="&owl;TransitiveProperty"/>
<rdfs:domain rdf:resource="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Subject"/>
<rdfs:range rdf:resource="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Tag"/>
</owl:ObjectProperty>
<!--
///////////////////////////////////////////////////////////////////////////////////////
//
// Classes
//
///////////////////////////////////////////////////////////////////////////////////////
-->
<!-- http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#C++ -->
<owl:Class rdf:about="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#C++">
<rdfs:subClassOf rdf:resource="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Programming"/>
</owl:Class>
<!-- http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#C++_programming -->
<owl:Class rdf:about="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#C++_programming">
<owl:equivalentClass>
<owl:Restriction>
<owl:onProperty rdf:resource="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#hasTags"/>
<owl:someValuesFrom rdf:resource="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#C++"/>
</owl:Restriction>
</owl:equivalentClass>
<rdfs:subClassOf rdf:resource="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Subject"/>
</owl:Class>
<!-- http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Java -->
<owl:Class rdf:about="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Java">
<rdfs:subClassOf rdf:resource="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Programming"/>
</owl:Class>
<!-- http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Java_programming -->
<owl:Class rdf:about="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Java_programming">
<owl:equivalentClass>
<owl:Restriction>
<owl:onProperty rdf:resource="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#hasTags"/>
<owl:someValuesFrom rdf:resource="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Java"/>
</owl:Restriction>
</owl:equivalentClass>
<rdfs:subClassOf rdf:resource="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Subject"/>
</owl:Class>
<!-- http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Programming -->
<owl:Class rdf:about="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Programming">
<rdfs:subClassOf rdf:resource="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Tag"/>
</owl:Class>
<!-- http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Subject -->
<owl:Class rdf:about="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Subject"/>
<!-- http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#System_Programming -->
<owl:Class rdf:about="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#System_Programming">
<owl:equivalentClass>
<owl:Restriction>
<owl:onProperty rdf:resource="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#hasTags"/>
<owl:someValuesFrom rdf:resource="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Java"/>
</owl:Restriction>
</owl:equivalentClass>
<owl:equivalentClass>
<owl:Restriction>
<owl:onProperty rdf:resource="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#hasTags"/>
<owl:someValuesFrom rdf:resource="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#C++"/>
</owl:Restriction>
</owl:equivalentClass>
<rdfs:subClassOf rdf:resource="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Subject"/>
</owl:Class>
<!-- http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Tag -->
<owl:Class rdf:about="http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#Tag"/>
</rdf:RDF>
<!-- Generated by the OWL API (version 3.4.2) http://owlapi.sourceforge.net -->
]>
大学学科本体论
您需要请求属于标记子类的内容。因此
?class rdfs:subClassOf* :Tag
*表示您需要匹配0个或多个rdfs的路径:Subassof,因此?类可以是Tag,或Tag的子类,或Tag的子类,等等。完整的工作查询将是:
前缀:
前缀owl:
前缀rdfs:
选择不同的?主题在哪里{
?受试者owl:等效类?限制。
?限制owl:onProperty:hasTags。
?限制?限制类型?等级。
?类别rdfs:子类别*:标签
}
-------------------------------------------------------------------------------------------
|主题|
===========================================================================================
|:Java_编程|
| |
|:系统编程|
-------------------------------------------------------------------------------------------
您目前的问题无法回答,您需要提供一个最小的完整示例。在这种情况下,您没有显示数据,因此我们无法说明为什么第二个查询与任何内容都不匹配Show some data。一旦你展示了你的本体数据,查询将很容易编写,我想,“每个类主题[sic]都有一些标记,比如“Java”,“C++”,你的意思是每个主题的实例都有三个“subject73 hasTag Java”,还是说你有一些子类公理,有一些限制,比如“SystemsProgramming subClassOf(hasTag value C)”?这些将导致非常不同的查询。您的查询建议使用后者,但正如@RobV所说,您需要在此处提供示例数据。感谢您的关注,@JoshuaTaylor和RobV。用数据更新了帖子。
-------------------------------------------------------------------------------------------
| subject |
===========================================================================================
| :Java_programming |
| <http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#C++_programming> |
| :System_Programming |
-------------------------------------------------------------------------------------------