如何使用代码找出 sparql 查询的类型?

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


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);
}

我找不到查明查询类型的方法。 也许有人以前见过它?

Rdf4j 有一个 QueryParserUtil 有一个方便的方法。您可以按如下方式使用它:

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;
   ...
}