Hi. I'm Mark Wallace. In my role as a an Ontologist and Software Architect, I am continually working with new and fun semantic technologies. Be it RDF/OWL, Triple-stores, Semantic Wikis, or Text Extraction, I am learning more all the time and want to share my experiences in hopes of helping others along with these technologies. I hope post a new article every month or two, so check back in every so often to see what’s cooking!

Showing posts with label convert. Show all posts
Showing posts with label convert. Show all posts

Monday, May 24, 2010

Using Jena to convert RDF/OWL file formats

As a way of getting started with this blog, let me show how to use the Jena Utilities to convert your RDF/OWL files from one format to another.

On the Web, most OWL and RDF content are formatted as as RDF/XML. This format works great for the web, and most OWL/RDF tools support this as the native format. However, it is less human-readable than other formats such as Turtle.

For me, Turtle is the easiest form to read, and the quickest format for generating test data when I am prototyping. But not all tools support Turtle format.

But that's OK! If you want to easily convert from RDF/XML to Turtle and back, you can use the freely available Jena utilities to do this. These are command line utilities, so you run them from a command window.

First: Download Jena. The latest version at the time of this writing is version 2.6.2. Unpack it anywhere - though I usually make sure the directory path has no spaces in it. E.g., let's say you unpack it on a Windows system under C:\Programs, making the Jena top level folder the C:\Programs\Jena-2.6.2 folder.

Second: Set up your Java classpath, e.g.:


set CLASSPATH=
set CLASSPATH=%CLASSPATH%;C:\Programs\Jena-2.6.2\lib\arq-extra.jar
set CLASSPATH=%CLASSPATH%;C:\Programs\Jena-2.6.2\lib\arq.jar
set CLASSPATH=%CLASSPATH%;C:\Programs\Jena-2.6.2\lib\icu4j_3_4.jar
set CLASSPATH=%CLASSPATH%;C:\Programs\Jena-2.6.2\lib\iri.jar
set CLASSPATH=%CLASSPATH%;C:\Programs\Jena-2.6.2\lib\jena.jar
set CLASSPATH=%CLASSPATH%;C:\Programs\Jena-2.6.2\lib\jenatest.jar
set CLASSPATH=%CLASSPATH%;C:\Programs\Jena-2.6.2\lib\json.jar
set CLASSPATH=%CLASSPATH%;C:\Programs\Jena-2.6.2\lib\junit-4.5.jar
set CLASSPATH=%CLASSPATH%;C:\Programs\Jena-2.6.2\lib\log4j-1.2.12.jar
set CLASSPATH=%CLASSPATH%;C:\Programs\Jena-2.6.2\lib\lucene-core-2.3.1.jar
set CLASSPATH=%CLASSPATH%;C:\Programs\Jena-2.6.2\lib\slf4j-api-1.5.6.jar
set CLASSPATH=%CLASSPATH%;C:\Programs\Jena-2.6.2\lib\slf4j-log4j12-1.5.6.jar
set CLASSPATH=%CLASSPATH%;C:\Programs\Jena-2.6.2\lib\stax-api-1.0.jar
set CLASSPATH=%CLASSPATH%;C:\Programs\Jena-2.6.2\lib\wstx-asl-3.0.0.jar
set CLASSPATH=%CLASSPATH%;C:\Programs\Jena-2.6.2\lib\xercesImpl.jar


Third: Convert stuff! E.g. I have the file vehicle.owl, with these contents:


<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:e="http://example.org/ex#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<rdfs:Class rdf:about="http://example.org/ex#Vehicle"/>
<e:Civic rdf:about="http://example.org/ex#Civic_8717383">
<e:hasVin>1H0243098430987</e:hasVin>
</e:Civic>
<rdfs:Property rdf:about="http://example.org/ex#hasVin"/>
<rdf:Description rdf:about="http://example.org/ex#HondaCar">
<rdfs:subClassOf>
<rdf:Description rdf:about="http://example.org/ex#Car">
<rdfs:subClassOf rdf:resource="http://example.org/ex#Vehicle"/>
</rdf:Description>
</rdfs:subClassOf>
</rdf:Description>
<rdf:Description rdf:about="http://example.org/ex#Civic">
<rdfs:subClassOf rdf:resource="http://example.org/ex#HondaCar"/>
</rdf:Description>
</rdf:RDF>


To convert it, I use the Jena rdfcat program, as follows:

java jena.rdfcat -out ttl vehicle.owl


And it prints this this out:

@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix e: <http://example.org/ex#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

e:Civic_8717383
a e:Civic ;
e:hasVin "1H0243098430987" .

e:Vehicle
a rdfs:Class .

e:HondaCar
rdfs:subClassOf e:Car .

e:hasVin
a rdfs:Property .

e:Car
rdfs:subClassOf e:Vehicle .

e:Civic
rdfs:subClassOf e:HondaCar .


I can redirect this to a file for safe keeping:

java jena.rdfcat -out ttl vehicle.owl > vehicle.ttl


Now I have my turtle file! I can view and edit it as I see fit. Then, if I need to publish back to RDF/XML format, I simply use the rdfcat utility again, but with no "-out" option:

java jena.rdfcat vehicle.ttl > vehicle-new.owl


That's it. If this was new to you, post a comment and let me know if it helped.

Followers