2

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?

roscminni
  • 65
  • 7
  • 3
    Pretty hard to debug this without a pointer to the dataset you're using. Also, can you add a println to ensure the query being executed is what you're expecting? – Alex Nov 02 '12 at 14:30
  • 1
    As Alex says it's impossible to debug without data, also I would note that Twinkle is very out of date (I believe it's using ARQ 2.6.4 as the query engine) while the current release of ARQ is 2.9.4. A lot has changed in the SPARQL spec (and thus ARQ) between those versions. Also you haven't shown how you create your data, did you perhaps use an InferenceModel in your Jena code which might explain the additional results. – RobV Nov 02 '12 at 22:57
  • Thanks for your comments. The data being used was an OntModel derived from my own .owl file. Also the query was identical to that tried in TWINKLE. After working on another part of my program and then returning to this problem the issue appears to have gone away. If I find out what was happening it I will post an answer. – roscminni Nov 05 '12 at 13:28

1 Answers1

2

The difference is that in your Jena code you use a OntModel while Twinkle uses only a normal Model

Internally in Jena a SPARQL query over a Model translates into a number of find() calls on the Model. With an OntModel those find() calls will include inferred triples hence the extra results.

What additional results you get with an OntModel will depend on what ruleset you chose when you created your model.

RobV
  • 26,016
  • 10
  • 71
  • 114
  • Yep, that appears to have done the trick. Looks like I will have to build two model objects as I require the inference capability of OntModel for other features I wish to incorporate. – roscminni Nov 06 '12 at 19:56
  • 1
    If you do have an `OntModel` you can call `getBaseModel()` to get the underlying model i.e. get a `Model` containing only the non-inferred triples – RobV Nov 06 '12 at 20:32