AntonioR
AntonioR

Reputation: 1

How can i print a rdfs label with Jena

Hi i have create a new ontology for relate Drugs and diseases; now i query its for recovering the name of Drug related a particular disease. My code is:

public class Main {

public static void main(String[] args) {

    org.apache.log4j.Logger.getRootLogger().setLevel(org.apache.log4j.Level.OFF);
    FileManager.get().addLocatorClassLoader(Main.class.getClassLoader());
    Model model = FileManager.get().loadModel("Drugs_Diseases.owl");
    String strQuery = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "+
                       "PREFIX des: <http://www.entrez.com/> "+
                       "SELECT DISTINCT ?subject ?object "
                       + "WHERE {   "
                       + "?subject des:isDrugOfDisease ?object .  "
                       + "?object rdfs:label ?label . "
                       + "FILTER regex(str(?label), \"pain\") }";

    Query query = QueryFactory.create(strQuery);
    QueryExecution execQuery = QueryExecutionFactory.create(query, model);
    try{
        ResultSet results = execQuery.execSelect();

        while(results.hasNext()){
            QuerySolution sol = results.nextSolution();
            RDFNode node = sol.get("subject");
            String stringPrint = node.asNode().toString();
            System.out.println(stringPrint);
        }
    } finally {
        execQuery.close();

}}
}

The results of this query is:

http://www.entrez.com/DAP000017
http://www.entrez.com/DAP000021

but my intent is to have:

Diazepam 
Dextropropoxyphene

in OWL i have:

 <owl:Class rdf:about="http://www.entrez.com/DAP000021">
    <entrez:isDrugOfDisease>
      <owl:Class rdf:about="http://orpha.net/ORDO/Orphanet_#DIS_ID_75"/>
    </entrez:isDrugOfDisease>
    <entrez:id>DAP000021</entrez:id>
    <rdfs:label>Diazepam</rdfs:label>
  </owl:Class>

Upvotes: 0

Views: 470

Answers (1)

UninformedUser
UninformedUser

Reputation: 8465

SPARQL only returns what you select, thus, you have to select the label in the SPARQL query of course:

SELECT DISTINCT ?drug ?disease ?drugLabel WHERE
 ?drug des:isDrugOfDisease ?disease .
 ?drug rdfs:label ?drugLabel .
 ?disease rdfs:label ?label . 
 FILTER regex(str(?label), \"pain\") 
}

In the code, get the label from the solution object:

while(results.hasNext()){
        QuerySolution sol = results.nextSolution();
        RDFNode node = sol.get("drug");
        String stringPrint = node.asNode().toString();
        System.out.println(stringPrint);
        // label
        String label = sol.getLiteral("drugLabel").getLexicalForm();
        System.out.println(label);
}

Note, I changed the variable names. In general, it's easier to read and understand a query if you use meaningful names for the variables.

Upvotes: 1

Related Questions