An open API service indexing awesome lists of open source software.

https://github.com/smartdataanalytics/owl2sparql

OWL To SPARQL Query Rewriter
https://github.com/smartdataanalytics/owl2sparql

sparql-query web-ontology-language

Last synced: 5 months ago
JSON representation

OWL To SPARQL Query Rewriter

Awesome Lists containing this project

README

        

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

[![Build Status](https://github.com/SmartDataAnalytics/OWL2SPARQL/workflows/Java%20CI%20with%20Maven/badge.svg)](https://github.com/SmartDataAnalytics/OWL2SPARQL/workflows/Java%20CI%20with%20Maven)

Coverity Scan Build Status

[![Codacy Badge](https://api.codacy.com/project/badge/Grade/9b16e876e6784a80b6d6a15b70df2ab2)](https://www.codacy.com/app/conpcomplete/OWL2SPARQL?utm_source=github.com&utm_medium=referral&utm_content=AKSW/OWL2SPARQL&utm_campaign=Badge_Grade)

This project provides a simple converter from OWL axioms and OWL class expressions to SPARQL queries.

## Maven Settings
```XML


maven.aksw.internal
University Leipzig, AKSW Maven2 Repository
http://maven.aksw.org/archiva/repository/internal


maven.aksw.snapshots
University Leipzig, AKSW Maven2 Repository
http://maven.aksw.org/archiva/repository/snapshots


org.aksw.owl2sparql
owl2sparql-core
0.1

...

```

## From OWL axiom to SPARQL query

### Usage
```Java
// 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: :
ObjectProperty: r
Domain: A
```
SPARQL query
```sparql
PREFIX :
SELECT DISTINCT ?s
WHERE
{ ?s :r ?s0 .
?s a :A
}
```
## From OWL class expression to SPARQL query

### Usage
```Java
// 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: :
A and ( B or not (r some B))
```
SPARQL query
```sparql
PREFIX :
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](https://github.com/AKSW/owl2sparql/blob/master/LICENSE).

This project makes use of several dependencies: When in doubt, please cross-check with the respective projects:
* [OWL API](https://github.com/owlcs/owlapi) (Apache License 2.0 and GNU LGPL Version 3.0)
* [Apache Jena](https://jena.apache.org/) (Apache License 2.0)
* [Guava](http://code.google.com/p/guava-libraries/) (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#>

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) )
}
}