SPIN representation to SPARQL

206 views Asked by At

Is there an API that could help convert SPIN representation (of a SPARQL query) back to its SPARQL query form?

From:

[ a                              <http://spinrdf.org/sp#Select> ;
  <http://spinrdf.org/sp#where>  ( [ <http://spinrdf.org/sp#object>  [ <http://spinrdf.org/sp#varName>
                                                       "o"^^<http://www.w3.org/2001/XMLSchema#string> ] ;
                                     <http://spinrdf.org/sp#predicate>
                                             [ <http://spinrdf.org/sp#varName>
                                                       "p"^^<http://www.w3.org/2001/XMLSchema#string> ] ;
                                     <http://spinrdf.org/sp#subject>
                                             [ <http://spinrdf.org/sp#varName>
                                                       "s"^^<http://www.w3.org/2001/XMLSchema#string> ]
                                   ] )
] .

To:

SELECT *
WHERE {
    ?s ?p ?o .
}

Thanks in advance.

1

There are 1 answers

0
ssz On

I know two jena-based APIs to work with SPIN.

You can use either org.topbraid:shacl:1.0.1 which is based on jena-arq:3.0.4 or the mentioned in the comment org.spinrdf:spinrdf:3.0.0-SNAPSHOT, which is a fork of the first one, but with changed namespaces and updated dependencies.

Note the first (original) API also may work with modern jena (3.13.x), at least your task can be solved in such circumstances. The second API has no maven release yet, but can be included into your project via jitpack.

To solve the problem you need to find the root org.apache.jena.rdf.model.Resource and cast it to org.topbraid.spin.model.Select (or org.spinrdf.model.Select) using jena polymorphism (i.e. the operation org.apache.jena.rdf.model.RDFNode#as(Class)). Then #toString() will return the desired query with the model's prefixes. Note that all personalities are already included into model via static initialization.

A demonstration of this approach is SpinTransformer from ONT-API test-scope, which transforms SPARQL-based queries to an equivalent form with sp:text.