How to get a simple inferencing example to work - graphdb

I might have understood something wrong, so bear with me.
I created a repository called 'reasoning' of type OWL2-RL (tried some other types already too).
I created a rule as follows:
prefix sys: <http://www.ontotext.com/owlim/system#>
INSERT DATA {
<_:pets> sys:addRuleset
'''Prefices { ex : <http://www.example.com#> }
Axioms {}
Rules
{
Id: custom
a <ex:hasPet> b
------------------------------------
b <ex:hasOwner> a
}'''
}
I insert some triples as follows:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX ex: <http://www.example.com#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
INSERT DATA {
<ex:hasPet> a <owl:ObjectProperty>;
<rdfs:domain> <ex:Human>;
<rdfs:range> <ex:Pet>.
<ex:someHuman> <ex:hasPet> <ex:somePet>.
}
Consequently I set my rule as default as follows :
PREFIX sys: <http://www.ontotext.com/owlim/system#>
INSERT DATA {
_:b sys:defaultRuleset "pets"
}
I expect the following query
PREFIX ex: <http://www.example.com#>
select * where {
<ex:somePet> ?p ?o .
} limit 100
to return the following inferred triple
<ex:somePet> <ex:hasOwner> <ex:someHuman>
but unfortunately no inferred triples are present in the repository.
Could you please help me? Having a working example with the different steps to take will help me get rules to work in my application.
Thanks in advance!
Kind regards

There are multiple issues, first IRIs, within the Prefixes section of the ruleset must be written without angle brackets, e.g. use ex : http://www.example.com# instead ...
also your data and sample query do not make use of the prefix ex but are absolute IRIs, e.g. instead of
INSERT DATA {
<ex:hasPet> a <owl:ObjectProperty>;
<rdfs:domain> <ex:Human>;
<rdfs:range> <ex:Pet>.
<ex:someHuman> <ex:hasPet> <ex:somePet>.
}
rewrite to
INSERT DATA {
ex:hasPet a owl:ObjectProperty;
rdfs:domain ex:Human;
rdfs:range ex:Pet.
ex:someHuman ex:hasPet ex:somePet.
}

Related

SPARQL: Conditional INSERT

I'm trying to create a SPARQL statement that inserts some triples only if a certain pattern isn't yet in the graph.
PREFIX ssb: <ssb:ontology:>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
INSERT {
<ssb:message/some=> rdf:type ssb:Message;
ssb:seq 7;
ssb:raw "some text";
ssb:author 1.
} WHERE {
FILTER NOT EXISTS {
[] ssb:seq 7; ssb:author 1
}
}
Unfortunately, this seems to create the new triples even if a resource with that ssb:seq and ssb:author already exist, tried the with quadstorejs and with oxigraph.
Any suggestion on how to perform such a conditional insert? The goal is that I don't end up with several resources having the same sequence number and author.
I believe your first attempt is correct and it looks like a bug in the systems that you tried.
The algebra for
FILTER NOT EXISTS {
[] ssb:seq 7; ssb:author 1
}
is the FILTER directly on top of Singleton and it must return a single (empty) solution when the [] ssb:seq 7; ssb:author 1 does not match data. Since there're no variables in your INSERT template, data should be inserted.
The version with OPTIONAL isn't much different, there's an implicit {} before the OPTIONAL, and it's the same Singleton.
I just tried a CONSTRUCT version of your 1st query with Stardog and it worked as expected.
I found a solution that seems to work:
PREFIX ssb: <ssb:ontology:>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
INSERT {
<ssb:message/some=> rdf:type ssb:Message;
ssb:seq 7;
ssb:raw "some text";
ssb:author 1.
} WHERE {
OPTIONAL {?x ssb:seq 7; ssb:author 1.}
FILTER (!BOUND(?x))
}
Not sure exactly why, though. I mean the WHERE-Clause either matches nothing because the pattern isn't there or because it is filtered out when it exists.

SPARQL: Inverse result on boolean query

I am trying to have SPARQL return 'false' if a set of triples exist in an RDF database. I'm able to return 'true' with an ASK query.
ASK WHERE { ?subjID rdf:type pref:Person. }
As described here, I tried adding NOT EXISTS { } inside of the WHERE, but this results in an error.
ASK WHERE { NOT EXISTS { ?subjID rdf:type pref:Person. } }
The documentation that I linked doesn't describe this but you must put FILTER in front of NOT EXISTS.
ASK WHERE { FILTER NOT EXISTS { ?subjID rdf:type pref:Person. } }

Best approach to create URIs with Sparql (like auto increment)

I am currently writing a service which creates new items (data) by user input. To save these items in a RDF Graph Store (currently using Sesame via Sparql 1.1), I need to add a subject URI to the data. My approach is to use a number that is incremented for each new item. So e.g.:
<http://example.org/item/15> dct:title "Example Title" .
<http://example.org/item/16> dct:title "Other Item" .
What's the best approach to get an incremented number for new items (like auto incement in MySQL/MongoDB) via Sparql? Or to issue some data and the endpoint autmatically creates a URI by a template (like done for blank nodes).
But I don't want to use blank nodes as subjects for these items.
Is there a better solution than using an incremented number? My users don't care about the the URI.... and I don't want to handle collisions like created by hashing the data and using the hash as part of the subject.
If you maintain a designated counter during updates, then something along these lines will do it,
fisrt insert a counter into your dataset
insert data {
graph <urn:counters> {<urn:Example> <urn:count> 1 }
}
then a typical update should looks like:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
delete {
#remove the old value of the counter
graph <urn:counters> {<urn:Example> <urn:count> ?old}
}
insert {
#update the new value of the counter
graph <urn:counters> {<urn:Example> <urn:count> ?new}
# add your new data using the constructed IRI
GRAPH <http://example.com> {
?id dct:title "Example Title" ;
a <http://example.org/ontology/Example> .
} .
} where {
# retrieve the counter
graph <urn:counters> {<urn:Example> <urn:count> ?old}
# compute the new value
bind(?old+1 as ?new)
#construct the IRI
bind(IRI(concat("http://example.org/item/", str(?old))) as ?id)
}
Assuming the class of your items is http://example.org/ontology/Example, the query becomes the following. Note: items must be inserted one by one, as only one new URI is computed at each transaction.
PREFIX dct: <http://purl.org/dc/terms/>
INSERT {
GRAPH <http://example.com> {
?id dct:title "Example Title" ;
a <http://example.org/ontology/Example> .
} .
} WHERE {
SELECT ?id WHERE {
{
SELECT (count(*) AS ?c) WHERE {
GRAPH <http://example.com> { ?s a <http://example.org/ontology/Example> }
}
}
BIND(IRI(CONCAT("http://example.org/item/", STR(?c))) AS ?id)
}
}
(Tested with GraphDB 8.4.0 using RDF4J 2.2.2)
You said you are open to other options than an auto-incremented number. One good alternative is to use UUIDs.
If you don't care at all what the URI looks like, you can use the UUID function:
INSERT {
?uri dct:title "Example Title"
}
WHERE {
BIND (UUID() AS ?uri)
}
This will generate URIs like <urn:uuid:b9302fb5-642e-4d3b-af19-29a8f6d894c9>.
If you'd rather have HTTP URIs in your own namespace, you can use strUUID:
INSERT {
?uri dct:title "Example Title"
}
WHERE {
BIND (IRI(CONCAT("http://example.org/item/", strUUID())) AS ?uri)
}
This will generate URIs like http://example.org/item/73cd4307-8a99-4691-a608-b5bda64fb6c1.
UUIDs are pretty good. Collision risk is negligible. The functions are part of the SPARQL standard. The only downside really is that they are long and ugly.

SPARQL cannot query "is of" relationship

I am querying some features like:
PREFIX dbo: <http://dbpedia.org/ontology/>
select ?field where {
dbpedia:Jeffrey_Karp dbo:field ?field
}
This return the content correctly. But if I query some feature in the form "is ** of", the query will return null, though the field exists. Here is the query:
select ?studentof where {
dbpedia:Jeffrey_Karp dbo:notableStudent ?studentof
}

Conditional subquery in SPIN function (SPARQL)

How do I change the query formula based on whether or not a variable is bound?
I am invoking the magic property like this:
WHERE {
VALUES (?subj) {
([my bound positional parameter value goes here...])
}
?subj :myMagicProperty ?result .
}
Inside the magic property, I do a union:
?result a :Rule .
{
?result :someProp ?subj .
}
UNION
{
FILTER NOT EXISTS {
?result :someProp ?anyValue .
}
}
In other words, get me all results where :someProp is this value or :someProp is not defined.
Here is the tricky part. If ?subj is unbound (i.e., I set it as UNDEF in the VALUES block), the above query goes wild and returns everything.
Instead, I want to check if ?subjis unbound. If ?subj is unbound, :myMagicProperty should only return the following results:
FILTER NOT EXISTS {
?result ?someProp ?anyValue .
}
I have experimented with using FILTER and the BOUND function, but I can't figure out how to get the correct behavior. How can I drop one of UNION clauses from my query when ?subj is not bound?
Updates
Revised the first query to add the VALUES block.
Added missing ?result a :Rule . statement.
Corrected ?someProp to :someProp.
First I'd like to confirm what your intent is. I'd like to do that by asking you to respond to the following query that you can run in TopBraid Composer.
SELECT *
WHERE { GRAPH <http://topbraid.org/examples/kennedys> {
VALUES (?property) {(kennedys:firstName) (kennedys:lastName) (UNDEF)}
{
FILTER(BOUND(?property) )
?s ?property ?result .
}
UNION
{
FILTER(!BOUND(?property))
BIND("not sure what you want to do in this case" AS ?result)
}
}
}
The difference in the code above to your code is that I am setting values of your ?someProp in the VALUES statement, whereas you are setting ?subj.
The UNIONed subgraphs are using BOUND and !BOUND as guards.
Before going further with help I'd like to hear from you with a clearer explanation of the query you are wanting to build. Then I can show you the magic property that will be needed.
It's this piece of your initial post I need to understand more:
Here is the tricky part. If ?subj is unbound (i.e., I set it as UNDEF in the VALUES block), the above query goes wild and returns everything.
Instead, I want to check if ?subj is unbound. If ?subj is unbound, myMagicProperty should only return the following results:
FILTER NOT EXISTS {
?result ?someProp ?anyValue .
}*
With ?someProp undefined, as well as ?result and ?anyValue, what were you expecting to come back? Also this subgraph of yours has no assertions that will populate the graph and therefore will return nothing.
Ralph
The trick is, I need to do the UNION using a variable different than the one passed in as an argument. This way, the UNION operation does not cause the unbound parameter to be bound. After the UNION, I can use a FILTER to control the results based on the input parameter.
SELECT ?result
WHERE {
?result a :Rule .
{
SELECT ?rule ?value ?anyValueMatch
WHERE {
{
?rule :someProp ?value .
BIND (false AS ?anyValueMatch) .
}
UNION
{
FILTER NOT EXISTS {
?rule :someProp ?any .
} .
BIND (true AS ?anyValueMatch) .
} .
}
} .
FILTER ((bound(?subj) && (?value = ?subj)) || (?anyValueMatch = true)) .
}
Another way to do this is with COALESCE:
SELECT ?result
WHERE {
?result a :Rule .
OPTIONAL {
?result :someProp ?value .
}
FILTER (COALESCE(?value = ?subj, !bound(?value)))
}
...this avoids the sub-select and simply filters to include only the ?result matches where '?value = ?subj', and if that clause fails the !bound() clause ensures matches that do not have a :someProp property are also included.