1

I am running some SPARQL queries using the class ForwardChainingRDFSInferencer, which basic constructs an inferencer. For my examples I use the schema.org ontology.

My code looks like the following example

MemoryStore store = new MemoryStore();
ForwardChainingRDFSInferencer inferencer = new ForwardChainingRDFSInferencer(store); //the inference class

Repository repo = new SailRepository(inferencer);
repo.initialize();

URL data = new URL("file:/home/user/Documents/schemaorg-current-https.nt");
            
RDFFormat fileRDFFormat = RDFFormat.NTRIPLES;
            
RepositoryConnection con = repo.getConnection();

con.add(data, null, fileRDFFormat);
    
System.out.println("Repository loaded");

String queryString =
    " PREFIX schema: <https://schema.org/>" +
    " SELECT DISTINCT ?subclassName_one" +
    " WHERE { " +
    "     ?type rdf:type rdfs:Class ." +
    "     ?type rdfs:subClassOf/rdfs:subClassOf? schema:Thing ." +
    "     ?type rdfs:label ?subclassName_one ." +
    " }";

TupleQuery tupleQuery = con.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
TupleQueryResult result = tupleQuery.evaluate();

while (result.hasNext()) {
    BindingSet bindingSet = result.next();
    System.out.println(bindingSet.toString());
    stdout.println(bindingSet.toString());
}
repo.close()

What I want is to print two subclass levels down the class Thing. So if for example we have,

Thing > Action (sub-class 1) > ConsumeAction (sub-class 2) > DrinkAction

What I want is to return the class CosumeAction which is two levels (subclasses) down from class Thing, while using the inference java class.

The current query, as given in the code sample above, returns all the classes and subclasses from every class of the schema.org ontology. Thus, there is something that I do wrong while using the inference class.

AndyS
  • 16,345
  • 17
  • 21
NikSp
  • 1,262
  • 2
  • 19
  • 42
  • If you use inference, indeed the query will return all subclasses of the level 1 and level 2 classes as well. this is quite obvious as this can be inferred exactly by rdfs:subClassOf transitivity. Why do you enable inference at all? – UninformedUser Nov 05 '22 at 07:11
  • 1
    I want to reproduce the same results with and without inference. Without the inference it's an easy solution by using property path syntax. But I wanted also to return the same result with the inference class. Do you know how can I solve this? – NikSp Nov 05 '22 at 07:37

1 Answers1

1

You could remove the classes you are not interested in with FILTER NOT EXISTS:

FILTER NOT EXISTS { 
  ?type rdfs:subClassOf+/rdfs:subClassOf/rdfs:subClassOf schema:Thing . 
}

Examples

For DrinkAction (level 3):

FILTER NOT EXISTS { 
  ?type              
    rdfs:subClassOf+ / # schema:ConsumeAction
    rdfs:subClassOf /  # schema:Action
    rdfs:subClassOf      schema:Thing . 
}

For WearAction (level 4):

FILTER NOT EXISTS { 
  ?type              
    rdfs:subClassOf+ / # schema:UseAction / schema:ConsumeAction
    rdfs:subClassOf /  # schema:Action
    rdfs:subClassOf      schema:Thing . 
}
  • Hey Stefan, Thanks for the answer. I have added the ```FILTER NOT EXIST{}``` but the output/query result is empty. This is happening when I use the inference class as described in my question. You can check the modified query here (https://drive.google.com/file/d/1evGBCtmozWdG6TNe5fkGDl4ZHsnTRZdr/view?usp=sharing) – NikSp Nov 07 '22 at 12:02
  • @NikSp: In line 6 (`?type rdfs:subClassOf/ schema:Thing .`), there’s an issue with the property path. I suppose you want `?type rdfs:subClassOf+ schema:Thing .` there. – Does this give results? – Stefan - brox IT-Solutions Nov 07 '22 at 16:11
  • 1
    that was a typo. Variations of ```rdfs:subClassOf+``` , ```rdfs:subClassOf*``` or ```^rdfs:subClassOf``` give empty results. – NikSp Nov 07 '22 at 16:28
  • @NikSp: I see. Could you provide an example that contains all the necessary Java class import statements? I’d like to try running it (I don’t use a Java IDE). – Stefan - brox IT-Solutions Nov 08 '22 at 08:18
  • 1
    Stefan you can find the java code here (https://drive.google.com/file/d/1lSp2eZlJS1nwf4rH_qjpOzIeiltzM1T0/view?usp=sharing). I execute the code from Eclipse IDE for Java Developers. – NikSp Nov 08 '22 at 10:56