Here my sparql query code is to find Least Common Subsumer and output of query must be 'A'. I am getting blank result here. I think my query is right but seems to be Apache Jena's "filter not exists" is not working. I have also checked with other example. Is there any alternative solution of "filter not exists" or shall i need to modify my java code for that?
OWL file structure
Thing:
|
A
|_P(p1,p2)
|_M(m1,m2)
|
B
Here A,P,M and B are the concepts. p1,p2,m1 and m2 are the instances. M is subclass of P and P is subclass of A. A and B is the subclass of Thing.
JAVA Code
import org.apache.jena.iri.impl.Main;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.InfModel;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.reasoner.Reasoner;
import com.hp.hpl.jena.reasoner.ReasonerRegistry;
import com.hp.hpl.jena.util.FileManager;
public class SPARQLReasoner {
public static void main(String args[]) {
sparqlTest();
}
public static void sparqlTest() {
FileManager.get().addLocatorClassLoader(Main.class.getClassLoader());
Model model;
model = FileManager.get().loadModel("C:\\Users\\Chetan\\Desktop\\test.owl");
Reasoner reasoner=ReasonerRegistry.getOWLReasoner();
reasoner = reasoner.bindSchema(model);
InfModel infmodel = ModelFactory.createInfModel(reasoner, model);
String queryString;
queryString = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "
+ "PREFIX owl: <http://www.w3.org/2002/07/owl#> "
+ "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> "
+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "
+ "PREFIX : <http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#> "
+"SELECT ?lcs WHERE{ "
+"?lcs ^(rdf:type/(rdfs:subClassOf)*) :p1 . "
+"?lcs ^(rdf:type/(rdfs:subClassOf)*) :m1 . "
+"?lcs rdf:type owl:Class . "
+"FILTER NOT EXISTS {?llcs ^(rdf:type/(rdfs:subClassOf)*) :p1 . "
+"?llcs ^(rdf:type/(rdfs:subClassOf)*) :m1 . "
+"?llcs rdf:type owl:Class . "
+"?llcs (rdfs:subClassOf)+ ?lcs . "
+"} "
+"}";
Query query= QueryFactory.create(queryString);
QueryExecution qexec= QueryExecutionFactory.create(query, infmodel);
try{
ResultSet results=qexec.execSelect();
while(results.hasNext()){
QuerySolution soln=results.nextSolution();
Resource r;
r=soln.getResource("lcs");
System.out.println(r);
}
}finally{
qexec.close();
}
}
}
OWL Code:
<rdf:RDF xmlns="http://www.semanticweb.org/chetan/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/chetan/ontologies/2014/5/untitled-ontology-11"/>
<owl:Class rdf:about="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#A"/>
<owl:Class rdf:about="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#B"/>
<owl:Class rdf:about="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#M">
<rdfs:subClassOf rdf:resource="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#P"/>
</owl:Class>
<owl:Class rdf:about="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#P">
<rdfs:subClassOf rdf:resource="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#A"/>
</owl:Class>
<owl:NamedIndividual rdf:about="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#m1">
<rdf:type rdf:resource="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#M"/>
</owl:NamedIndividual>
<owl:NamedIndividual rdf:about="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#m2">
<rdf:type rdf:resource="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#M"/>
</owl:NamedIndividual>
<owl:NamedIndividual rdf:about="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#p1">
<rdf:type rdf:resource="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#P"/>
</owl:NamedIndividual>
<owl:NamedIndividual rdf:about="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#p2">
<rdf:type rdf:resource="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#P"/>
</owl:NamedIndividual>
</rdf:RDF>
Please give your views here.