I’ve been using Jena for a while in my research project, however I have recently been trying to use SPARQL queries to help my program do some tasks more efficiently.
I tested a set of queries using Twinkle ( http://www.ldodds.com/projects/twinkle/ ) and got the desired results – but when implementing them in Jena additional results were returned.
For example in Twinkle
SELECT ?x WHERE { ?x rdfs:domain ns:Area . ?x rdfs:range ns:Structure }
returns 1 result in twinkle ( ns:Contains ), whereas when run in my program using Jena it returns another property ( ns:testProperty ) which it should not as the range and domain do not match. I can’t figure out why there is this discrepancy, any pointers would be appreciated.
My Java code is as follows:
Query q = sparqlQueryGetProperties(s, o);
QueryExecution qexec = QueryExecutionFactory.create(q, m);
try {
Iterator<QuerySolution> rs = qexec.execSelect();
for (; rs.hasNext();) {
QuerySolution soln = rs.next();
if(soln.contains("x")){
RDFNode r = soln.get("x");
Resource rss = r.asResource();
props.add(rss.getLocalName());
}
}
} finally {
qexec.close();
}
Other information:
Sparql v1.0
Jena Core 2.7.4
Jena ARQ 2.9.4
Full query used for testing:
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
PREFIX ns:<http://www.semanticweb.org/ontologies/2012/1/Ontology1328444427428.owl#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
SELECT ?x WHERE { ?x rdfs:domain ns:Area . ?x rdfs:range ns:Structure }
UPDATE
Unfortunately I am still getting additional resources returned from my test cases in TWINKLE
Here is the exact SPARQL query being run on both Twinkle and in Jena:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX ns: <http://www.semanticweb.org/ontologies/2012/1/Ontology1328444427428.owl#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?prp ?x
WHERE
{ ns:cathedral ?prp ?x .
?x rdf:type owl:NamedIndividual .
FILTER ( ?prp != ns:hasShape )
}
Results in TWINKLE:
?prp = ns:within ?x = ns:Campus
Results in JENA:
(?prp -> ?x)
sameAs -> cathedral
disjoin -> cathedral
differentFrom -> Cath_point_4 //This particular relationship seems completely random.
topObjectProperty -> cathedral
within -> Campus
Here is the data I am using (this is a ontology I have developed for testing only):
http://cgi.csc.liv.ac.uk/~roscminni/ontResources/spatialOntCopy.owl
Is there a better tool for testing queries that Twinkle taking into account that Twinkle appears to be out of date?
The difference is that in your Jena code you use a
OntModelwhile Twinkle uses only a normalModelInternally in Jena a SPARQL query over a
Modeltranslates into a number offind()calls on theModel. With anOntModelthosefind()calls will include inferred triples hence the extra results.What additional results you get with an
OntModelwill depend on what ruleset you chose when you created your model.