I'm using Jena and Sparql to query the ontology file.
I have
- class Tag with two subclasses : C++ and Java.
- class Subject with several subclasses, which stand for particular University subjects: "C++ programming","System programming", "Java programming" etc.
- 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
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: