<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <link href="http://sadiframework.org/style/sadi.css" type="text/css" rel="stylesheet"/>
        <style type="text/css">
            h2 {
                margin-top: 2em;
                margin-left: 2em
            }
            
            h3 {
                margin-top: 2em;
                margin-left: 4em
            }
            
            h4 {
                margin-top: 2em;
                margin-left: 6em
            }
            
            p {
                margin-left: 6em
            }
            
            pre {
                margin-left: 8em;
                margin-right: 8em;
                padding: 4px;
                font-family: monospace;
                font-size: 125%;
                background: #eee;
            }
        </style>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <title>Working with Perl Modules Representing OWL Classes</title>
    </head>
    <body>
        <h1>Working with Perl Modules Representing OWL Classes</h1>
<p>
            This document summarises the creation and utilization of Perl modules, generated with the sadi-generate-datatypes.pl script, that represent OWL classes and their associated properties.</p>
        <p>
            The main thing to understand is that using these perl modules will make your life much more simple with regards to implementing your own SADI services. The generated classes extrapolate the OWL semantics and allow you to use any OWL class, datatype property or object property in an object oriented manner.</p>
        <p>At the end of this tutorial, you should be able to generate perl modules from an OWL file and be able to use these modules in your service provision.</p>
        <p>Let's now move on towards seeing how this all works.</p>
<h2>Table of Contents</h2>
<ul>
  <ul>
    <li><a href="#0">Quick Synopsis for the Impatient</a></li>
    <li><a href="#1">Prerequisites</a></li>
    <li><a href="#2">Generating Perl Modules from OWL Classes</a></li>
    <li><a href="#3">Using Perl Modules Representing OWL Classes</a></li>
    <li><a href="#4">Putting it all together (a complete example script)</a><br/>
    </li>
  </ul>
  </ul>
<h3><a name="0"/>Quick Synopsis for the Impatient</h3>
<p>For those of you that wish to see how to use the Perl modules without reading further (or to refresh your memory), the following is for you!</p>
<pre>
# perl modules generated using the following command:
# sadi-generate-datatypes.pl example.owl

# our use statements (for completion of this example)
use sadiframework::org::examples::example::AnnotatedGeneID_Record;
use ontology::dumontierlab::com::hasSymbol;
use sadiframework::org::ontologies::predicates::hasDescription;
use sadiframework::org::ontologies::predicates::hasProteinName;
use sadiframework::org::ontologies::predicates::hasName;

# instantiate a new record
my $class = new sadiframework::org::examples::example::AnnotatedGeneID_Record(&quot;http://some_uri/for/my/record&quot;);

# set a label
$class-&gt;label(&quot;this is my label for this record&quot;);

# get the label
my $label = $class-&gt;label();

# get the the uri for this record
my $uri = $class-&gt;uri();

# add some symbols to this record (datatype property)
$class-&gt;add_hasSymbol(ontology::dumontierlab::com::hasSymbol-&gt;new('Some_Gene_Symbol'));
$class-&gt;add_hasSymbol(ontology::dumontierlab::com::hasSymbol-&gt;new('Another_Gene_Symbol'));

# you can still get/set hasDescription(), hasProteinName() and hasName() (all are datatype properties in this example)

# for imaginary hasReference() object property described <a href="#object_properties">here</a>
$class-&gt;add_hasReference(new sadiframework::org::examples::example::AnnotatedGeneID_Record(&quot;http://lsrn.org/Gene:642&quot;));
</pre>
<h3><a name="1"/>Prerequisites</h3>
        <p>Of course, the main prerequisite is that you have installed SADISeS on your machine. SADISeS can be found on <a href="http://search.cpan.org/dist/SADI/" target="_blank">CPAN</a>. When you install this Perl module onto your computer, you will also be prompted to install any missing Perl modules required by SADISeS. After installation, (i.e. make install), you are required to also run a SADI installation script, sadi-install.pl. This is only done the first time that you install SADISeS (and sometimes when you upgrading your SADISeS codebase; Looking at the README or CHANGES will make it obvious as to when this is required).</p>
        <p>Another prerequiste for generating Perl modules from OWL is an OWL document! In this tutorial, we will present all OWL that we use for you to download.        </p>
        <h3><a name="2"/>Generating Perl Modules from OWL Classes</h3>
        <p>The generation of Perl modules representing OWL entities is done by the sadi-generate-datatypes.pl script, from now on refered to as 'the script', that is installed onto your computer when you install the SADI perl module.</p>
        <p>If you run the script without arguments, you are shown something like:</p>
        <pre>Generate perl modules from OWL files.
Usage: [-vdsib] owl-class-file
       [-vdsi] -u owl-class-url

    -u ... owl is from url
    -s ... show generated code on STDOUT
           (no file is created, disabled when no data type name given)

    -b ... option to specify the base uri for the owl document (you will be prompted)

    -i ... follow owl import statements

    -v ... verbose
    -d ... debug
    -h ... help

Note: This script requires that the PERL module ODO, from IBM Semantic Layered
      Research Platform be installed on your workstation! ODO is available on CPAN
      under the name PLUTO.
</pre>
        <p>As you can see, the script consumes either a file or url that points to a file containing the OWL entities that you wish to generate Perl modules. Many OWL ontologies import concepts from other files and so the script also allows you to follow these import statements. Additionally for your convenience, the script also allows for showing generated code on STDOUT.</p>
        <p>For the rest of this document, the following OWL ontology will be used:</p>
        <pre><a name="owl_doc"></a>&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;rdf:RDF
    xmlns:rdf=&quot;http://www.w3.org/1999/02/22-rdf-syntax-ns#&quot;
    xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema#&quot;
    xmlns:rdfs=&quot;http://www.w3.org/2000/01/rdf-schema#&quot;
    xmlns:owl=&quot;http://www.w3.org/2002/07/owl#&quot;
    xml:base=&quot;http://sadiframework.org/examples/example.owl#&quot;&gt;
  &lt;owl:Ontology rdf:about=&quot;&quot;/&gt;
  
  &lt;!-- in lieu of importing the whole ontology... --&gt;
  &lt;owl:DatatypeProperty rdf:about=&quot;http://ontology.dumontierlab.com/hasSymbol&quot;/&gt;
  &lt;owl:DatatypeProperty rdf:about=&quot;http://sadiframework.org/ontologies/predicates.owl#hasDescription&quot;/&gt;
  &lt;owl:DatatypeProperty rdf:about=&quot;http://sadiframework.org/ontologies/predicates.owl#hasProteinName&quot;/&gt;
  &lt;owl:DatatypeProperty rdf:about=&quot;http://sadiframework.org/ontologies/predicates.owl#hasName&quot;/&gt;
  &lt;owl:Class rdf:about="http://purl.oclc.org/SADI/LSRN/GeneID_Record"/&gt;
  
  &lt;owl:Class rdf:ID=&quot;AnnotatedGeneID_Record&quot;&gt;
    &lt;rdfs:subClassOf rdf:resource=&quot;http://purl.oclc.org/SADI/LSRN/GeneID_Record&quot;/&gt;
    &lt;rdfs:subClassOf&gt;
      &lt;owl:Class&gt;
        &lt;owl:intersectionOf rdf:parseType=&quot;Collection&quot;&gt;
          &lt;owl:Restriction&gt;
            &lt;owl:onProperty rdf:resource=&quot;http://ontology.dumontierlab.com/hasSymbol&quot;/&gt;
            &lt;owl:minCardinality rdf:datatype=&quot;http://www.w3.org/2001/XMLSchema#int&quot;&gt;0&lt;/owl:minCardinality&gt;
          &lt;/owl:Restriction&gt;
          &lt;owl:Restriction&gt;
            &lt;owl:onProperty rdf:resource=&quot;http://sadiframework.org/ontologies/predicates.owl#hasDescription&quot;/&gt;
            &lt;owl:minCardinality rdf:datatype=&quot;http://www.w3.org/2001/XMLSchema#int&quot;&gt;0&lt;/owl:minCardinality&gt;
          &lt;/owl:Restriction&gt;
          &lt;owl:Restriction&gt;
            &lt;owl:onProperty rdf:resource=&quot;http://sadiframework.org/ontologies/predicates.owl#hasProteinName&quot;/&gt;
            &lt;owl:minCardinality rdf:datatype=&quot;http://www.w3.org/2001/XMLSchema#int&quot;&gt;0&lt;/owl:minCardinality&gt;
          &lt;/owl:Restriction&gt;
          &lt;owl:Restriction&gt;
            &lt;owl:onProperty rdf:resource=&quot;http://sadiframework.org/ontologies/predicates.owl#hasName&quot;/&gt;
            &lt;owl:minCardinality rdf:datatype=&quot;http://www.w3.org/2001/XMLSchema#int&quot;&gt;0&lt;/owl:minCardinality&gt;
          &lt;/owl:Restriction&gt;
        &lt;/owl:intersectionOf&gt;
      &lt;/owl:Class&gt;
    &lt;/rdfs:subClassOf&gt;
  &lt;/owl:Class&gt;
&lt;/rdf:RDF&gt;</pre>
<p>Please copy and paste the above document into a local file as we will be generating Perl modules from this ontology. Save the document as &quot;example.owl&quot;.</p>
<p>To begin, issue the following command at the command prompt:</p>
<pre>sadi-generate-datatypes.pl example.owl</pre>
<p>You should then see something along the lines of:</p>
<pre>Generating perl modules for: example.owl
Parsing schema file: example.owl
Done.</pre>
<p>What has happened is that the ontology described above had been parsed and Perl Modules for AnnotatedGeneID_Record (an owl class), hasSymbol, hasName, hasProteinName and hasDescription (all of which are datatype properties) were generated. As well, there was an anonymous class that was generated.</p>
<p><strong><em>One obvious question is "where are these perl modules being generated"?
</em></strong></p>
<p>You can always determine this after generation by looking in the <em><strong>services.log</strong></em> log file - the message has the INFO level which means it is almost always logged. But, if you want to know in advance here are the rules: </p>
<ul>
  <ul>
    <ul>
      <li>If there is a generators.outdir parameter in the sadi configuration file, it is used (by default, this is the directory Perl-SADI/generated contained in your home directory). </li>
      <li>Otherwise, the script tries to find an existing directory named 'generated' anywhere in the @INC (a set of directories used by Perl to locate its modules).</li>
      <li> If this fails, it creates a new directory called 'generated' in the "current" directory.
      </li>
    </ul>
  </ul>
  </ul>
<p>Please take some time to investigate the generated files. In the next section, we will explain how we can use these generated modules.</p>
<h3><a name="3"/>Using Perl Modules Representing OWL Classes</h3>
        <p>Every generated module that represents an OWL Class ultimately inherits from <a href="http://search.cpan.org/perldoc?SADI::Data::OWL::Class" target="_blank">SADI::Data::OWL::Class</a>. This class defines the methods:</p>
        <ol>
          <ol>
            <ol>
              <li> label - an RDF label for the class,</li>
              <li>uri/value - the URI for the entity, and</li>
              <li>type - the RDF:type of this entity. </li>
            </ol>
          </ol>
        </ol>
<p>All of these methods can be invoked with or without arguments. When invoked without arguments, the methods act as getters. And when a scalar value is used as an argument, the method acts as a setter.</p>
<pre>
# instantiate a new record
my $class = new sadiframework::org::examples::example::AnnotatedGeneID_Record(&quot;http://some_uri/for/my/record&quot;);

# set a label
$class-&gt;label(&quot;this is my label for this record&quot;);

# get the label
my $label = $class-&gt;label();

# get the the uri for this record
my $uri = $class-&gt;uri();
</pre>
<p>OWL properties are similar to their OWL Class counterparts mentioned above. Datatype properties inherit from <a href="http://search.cpan.org/perldoc?SADI::Data::OWL::DatatypeProperty" target="_blank">SADI::Data::OWL::DatatypeProperty</a>, while object properties inherit from <a href="http://search.cpan.org/perldoc?SADI::Data::OWL::ObjectProperty" target="_blank">SADI::Data::OWL::ObjectProperty</a>.</p>
<p>SADI::Data::OWL::DatatypeProperty defines the following methods:</p>
<ol>
  <ol>
    <ol>
      <li>value - the value for the property of interest,</li>
      <li>domain - the domain of this datatype property,</li>
      <li>range - the range of this datatype property, and</li>
      <li>uri - the type of this datatype property.</li>
    </ol>
  </ol>
</ol>
<p>while SADI::Data::OWL::ObjectProperty defines the following, similarly named, methods:</p>
<ol>
  <ol>
    <ol>
      <li>value - the URI for the subject of this object property,</li>
      <li>domain - the domain of this property, and</li>
      <li>range - the range for this property.</li>
    </ol>
  </ol>
</ol>
<p>Again, all of these methods can be invoked with or without arguments. When invoked without arguments, the methods act as getters. And when a scalar value is used as an argument, the method acts as a setter. </p>
<p>Some OWL classes, like AnnotatedGeneID_Record, their inherited methods from SADI::Data::OWL::Class, as well as the ability to get/set their own object/datatype properties. For instance, members of the the OWL class <a href="#owl_doc">AnnotatedGeneID_Record</a> have zero or more of the the following datatype properties: hasSymbol, hasDescription, hasName, hasProteinName. The generated Perl module for this OWL class also has the ability to get/set these properties. How you ask? Take a look at the following Perl snippet (yours make look similar) for AnnotatedGeneID_Record:</p>
<pre>@ISA = qw(
 SADI::Data::OWL::Class
 Blank::genid4b2bb18702dc 
);</pre>
<p>Notice that our class has @ISA of SADI::Data::OWL::Class (you already knew this!) and Blank::genid4b2bb18702dc. This is generated file for an anonymous OWLclass. If we examine the source for this class, we see the following:</p>
<pre>#-----------------------------------------------------------------
# Blank::genid4b2bb18702dc
# Generated: 18-Dec-2009 08:44:56 PST
# Contact: Edward Kawas &lt;edward.kawas+sadi@gmail.com&gt;
#-----------------------------------------------------------------
package Blank::genid4b2bb18702dc;

#use SADI::Base;

use SADI::Data::OWL::Class;


no strict;
use vars qw( @ISA );
@ISA = qw(
 SADI::Data::OWL::Class 
);
use strict;

# imports 
use SADI::Data::String;
use SADI::Data::Integer;
use SADI::Data::Float;
use SADI::Data::Boolean;
use SADI::Data::DateTime;



use ontology::dumontierlab::com::hasSymbol;
use sadiframework::org::ontologies::predicates::hasDescription;
use sadiframework::org::ontologies::predicates::hasProteinName;
use sadiframework::org::ontologies::predicates::hasName;

{
    my %_allowed = (
    
    
        <a name="hasSymbol"></a>hasSymbol =&gt; {
            type =&gt; &#39;ontology::dumontierlab::com::hasSymbol&#39;,
            is_array =&gt; 1, },
    
        <a name="hasDescription"></a>hasDescription =&gt; {
            type =&gt; &#39;sadiframework::org::ontologies::predicates::hasDescription&#39;,
            is_array =&gt; 1, },
    
        <a name="hasProteinName"></a>hasProteinName =&gt; {
            type =&gt; &#39;sadiframework::org::ontologies::predicates::hasProteinName&#39;,
            is_array =&gt; 1, },
    
        <a name="hasName"></a>hasName =&gt; {
            type =&gt; &#39;sadiframework::org::ontologies::predicates::hasName&#39;,
            is_array =&gt; 1, },
    
    );

...
</pre>
<p>Notice the methods <a href="#hasSymbol">hasSymbol</a>, <a href="#hasName">hasName</a>, <a href="#hasProteinName">hasProteinName</a>, and <a href="#hasDescription">hasDescription</a>. Each of these methods corresponds to a datatype property that members of the OWL class AnnotatedGeneID_Records can have values for. Additionally, you will notice that each method has 'type' information associated with it. When using the generated Perl OWL classes, if you try to incorrectly set a value of a property with an incorrect type, an exception will be raised.</p>
<p>Now you might ask yourself, how do I actually use these methods? That's simple! If you only need to set one value for the datatype property, then you simple call the method, e.g. hasSymbol(), and provide the correctly typed argument.</p>
<pre>$class-&gt;hasSymbol(ontology::dumontierlab::com::hasSymbol-&gt;new('Some_Gene_Symbol'));
</pre>
<p>However, if you are required to set multiple values, prepend <em><strong>add_</strong></em><strong></strong>to the method.</p>
<pre>$class-&gt;add_hasSymbol(ontology::dumontierlab::com::hasSymbol-&gt;new('Some_Gene_Symbol'));
$class-&gt;add_hasSymbol(ontology::dumontierlab::com::hasSymbol-&gt;new('Another_Gene_Symbol'));
</pre>
<p>All datatype properties use this convention.</p>
<p><a name="object_properties"></a>Object properties are a bit different. Imagine for a second that our instances of our class AnnotatedGeneID_Record also had an object property called <em><strong>hasReference</strong></em>. Then the generated source might look something like:</p>
<pre>#-----------------------------------------------------------------
# Blank::genid4b2bb18702dc
# Generated: 18-Dec-2009 08:44:56 PST
# Contact: Edward Kawas &lt;edward.kawas+sadi@gmail.com&gt;
#-----------------------------------------------------------------
package Blank::genid4b2bb18702dc;

#use SADI::Base;

use SADI::Data::OWL::Class;


no strict;
use vars qw( @ISA );
@ISA = qw(
 SADI::Data::OWL::Class 
);
use strict;

# imports 
use SADI::Data::String;
use SADI::Data::Integer;
use SADI::Data::Float;
use SADI::Data::Boolean;
use SADI::Data::DateTime;

use ontology::dumontierlab::com::hasSymbol;
use ontology::dumontierlab::com::hasReference;
use sadiframework::org::ontologies::predicates::hasDescription;
use sadiframework::org::ontologies::predicates::hasProteinName;
use sadiframework::org::ontologies::predicates::hasName;

{
    my %_allowed = (
    
    
        hasSymbol =&gt; {
            type =&gt; &#39;ontology::dumontierlab::com::hasSymbol&#39;,
            is_array =&gt; 1, },
    
        hasDescription =&gt; {
            type =&gt; &#39;sadiframework::org::ontologies::predicates::hasDescription&#39;,
            is_array =&gt; 1, },
    
        hasProteinName =&gt; {
            type =&gt; &#39;sadiframework::org::ontologies::predicates::hasProteinName&#39;,
            is_array =&gt; 1, },
    
        hasName =&gt; {
            type =&gt; &#39;sadiframework::org::ontologies::predicates::hasName&#39;,
            is_array =&gt; 1, },

         hasReference =&gt; {
            type =&gt; &#39;SADI::Data::OWL::Class&#39;,
            # range checking of classes added 
            post =&gt; sub {
                my ($self) = shift;
                my $type = @{$self-&gt;hasReference}[-1];
                return unless defined $type and $type-&gt;type;
                my $range = new ontology::dumontierlab::com::hasReference-&gt;range();
                return unless $range;
                $range = $self-&gt;uri2package($range);
                eval {
                    $range = $range-&gt;new();
                };
                return if $@;
                $self-&gt;throw(&quot;\n&quot; . $type-&gt;type() . &quot;\nis not related to\n&quot; . $range-&gt;type()) unless $type-&gt;isa(ref($range));
            },
            is_array =&gt; 1, },
    
    );

...</pre>
<p>So our class now has the additional method hasReference and it accepts arguments of type SADI::Data::OWL::Class. In otherwords, you will have to instantiate classes and pass them as arguments to this method.</p>
<pre># for single values
$class-&gt;hasReference(sadiframework::org::examples::example::AnnotatedGeneID_Record-&gt;new(&quot;http://lsrn.org/Gene:642&quot;));

# for setting multiple values
$class-&gt;add_hasReference(sadiframework::org::examples::example::AnnotatedGeneID_Record-&gt;new(&quot;http://lsrn.org/Gene:642&quot;));
$class-&gt;add_hasReference(sadiframework::org::examples::example::AnnotatedGeneID_Record-&gt;new(&quot;http://lsrn.org/Gene:123&quot;));</pre>
<p>When developing SADI services with SADISeS, you will not have to add the location of generated modules because the entry scripts generated for each service already do this for you. For those of you wishing to use these generated modules on their own, you simply need to add a <em><strong>use lib</strong></em> statement to your Perl script.</p>
<pre>#for example, 
use lib '/your_home_directory/Perl-SADI/generated';</pre>
<h3><a name="4"></a>Putting it all together (a complete example script)<br/>
</h3>
<p>The following illustrates a simple script that utilizes the modules mentioned in this tutorial and outputs RDF/XML describing an instance of a AnnotatedGeneID_Record.</p>
<pre>#!/usr/bin/perl -w
use strict;

# add the generated libs to @INC 
<em><strong># modify this as necessary to point to your generated directory</strong></em>
use lib &#39;/home/ubuntu/Perl-SADI/generated&#39;;

# use statements for OWL classes
use sadiframework::org::examples::example::AnnotatedGeneID_Record;
use sadiframework::org::ontologies::predicates::hasName;
use ontology::dumontierlab::com::hasSymbol;
use sadiframework::org::ontologies::predicates::hasDescription;
use sadiframework::org::ontologies::predicates::hasProteinName;

# use statements for serializing OWL class
use RDF::Core::Storage::Memory;
use RDF::Core::Model;
use RDF::Core::Model::Serializer;

# instantiate our OWL class
my $class =
  sadiframework::org::examples::example::AnnotatedGeneID_Record-&gt;new(
                                                  &#39;http://lsrn.org/GeneID:642&#39;);
# add a label
$class-&gt;label(&quot;some label&quot;);

# add datatype properties
$class-&gt;add_hasSymbol(
              new ontology::dumontierlab::com::hasSymbol(&#39;some symbol value&#39;) );
$class-&gt;add_hasDescription(
            new sadiframework::org::ontologies::predicates::hasDescription(
                                                             &#39;some description&#39;)
);
$class-&gt;add_hasProteinName(
            new sadiframework::org::ontologies::predicates::hasProteinName(
                                                            &#39;some protein name&#39;)
);
$class-&gt;add_hasName(
                   new sadiframework::org::ontologies::predicates::hasName(
                                                              &#39;some name value&#39;)
);

# add another symbol
$class-&gt;add_hasSymbol(
           new ontology::dumontierlab::com::hasSymbol(&#39;another symbol value&#39;) );

# here we serialze the OWL class
use OWL::Utils;
print OWL::Utils::serialize($class)</pre>
<p>This produces the following RDF/XML:</p>
<pre>&lt;rdf:RDF
  xmlns:a=&quot;http://sadiframework.org/ontologies/predicates.owl#&quot;
  xmlns:c=&quot;http://www.w3.org/2000/01/rdf-schema#&quot;
  xmlns:b=&quot;http://ontology.dumontierlab.com/&quot;
  xmlns:rdf=&quot;http://www.w3.org/1999/02/22-rdf-syntax-ns#&quot;&gt;
  &lt;rdf:Description rdf:about=&quot;http://lsrn.org/GeneID:642&quot;&gt;
    &lt;rdf:type rdf:resource=&quot;http://sadiframework.org/examples/example.owl#AnnotatedGeneID_Record&quot;/&gt;
    &lt;c:label&gt;some label&lt;/c:label&gt;
    &lt;b:hasSymbol&gt;some symbol value&lt;/b:hasSymbol&gt;
    &lt;b:hasSymbol&gt;another symbol value&lt;/b:hasSymbol&gt;
    &lt;a:hasDescription&gt;some description&lt;/a:hasDescription&gt;
    &lt;a:hasProteinName&gt;some protein name&lt;/a:hasProteinName&gt;
    &lt;a:hasName&gt;some name value&lt;/a:hasName&gt;
  &lt;/rdf:Description&gt;
&lt;/rdf:RDF&gt;</pre>
</body>
</html>