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] <http://www.disa.mil/dso/a2i/ontologies/PBSM/Interface/Pub7#Pub7Proposal_makeRxSCMPointSurface> 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 .
?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] <http://www.disa.mil/dso/a2i/ontologies/PBSM/Interface/Pub7#Pub7Proposal_makeRxSCMPointSurface> 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>
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):
来源:https://stackoverflow.com/questions/45829446/how-to-turn-a-sparql-spin-query-rule-into-an-rdf-structure-from-java