Friday, August 10, 2012

Accessing Data Property Range values using Jena Framework

Recently, I'm working with semantic web technologies.. I have built an ontology, and now my task is to parse this ontology and generate individuals for classes..

Jena Framework is quite popular to use for building semantic web applications.. I use it for working with my ontology.. There are many tutorials and example codes on Web, so it is easy to work with Jena. And of course, Jena is an ongoing project, and it's not complete in terms of its methods..

One problem that I encountered with is that getRange() method for data properties is not working properly.. It is ok when you use xsd variables such integer, string and such.. But when you use an enumerated list as data property range, this method returns null..

I realized that this method returns a Class rather than a DataRange type.. So it is possible to iterate over enumerated list values using EnumeratedClass..

Now, I will detail how I solved this problem.. Will take an example from my ontology.. I have a class called, Liver. This class has some data properties, and one of them is, hasMarginType. This property has a value which is one of:

{"irregular"^^string , "lobulated"^^string , "nodular"^^string , "other"^^string , "regular"^^string}


And this is how it looks like in Protege:
Now, let's look at the code for parsing this range values using Jena.

//print Liver class data properties
        ExtendedIterator<OntProperty> it = Liver.listDeclaredProperties();
        while(it.hasNext()){
            OntProperty p = it.next();
            if (p.isDatatypeProperty() && p.getDomain()!=null && p.getRange()!=null){
                pr("Data Property Name: "+ p.getLocalName());
                pr("Domain: "+ p.getDomain().getLocalName());
               
                EnumeratedClass e = null;
                ExtendedIterator<RDFNode> i = null;
                if(p.getRange().asClass().isEnumeratedClass()){
                    e = p.getRange().asClass().asEnumeratedClass();
                    i = e.getOneOf().iterator();
                   
                    RDFNode prop = null;
                    String s=null;
                    pr("Range: ");
                    while(i.hasNext()){
                        prop = i.next();
                        s=prop.asLiteral().toString().split("\\^\\^")[0];
                        pr(s);
                    }
                }else{
                    pr("Range: "+ p.getRange().getLocalName());
                }
               
                pr("\n");
            }
           
        }


 This code prints name of a data property, domain of this data property and range value for this data property. Note that pr is a function used to print data on console. My focus was not having the namespaces, thus my output is like this:

Data Property Name: hasMarginType
Domain: Liver
Range:
irregular
lobulated
nodular
other
regular


I did not find a code snippet to work with.. So here it is my solution.. Hope it helps..

Wednesday, August 8, 2012

Installing Sesame Server on Ubuntu 12.04

I'm currently checking triple stores and wanted to install one of the most popular ones: Sesame. I hope seeing Ali Baba in few days :) But before that I had some issues while installing Sesame on Ubuntu 12.04.

Sesame offers a built-in console utility. And there is also a web interface: Sesame Openrdf-Workbench which seems to be easier than Sesame console. Current version of Sesame is OpenRDF Sesame 2.6.8 SDK.

In order to install Sesame server, there are two war files that need to be deployed to a java servlet container. And thus, it is expected that you have java (5 or 5+) installed in your system. 

As a java servlet container, I used Tomcat 7. First of all, I installed it via ubuntu software center. Default configuration creates a new user, tomcat7. /usr/share/tomcat7 is default home directory for this user. Once you install tomcat, you can start the server as:

/etc/init.d/tomcat7 start

stop and restart commands can be used in the same way. Once the server is up, you can see tomcat page in http://localhost:8080/. Now, try to use manager webapp in order to deploy Sesame wars. Note that you have to configure users by editing tomcat-users.xml. The sample entry has to be something similar to:

<tomcat-users>
  <role rolename="manager-gui"/>
  <role rolename="admin"/>
  <user name="admin" password="admin" roles="manager-gui,admin"/>
</tomcat-users>

After deploying the wars, you'll see that Sesame Workbench is not working and gives an error like: java.io.IOException: Unable to create logging directory /usr/share/tomcat7/.aduna/openrdf-sesame/logs

You get this error because Sesame tries to write to home directory, but tomcat7 has no write access to that folder. You can simply solve the problem with this:

sudo mkdir -p /usr/share/tomcat7/.aduna
sudo chown -R tomcat7:tomcat7 /usr/share/tomcat7 

Once you restart the tomcat server with:

/etc/init.d/tomcat7 restart

And try to access: http://localhost:8080/openrdf-workbench

You'll see that it works! By default, Sesame comes with a System repository. And it is possible to create new repositories, delete existing ones, querying and so much. Open Sesame!!