Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

If You Build It, They Can Know : Page 2

Learn how to build ontologies to create more expressive applications.


advertisement
Developing the Type-Oriented Hierarchy
I'll present a step-by-step guide to developing a small ontology such as the clothing hierarchy described here. Because the focus of this article is on developing the ontology itself I won't delve deeply into the Java code used to query it; you can see more such code in my previous article, or you can download the accompanying source code for this article.

In the spirit of limiting the modeling work to tasks that will provide value from the user's perspective, it's useful to start by developing some unit tests upfront. For this example I have added eight clothing items for purchase into the store's catalog as shown in Table 1.

Table 1. The table shows an initial eight-item set created for the clothing ontology.
Name Type
RelaxedFitStonewashedJeans Jeans
StretchGauchoJeans Jeans
BeachTrekkerShoes Shoes
MonkStrapLoafers Shoes
PlainToeHighHeelPumps Shoes
ModernKnitSkirt Skirt
CottonTartanNecktie Tie
SilkMiniBoxesNecktie Tie

With this inventory I would expect to be able to execute the following unit tests against the application:

src/test/java/com/devx/clothing/catalog/CategoryTest.java ... @Test public void testListItems_Clothing() { // ensure eight items Category category = catalogManager.findCategoryByName("Clothing"); assertEquals(8, category.listItems().size()); } @Test public void testListItems_Jeans() { // ensure two items of type "Jeans" Category category = catalogManager. findCategoryByName("Jeans"); assertEquals(2, category.listItems().size()); } @Test public void testListItems_Shoes() { // ensure three items of type "Shoes" Category category = catalogManager. findCategoryByName("Shoes"); assertEquals(3, category.listItems().size()); } @Test public void testListItems_Skirt() { // ensure one item of type "Skirt" Category category = catalogManager. findCategoryByName("Skirt"); assertEquals(1, category.listItems().size()); } @Test public void testListItems_Ties() { // ensure two items of type "Tie" Category category = catalogManager. findCategoryByName("Tie"); assertEquals(2, category.listItems().size()); } ...

Note that the tests assert that the number of instances defined in the various categories all roll up to the root clothing category. Now you need to build enough of the clothing catalog ontology to make these tests pass.

An ontology consists of a collection of statements about the domain in the form of "<Subject><Predicate><Object>." An example statement for the clothing domain would be "<A skirt><is worn by><a woman>." There are many standards for representing ontologies but I have chosen to use OWL for this article because it is well-suited for web applications and supports inferencing (see the OWL Guide for more information).

OWL-based ontologies can be stored in many forms including XML files, databases, and in-memory representations. I've used XML here because it is the simplest way to begin developing ontologies quickly. OWL extends the W3C Resource Description Framework (RDF), so OWL documents begin with an RDF element and a namespace declaration.

<?xml version="1.0"?> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:xsd="http://www.w3.org/2001/XMLSchema#" xmlns="http://www.devx.com/clothing-1181607401#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:owl="http://www.w3.org/2002/07/owl#" xmlns:p1="http://www.owl-ontologies.com/assert.owl#" xml:base="http://www.devx.com/clothing-1181607401">

This header declares contained elements to be valid RDF elements and also declares namespaces that will be referenced later in the document. The RDF and RDFS (RDF Schema) namespaces correspond to their respective W3C standards for defining metamodels, or ontologies. The XSD namespace refers to the standard XML schema specification and will be used to define relationships to primitive elements. And the OWL namespace of course refers to the OWL specification. The base element declares a namespace for the unique elements declared in this document.

After the namespace declaration OWL documents may declare an ontology header. This header can include information useful in describing the ontology, such as imported ontologies, versioning information, and comments. Because this clothing catalog ontology is simple and doesn't build on other ontologies, I simply added an rdf:comment element to provide a simple description for the ontology.



<owl:Ontology rdf:about=""> <rdfs:comment rdf:datatype="http://www.w3.org/2001/XMLSchema#string"> A simple ontology to model a clothing catalog </rdfs:comment> </owl:Ontology>

 
Figure 4. Jeans Class: The Jeans class contains all instances of Jeans defined in the ontology.
Ontologies in OWL are composed of classes, instances, and properties. Classes are sets that contain like individuals and are related to each other via subclassing. In the example clothing catalog ontology the individual items that people can purchase from the store are instances, while classes in this case are the types that relate sets of instances. For example, the Jeans class contains all the Jeans instances as shown in Figure 4.

You declare the Jeans class in OWL using the owl:Class construct. The example below declares that there a class exists in the ontology with the name "Jeans."



Now you can declare the two jeans instances in the table to be of this type:

<Jeans rdf:ID="RelaxedFitStonewashedJeans"/> <Jeans rdf:ID="StretchGauchoJeans"/>

This definition of the Jeans class and instances will pass the testListItems_Jeans unit test shown earlier, but the testListItems_Clothing test will continue to fail because the two jeans instances defined are not yet types of clothing. As illustrated in Figure 2, what we really want to express is that jeans are a type of pants and that pants are a type of clothing. In OWL you express this "type-of" relationship through subclassing as shown below:

 
Figure 5. Clothing Subclasses: Because the jeans class is a subclass of pants, which is a subclass of clothing, all jeans instances are also instances of pants and clothing.

<owl:Class rdf:about="#Clothing"/> <owl:Class rdf:about="#Pants"> <rdfs:subClassOf rdf:resource="#Clothing"/> </owl:Class> <owl:Class rdf:about="#Jeans"> <rdfs:subClassOf rdf:resource="#Pants"/> </owl:Class>

This declares that all instances of jeans are also instances of the pants and clothing classes as shown in Figure 5. The remaining classes and instances can similarly be defined to pass the unit tests defined above. You can do this yourself or download the accompanying source code for the completed definitions.



Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap