0

I have been using TopQuadrant Composer Free Edition (TBC FE) to embed SPARQL/SPIN rules (primarily SPIN constructors) in my OWL ontologies stored as RDF. Part of this process is that the SPARQL source code is tokenized/encoded in an RDF structure according to http://spinrdf.org/sp.html which the schema specified in http://spinrdf.org/sp. It is this structure that actually gets interpreted by RDF4J to run the SPIN rules.

I'm also using RDF4J as my triple store, reasoner, SPARQL endpoint, and SPIN rule engine. In addition, I'm generating custom Java code and GUIs to manipulate my data and rules.

My question is what can I use in Java to encode SPARQL/SPIN source code as RDF? Note that I'm not asking how to encode query results (which was answered in another stackoverflow question/response) but rather how to encode the query itself. The reason is that I would like to enable editing of SPIN rules from my own Java code rather than being solely reliant on TBC FE.

Also note that I'm aware of the option to store the original SPARQL query text. However, my experience has been that this is not correctly interpreted whereas the tokenized/structured RDF is correctly interpreted. Therefore, I must use the structured RDF.

I'm hoping that much of the Java code to do this encoding is already written, perhaps as part of Apache Jena. I just need a pointer as to where to look.

Thanks!

PS: Here's the start of example SPIN constructor encoded by TBC FE. It includes both the original sp:text of the SPARQL/SPIN source code and the start of the structured RDF (after the sp:text block). It's the structured RDF that I need to be able to generate using Java from the SPARQL source code.

     <sp:Construct>
        <sp:text rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
        ># [enabled] &lt;http://www.disa.mil/dso/a2i/ontologies/PBSM/Interface/Pub7#Pub7Proposal_makeRxSCMPointSurface&gt; construct an SCM Point Surface (zero radius) from supporting rx data items under an SCM receiver model
CONSTRUCT {
    ?this soo:hasSCMReceiverModel ?receiverModelURI . 
    ?receiverModelURI soo:hasSCMPointSurfaceLocation ?pointSurfaceURI .
    ?pointSurfaceURI soo:SCMPointSurfaceHasPoint ?pointURI .&#xD;
    ?pointSurfaceURI a soo:SCMPointSurfaceLocation .
}
WHERE {
    ?this pub7:pub7ProposalHasDataItem ?rxRadiusDataItem .
    ?rxRadiusDataItem a pub7:Pub7DataItem406 .
    ?rxRadiusDataItem soo:hasSCMRadius ?radiusURI .
    ?radiusURI Nuvio:hasDataValue ?radiusValue .
    FILTER (?radiusValue = 0.0000) .
    ?this pub7:pub7ProposalHasDataItem ?rxPointDataItem .
    ?rxPointDataItem a pub7:Pub7DataItem403 .
    ?rxPointDataItem soo:hasSCMPointLocation ?pointURI .
    BIND (URI(CONCAT(str(?this), "_rxModel")) AS ?newReceiverModelURI) .
    BIND (IF(bound(?existingReceiverModelURI), ?existingReceiverModelURI, ?newReceiverModelURI) AS ?receiverModelURI) .
    BIND (URI(CONCAT(str(?receiverModelURI), "_pointSurface")) AS ?pointSurfaceURI) .
}</sp:text>
        <rdfs:comment rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
        >[enabled] &lt;http://www.disa.mil/dso/a2i/ontologies/PBSM/Interface/Pub7#Pub7Proposal_makeRxSCMPointSurface&gt; construct an SCM Point Surface (zero radius) from supporting rx data items under an SCM receiver model</rdfs:comment>
        <sp:templates rdf:parseType="Collection">
          <rdf:Description>
            <sp:object rdf:parseType="Resource">
              <sp:varName rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
              >receiverModelURI</sp:varName>
            </sp:object>
            <sp:predicate rdf:resource="http://www.disa.mil/dso/a2i/ontologies/PBSM/Sharing/SpectrumOperationsOntology#hasSCMReceiverModel"/>
            <sp:subject rdf:resource="http://spinrdf.org/spin#_this"/>
          </rdf:Description>
          <rdf:Description>
            <sp:object rdf:parseType="Resource">
              <sp:varName rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
              >pointSurfaceURI</sp:varName>
            </sp:object>
            <sp:predicate rdf:resource="http://www.disa.mil/dso/a2i/ontologies/PBSM/Sharing/SpectrumOperationsOntology#hasSCMPointSurfaceLocation"/>
            <sp:subject rdf:parseType="Resource">
              <sp:varName rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
              >receiverModelURI</sp:varName>
            </sp:subject>
          </rdf:Description>
          <rdf:Description>
            <sp:object rdf:parseType="Resource">
              <sp:varName rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
              >pointURI</sp:varName>
            </sp:object>
            <sp:predicate rdf:resource="http://www.disa.mil/dso/a2i/ontologies/PBSM/Sharing/SpectrumOperationsOntology#SCMPointSurfaceHasPoint"/>
            <sp:subject rdf:parseType="Resource">
              <sp:varName rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
              >pointSurfaceURI</sp:varName>
            </sp:subject>
          </rdf:Description>
          <rdf:Description>
            <sp:object rdf:resource="http://www.disa.mil/dso/a2i/ontologies/PBSM/Sharing/SpectrumOperationsOntology#SCMPointSurfaceLocation"/>
            <sp:predicate rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#type"/>
            <sp:subject rdf:parseType="Resource">
              <sp:varName rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
              >pointSurfaceURI</sp:varName>
            </sp:subject>
          </rdf:Description>
        </sp:templates>
Greg Cox
  • 287
  • 1
  • 12
  • 1
    Maybe I missed something in your text, but why can't you simply use the [API](http://topbraid.org/spin/api/)? One of its features is *"Converters between textual SPARQL syntax and the SPIN RDF Vocabulary"* – UninformedUser Aug 23 '17 at 02:14
  • An [example](https://github.com/spinrdf/spinrdf/blob/master/src-examples/org/topbraid/spin/examples/SPINParsingExample.java) shows conversion from query string to SPIN RDF and back – UninformedUser Aug 23 '17 at 02:20
  • Actually @AKSW, that's the answer I was looking for. I hope my question wasn't too simple. I've pulled xercesimpl-2.7.1.jar, libthrift-0.9.1.jar, spin-2.0.0.jar and the jena 3.4.0 jar files into my my project and build path, and now the example you pointed out runs. Thanks! I can post this as an answer to my own question, but it's really your answer, so you can post an answer if you wish. Your choice. Thanks! – Greg Cox Aug 23 '17 at 14:53
  • Don't worry. Often the simple ways to find the solutions are the best. Feel free to add it as an answer and don't forget to accept it. Cheers – UninformedUser Aug 23 '17 at 19:08

1 Answers1

1

As @AKSW pointed out, a SPIN API coupled with the Jena libraries can accomplish what I'm after. TopBraid's SPINParsingExample.java is very close to what I want to do.

I did need to add one SPIN library and several Jena library files (.jar files) to my Eclipse project and to the Build Path. I'm aware that there are automated ways to handle these dependencies, but for now I'm doing it manually. For those also doing it manually, these files are listed below...

SPIN library:

Apache Jena libraries (better to use 3.0.1 as suggested in comments):

TallTed
  • 9,069
  • 2
  • 22
  • 37
Greg Cox
  • 287
  • 1
  • 12
  • 1
    Don't forget to "Accept" the answer :D (By the way, you should really thing about Maven/Gradle for the future, it's absolutely convenient, believe me) – UninformedUser Aug 24 '17 at 05:29
  • 1
    Please NOTE: SPIN-2.0.0 is based on jena-3.0.1, you can't use jena 3.4+spin-2.0.0 together! At least for spin-inferencing, (but: i tested that there are no conflicts with example above, suddenly). – ssz Aug 24 '17 at 08:44
  • 1
    Not to be unfounded, that's because there are no changes in jena model api between 3.0.1 and 3.4.0, and rdf-parsing is using only this functionality ('jena-resource polymorphism'). But while inferencing you will get something like `java.lang.AbstractMethodError: Method org/topbraid/spin/util/DatasetWrappingDatasetGraph.supportsTransactions()Z is abstract` – ssz Aug 24 '17 at 08:59
  • Good to know regarding version incompatibility @Sigrem. Thanks! My inferencing is running in an rdf4j server which is independent of this code, I think. Never-the-less, I'll probably down-rev the jena libraries for full functionality in case I change where inferencing is done in the future. – Greg Cox Aug 24 '17 at 10:41
  • @ssz can you explain what causes the `AbstractMethodError`? I'm getting it as well. Looking at the SPINRDF code, `DatasetWrappingDatasetGraph.supportsTransactions()` is not abstract: https://github.com/spinrdf/spinrdf/blob/master/src/main/java/org/spinrdf/util/DatasetWrappingDatasetGraph.java#L222 – Martynas Jusevičius May 18 '20 at 14:45
  • incompatible `org.spinrdf:spinrdf` and `org.apache:jena` versions, perhaps. I don't know you configuration, but `org.topbraid:spin` and `org.spinrdf:spinrdf` are different things – ssz May 18 '20 at 15:30