SmartDataAnalytics / OWL2SPARQL

OWL To SPARQL Query Rewriter
Apache License 2.0
20 stars 8 forks source link
sparql-query web-ontology-language

OWL2SPARQL - "Yet another OWL To SPARQL Query rewriter?!"

Build Status <img alt="Coverity Scan Build Status" src="https://scan.coverity.com/projects/9256/badge.svg"/> Codacy Badge

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:

More Examples

Class Expression SPARQL Query
A
BASE    <http://example.org/ontology/>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT DISTINCT  ?x
WHERE
  { ?x rdf:type <A>}
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#>

SELECT DISTINCT  ?x
WHERE
  { ?x rdf:type <A>
      { ?x rdf:type <B>}
    UNION
      { ?x ?p ?o
        FILTER NOT EXISTS {?x <r> ?s0}
      }
  }
A
 and (not (B))
BASE    <http://example.org/ontology/>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT DISTINCT  ?x
WHERE
  { ?x rdf:type <A>
    FILTER NOT EXISTS {?x rdf:type <B>}
  }
A
 and (not (r some B))
BASE    <http://example.org/ontology/>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT DISTINCT  ?x
WHERE
  { ?x rdf:type <A>
    FILTER NOT EXISTS {?x <r> ?s0 .
      ?s0 rdf:type <B>
    }
  }
A
 and (r some  Self )
BASE    <http://example.org/ontology/>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT DISTINCT  ?x
WHERE
  { ?x rdf:type <A> .
    ?x <r> ?x
  }
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#>

SELECT DISTINCT  ?x
WHERE
  { ?x rdf:type <A> .
    ?x <t> ?s0
    FILTER ( datatype(?s0) = xsd:boolean )
  }
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#>

SELECT DISTINCT  ?x
WHERE
  { ?x rdf:type <A> .
    ?x <t> ?s0
    FILTER ( datatype(?s0) != xsd:boolean )
  }
A
 and (t value 1)
BASE    <http://example.org/ontology/>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT DISTINCT  ?x
WHERE
  { ?x rdf:type <A> .
    ?x <t> 1
  }
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#>

SELECT DISTINCT  ?x
WHERE
  { ?x rdf:type <A>
    { SELECT  ?x
      WHERE
        { ?x <t> ?s0
          FILTER ( datatype(?s0) = xsd:boolean )
        }
      GROUP BY ?x
      HAVING ( COUNT(?s0) >= 2 )
    }
  }
B
 and (r some B)
BASE    <http://example.org/ontology/>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT DISTINCT  ?x
WHERE
  { ?x rdf:type <B> .
    ?x <r> ?s0 .
    ?s0 rdf:type <B>
  }
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#>

SELECT DISTINCT  ?x
WHERE
  { ?x rdf:type <B> .
    ?x <r> ?s0 .
    ?s0 rdf:type <B> .
    ?x <s> ?s1 .
    ?s1 rdf:type <A>
  }
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#>

SELECT DISTINCT  ?x
WHERE
  { ?x rdf:type <B> .
    ?x <r> ?s0 .
    ?s0 rdf:type <C> .
    ?s0 <s> ?s1 .
    ?s1 rdf:type <A>
  }
Place
 and (language min 2 Language)
BASE    <http://example.org/ontology/>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT DISTINCT  ?x
WHERE
  { ?x rdf:type <Place>
    { SELECT  ?x
      WHERE
        { ?x <language> ?s0 .
          ?s0 rdf:type <Language>
        }
      GROUP BY ?x
      HAVING ( COUNT(?s0) >= 2 )
    }
  }
(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#>

SELECT DISTINCT  ?x
WHERE
  { FILTER NOT EXISTS {?x rdf:type <A>}
    ?x <r> ?s0 .
    ?s0 <s> ?s1
    FILTER NOT EXISTS {?s1 rdf:type <B>}
  }
A or B
BASE    <http://example.org/ontology/>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT DISTINCT  ?x
WHERE
  {   { ?x rdf:type <A>}
    UNION
      { ?x rdf:type <B>}
  }
(not (A)) or (not (B))
BASE    <http://example.org/ontology/>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT DISTINCT  ?x
WHERE
  {   { ?x ?p ?o
        FILTER NOT EXISTS {?x rdf:type <A>}
      }
    UNION
      { ?x ?p ?o
        FILTER NOT EXISTS {?x rdf:type <B>}
      }
  }
not (B)
BASE    <http://example.org/ontology/>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT DISTINCT  ?x
WHERE
  { ?x ?p ?o
    FILTER NOT EXISTS {?x rdf:type <B>}
  }
{a , b}
BASE    <http://example.org/ontology/>

SELECT DISTINCT  ?x
WHERE
  { VALUES ?x { <a> <b> }}
r some B
BASE    <http://example.org/ontology/>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT DISTINCT  ?x
WHERE
  { ?x <r> ?s0 .
    ?s0 rdf:type <B>
  }
r some 
    (A
     and (not (B)))
BASE    <http://example.org/ontology/>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT DISTINCT  ?x
WHERE
  { ?x <r> ?s0 .
    ?s0 rdf:type <A>
    FILTER NOT EXISTS {?s0 rdf:type <B>}
  }
r some ({a})
(logically equivalent to
r value a)
BASE    <http://example.org/ontology/>

SELECT DISTINCT  ?x
WHERE
  { ?x <r> <a>}
r some ({a , b})
BASE    <http://example.org/ontology/>

SELECT DISTINCT  ?x
WHERE
  { ?x <r> ?s0
    VALUES ?s0 { <a> <b> }
  }
language only Language
BASE    <http://example.org/ontology/>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT DISTINCT  ?x
WHERE
  { ?x ?p ?o
    FILTER NOT EXISTS {?x <language> ?s1
      FILTER NOT EXISTS {?s1 rdf:type <Language>}
    }
  }
r only B
BASE    <http://example.org/ontology/>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT DISTINCT  ?x
WHERE
  { ?x ?p ?o
    FILTER NOT EXISTS {?x <r> ?s1
      FILTER NOT EXISTS {?s1 rdf:type <B>}
    }
  }
r only Thing
(logically equivalent to
Thing)
BASE    <http://example.org/ontology/>

SELECT DISTINCT  ?x
WHERE
  { ?x ?p ?o}
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#>

SELECT DISTINCT  ?x
WHERE
  { ?x ?p ?o
    FILTER NOT EXISTS {?x <r> ?s1
      FILTER NOT EXISTS {?s1 rdf:type <A>}
    }
  }
r only 
    (A or (s only Thing))
(logically equivalent to
Thing)
BASE    <http://example.org/ontology/>

SELECT DISTINCT  ?x
WHERE
  { ?x ?p ?o}
r only (s only Thing)
(logically equivalent to
Thing)
BASE    <http://example.org/ontology/>

SELECT DISTINCT  ?x
WHERE
  { ?x ?p ?o}
r value a
BASE    <http://example.org/ontology/>

SELECT DISTINCT  ?x
WHERE
  { ?x <r> <a>}
language min 2 Language
BASE    <http://example.org/ontology/>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT DISTINCT  ?x
WHERE
  { ?x <language> ?s0 .
    ?s0 rdf:type <Language>
  }
GROUP BY ?x
HAVING ( COUNT(?s0) >= 2 )
t some (integer or (boolean and {1 , 2}))
BASE    <http://example.org/ontology/>
PREFIX  xsd:  <http://www.w3.org/2001/XMLSchema#>

SELECT DISTINCT  ?x
WHERE
  { ?x <t> ?s0
    FILTER ( ( datatype(?s0) = xsd:integer ) || ( ( datatype(?s0) = xsd:boolean ) && ( ?s0 IN (1, 2) ) ) )
  }
t some  not ({1 , 2})
BASE    <http://example.org/ontology/>

SELECT DISTINCT  ?x
WHERE
  { ?x <t> ?s0
    FILTER ( ?s0 NOT IN (1, 2) )
  }
t some {1 , 2}
BASE    <http://example.org/ontology/>

SELECT DISTINCT  ?x
WHERE
  { ?x <t> ?s0
    FILTER ( ?s0 IN (1, 2) )
  }
t some (boolean and {1 , 2})
BASE    <http://example.org/ontology/>
PREFIX  xsd:  <http://www.w3.org/2001/XMLSchema#>

SELECT DISTINCT  ?x
WHERE
  { ?x <t> ?s0
    FILTER ( ( datatype(?s0) = xsd:boolean ) && ( ?s0 IN (1, 2) ) )
  }
t some PlainLiteral[length 10]
BASE    <http://example.org/ontology/>

SELECT DISTINCT  ?x
WHERE
  { ?x <t> ?s0
    FILTER strlen(( str(?s0) = 10 ))
  }
t some integer[>= 3 , < 10]
BASE    <http://example.org/ontology/>

SELECT DISTINCT  ?x
WHERE
  { ?x <t> ?s0
    FILTER ( ( ?s0 >= 3 ) && ( ?s0 < 10 ) )
  }
t only boolean
BASE    <http://example.org/ontology/>
PREFIX  xsd:  <http://www.w3.org/2001/XMLSchema#>

SELECT DISTINCT  ?x
WHERE
  { ?x ?p ?o
    FILTER NOT EXISTS {?x <t> ?s1
      FILTER ( datatype(?s1) != xsd:boolean )
    }
  }
t only {1}
BASE    <http://example.org/ontology/>

SELECT DISTINCT  ?x
WHERE
  { ?x ?p ?o
    FILTER NOT EXISTS {?x <t> ?s1
      FILTER ( ?s1 NOT IN (1) )
    }
  }