如何通过Sparql Query在Jena中进行推理

时间:2014-06-10 08:43:37

标签: rdf sparql jena owl ontology

我使用Jena和Sparql查询本体文件。

我有

  1. class带有两个子类的标签:C ++和Java。
  2. class具有多个子类的主题,代表特定的大学科目:" C ++编程","系统编程"," Java编程"等
  3. ObjectProperty" hasTags"域:主题范围:标记。每个课程科目都有一些标签,如" Java"," C ++"
  4. 执行查询时

    SELECT ?subject
    WHERE
    {  ?subject owl:equivalentClass ?restriction .
       ?restriction owl:onProperty ont:hasTags .
       ?restriction ?restrictType ont:Java
    }
    

    代表接收所有带标签的主题" Java"我成功了。

    因此,目标是接收标记为" Java"和" C ++",通过查询" Tag",像这样:

    SELECT ?subject
    WHERE
    {  ?subject owl:equivalentClass ?restriction .
       ?restriction owl:onProperty ont:hasTags .
       ?restriction ?restrictType ont:Tag
    }
    

    我认为这个查询将返回标记为" Java"的所有实体。或" C ++",但它什么都不返回。

    我希望收到带有标签的对象" Java"或" C ++",只写"标记"在查询中。我需要做些什么来实现这一点,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 -->
    

1 个答案:

答案 0 :(得分:1)

您需要询问Tag的子类。因此,像

?class rdfs:subClassOf* :Tag

*表示你需要匹配一个0或更多次出现的rdfs:subClassOf的路径,所以?class可以是Tag,Tag的子类,或者Tag的子类的子类,等等。一个完整的工作查询将是:

prefix :      <http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#> 
prefix owl:   <http://www.w3.org/2002/07/owl#> 
prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> 

select distinct ?subject where {
   ?subject owl:equivalentClass ?restriction .
   ?restriction owl:onProperty :hasTags .
   ?restriction ?restrictType ?class .
   ?class rdfs:subClassOf* :Tag 
}
-------------------------------------------------------------------------------------------
| subject                                                                                 |
===========================================================================================
| :Java_programming                                                                       |
| <http://www.semanticweb.org/man/ontologies/2014/5/untitled-ontology-11#C++_programming> |
| :System_Programming                                                                     |
-------------------------------------------------------------------------------------------