Twinkql is a SPARQL to Object Mapping Framework that allows users to bind named SPARQL queries to ResultMaps, which in turn may be bound to beans.
Twinkql has a few simple goals
-
Many times accessing a SPARQL endpoint is only one layer of a complex application. An application may use data from a SPARQL endpoint to power a REST or SOAP service, to validate parts of the system, or any number of different applications. In this case, we need to go from SPARQL query to application-specific beans as quickly as possible, and with the least amount of code.
-
SPARQL in your code is hard to maintain. In Java, for example, complex SPARQL in in Java files is represented as large String concatenations. This limits the readability of your code. Moving the SPARQL queries to XML files let you have a clean separation between application logic and SPARQL queries.
-
Accessing a SPARQL endpoint should be no harder than a SQL datasource. In fact, your application should only be concerned with the query you want to execute and what parameters you need put in that query.
For example, say I want to find all of the labels and comments from all the owl Ontologies. In Twinkql, I can specify that query in XML, keeping it separate from the rest of my program code.
<select id="myTestQuery">
SELECT ?label ?note
WHERE {
?s a <http://www.w3.org/2002/07/owl#Ontology>;
rdfs:label ?label ;
rdfs:comment ?comment .
}
</select>
What if there is a query parameter in my search? Twinkql can handle that with Parameter Substitution.
<select id="myTestQuery">
SELECT ?label ?note
WHERE {
?s a <http://www.w3.org/2002/07/owl#Ontology>;
rdfs:label ?label ;
rdfs:comment ?comment ;
<http://my/ns#myproperty> #{myQueryParam}
}
</select>
In this case, when I execute my query, I simply pass in a Map with a key of 'myQueryParam' - and whatever value is associated with that key will be injected into my query.
In many applications, the SPARQL query is just part of the architecture. Results from a SPARQL query may need to be manipulated further, passed into a messaging queue, processed into a different format, etc. Twinql allows you to bind beans to SPARQL query results via 'ResultMaps'. Result maps simply take a variable from the SPARQL query (the 'var' attribute) and map it to a property on your result bean (the 'beanProperty' attribute).
<resultMap id="myTestResultMap"
resultClass="com.sample.twinkql.Bean">
<rowMap var="label" varType="literalValue" beanProperty="name"/>
<rowMap var="comment" varType="literalValue" beanProperty="node"/>
</resultMap>
In this example, a bean of class 'com.sample.twinkql.Bean' will be instantiated, and for each row of the SPARQL result set returned, the variables will be bound. The user will be returned a Collectin of the resulting beans.
[Apache 2.0 license](http://www.apache.org/licenses/LICENSE-2.0">Apache 2.0 license)
Twinkql has been heavily inspired by the MyBatis Project, and we thank them for a great project. Twinkql aims to be the MyBatis for the SPARQL community.
For more info, please see the Project Page and follow us on Twitter.