0

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 -->
  • 1
    Your question as it stands is unanswerable, you need to provide a minimal complete example. In this case you haven't shown your data so we can't tell why your second query does not match anything – RobV Jun 10 '14 at 13:02
  • Show some data. Once you've shown your ontology data, the query will be easy to write, I expect. – Joshua Taylor Jun 10 '14 at 13:29
  • "Each class subject [sic] has some tag like "Java", "C++"" Do you mean that for each instance of subject there's a triple "subject73 hasTag Java", or that you've got subclass axioms with restrictions like "SystemsProgramming subClassOf (hasTag value C)"? These will lead to very different queries. Your queries suggest the latter, but as @RobV says, you need to provide sample data here. – Joshua Taylor Jun 10 '14 at 14:32
  • Thankx for attention, @JoshuaTaylor and RobV . updated post with data. – Oleh Zaiats Jun 26 '14 at 18:29

1 Answers1

1

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                                                                     |
-------------------------------------------------------------------------------------------
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353