OwlThing

mwarnock edited this page Sep 12, 2010 · 14 revisions

OwlThing is an Object Relational Mapping around the OWL class “Thing.” The ActiveSesame::OwlThing class can be instantiated to create new triples that are of rdf:type owl:Thing. It uses the ActiveSesame::Ontology::Term class to create new triples and store relationships that can be saved to the triple store. OwlThing can also be used as a parent of a class to create a new Term that is the owl:subClassOf owl:Thing. Below are examples of how to use this class to build an ontology.

Creating Instances (individuals)

Though the w3c recommendation does not dictate how to create ontologies there are some obvious standards that describe a general framework. One of these standards is the rdf:type property and owl:Thing. owl:Thing is a starting point for anything you might want to represent in your mathematical graph. It’s not required that you use them, but it’s not a bad idea. You can think of owl:Thing as a ruby class (which it is in our case) and anything that is an owl thing as an instance or, in semantic web language, an individual. Creating owl:Thing individuals can be done like this:

# Examine the structure of OwlThing (not needed to use it):
# OwlThing, its instances and any sub classes will have a term object associated with them
ActiveSesame::OwlThing #The abstract class that represents owl:Thing
ActiveSesame::OwlThing.term #The term object (based on ActiveSesame::Ontology::Term) in the triples store
ActiveSesame::OwlThing.term.term #The string "http://www.w3.org/2002/07/owl#Thing" (might not be prefix notation)

#Creating an Individual of OwlThing
my_thing = ActiveSesame::OwlThing.new  #Create an "individual" which has an rdf:type of owl:Thing
my_thing.term # The term object created to represent the individual
my_thing.term.term # the unique uri created for this individual (this can be changed using the :uri => "my_uri" option on new)
my_thing.unsaved_triples # The triples created automatically when thing was instantiated that have yet to be saved to the triple store

The individual created in the example above (my_thing) will use a generated uri rather than a blank node which would make a measure of sense in this case. However, blank nodes are a sticky subject and I’ve decided to avoid them by default. However, you can specify a uri for your individual like this:

my_thing = ActiveSesame::OwlThing.new(:uri => "my_uri_for_my_thing")

It is possible to override the uri generation method as well. An example can be seen in the Subclassing section.

Subclassing OwlThing

rdfs:subClassOf is a standard property to define a sub class relationship. To create a subclass relationship using OwlThing use the following code:

class Book < ActiveSesame::OwlThing
end
Book.term #The term object created for the Book class
Book.term.unsaved_triples # A list of triples that were created automatically when the class inherited from OwlThing

#Individuals can be instantiated from Book as they are with OwlThing
enders_game = Book.new(:uri => "base:EndersGame")
enders_game.term # The term object created for the book instance
enders_game.term.unsaved_triples # The triples that were automatically created from the instantiation of book.

Now that a new class was created based on OwlThing you can continue to subclass it. The triples generated (but not saved) will reflect the sub classing. In the case below a triple will be created as base:FictionNovel rdfs:subClassOf base:Book.

class FictionNovel < Book
end
FictionNovel.term.unsaved_triples # A list of triples that were created automatically when the class inherited from Book

Creating Triples and Saving

As this class uses ActiveSesame::Ontology::Term to store its information for both individuals and terms (instances and classes) you can use the same methods to add triples and save them.

class Book < ActiveSesame::OwlThing
end
Book.term.add_triple({:subject => Book.term.term, :predicate => "rdfs:label", :object => "A class for representing Books"})
Book.term.save # Save the triple to the triple store

Configuring and Customizing

As triples are based on uri’s and OwlThing abstracts much of this into the Reporitory connection so that you don’t have to type the same junk over and over, it is very important to follow the steps below as necessary. Keep a close watch on your base_uri (repository.base_uri) as saving and searching for triples under different base_uri’s may be the source of much confusion. It would be a bit like switching database namespaces in the middle of an application and wondering where your data has gone.

Repository

Using OwlThing implies the use of ActiveSesame::Repository. The Repository class is instantiated with defaults by OwlThing and any subclass you create. This is unlikely to be what you want unless you change the defaults in the Repository class itself. To change the repository for a class use the set_repository class method. For the options that can be given to Repository.new see the Repository wiki page.

# It can be called within or without the class definition
ActiveSesame::OwlThing.set_repository ActiveSesame::Repository.new(my_options)
class Book < ActiveSesame::OwlThing
  set_repository ActiveSesame::Repository.new(my_options)
end

URI for your Class

As you may want to specify a URI for an individual, you may also want to override the uri generated when inheriting from OwlThing. To do so you can use the set_uri method. As with set_repository this can be done within or without the class definition.

class Book < ActiveSesame::OwlThing
  set_uri "http://mysite.com/ontology.owl#BooksAreSmarted"
end
Book.set_uri "http://mysite.com/ontology.owl#Book"

In both cases the term for these classes will be reset to reflect the new uri and repository.

Creating your own individual uri generator

The default individual uri generator is a combination of the class name, time of instantiation, and the ruby object id added together and hashed (SHA1). While this will make unique names it’s not necessarily what you want. As shown in the “Creating Instances” section, you can set individual uri’s directly, but you can also override the generator:

class Book < ActiveSesame::OwlThing
  def generate_uri
    #your method for creating a uri
  end
end