javasemantic-webjenareasoninginference-engine

Basic RDFS inferencing with the Jena API


I'm currently following the Jena API inferencing tutorial:

https://jena.apache.org/documentation/inference/

and as an exercise to test my understanding, I'd like to rewrite the first example, which demonstrates a trivial RDFS reasoning from a programmatically built model:

import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.vocabulary.*;

public class Test1 {
    static public void main(String...argv) {
        String NS = "foo:";
        Model m = ModelFactory.createDefaultModel();
        Property p = m.createProperty(NS, "p");
        Property q = m.createProperty(NS, "q");
        m.add(p, RDFS.subPropertyOf, q);
        m.createResource(NS + "x").addProperty(p, "bar");
        InfModel im = ModelFactory.createRDFSModel(m);
        Resource x = im.getResource(NS + "x");
        // verify that property q of x is "bar" (which follows 
        // from x having property p, and p being a subproperty of q)
        System.out.println("Statement: " + x.getProperty(q));
    }
}

to something which does the same, but with the model read from this Turtle file instead (which is my own translation of the above, and thus might be buggy):

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
@prefix foo: <http://example.org/foo#>.

foo:p a rdf:Property.
foo:q a rdf:Property.
foo:p rdfs:subPropertyOf foo:q.
foo:x foo:p "bar".

with this code:

public class Test2 {
    static public void main(String...argv) {
        String NS = "foo:";
        Model m = ModelFactory.createDefaultModel();
        m.read("foo.ttl");
        InfModel im = ModelFactory.createRDFSModel(m);
        Property q = im.getProperty(NS + "q");
        Resource x = im.getResource(NS + "x");
        System.out.println("Statement: " + x.getProperty(q));
    }
}

which doesn't seem to be the right approach (I suspect in particular that my extraction of the q property is somehow not right). What am I doing wrong?


Solution

  • String NS = "foo:";
    m.createResource(NS + "x")
    

    creates a URI but the Turtle version has foo:x = http://example.org/foo#x

    See the differences by printing the model im.write(System.out, "TTL");

    Change NS = "foo:" to NS = "http://example.org/foo#"