I am looking to run a SPARQL query over any dataset. We dont know the names of the named graphs in the datasets.
These are lots of documentation and examples of selection from named graphs when you know the name of the named graph/s. There are examples showing listing named graphs.
We are running the Jena from Java so it would be possible to run 2 queries, the first gets the named graphs and we inject these into the 2nd.
But surely you can write a single query that reads from all named graphs when you dont know their names?
Note: we are looking to stay away from using default graph/s as their behaviour seems implementation dependent.
Example:
{
?s foaf:name ?name ;
vCard:nickname ?nickName .
}
If you want the pattern to match within one graph and wish to try each graph, use the GRAPH ?g form.
GRAPH ?g
{ ?s foaf:name ?name ;
vc:nickname ?nickName .
}
If you want to make a query where the pattern matches across named graphs, -- e.g. foaf:name in one graph and vCard:nickname in another, same subject --
then set union default graph tdb2:unionDefaultGraph true then the default graph as seen by the query is the union (actually, RDF merge - no duplicates) of all the named graphs. Use the pattern as originally given.
Fuseki configuration file extract:
:dataset_tdb2 rdf:type tdb2:DatasetTDB2 ;
tdb2:location "DB2" ;
## Optional - with union default for query and update WHERE matching.
tdb2:unionDefaultGraph true ;
.
In code, not Fuseki, the application can use Dataset.getUnionModel().
I've just started using the triple store RDF4J (I am using its workbench, version 2.3.1, run on Windows 10 with Tomcat 9.0)
I want to use the SPIN rules in RDF4J. Therefore, I created a new repository (In memory with RDFS+SPIN support).
I wanted to start with the SPIN example in RDF4J documentation concerning how to add SPIN rules. That is, I added the data (in Turtle, and imported to RDF4J)
#prefix ex: <http://example.org/>.
ex:John a ex:Father ;
ex:parentOf ex:Lucy .
ex:Lucy a ex:Person .
And the rule:
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
#prefix sp: <http://spinrdf.org/sp#>.
#prefix spin: <http://spinrdf.org/spin#>.
#prefix ex: <http://example.org/>.
ex:Person a rdfs:Class ;
spin:rule [
a sp:Construct ;
sp:text """PREFIX ex: <http://example.org/>
CONSTRUCT { ?this ex:childOf ?parent . }
WHERE { ?parent ex:parentOf ?this . }"""
] .
And as instructed in the documentation, I exposed the query (with the checkbox 'Include inferred statements' checked),
PREFIX ex: <http://example.org/>
SELECT ?child
WHERE { ?child ex:childOf ?parent }
However, no result returned:
Could someone, please tell me am I doing something wrong, why the SPIN rule doesn't work in my RDF4J workbench, have I missed something?
(reposting my comment as an answer for future readers)
The SPIN reasoner currently assumes that all data is in the default context, I think. Make sure that your data was not added to a named graph.
I'm using RDF4J server and workbench version 2.2.2. I'm using a SPIN-capable repository in RDF4J, and I'm reading an RDF file I've created in TobBraid Composer Free Edition (TBC FE) containing many spin:construct rules. TBD FE is set to include the SPARQL source code for my constructors via the sp:text property. I've observed that, when the sp:text triple is present making the SPARQL source code available to RDF4J, RDF4J appears to use the SPARQL source instead of using the RDF tokenized representation of the same query. My primary and secondary questions are:
How can I direct RDF4J to use the RDF representation of a SPIN constructor when both the sp:text and the RDF representation are present in the imported RDF file?
If RDF4J uses the sp:text representation of a SPIN constructor, how can it be directed to use the prefix definitions present at the head of the imported RDF file? I have 69 constructors so far and counting. Although I can embed prefix definitions in each constructor, it seems wasteful to do so.
When I deprive RDF4J of the sp:text SPARQL source code representation, the constructors run as expected using prefixes defined at the head of the imported RDF file. I can deprive RDF4J of the source code by executing a query to delete to the source code before I instantiate the class for which I have defined a constructor. I've used this SPARQL update query to accomplish that end:
PREFIX sp: <http://spinrdf.org/sp#>
DELETE {?s sp:text ?o .} WHERE {?s sp:text ?o .}
I'd like to keep the SPARQL source code around for display purposes in a GUI I'm building that communicates with the RDF4J server via SPARQL queries. My awkward interim fix is to substitute another custom data property for the sp:text property using the SPARQL update query used in step 5 below. This property substitution successfully prevents RDF4J from finding the SPARQL source code for the SPIN constructors. RDF4J then interprets the tokenized RDF representation of the rule instead. Is there a cleaner way to force RDF4J to execute SPIN constructors from the RDF instead of from the SPARQL source code? I'm thinking there must be some way to signal RDF4J to interpret the RDF representation instead of the SPARQL source code, but I don't know how.
And now the gory details to make this situation reproducible...
Create a SPIN-capable repository in RDF4J using RDF4J workbench. Mine is named TakeRDF4J4SPIN (Repositories → New Repository)
Clear the repository using RDF4J workbench (Modify → Clear)
Load pub7.rdf using RDF4J workbench... (Modify → Add w/ use base URI as context identifier unchecked)
Note that this RDF file defines all relevant prefixes at the head of the file:
<?xml version="1.0"?>
<rdf:RDF
xmlns:fn="http://www.w3.org/2005/xpath-functions#"
xmlns:soo="http://www.disa.mil/dso/a2i/ontologies/PBSM/Sharing/SpectrumOperationsOntology#"
xmlns:spolicy="http://www.disa.mil/dso/a2i/ontologies/PBSM/Sharing/spolicy#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:spin="http://spinrdf.org/spin#"
xmlns:sp="http://spinrdf.org/sp#"
xmlns:smf="http://topbraid.org/sparqlmotionfunctions#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:arg="http://spinrdf.org/arg#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:pub7="http://www.disa.mil/dso/a2i/ontologies/PBSM/Interface/Pub7#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:spl="http://spinrdf.org/spl#"
xml:base="http://www.disa.mil/dso/a2i/ontologies/PBSM/Interface/Pub7">
Also note that the SPIN constructor attached to the class pub7:Pub7TestClass is:
CONSTRUCT {
?this spin:hasConstraintViolation _:b0 .
_:b0 a spin:ConstraintViolation ;
rdfs:label "Test message." ;
spin:violationRoot ?this ;
spin:violationLevel spin:Error ;
spolicy:sPActionRecordHasTimestamp ?timestamp .
}
WHERE {
BIND(now() AS ?timestamp) .
}
Attempt to create an instance of pub7:Pub7TestClass...
I use this SPARQL update query in RDF4J workbench's Modify → SPARQL Update to create the instance...
PREFIX inst: <http://www.disa.mil/dso/a2i/ontologies/PBSM/Sharing/Instantiations#>
PREFIX pub7: <http://www.disa.mil/dso/a2i/ontologies/PBSM/Interface/Pub7#>
INSERT DATA {
inst:aPub7TestClassInstance_test1 a pub7:Pub7TestClass .
}
Which should result in an prefix undefined error reported in RDF4J workbench due to attempting to execute the above constructor based on the sp:text SPARQL source code representation:
org.eclipse.rdf4j.query.UpdateExecutionException: org.eclipse.rdf4j.repository.RepositoryException: org.eclipse.rdf4j.sail.SailException: org.eclipse.rdf4j.query.MalformedQueryException: org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException: QName 'spin:hasConstraintViolation' uses an undefined prefix
Now hide the SPIN constructors' sp:text from RDF4J using the following SPARQL update query:
.
PREFIX sp: <http://spinrdf.org/sp#>
PREFIX soo: <http://www.disa.mil/dso/a2i/ontologies/PBSM/Sharing/SpectrumOperationsOntology#>
DELETE {
?originalPolicyURI sp:systemProperty ?policySourceCodeString .
?originalPolicyURI sp:text ?policySourceCodeString .
}
INSERT {
?originalPolicyURI soo:policySourceCode ?policySourceCodeString .
}
WHERE {
?originalPolicyURI sp:text ?policySourceCodeString .
}
Now re-run the SPARQL update query shown in step 4 to instantiate the test class. The update query should run without errors this time.
Now use RDF4J Workbench's Explore → Explore function to look at the instantiation/individual we just created, inst:aPub7TestClassInstance_test1. One should see that the instantiation has a constraint violation notice via the spin:hasConstratintViolation property, providing evidence that the test constructor did indeed run this time absent the sp:text representation, forced to use the RDF tokenized representation of the SPIN constructor.
I have a partial, inelegant work-around to address my secondary question about including prefixes in the spin rules. As an initialization step, I run the following SPARQL update query that adds needed prefixes to each of the sp:text strings. Of course, this work-around is limited since the query embeds the prefix mappings I'm using. If other prefixes were required, then the query below would need to be edited to embed the new query definitions.
Here's the SPARQL Update query I'm using as an initialization step after my class/rule/property definition ontologies have been loaded into RDF4J but before I load my instantiations ontology is loaded. (Timing is critical since the instantiations ontology begins invoking SPIN constructors.)
PREFIX sp: <http://spinrdf.org/sp#>
DELETE {
?rule sp:text ?ruleText .
}
INSERT {
?rule sp:text ?newRuleText .
}
WHERE {
?rule sp:text ?ruleText .
BIND (IF (CONTAINS(?ruleText, "fn:"), "PREFIX fn: <http://www.w3.org/2005/xpath-functions#>\n","")
AS ?prefixComponent00) .
BIND (IF (CONTAINS(?ruleText, "owl:"), "PREFIX owl: <http://www.w3.org/2002/07/owl#>\n","")
AS ?prefixComponent01) .
BIND (IF (CONTAINS(?ruleText, "spin:"), "PREFIX spin: <http://spinrdf.org/spin#>\n","")
AS ?prefixComponent02) .
BIND (IF (CONTAINS(?ruleText, "spl:"), "PREFIX spl: <http://spinrdf.org/spl#>\n","")
AS ?prefixComponent03) .
BIND (IF (CONTAINS(?ruleText, "sp:"), "PREFIX sp: <http://spinrdf.org/sp#>\n","")
AS ?prefixComponent04) .
BIND (IF (CONTAINS(?ruleText, "par:"), "PREFIX par: <http://parliament.semwebcentral.org/parliament#>\n","")
AS ?prefixComponent05) .
BIND (IF (CONTAINS(?ruleText, "rdf:"), "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n","")
AS ?prefixComponent06) .
BIND (IF (CONTAINS(?ruleText, "rdfs:"), "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n","")
AS ?prefixComponent07) .
BIND (IF (CONTAINS(?ruleText, "time:"), "PREFIX time: <http://www.w3.org/2006/time#>\n","")
AS ?prefixComponent08) .
BIND (IF (CONTAINS(?ruleText, "xsd:"), "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n","")
AS ?prefixComponent09) .
BIND (IF (CONTAINS(?ruleText, "geo:"), "PREFIX geo: <http://www.opengis.net/ont/geosparql#>\n","")
AS ?prefixComponent10) .
BIND (IF (CONTAINS(?ruleText, "geof:"), "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>\n:","")
AS ?prefixComponent11) .
BIND (IF (CONTAINS(?ruleText, "inst:"), "PREFIX inst: <http://www.disa.mil/dso/a2i/ontologies/PBSM/Sharing/Instantiations#>\n","")
AS ?prefixComponent12) .
BIND (IF (CONTAINS(?ruleText, "pub7:"), "PREFIX pub7: <http://www.disa.mil/dso/a2i/ontologies/PBSM/Interface/Pub7#>\n","")
AS ?prefixComponent13) .
BIND (IF (CONTAINS(?ruleText, "pub8:"), "PREFIX pub8: <http://www.disa.mil/dso/a2i/ontologies/PBSM/Interface/Pub8#>\n","")
AS ?prefixComponent14) .
BIND (IF (CONTAINS(?ruleText, "soo:"), "PREFIX soo: <http://www.disa.mil/dso/a2i/ontologies/PBSM/Sharing/SpectrumOperationsOntology#>\n","")
AS ?prefixComponent15) .
BIND (IF (CONTAINS(?ruleText, "spolicy:"), "PREFIX spolicy: <http://www.disa.mil/dso/a2i/ontologies/PBSM/Sharing/spolicy#>\n","")
AS ?prefixComponent16) .
BIND (IF (CONTAINS(?ruleText, "sharing:"), "PREFIX sharing: <http://www.disa.mil/dso/a2i/ontologies/PBSM/Sharing/sharing#>\n","")
AS ?prefixComponent17) .
BIND (IF (CONTAINS(?ruleText, "dd1494:"), "PREFIX dd1494: <http://www.disa.mil/dso/a2i/ontologies/PBSM/Sharing/dd1494#>\n","")
AS ?prefixComponent18) .
BIND (IF (CONTAINS(?ruleText, "Nuvio:"), "PREFIX Nuvio: <http://cogradio.org/ont/Nuvio.owl#>\n","")
AS ?prefixComponent19) .
BIND (IF (CONTAINS(?ruleText, "CRO2:"), "PREFIX CRO2: <http://cogradio.org/ont/CRO2.owl#>\n","")
AS ?prefixComponent20) .
BIND (IF (CONTAINS(?ruleText, "olo:"), "PREFIX olo: <http://purl.org/ontology/olo/core#>\n","")
AS ?prefixComponent21) .
BIND (CONCAT(
?prefixComponent00,
?prefixComponent01,
?prefixComponent02,
?prefixComponent03,
?prefixComponent04,
?prefixComponent05,
?prefixComponent06,
?prefixComponent07,
?prefixComponent08,
?prefixComponent09,
?prefixComponent10,
?prefixComponent11,
?prefixComponent12,
?prefixComponent13,
?prefixComponent14,
?prefixComponent15,
?prefixComponent16,
?prefixComponent17,
?prefixComponent18,
?prefixComponent19,
?prefixComponent20,
?prefixComponent21
) AS ?prefixes ) .
BIND (CONCAT(?prefixes, "\n", ?ruleText) AS ?newRuleText) .
}
It is possible that the query above could include a prefix definition based on a false positive if the prefix definition happened to be contained in an embedded string or comment. For example, the literal "Greg’s solo: five measures"^^xsd:string would cause the above query to include the definition of the olo: prefix, perhaps needlessly. However, inclusion of non-required prefixes had no major impact on performance and no impact on correctness. One could do better by using regular expression in place of the simple CONTAINS matches in the query above.
I have a RDF graph with several types of relations (relations with the same prefix and with different prefixes also). I need to call a service over the graph but filtering out some relations.
Example:
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
#prefix myPref: <http://www.myPref.com/>.
#prefix otherPref: <http://www.otherPref.com/>.
myPref:1
myPref:label "1" ;
myPref:solid myPref:2 ;
myPref:dotted myPref:4 ;
otherPref:dashed myPref:3 ;
otherPref:dashed2 myPref:3 .
myPref:2
myPref:label "2" ;
myPref:solid myPref:3 .
myPref:3
myPref:label "3" .
myPref:4
myPref:label "4" ;
myPref:dotted myPref:3 .
I would like to run the service call over an extracted sub-graph containing only the solid and dotted relations (In this particular case, running a service calculating the shortest path between 1 to 3, I want to exclude those direct links).
I run the service (Over the entire graph) like this:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
PREFIX myPref: <http://www.myPref.com/>.
PREFIX otherPref: <http://www.otherPref.com/>.
PREFIX gas: <http://www.bigdata.com/rdf/gas#>
SELECT ?sp ?out {
SERVICE gas:service {
gas:program gas:gasClass "com.bigdata.rdf.graph.analytics.SSSP" .
gas:program gas:in myPref:1 .
gas:program gas:target myPref:3 .
gas:program gas:out ?out .
gas:program gas:out1 ?sp .
}
}
How can I extract a subgraph containing only the links I want (Dotted and solid) and the run the service call over the extracted sub-graph?
SPARQL doesn't provide any functionality for querying a constructed graph, unfortunately. I've come across places where it would make some queries very easy. Some endpoints do have extensions to support it, though. I think that dotNetRDF might support it. There are probably a few aspects: in many cases, it's not actually necessary; if the endpoint supports updates, you can create a new named graph and construct into it, and then launch a second query against it (which is pretty much what you're asking for, but in two steps); this could be a very expensive operation, so endpoints might disable it anyway, even if it was directly supported.
The first note, though, that it's often times not necessary, appears that it might be the case here.
I need to call a service over the graph but filtering out some relations.
In this case, you can query over the subgraph that you want, I think, by using property paths. You can ask for paths built from just solid and dashed edges like:
?s myPref:solid|myPref:dotted ?t
If you want an arbitrary path of them, you can repeat it:
?s (myPref:solid|myPref:dotted)+ ?t
If you have unique paths between sources and destinations, then you can figure out the lengths of paths using the standard "count the ways of splitting the path" technique:
select (count(?t) as ?length) {
?s (myPref:solid|myPref:dotted)* ?t
?t (myPref:solid|myPref:dotted)* ?u
}
group by ?s ?t
I am doing some experiments with importing triples formulated in the turtle language
within the openrdf-workbench webapp in Tomcat, which has incorporated a SPARQL endpoint.
I wonder if with turtle, or, generally, in RDF / RDFS is it possible to add a certain predicate/object declaration on all (implicit) subjects conditionally to the existence of another predicate/object.
For example, if I have the following triples defined:
foo:a foo:b foo:c
foo:d foo:b foo:c
foo:e foo:b foo:c
foo:f foo:b foo:c
I would like to automatically add the following predicate/subject to all subjects that match predicate=foo:b and object=foo:c:
(implicit subject) foo:g foo:h
in order to automatically produce the following triples:
foo:a foo:g foo:h
foo:d foo:g foo:h
foo:e foo:g foo:h
foo:f foo:g foo:h
Is this possible?
Alternatively: is there any way to define some triples in order to enable SPARQL to find foo:a/d/e/f when queried for subjects that have foo:g foo:h as predicate/object?
Part 1 - Creating additional information
The first part of your question can be solved in one of two ways:
Using Inference
Using SPARQL Update
Inferencing
Inference is a technique whereby you define rules that infer additional triple based on your existing triples. You typically either use a pre-defined set of rules or use your own custom rules. I think Sesame only supports pre-defined rule sets out of the box so you may want to take a look at OWLIM which is an alternative back end that can be used with Sesame and has much more customisable rules AFAIK.
Inferencing can typically be applied in two ways, one where you only store the rules and you compute the additional information every time a rule fires and another where you pre-compute all the additional information and add it to your database. Which you will want to use depends on how you intend to use your system and there are performance trade offs involved. I'm not going into detail because that's really a whole other question - see Forward vs Backward Chaining for some discussion
SPARQL Update
Alternatively if your rules are relatively simple and you are OK with pre-computing the extra information and adding it to your database you can write SPARQL Updates to do this e.g.
PREFIX foo: <http://example.org/foo#>
INSERT
{
?x foo:g foo:h .
}
WHERE
{
?x foo:b foo:c .
}
Part 2 - Querying the Data
I am guessing you are fairly new to SPARQL because from what you've described this sounds trivial to me.
If I wanted to find all subjects which had the predicate foo:g and the object foo:h I would simply write the following:
PREFIX foo: <http://example.org/foo#>
SELECT ?x
WHERE
{
?x foo:g foo:h .
}
You can do this type of inference using OWL with an axiom of the form
p value a ⊑ q value b
which says that if something has a as a value for property p, then it also has b as a value for property q. As an example, here's an ontology with four individuals (a, b, c, d), two object properties (p, q), and the axiom (p value c ⊑ q value d).
#prefix : <http://example.org/add-predicate-object#> .
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
#prefix owl: <http://www.w3.org/2002/07/owl#> .
#prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
<http://example.org/add-predicate-object> a owl:Ontology .
:p a owl:ObjectProperty .
:q a owl:ObjectProperty .
[ a owl:Restriction ;
owl:onProperty :p ;
owl:hasValue :c ;
rdfs:subClassOf [ a owl:Restriction ;
owl:onProperty :q ;
owl:hasValue :d ] . ] .
:a a owl:Thing, owl:NamedIndividual ; :p :c .
:b a owl:Thing, owl:NamedIndividual ; :p :c .
:c a owl:Thing, owl:NamedIndividual .
:d a owl:Thing, owl:NamedIndividual .
In Protégé, the axiom looks like this:
You can enable a reasoner and query for instances of q value d and see:
or you can browse to individuals and see the results: