Djenaoui Hamza
Djenaoui Hamza

Reputation: 15

SPARQL query result in jena syntax

I have a problem with the result of a SPARQL query

The same query gives different results between Protege and Jena

In Protege, the query is:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?subject
    WHERE { ?subject rdfs:label   ?object}

The result is: Strings (label of the symptom)

In Jena, the code :

String path = "file:///D:/onto/owl ontologies/symp.owl";
OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM);
String stringQuery
        = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "
        + "SELECT  ?symptoms "
        + " WHERE { ?symptoms rdfs:label ?object }";



    Query query = QueryFactory.create(stringQuery);
    QueryExecution executeQuery = QueryExecutionFactory.create(query,model);
    org.apache.jena.query.ResultSet res = executeQuery.execSelect();

    model.read(path);

    while (res.hasNext()) {
        QuerySolution qs = res.nextSolution();
        Resource symp = qs.getResource("symptoms");

        System.out.println(symp);
    }

The result is: the URIs

The ontology used: http://purl.obolibrary.org/obo/symp.owl

How can I get only the labels "Symptoms" Thanks for help.

Upvotes: 1

Views: 759

Answers (1)

UninformedUser
UninformedUser

Reputation: 8465

You have to select the object instead of the subject in your query:

PREFIX rdfs: http://www.w3.org/2000/01/rdf-schema#

SELECT ?label WHERE { ?symptoms rdfs:label ?label}

In general, giving the variables "better" names can avoid such problems like in your case.

And then you have to get the lexical form of the literal in the Java code:

while (res.hasNext()) {
        QuerySolution qs = res.next();
        String symp = qs.getLiteral("label").getLexicalForm();

        System.out.println(symp);
}

The reason why Protege shows you some human readable form even for your query is that a default renderer is set in the UI. Either something that uses the short from of a URI, or the rdfs:label or some custom rendering.

Upvotes: 4

Related Questions