简体   繁体   中英

how to do reasoning in Jena via Sparql Query

I'm using Jena and Sparql to query the ontology file.

I have

  1. class Tag with two subclasses : C++ and Java.
  2. class Subject with several subclasses, which stand for particular University subjects: "C++ programming","System programming", "Java programming" etc.
  3. ObjectProperty "hasTags" domain:Subject range:Tag. Each class subject has some tag like "Java", "C++"

When executing query

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

which stands for receiving all subjects with tag "Java" I succeed.

So, the aim is to receive all subjects tagged with "Java" and "C++", via quering for "Tag", like this:

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

I supposed this query would return all entities tagged with "Java" or "C++", but it returns nothing.

I want to receive objects with tags "Java" or "C++", writing just "Tag" in a query. What I have to do to achieve this, and is it possible with Jena API?

UPD: here is my ontology file in RDF/XML syntax.

   <?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 -->

You need to ask for things that are subclasses of Tag. Thus, something like

?class rdfs:subClassOf* :Tag

The * means you need to match a path of 0 or more occurrences of rdfs:subClassOf, so ?class can be Tag, or a subclass of Tag, or or subclass of a subclass of Tag, etc. A complete working query would be:

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                                                                     |
-------------------------------------------------------------------------------------------

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM