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

时间:2019-05-08 11:43:31

标签: java sparql rdf rdf4j


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

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

1 个答案:

答案 0 :(得分:2)

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