Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SPARQL parser for Java Code

Tags:

java

sparql

Is any easy way to parse sparql query in variables for java, like Fyzz in python? How can Jena or sesame APIs been used?

like image 999
Eleftherios K. Avatar asked Jan 24 '13 11:01

Eleftherios K.


2 Answers

You can parse and manipulate SPARQL in java using Apache Jena's ARQ pretty simply, at either the syntax or algebra level. QueryFactory.create(queryString) will provide a java representation of the query. Then poke around:

Query query = QueryFactory.create(queryString);
query.isSelectType() && query.isQueryResultStar(); // of the form SELECT *?
query.getDatasetDescription(); // FROM / FROM NAMED bits
query.getQueryPattern(); // The meat of the query, the WHERE bit
...etc etc..
Op op = Algebra.compile(query); // Get the algebra for the query

(see the Query java documentation)

Try starting with the tutorial 'Manipulating SPARQL using ARQ'. That will give you a feel for how queries are represented, and how to pull things out from them (visitors are especially useful). Although initially the syntax level is most familiar, for many tasks the algebra works better since it corresponds to what a query actually does.

like image 100
user205512 Avatar answered Nov 02 '22 08:11

user205512


Here's how you can parse and manipulate a SPARQL query using Sesame:

To parse:

ParsedQuery pq = QueryParserUtil.parseQuery(QueryLanguage.SPARQL, queryString);

The output of this is a ParsedQuery, which is an algebraic object representation of the query. If you wish to specifically get the parse tree itself, that is also possible:

ASTQueryContainer parseTree = SyntaxTreeBuilder.parseQuery(queryString);

You can then directly manipulate this abstract syntax tree by implementing a custom SyntaxTreeBuilderVisitor (tip: extend ASTVisitorBase so you only have to override the methods where you actually want to do something).

If we go back to the algebraic model, you can execute the ParsedQuery on a Sesame Sail Repository:

if (pq instanceof ParsedTupleQuery) {
        SailTupleQuery query = new SailTupleQuery(pq, repositoryConnection);
        TupleQueryResult result = query.evaluate();
} else if (pq instanceof ParsedGraphQuery) {
        // etc for other query types
}

To manipulate the ParsedQuery before executing it, use a QueryModelVisitor implementation, e.g. your own custom query manipulator:

QueryModelVisitor myVisitor = new MyCustomQueryModelVisitor();
pq.getTupleExpr().visit(myVisitor);

With such a custom query model visitor you have complete control over the query, to optimize it or indeed to rewrite into a different syntax.

Whether to do this manipulation at the level of the abstract syntax tree (AST) or at the level of the query model is a matter of taste: the query model gives you more flexbility in terms of query planning/optimization and partial rewrites (for later execution on a Sesame store), while if your goal is to completely rewrite the query for another purpose (e.g. executing it on a non-Sesame store), manipulating the syntax tree directly might be easier.

As an aside, the above way to parse and execute a query is a roundabout way of doing things. If you do not need to manipulate the parsed query before executing it, you can simply prepare and execute a query on a repository like so:

String queryString = "SELECT ...";
RepositoryConnection conn = repo.getConnection();
try {
   TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
   TupleQueryResult result = tq.evaluate();
}
finally {
   conn.close();
}
like image 5
Jeen Broekstra Avatar answered Nov 02 '22 08:11

Jeen Broekstra