Using Sesame Practical Artificial Intelligence Programming With Java
processResultListString data
ISparqlProcessResults
Interface
loadRDFString rdf_file_path_name
close doSparqlQueryString
sparql_query, ISparqlProcessResults
handler : String saveRepositoryAsN3String
output_file_path
TripleStoreSesameManager
Figure 4.2: Java utility classes and interface for using Sesame Figure 4.2 shows a UML diagram for the wrapper classes and interface that I wrote
for Sesame to make it easier for you to get started. My wrapper uses an in-memory RDF repository that supports inference, loading RDFRDFSOWL files, and per-
forming queries. If you decide to use Semantic Web technologies in your develop- ment you will eventually want to use the full Sesame APIs for programatically creat-
ing new RDF triples, finer control of the type of repository options are in-memory, disk based, and database and inferencing, and programatically using query results.
That said, using my wrapper library is a good place for you to start to start experi- menting.
The class constructor T ripleStoreSesameM anager opens a new in-memory RDF triple store. I will not cover the internal implementation of the classes and interface
seen in Figure 4.2 but you can read the source code in the subdirectory src-semantic- web.
We will look in some detail at an example program that uses Sesame and my wrapper library for Sesame. The source code for this example is in the file ExampleSparql-
Queries.java. This example class implements the ISparqlP rocessResults inter- face:
public class ExampleSparqlQueries implements ISparqlProcessResults {
and does this by defining the method: public void processResultListString data {
System.out.printnext result: ; for String s : data
System.out.print|+s+| + \t ;
System.out.println . ; }
68
that simply prints out the subject, predicate, and object of each matched triple. The class T ripleStoreSesameM anager method
public String doSparqlQueryString sparql_query, ISparqlProcessResults
handler { calls a defined processResult method once for each triple that matches a query.
The ExampleSparqlQueries class makes several SPARQL queries and prints the results. These queries are the example queries from the last section. Here is an
example query with the program output:
TripleStoreSesameManager ts = new TripleStoreSesameManager;
ts.loadRDFtest_datanews.n3; sparql_query =
PREFIX kb: http:knowledgebooks.comontology + SELECT ?subject +
WHERE { ?subject kb:containsState \Maryland\ . }; ts.doSparqlQuerysparql_query, this;
Here is the single line of output Sesame debug printout is not shown and the long line is split into two lines to fit the page width:
next result: |http:news.yahoo.comsnm \\ 20080616ts_nmworldleaders_trust_dc_1 |
Other queries in the last section return two or three values per result; this example only returns the subject article URL. You can run the text program implemented in
the class ExampleSparqlQueries to see all of the query results for the examples in the last section.
There is a lot more to RDFS than what I have covered so far in this chapter but I believe you have a sufficient introduction in order to use the example programs to
experiment with using RDF and RDFS to define data and use Sesame in an imbedded mode in your java applications.