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


Let Semantics Bring Sophistication to Your Applications : Page 4

Leverage semantic technology and the W3C's Web Ontology Language standard to spice up a query-based application.

Querying Ontologies
You've either built an ontology or have found one from some source—now what? By themselves, ontologies are interesting but not very useful. To do valuable things with ontologies, you must be able to get meaningful information from them. Different tools have different ways of getting information out of an ontology, and you will see two of them in this discussion. The first will be to use the Java Jena library to access your wine ontology programmatically. The Jena interface includes facilities to read and navigate an ontology, which will be demonstrated shortly as you build your sommelier application.

A draft W3C specification is in the works to create a standard query language for RDF data named SPARQL (which recursively stands for SPARQL Protocol and RDF Query Language). Because OWL is an extension of RDF, you will be able to mine information from your OWL-based wine ontology using SPARQL.

SPARQL is similar to SQL in form but different in function. Whereas SQL is based on set theory, SPARQL is based on graph theory. SPARQL queries construct a graph that is pattern-matched against the RDF graph, and matching structures are returned. This querying technique is pretty abstract stuff, so look at a couple of examples. A basic SPARQL query that selects all the triples from an ontology would look like this:

SELECT ?subject ?predicate ?object WHERE { ?subject ?predicate ?object }

Did you notice that this form looks a lot like how I defined statements previously? It looks similar because the WHERE clause is basically creating a small ontology to query with. I can specify a combination of subjects, predicates, and objects to query with, and the wildcards that I leave blank will be accessible through the SELECT clause. For example, to list all subjects and objects from the wine ontology related by the hasColor predicate I could execute this query:

SELECT ?subject ?object WHERE { ?subject <#hasColor> ?object }

Because hasColor is a property, this predicate will apply only to individuals in the ontology. Thus, this query will return all the individual instances related to other instances through the hasColor property.

I can also select only those instances having a particular color by specifying the value in which I'm interested:

SELECT ?subject ?object WHERE { ?subject <#hasColor> <#Red> }

And, I can further refine my wine search by including more triples in the graph I specify in the WHERE clause:

SELECT ?subject ?object WHERE { ?subject <#hasColor> <#Red> . ?subject <#hasFlavor> <#Strong> . ?subject <#hasBody> <#Full> . }

Notice that statements are separated by a period. The ontology will be searched for patterns matching the union of the three statements listed previously.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date