-2

I want to code a parameterized SPARQL Query in Java Jena where one of the triple in query be injected

so in the code below I need to inject value as a string the pass to the class

However, the SPARQL query is correct so when I replace "value" with the class name I got the right result

I tried two code non of them worked No result or run time error

the first code:

package ontology;

import org.apache.jena.iri.impl.Main;
import com.hp.hpl.jena.query.ParameterizedSparqlString;
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.Model;
import com.hp.hpl.jena.util.FileManager;

public class SPARQL {
     public static void sparqlTest( String str)
     {
FileManager.get().addLocatorClassLoader(Main.class.getClassLoader());
Model model=FileManager.get().loadModel("ASO.owl");
    String queryString=

                "PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>"+ 
                "PREFIX rdf:< http://www.w3.org/1999/02/22-rdf-syntax-ns#>"+
                "PREFIX HASO:< http://www.semanticweb.org/rabaa006/ontologies/2014/4/HASO#>"+

                 "SELECT  ?x  "+

                 "WHERE"+ 

                 "      {?x rdfs:subClassOf  HASO:Affective_State}";


   ParameterizedSparqlString queryStr = new   ParameterizedSparqlString(queryString);
   queryStr.setLiteral("value", str);

   Query  query=QueryFactory.create(queryStr.toString());

   QueryExecution qexec = QueryExecutionFactory.create(query,model);

   try {
   ResultSet results = qexec.execSelect();

   while ( results.hasNext()){
           QuerySolution soln = results.nextSolution();
           String strg=soln.getResource("?x").toString();
           //System.out.println(strg);
             String number = strg.substring(strg.lastIndexOf("#") + 1);
             System.out.println(number);

   }}

   finally{
   qexec.close();}

}
}

The Second code:

package ontology;

import org.apache.jena.iri.impl.Main;
import com.hp.hpl.jena.query.ParameterizedSparqlString;
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.Model;
import com.hp.hpl.jena.util.FileManager;

public class SPARQL {
public static void sparqlTest( String str)
     {

FileManager.get().addLocatorClassLoader(Main.class.getClassLoader());
Model model=FileManager.get().loadModel("ASO.owl");

   ParameterizedSparqlString pss = new ParameterizedSparqlString();
   pss.setCommandText (
                       "PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>"+ 

                       "PREFIX rdf:< http://www.w3.org/1999/02/22-rdf-syntax-ns#>"+

                       "PREFIX HASO:< http://www.semanticweb.org/rabaa006/ontologies/2014/4/HASO#>"+


                 "SELECT  ?x  "+
                  "WHERE"+ 
    "      {?x rdfs:subClassOf  HASO:valuee}");
    pss.setLiteral("value", str);

   Query  query=QueryFactory.create(pss.toString());
   QueryExecution qexec = QueryExecutionFactory.create(query,model);

   try {
    ResultSet results = qexec.execSelect();

   while ( results.hasNext()){
           QuerySolution soln = results.nextSolution();
           String strg=soln.getResource("?x").toString();
           //System.out.println(strg);
             String number = strg.substring(strg.lastIndexOf("#") + 1);
             System.out.println(number);

   }}

  finally{

 qexec.close();}
 }
}
zoran jeremic
  • 2,046
  • 4
  • 21
  • 47

2 Answers2

0

It's been a while since I didn't use SPARQL, but I don't see how you are actually injecting value parameter in your code. It should be something like this:

   pss.setCommandText (
        "PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>"+ 
        "PREFIX rdf:< http://www.w3.org/1999/02/22-rdf-syntax-ns#>"+
        "PREFIX HASO:< http://www.semanticweb.org/rabaa006/ontologies/2014/4/HASO#>"+


             "SELECT  ?x  "+
              "WHERE"+ 
                   "{?x HASO:value ?value}");
                    pss.setLiteral("value", str);

Maybe you will use some other predicate, but in order to inject value, you should have ?value in your query as object.

zoran jeremic
  • 2,046
  • 4
  • 21
  • 47
  • I tried the following , but with no result pss.setCommandText ( "PREFIX rdfs:"+ "PREFIX rdf: "+ "PREFIX HASO: "+ "SELECT ?x "+ "WHERE"+ " {?x rdfs:subClassOf ?value}"); pss.setLiteral("value", "HASO:Affective_State"); Query query=QueryFactory.create(pss.toString()); – rana othman Mar 24 '17 at 15:16
  • The problem is that you are trying to set resource as literal value. When you have triple {?person name ?namevalue} you will set namevalue as literal, and you should use pss.setLiteral("nameValue", "Bob"). If you have triple {?person rdfs:subClassOf ?entity} you should not set it as literal but as resource. It should be something like pss.setIni("entity","http://someurihere/Entity") – zoran jeremic Mar 24 '17 at 15:54
  • setIni is for ( string, iri) not ( string, string ) the next String will be passed by another class to the class that has SPARQL Query – rana othman Mar 24 '17 at 19:03
  • what if I write the following ? Node n= NodeFactory.createVariable("HASO:Affective_State"); pss.setParam("value", n); or Node n= NodeFactory.createLiteral("HASO:Affective_State"); pss.setParam("value", n); – rana othman Mar 24 '17 at 19:40
  • ParameterizedSparqlString pss = new ParameterizedSparqlString(); pss.setCommandText ( "SELECT ?x "+ "WHERE"+ " {?x rdfs:subClassOf ?value}"); pss.setNsPrefix("rdfs", "http://www.w3.org/2000/01/rdf-schema#"); pss.setNsPrefix("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); pss.setNsPrefix("HASO","http://www.semanticweb.org/rabaa006/ontologies/2014/4/HASO#"); Node n= NodeFactory.createLiteral("HASO:Affective_State"); System.out.println(n); pss.setParam("value", n); n= "HASO:Affective_State" maybe this cause the error the " in n – rana othman Mar 24 '17 at 20:01
  • 1
    setIri has 2 APIs one with IRI and one with String as second argument, so you can pass it as I suggested as string value. You can also pass Node using setParam method as you suggested, but you are again creating Literal instead. You should rather use NodeFactory.createURI in your case. – zoran jeremic Mar 25 '17 at 01:09
0

The problem with using setLitral() is injecting String into query bounded by double quotes like:

String queryString
            = "PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
            + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"
            + "PREFIX HASO: <http://www.semanticweb.org/rabaa006/ontologies/2014/4/HASO#>"
            + "SELECT ?x WHERE {\n"
            + "  ?x rdfs:subClassOf ?clazz \n"
            + "}\n";

    ParameterizedSparqlString queryStr = new ParameterizedSparqlString(queryString);

    queryStr.setLiteral("clazz", "HASO:Affective_State");

    System.out.println(queryStr.toString());

The result of Query will be like:

PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX HASO: <http://www.semanticweb.org/rabaa006/ontologies/2014/4/HASO#>SELECT ?x WHERE {
  ?x rdfs:subClassOf "HASO:Affective_State" 
}

As you see above ?x rdfs:subClassOf "HASO:Affective_State" where "HASO:Affective_State" . is wrong class name because it contains "" so no results will return if you execute that query. The SOLUTION Use String.format instead of ParameterizedSparqlString:: setLiteral:

String q
            = "PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
            + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"
            + "PREFIX HASO: <http://www.semanticweb.org/rabaa006/ontologies/2014/4/HASO#>"
            + "SELECT ?x WHERE {\n"
            + "  ?x rdfs:subClassOf %s \n"
            + "}\n"
            + "LIMIT %d";

    q = String.format(q, "HASO:Affective_State", 2);
    System.out.println(q);

The Result :

PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX HASO: <http://www.semanticweb.org/rabaa006/ontologies/2014/4/HASO#>SELECT ?x WHERE {
  ?x rdfs:subClassOf HASO:Affective_State 
}
LIMIT 2

As you see above HASO:Affective_State and 2 have been injected correctly, so you can inject as many params as you like using String.format [%s for string, %d for digit, and so on..]

Full Example:

public static void main(String[] args) {

    JenaSystem.init();
    UpdateFactory.create();

    Model model = FileManager.get().loadModel("ASO.owl");

    String q
            = "PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
            + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"
            + "PREFIX HASO: <http://www.semanticweb.org/rabaa006/ontologies/2014/4/HASO#>"
            + "SELECT ?x WHERE {\n"
            + "  ?x rdfs:subClassOf %s \n"
            + "}\n"
            + "LIMIT %d";

    q = String.format(q, "HASO:Affective_State", 2);
    System.out.println(q);
    Query query = QueryFactory.create(q);
    QueryExecution qexec = QueryExecutionFactory.create(query, model);
    ResultSet resSet = qexec.execSelect();

    while (resSet.hasNext()) {

        QuerySolution soln = resSet.nextSolution();
        String strg = soln.getResource("?x").toString();
        System.out.println(">>>>>>> " + strg);
        String number = strg.substring(strg.lastIndexOf("#") + 1);
        System.out.println("<<<<<< " + number);

    }


}
  • zoran jeremic here the code with SetParam, but still no result ParameterizedSparqlString pss = new ParameterizedSparqlString(); pss.setCommandText ( "SELECT ?x "+ "WHERE"+ " {?x rdfs:subClassOf ?value}"); pss.setNsPrefix("rdfs", "http://www.w3.org/2000/01/rdf-schema#"); pss.setNsPrefix("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); pss.setNsPrefix("HASO","http://www.semanticweb.org/rabaa006/ontologies/2014/4/HASO#"); Node n= NodeFactory.createURI ("HASO:Affective_State"); System.out.println(n); pss.setParam("value", n); – rana othman Mar 28 '17 at 15:25