You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Christian Jauvin <cj...@gmail.com> on 2013/04/04 19:23:07 UTC

Basic RDFS inferencing with the Jena API

Hi,

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 qproperty is somehow not right). What am I
doing wrong?

Thanks,

Christian

Re: Basic RDFS inferencing with the Jena API

Posted by Christian Jauvin <cj...@gmail.com>.
It works, thanks for your help Dave!

On 5 April 2013 04:33, Dave Reynolds <da...@gmail.com> wrote:
> Hi Christian,
>
>
> On 04/04/13 18:23, Christian Jauvin wrote:
>>
>> Hi,
>>
>> 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:";
>
>
> You want to make that:
>           String NS = "http://example.org/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 qproperty is somehow not right). What am I
>> doing wrong?
>
>
> The calls like getProperty need a full URI, not a prefixed name. The easy
> fix is what I suggested above.
>
> Alternatively you can use the prefixes read in from your file to reconstruct
> the URIs. E.g.
>
>     Property q = im.getProperty( m.expandPrefix("foo:q") )
>
> Dave
>
>

Re: Basic RDFS inferencing with the Jena API

Posted by Dave Reynolds <da...@gmail.com>.
Hi Christian,

On 04/04/13 18:23, Christian Jauvin wrote:
> Hi,
>
> 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:";

You want to make that:
           String NS = "http://example.org/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 qproperty is somehow not right). What am I
> doing wrong?

The calls like getProperty need a full URI, not a prefixed name. The 
easy fix is what I suggested above.

Alternatively you can use the prefixes read in from your file to 
reconstruct the URIs. E.g.

     Property q = im.getProperty( m.expandPrefix("foo:q") )

Dave