SPARQL to SPIN Converter

In this tutorial we create a SPARQL to SPIN converter using the Topraid SPIN API. Furthermore we use Apache Jena and Maven to setup a simple FileChooser UI to convert a selected SPARQL Query.

SPIN is a de-facto industry standard to represent SPARQL, rules and constraints on Semantic Web models. Among others, SPIN is used to:

  1. Calculate the value of a property based on other properties:
    • Caluclate the area of a geometric figure as a product of its height and width
    • Caluclate the age of a person as a difference between today’s date and person’s birthday date
  2. Isolate a set of rules to be executed under certain conditions:
    • Support incremental reasoning, to initialize certain values when a resource is first created
  3. Constraint checking
  4. Definition of new SPARQL functions

1. Prerequisites

  1. SPIN API:  Download SPIN 1.4
  2. Apache Jena: Download Jena
  3. Optional: Install Maven

2. Project Setup

  1. Without Maven:
    • Create a standard Java project
    • Include the SPIN and Jena JAR files in your classpath
  2. Create a Maven project and adapt the POM file with the following code

    Update or rebuild your project to download the required libraries. Afterwards create a package and create a Java class called SPARQL2SPINConverter. The necessary code is provided in the next chapter.

3. Code Example

The following example will open a FileChooser UI to select a SPARQL rule that should be converted to the SPIN syntax. We use the following SPARQL query stored in sparqlQuery.txt:

The necessary Java code is shown below:

Now you can execute the Java code and select the sparqlQuery.txt file.


Doubleclick the specified SPARQL query and check the console output of your Java programm. You should see something like:

Now you converted an existing SPARQL query into the SPIN syntax representation.

If you have problems or questions, feel free to comment.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.