Graphdb seem to be missing rdfs:subClassOf inference - sparql

I am using the RDFS-Plus (Optimized) ruleset. According to this: https://docs.cambridgesemantics.com/anzograph/v2.2/userdoc/inferences.htm
If something is of type owl:Class it should be inferred it is an rdfs:subClassof owl:Thing.
If I run the query
PREFIX owl: <http://www.w3.org/2002/07/owl#>
select ?s ?p where {
?s ?p owl:Thing .
}
I only get results for when I have explicitly stated that a class is an rdfs:subClassOf owl:Thing
Can someone explain what I am missing and why this does not seem to work as expected?

I looked at the .pie rule files located in $GDB_HOME/configs/rules and found that the graphdb rulesets do not match the link in the question. The least complex ruleset (according to https://graphdb.ontotext.com/documentation/free/rules-optimisations.html#hints-on-optimizing-graphdb-s-rulesets) that provides the functionality needed is owl2-ql-optimized.

Related

Conditional insertion of a new concept class to an ontology model

I am in a learning phase of SPARQL and ontology building. I have a model and I would like to add a new concept class to multiple concepts in a model using regex/filter.
I have following concepts:
A647674
A878678
RR36868
DD36868
The expected output is :
A647674
A878678
RR36868 rdf:type http://schemas.aaaaaaa.com/ontologies/drug#SmallMolecule
DD36868 rdf:type http://schemas.aaaaaaa.com/ontologies/drug#SmallMolecule
I am using below SPARQL query to do this.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
INSERT {
?s rdf:type 'http://schemas.aaaaaaa.com/ontologies/drug#SmallMolecule' .
}
WHERE
{
{?s ?p ?o .
filter regex(str(?s), "http://ontology.aaaaaaa.com/drugs/aaaaaaa#RR-").
}
union
{?s ?p ?o .
filter regex(str(?s), "http://ontology.aaaaaaa.com/drugs/aaaaaaa#DD").
}
};
#LIMIT 100
I am getting below error using above query.
OmServerGenericException[message="http://schemas.aaaaaaa.com/ontologies/drug#SmallMolecule",responseCode=500]
Caused by: org.apache.jena.rdf.model.ResourceRequiredException: "http://schemas.aaaaaaa.com/ontologies/drug#SmallMolecule"
Any help is highly appreciated
You are providing a string value:
?s rdf:type 'http://schemas.aaaaaaa.com/ontologies/drug#SmallMolecule' .
You need to provide a URI value:
?s rdf:type <http://schemas.aaaaaaa.com/ontologies/drug#SmallMolecule> .
Or you could define a prefix (PREFIX drug: <http://schemas.aaaaaaa.com/ontologies/drug#>) to use it like this:
?s rdf:type drug:SmallMolecule .
(The suggestions given in my answer to your previous question apply here, too: you could use STRSTARTS instead of REGEX, and one FILTER with || instead of UNION.)

Returning full subgraph from node in SPARQL with GraphDB

I am trying to get a full subgraph, with all relations intact from the following ontology: http://purl.obolibrary.org/obo/NCBITaxon_9443
The NCBI ontology is very large and we only want the subset of all the primates for example. I currently use the following query:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?s2 ?s3 ?s4 ?s5 FROM <http://purl.obolibrary.org/obo/merged/NCBITAXON> WHERE {
SERVICE <http://sparql.hegroup.org/sparql>
{
?s rdf:type ?o .
?s2 rdfs:subClassOf ?s.
?s3 rdfs:subClassOf ?s2.
?s4 rdfs:subClassOf ?s3.
?s5 rdfs:subClassOf ?s4.
FILTER ( ?s = <http://purl.obolibrary.org/obo/NCBITaxon_9443> )
}
}
I don't think this is extensive enough and it also does not return a "nice" format. I would preferably get a subgraph in OWL or RDF(S) format back.
Can anyone help me with this? Thanks!

Infer data with SPARQL in Protege

I'm trying to get my head around inferring RDF data. Say that I have these triples (RDF Turtle), which I created using Protege:
#prefix owl: <http://www.w3.org/2002/07/owl#> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
:hasSpouse rdf:type owl:ObjectProperty ,
owl:SymmetricProperty ;
rdfs:domain :People ;
rdfs:range :People .
:People rdf:type owl:Class .
:Jane_Doe rdf:type owl:NamedIndividual ,
:People .
:John_Doe rdf:type owl:NamedIndividual ,
:People ;
:hasSpouse :Jane_Doe .
The reasoner in Protege will kindly highlight the expected inference, that is :Jane_Doe :hasSpouse :John_Doe.
How can I see that inference with SPARQL? If I run this query in Protege (SPARQL tab):
SELECT ?subject
WHERE {?subject hasSpouse ?object .}
It shows the asserted triple, not the inferred one. I understand how to do it manually, e.g. :
CONSTRUCT {?object ?prop ?subject }
WHERE { ?prop rdf:type owl:SymmetricProperty .
?subject ?prop ?object .}
I'd see now the inferred data I'm expecting but 1) that would be losing the point imho (i.e; reinventing the wheel) 2) I cannot have 2 queries in this tab (construct, then select). There's got to be a way to do this automatically, just like the reasoner did.
I read in Stack Overflow a post saying to use 'Snap SPARQL' plugin in Protege. I tried but simple queries don't work (like the first one above). It's like it's a different language. How does it work?
So, how can I get the benefit of these owl properties with SPARQL? How can I have an OWL-aware SPARQL in Protege? Am I taking this the wrong way? What's the right way?
thanks for your help.
Nicolas
You need to make your inferences a part of your knowledge.
To do so, go to the SWRL Tab and click successively on the button
at the bottom of that Tab, start from the left to the right.

Removing unwanted superclass answers in SPARQL

I have an OWL file that includes a taxonomic hierarchy that I want to write a query where the answer includes each individual and its immediate taxonomic parent. Here's an example (the full query is rather messier).
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
#prefix rdf: <http:://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#prefix : <urn:ex:> .
:fido rdf:type :Dog .
:Dog rdfs:subClassOf :Mammal .
:Mammal rdfs:subClassOf :Vertebrate .
:Vertebrate rdfs:subClassOf :Animal .
:fido :hasToy :bone
:kitty rdf:type :Cat .
:Cat rdfs:subClassOf :Mammal .
:kitty :hasToy :catnipMouse .
And this query does what I want.
prefix rdf: <http:://www.w3.org/1999/02/22-rdf-syntax-ns#> .
prefix : <urn:ex:> .
SELECT ?individual ?type
WHERE {
?individual :hasToy :bone .
?individual rdf:type ?type .
}
The problem is that I'd rather use a reasoned-over version of the OWL file, which unsurprisingly includes additional statements:
:fido rdf:type :Mammal .
:fido rdf:type :Vertebrate .
:fido rdf:type :Animal .
:kitty rdf:type :Mammal .
:kitty rdf:type :Vertebrate .
:kitty rdf:type :Animal .
And now the query results in additional answers about Fido being a Mammal, etc. I could just give up on using the reasoned version of the file, or, since the SPARQL queries are called from java, I could do a bunch of additional queries to find the least inclusive type that appears. My question is whether there is a reasonable pure SPARQL solution to only returning the Dog solution.
A generic solution is that you make sure you ask for the direct type only. A class C is the direct type of an instance X if:
X is of type C
there is no C' such that:
X is of type C'
C' is a subclass of C
C' is not equal to C
(that last condition is necessary, by the way, because in RDF/OWL, the subclass-relation is reflexive: every class is a subclass of itself)
In SPARQL, this becomes something like this:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX : <urn:ex:> .
SELECT ?individual ?type
WHERE {
?individual :hasToy :bone .
?individual a ?type .
FILTER NOT EXISTS { ?individual a ?other .
?other rdfs:subClassOf ?type .
FILTER(?other != ?type)
}
}
Depending on which API/triplestore/library you use to execute these queries, there may also be other, tool-specific solutions. For example, the Sesame API (disclosure: I am on the Sesame dev team) has the option to disable reasoning for the purpose of a single query:
TupleQuery query = conn.prepareTupleQuery(SPARQL, "SELECT ...");
query.setIncludeInferred(false);
TupleQueryResult result = query.evaluate();
Sesame also offers an optional additional inferencer (called the 'direct type inferencer') which introduces additional 'virtual' properties you can query, such as sesame:directType, sesame:directSubClassOf, etc. Other tools will undoubtedly have similar options.

add RDFS inference rules support in endpoint SPARQL

I have create an endpoint SPAQL on OpenLink Virtuoso.
All work well, but i have to access on the data in a Container, in particular a rdf:Seq.
I have a Seq like this:
<myrdf:has_stoptimes>
<rdf:Seq rdf:about="http://test.com/343">
<rdf:li>
<myrdf:StopTime rdf:about="http://test.com/StopTime/434">
...
</ns0:StopTime>
</rdf:li>
<rdf:li>
<myrdf:StopTime rdf:about="http://test.com/StopTime/435">
...
</ns0:StopTime>
</rdf:li>
</rdf:Seq>
Now i see that to access data in a container i can use rdfs:member or FILTER (strstarts(str(?prop), str(rdf:_)) how is explained here
But for my project i have to adopt the first solution because i'm working with Silk and i will use the code syntax like ?a/myrdf:has_stoptimes/rdfs:member without use of "complex" filter.
I have tried to follow this guide but querying the endpoint nothing work how i hoped.
So my question is: how can i query ?a/myrdf:has_stoptimes/rdfs:member on a Virtuoso endpoint SPARQL?Which inference rule i have to add in endpoint SPARQL?
Thank you in advance
UPDATE
I have created the following inference rules in Virtuoso:
ttlp (' #prefix rdfs: .
#prefix rdf: .
rdfs:Container rdf:type rdfs:Class ; rdfs:subClassOf rdfs:Resource .
rdfs:ContainerMembershipProperty a rdfs:Class ; rdfs:subClassOf rdf:Property .
rdf:Seq rdf:type rdfs:Class ; rdfs:subClassOf rdfs:Container .
rdfs:member rdf:type rdf:Property ; rdfs:domain rdfs:Resource ; rdfs:range rdfs:Resource .
', '', 'http://localhost:8890/schema/test') ;
Nothing work querying the SPARQL endpoint like:
define input:inference "http://localhost:8890/schema/property_rules1"
SELECT *
FROM
WHERE {?sep a rdf:Seq.
?seq rdfs:member ?p}
After i tried adding the follow line to the ttl file: rdf:_1 rdfs:subPropertyOf rdfs:member . In this way it work but obviously the results are only for the first element of the container. So is very unconvenient add a line for all of rdf:_n, and i think this is only a temporary solution, it is not correct.
I have tried to add an RDF dump on SILK 2.6.1, and on the section SPARQL of the data source if i run the query:
SELECT *
FROM
WHERE {?sep a rdf:Seq.
?seq rdfs:member ?p}
I obtain the correct result, without specify any inference rules. So i think that in this functionality of SILK there is something that i’m missing in my endpoint SPARQL or am i saying nonsense things?
You can't use variables in property paths, so you can't actually do
?x ?a/has_stoptimes/rdfs:member ?y
Instead, you have to use another variable or blank node in between:
?x ?a ?z . ?z has_stoptimes/rdfs:member ?y
?x ?a [ has_stoptimes/rdfs:member ?y ] .