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.

4.6 OWL: The Web Ontology Language

We have already seen a few examples of using RDFS to define sub-properties in the this chapter. The Web Ontology Language OWL extends the expressive power of 69 RDFS. We will not cover OWL programming examples in this book but this section will provide some background material. Sesame version 2.1 included in the ZIP file for this book does not support OWL DL reasoning “out of the box.” When I need to use OWL DL reasoning in Java applications I use one or more of the following: • ProtegeOwlApis – compatible with the Protege Ontology editor • Pellet – DL reasoner • Owlim – OWL DL reasoner compatible with some versions of Sesame • Jena – General purpose library • OWLAPI – a simpler API using many other libraries OWL is more expressive than RDFS in that it supports cardinality, richer class re- lationships, and Descriptive Logic DL reasoning. OWL treats the idea of classes very differently than object oriented programming languages like Java and Smalltalk, but similar to the way PowerLoom Chapter 3 uses concepts PowerLoom’s rough equivalent to a class. In OWL instances of a class are referred to as individuals and class membership is determined by a set of properties that allow a DL reasoner to infer class membership of an individual this is called entailment. We saw an example of expressing transitive relationships when we were using Pow- erLoom in Section 3.3 where we defined a PowerLoom rule to express that the rela- tion “contains” is transitive. We will now look at a similar example using OWL. We have been using the RDF file news.n3 in previous examples and we will layer new examples by adding new triples that represent RDF, RDFS, and OWL. We saw in news.n3 the definition of three triples using rdfs:subPropertyOf properties to cre- ate a more general kb:containsPlace property: kb:containsCity rdfs:subPropertyOf kb:containsPlace . kb:containsCountry rdfs:subPropertyOf kb:containsPlace . kb:containsState rdfs:subPropertyOf kb:containsPlace . kb:containsPlace rdf:type owl:transitiveProperty . kbplace:UnitedStates kb:containsState kbplace:Illinois . kbplace:Illinois kb:containsCity kbplace:Chicago . We can also infer that: kbplace:UnitedStates kb:containsPlace kbplace:Chicago . 70