11 June 2008

Building a Web Service with Java, Jena and mySQL [Part 3]

Welcome to part three of the Jena Web Service tutorial, aptly named JAWS. In part one and part two of this tutorial we covered the basics with a Java web service. At this point, you should be familiar with the Jena API and already experimenting with your own ways of working with it.
This part of the tutorial is by far the largest method. We are going to build an engine to perform SPARQL queries.

You can get all (a little) of the information you may need on SPARQL with a few searches. If necessary we can discuss how it works in subtle detail post-tutorial. We know that SPARQL is an effective, yet simplified way to query RDF. Because RDF = triples = the way Jena stores data, we can use SPARQL to pull our data.

Since the data will be pulled out into a web service, a multi dimensional array seems most effective here as a return type. Also, this method is specifically written with the assumption that SPARQL is one-directional, that is read-only. In the next tutorial we will cover the insert, update, delete functions not through SPARQL but through Jena's methods.

The first part of the method assigns a name sparqlSelect and looks for two passed parameters. The first is the actual SPARQL query formatted as a string and the second is the Jena model that you created earlier and loaded data into. We also see the String[][] return type. This is means all of our returned data will be stored in this format. For details on how to parse this type of data see my earlier post How to Read Multi-dimensional Arrays in Java.

public static String[][] sparqlSelect(String query, String modelName) {
// open model here
Query sql = QueryFactory.create(query);
QueryExecution qexec = QueryExecutionFactory.create(sql, model);
// next line assumes we are running a SELECT query (very intuitive).
ResultSet results = qexec.execSelect();
// this next little object will let us move around through the results.
ResultSetRewindable rsw = ResultSetFactory.makeRewindable(results);
// ... not done yet ...


Next, we retrieve the variables we queried for into the resultVars ArrayList. This keeps the method dynamic and as a web service you don't want to have to know what is being passed to you, just how to work with it. One could parse this out of the SPARQL but you are better off reading in the results rather than making assumptions about the results. We loop through the results adding them to our ArrayList so we know how big to make our soon to be born multidimensional array.


// ... continued ...

// get the result variables passed from the SQL statement
// and parse them into a list for later retrieval.
// typically s,p,o could be more could be less
ArrayList<string> resultVars = new ArrayList<string>();

int k = 0;
for (Iterator j = results.getResultVars().iterator(); j.hasNext();) {
resultVars.add(k, j.next().toString());
k++;
}
// ... not done yet ...

In the closing parts of the method, we will be declaring our array. The first line in this part sets the array dimensions into a new variable tripleOut. This will ultimately be our returned variable. Next, we set a counter and loop through our results.


String[][] tripleOut = new String[rsw.size()][resultVars.size()];
int m = 0;
while (rsw.hasNext()) {
// get next statement
QuerySolution rs = rsw.nextSolution();
for (int i = 0; i < resultVars.size(); i++) {
tripleOut[m][i] = rs.get(resultVars.get(i)).toString();
}
m++;
}

If you don't like the idea of returning a multidimensional array, return something else. Keep in mind though that if you're working with this as a web service you are limited in how (not what) you can return. This part of the tutorial can be expanded on numerous ways as we will see later when we extend this and query an inferred model.

Here is part three organized into a Java file. Please note if you want to run this as a Java class you will need to add a main method in. This article intends the files to be run as a web service.
JawsPart3.java
In the next part our the tutorial, we will put in the insert, update and delete methods for our triple store.

3 comments:

Indião said...
This comment has been removed by the author.
Indião said...

Hi!
I´m a Brazilian student of Semantic Web.
Your Tutorials are very interesting. I'm anxious to see the 4Th part.

Thanks!

Shamshar said...

Hey!
Thanks for your great work, it's helped me a lot too... but I'm struggling to return an array from my ResultSet? Any help would be appreciated!
Sonny