OWL2SPARQL - "Yet another OWL To SPARQL Query rewriter?!"
  
  
 This project provides a simple converter from OWL axioms and OWL class expressions to SPARQL queries.
Maven Settings
<repositories>
    <repository>
        <id>maven.aksw.internal</id>
        <name>University Leipzig, AKSW Maven2 Repository</name>
        <url>http://maven.aksw.org/archiva/repository/internal</url>
    </repository>
    <repository>
        <id>maven.aksw.snapshots</id>
        <name>University Leipzig, AKSW Maven2 Repository</name>
        <url>http://maven.aksw.org/archiva/repository/snapshots</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>org.aksw.owl2sparql</groupId>
        <artifactId>owl2sparql-core</artifactId>
        <version>0.1</version>
    </dependency>
    ...
</dependencies> 
From OWL axiom to SPARQL query
Usage
// create the converter
OWLAxiomToSPARQLConverter converter = new OWLAxiomToSPARQLConverter("?s","?o");
// provide some OWL axiom using OWL API datastructures
OWLAxiom axiom = ...;
// convert the axiom into a SPARQL query
String queryString = converter.convert(axiom); 
Example
OWL axiom (in Manchester OWL Syntax)
PREFIX: : <http://example.org#>
ObjectProperty: r
  Domain: A
 
SPARQL query
PREFIX : <http://example.org#>
SELECT DISTINCT  ?s
WHERE
  { ?s :r ?s0 .
    ?s a :A
  } 
From OWL class expression to SPARQL query
Usage
// create the converter
OWLClassExpressionToSPARQLConverter converter = new OWLClassExpressionToSPARQLConverter();
// provide some OWL class expression using OWL API datastructures
OWLClassExpression ce = ...;
// convert the class expression into a SPARQL query
String queryString = converter.convert(ce); 
Example
OWL class expression (in Manchester OWL Syntax)
PREFIX: : <http://example.org#>
A and ( B or not (r some B))
 
SPARQL query
PREFIX : <http://example.org#>
SELECT DISTINCT  ?x
WHERE
  { ?x a :A
      { ?x a :B }
    UNION
      { ?x ?p ?o
        FILTER NOT EXISTS {
          ?x :r ?s0 .
          ?s0 a :B
        }
      }  
  } 
License
The source code of this repo is published under the Apache License Version 2.0.
This project makes use of several dependencies: When in doubt, please cross-check with the respective projects:
- OWL API (Apache License 2.0 and GNU LGPL Version 3.0)
 - Apache Jena (Apache License 2.0)
 - Guava (Apache License 2.0)
 
More Examples
| Class Expression | SPARQL Query | 
|---|---|
A  |  
   BASE <http://example.org/ontology/> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>  |  
  
A and (B or (not (r some Thing)))  |  
   BASE <http://example.org/ontology/> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>  |  
  
A and (not (B))  |  
   BASE <http://example.org/ontology/> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>  |  
  
A and (not (r some B))  |  
   BASE <http://example.org/ontology/> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>  |  
  
A and (r some Self )  |  
   BASE <http://example.org/ontology/> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>  |  
  
A and (t some boolean)  |  
   BASE <http://example.org/ontology/> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>  |  
  
A and (t some not boolean)  |  
   BASE <http://example.org/ontology/> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>  |  
  
A and (t value 1)  |  
   BASE <http://example.org/ontology/> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>  |  
  
A and (t min 2 boolean)  |  
   BASE <http://example.org/ontology/> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>  |  
  
B and (r some B)  |  
   BASE <http://example.org/ontology/> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>  |  
  
B and (r some B) and (s some A)  |  
   BASE <http://example.org/ontology/> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>  |  
  
B
 and (r some 
    (C
     and (s some A))) |  
   BASE <http://example.org/ontology/> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>  |  
  
Place and (language min 2 Language)  |  
   BASE <http://example.org/ontology/> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>  |  
  
(not (A)) and (r some (s some (not (B))))  |  
   BASE <http://example.org/ontology/> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>  |  
  
A or B  |  
   BASE <http://example.org/ontology/> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>  |  
  
(not (A)) or (not (B))  |  
   BASE <http://example.org/ontology/> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>  |  
  
not (B)  |  
   BASE <http://example.org/ontology/> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>  |  
  
{a , b} |  
   BASE <http://example.org/ontology/>  |  
  
r some B  |  
   BASE <http://example.org/ontology/> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>  |  
  
r some 
    (A
     and (not (B))) |  
   BASE <http://example.org/ontology/> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>  |  
  
r some ({a})
(logically equivalent to
r value a) |  
   BASE <http://example.org/ontology/>  |  
  
r some ({a , b}) |  
   BASE <http://example.org/ontology/>  |  
  
language only Language  |  
   BASE <http://example.org/ontology/> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>  |  
  
r only B  |  
   BASE <http://example.org/ontology/> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>  |  
  
r only Thing (logically equivalent to Thing)  |  
   BASE <http://example.org/ontology/>  |  
  
r only 
    (A
     and (s only Thing))
(logically equivalent to
r only A) |  
   BASE <http://example.org/ontology/> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>  |  
  
r only 
    (A or (s only Thing))
(logically equivalent to
Thing) |  
   BASE <http://example.org/ontology/>  |  
  
r only (s only Thing) (logically equivalent to Thing)  |  
   BASE <http://example.org/ontology/>  |  
  
r value a  |  
   BASE <http://example.org/ontology/>  |  
  
language min 2 Language  |  
   BASE <http://example.org/ontology/> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>  |  
  
t some (integer or (boolean and {1 , 2})) |  
   BASE <http://example.org/ontology/> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>  |  
  
t some  not ({1 , 2}) |  
   BASE <http://example.org/ontology/>  |  
  
t some {1 , 2} |  
   BASE <http://example.org/ontology/>  |  
  
t some (boolean and {1 , 2}) |  
   BASE <http://example.org/ontology/> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>  |  
  
t some PlainLiteral[length 10]  |  
   BASE <http://example.org/ontology/>  |  
  
t some integer[>= 3 , < 10]  |  
   BASE <http://example.org/ontology/>  |  
  
t only boolean  |  
   BASE <http://example.org/ontology/> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>  |  
  
t only {1} |  
   BASE <http://example.org/ontology/>  |