RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Java/JRuby Developers, Say Open 'Sesame' to the Semantic Web : Page 5

The semantic web enables you to use information from disparate sources in different formats/schemas without having to convert the data to a standard format. Get an introduction to semantic web application development using Java and JRuby.

Using Classes in RDFS Modeling
You may be surprised that all of the examples so far have dealt with RDFS properties and not RDFS classes. As previously mentioned, RDFS properties and RDFS classes are orthogonal in the sense that properties are not used to define attributes (or class variables) for RDFS classes. You can add and use properties with classes in an ad hoc way, extending classes and the use of properties at any time. The following example for using classes in RDFS modeling uses the N3 file rdf_files/class_example.n3:

@prefix kb:  <http://knowledgebooks.com/ontology#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/#> .

foaf:Person rdfs:subClassOf foaf:Agent .
kb:KnowledgeEngineer rdfs:subClassOf foaf:Person .

<http://www.markwatson.com/index.rdf> a kb:KnowledgeEngineer .

Notice that the predicate uses the abbreviation a, which means that the subject URI is a member of the class kb:KnowledgeEngineer. The following SPARQL query will print out all the subjects and predicates for triples whose object is equal to foaf:Agent:

require "java"
require "sesame_wrapper.jar"
require 'pp'

include_class "TripleStoreSesameManager"
include_class "DefaultSparqlResultHandler"

tsm = TripleStoreSesameManager.new


sparql_query =
   PREFIX foaf:  <http://xmlns.com/foaf/0.1/#>
   SELECT ?subject ?predicate WHERE { ?subject ?predicate foaf:Agent . }";

tsm.doSparqlQuery(sparql_query, DefaultSparqlResultHandler.new)  

Listing 7 shows the output from running this example. Notice a couple of interesting things:

  • The subject URI <http://knowledgebooks.com/ontology#KnowledgeEngineer> is of type foaf:Agent. By logical inference, my URI is of type kb:KnowledgeEngineer, which is of type foaf:Person, which is of type foaf:Agent.
  • Both foaf:Person and <http://knowledgebooks.com/ontology#KnowledgeEngineer> are of type foaf:Agent.

It is often interesting and useful to make "broad" SPARQL queries like this example to see the triples that Sesame (or any other RDF triple store) asserts through inference.

Where to Go from Here
Now that you have seen how to employ the semantic web's functionality using Java and JRuby, you can write derivative code from the examples in this article to build your own semantic web programs.

I suggest that you look in two directions for starting your own semantic web projects:

  • Publish your own data sources as RDF, and then provide consumers of your data with RDFS and example SPARQL queries to help them get started.
  • Identify sources of RDF data than can enhance your own web applications. Use SPARQL queries to collect data for your own use.

Mark Watson is a Java consultant and the author of 14 books on Java, artificial intelligence, C++, and intelligent agents.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date