How can I find out the type of sparql query using the code?

Gevorg Arutiunian :

String qb = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
                "INSERT DATA\n" +
                "{ \n" +
                "  <http://example/book1> dc:title \"A new book\" ;\n" +
                "                         dc:creator \"A.N.Other\" .\n" +
                "}";

// Here I need to check what type of query I got
String type = ... //some code for checking

if (type == "select") {
   ParsedTupleQuery q = (ParsedTupleQuery)parser.parseQuery(qb, null);
}else if(type == "costruct") {
   ParsedGraphQuery q = (ParsedGraphQuery)parser.parseQuery(qb, null);
}else if(type == "update"){ //here can be insert or delete
   ParsedUpdate q = parser.parseUpdate(qb, null);
}

I can't find a way to find out what type of query it is. Maybe somebody's ever seen it before?

Jeen Broekstra :

Rdf4j has a QueryParserUtil with a convenience method for this. You can use it as follows:

ParsedOperation operation = QueryParserUtil.parseOperation(QueryLanguage.SPARQL, qb, null); 
if (operation instanceof ParsedTupleQuery) {
   ParsedTupleQuery q = (ParsedTupleQuery)operation;
   ...
} else if (operation instanceof ParsedGraphQuery) {
   ParsedGraphQuery q = (ParsedGraphQuery)operation;
   ...
} else if (operation instance ParsedUpdate) {
   ParsedUpdate u = (ParsedUpdate)operation;
   ...
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=149278&siteId=1