You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Luciane Monteiro <lu...@gmail.com> on 2013/11/08 18:23:53 UTC

Dereferencing URIs using Jena.

Hi!

I need to dereference *URIs*, like:

http://dbpedia.org/resource/Google

 and

 http://dbpedia.org/resource/American_Airlines

 returned from *Spotlight*, to get their *RDFS:comments* property.


Is it something like this? Look:

Model model = ModelFactory.createDefaultModel();
model.read( "http://dbpedia.org/resource/Google" );

But how do I get the property?

Thank you!

Re: Dereferencing URIs using Jena.

Posted by Andy Seaborne <an...@apache.org>.
You haven't actually put any triples into model.

If you want to read from the web, then do:

RDFDataMgr.read(model, uri) ;

model.createResource(uri); just creates a local object for the URI - it 
does not fetch data.

	Andy

On 10/11/13 14:28, Luciane Monteiro wrote:
> *Yes, I did this: ( Here response is a List<DBpediaResource> )*
>
>   Model model = ModelFactory.createDefaultModel();
>       List<String> uriList = new ArrayList<String>();
>
>
>          for( DBpediaResource dbResource : response ) {
>
>              String uri = dbResource.getFullUri();
>              uriList.add(uri);
>          }
>
>          for( String uri : uriList ) {
>
>              Resource resource = model.createResource(uri);
>              StmtIterator stmts = resource.listProperties(RDFS.comment);
>
>              while( stmts.hasNext() ) {
>                  Statement stmt = stmts.next();
>                  RDFNode comment = stmt.getObject();
>
>                 * System.out.println("Comment: " + comment.toString());*
>              }
>          }
>
> *But when I try to print the Comment it returns me nothing. Is there
> anything wrong?*
>
>
> 2013/11/9 Joshua TAYLOR <jo...@gmail.com>
>
>> On Fri, Nov 8, 2013 at 12:23 PM, Luciane Monteiro <lu...@gmail.com>
>> wrote:
>>> Hi!
>>>
>>> I need to dereference *URIs*, like:
>>>
>>> http://dbpedia.org/resource/Google
>>>
>>>   and
>>>
>>>   http://dbpedia.org/resource/American_Airlines
>>>
>>>   returned from *Spotlight*, to get their *RDFS:comments* property.
>>>
>>>
>>> Is it something like this? Look:
>>>
>>> Model model = ModelFactory.createDefaultModel();
>>> model.read( "http://dbpedia.org/resource/Google" );
>>>
>>> But how do I get the property?
>>
>> When you asked this on the DBpedia mailing list, I did include code in
>> my response, in addition to pointing you to the Jena mailing list.  As
>> Andy Seaborne mentioned, the Model API (particularly the Model and
>> Resource interfaces) will help you out here:
>>
>> Resource google = model.createResource( "
>> http://dbpedia.org/resource/Google" );
>> StmtIterator stmts = google.listProperties( RDFS.comment );
>> while ( stmts.hasNext() ) {
>>    Statement stmt = stmts.next();
>>    RDFNode comment = stmt.getObject();
>>    /* do something with the comment */
>> }
>>
>>
>>
>> --
>> Joshua Taylor, http://www.cs.rpi.edu/~tayloj/
>>
>


Re: Dereferencing URIs using Jena.

Posted by Luciane Monteiro <lu...@gmail.com>.
Thank you! Huge help!


2013/11/11 Andy Seaborne <an...@apache.org>

> On 11/11/13 12:24, Luciane Monteiro wrote:
>
>> I tested this code, it works!   But I have 2 questions:
>>
>> 1- How can I get the comment in a specific language ?
>>
>
> See the Jena API operations on Literals.
>
>
>
>> 2- You answered me 2 ways of doing that, is there any specific reason to
>> choose one or the other?
>>
>
> No - they amount to the same thing, just two routes to much the same code.
>
>
>> Thank you very much!
>>
>
>         Andy
>
>
>
>>
>> 2013/11/10 Joshua TAYLOR <jo...@gmail.com>
>>
>>
>>> On Sun, Nov 10, 2013 at 9:28 AM, Luciane Monteiro <luciane.lpm@gmail.com
>>> >
>>> wrote:
>>>
>>>> Yes, I did this: ( Here response is a List<DBpediaResource> )
>>>>
>>>>   Model model = ModelFactory.createDefaultModel();
>>>>       List<String> uriList = new ArrayList<String>();
>>>>
>>>>
>>>>          for( DBpediaResource dbResource : response ) {
>>>>
>>>>              String uri = dbResource.getFullUri();
>>>>              uriList.add(uri);
>>>>          }
>>>>
>>>>          for( String uri : uriList ) {
>>>>
>>>>              Resource resource = model.createResource(uri);
>>>>              StmtIterator stmts = resource.listProperties(RDFS.
>>>> comment);
>>>>
>>>>              while( stmts.hasNext() ) {
>>>>
>>>>                  Statement stmt = stmts.next();
>>>>                  RDFNode comment = stmt.getObject();
>>>>
>>>>                  System.out.println("Comment: " + comment.toString());
>>>>              }
>>>>          }
>>>>
>>>> But when I try to print the Comment it returns me nothing. Is there
>>>>
>>> anything
>>>
>>>> wrong?
>>>>
>>>
>>> When you say it "returns you nothing", do you mean that you don't get any
>>> output at all, or that you're getting "Comment: null" (i.e., that
>>> comment.toString() is returning null for some values)?  At any rate, it
>>> looks like you're doing a lot more work than you need to.  When you read
>>> the remote content (e.g., with model.read(...) or RDFDataMgr.read(model,
>>> uri) as Andy mentioned) you get a model back.  Then you can either ask
>>> the
>>> model for statements directly, or you can get a resource for
>>> http://dbpedia.org/resource/Google directly and then ask for properties
>>> from it.  E.g.:
>>>
>>>
>>> import com.hp.hpl.jena.rdf.model.Model;
>>> import com.hp.hpl.jena.rdf.model.ModelFactory;
>>> import com.hp.hpl.jena.rdf.model.RDFNode;
>>> import com.hp.hpl.jena.rdf.model.Resource;
>>> import com.hp.hpl.jena.rdf.model.ResourceFactory;
>>> import com.hp.hpl.jena.rdf.model.StmtIterator;
>>> import com.hp.hpl.jena.vocabulary.RDFS;
>>>
>>> public class DBpediaExample {
>>>   final static String DBPEDIA_GOOGLE = "http://dbpedia.org/resource/
>>> Google
>>> ";
>>>   public static void main(String[] args) {
>>> // Create a model and read the DBpedia content into it.
>>> final Model model = ModelFactory.createDefaultModel().read(
>>> DBPEDIA_GOOGLE
>>> );
>>>   // Create a resource not associate with any model, and ask the model
>>> // for statements with it as a subject and rdfs:comment as the property.
>>>   // null (for the object) is a wildcard.  You could also use a resource
>>> // that is associated with a model (e.g., model.createResource(
>>> dbpediaGoogle )).
>>>   final Resource google1 = ResourceFactory.createResource(
>>> DBPEDIA_GOOGLE
>>> );
>>> StmtIterator stmts1 = model.listStatements( google1, RDFS.comment,
>>> (RDFNode) null );
>>>   showObjects( stmts1 );
>>>   // Create a resource based on the model. This one is based on the
>>> model,
>>>   // so when you ask for its properties, you're asking about statements
>>> in
>>> // the same model.
>>> final Resource google2 = model.getResource( DBPEDIA_GOOGLE );
>>>   showObjects( google2.listProperties( RDFS.comment ));
>>> }
>>>   public static void showObjects( final StmtIterator it ) {
>>> while ( it.hasNext() ) {
>>> System.out.println( it.next().getObject() );
>>>   }
>>> }
>>> }
>>>
>>> --
>>> Joshua Taylor, http://www.cs.rpi.edu/~tayloj/
>>>
>>>
>>
>

Re: Dereferencing URIs using Jena.

Posted by Andy Seaborne <an...@apache.org>.
On 11/11/13 12:24, Luciane Monteiro wrote:
> I tested this code, it works!   But I have 2 questions:
>
> 1- How can I get the comment in a specific language ?

See the Jena API operations on Literals.

>
> 2- You answered me 2 ways of doing that, is there any specific reason to
> choose one or the other?

No - they amount to the same thing, just two routes to much the same code.

>
> Thank you very much!

	Andy

>
>
> 2013/11/10 Joshua TAYLOR <jo...@gmail.com>
>
>>
>> On Sun, Nov 10, 2013 at 9:28 AM, Luciane Monteiro <lu...@gmail.com>
>> wrote:
>>> Yes, I did this: ( Here response is a List<DBpediaResource> )
>>>
>>>   Model model = ModelFactory.createDefaultModel();
>>>       List<String> uriList = new ArrayList<String>();
>>>
>>>
>>>          for( DBpediaResource dbResource : response ) {
>>>
>>>              String uri = dbResource.getFullUri();
>>>              uriList.add(uri);
>>>          }
>>>
>>>          for( String uri : uriList ) {
>>>
>>>              Resource resource = model.createResource(uri);
>>>              StmtIterator stmts = resource.listProperties(RDFS.comment);
>>>
>>>              while( stmts.hasNext() ) {
>>>
>>>                  Statement stmt = stmts.next();
>>>                  RDFNode comment = stmt.getObject();
>>>
>>>                  System.out.println("Comment: " + comment.toString());
>>>              }
>>>          }
>>>
>>> But when I try to print the Comment it returns me nothing. Is there
>> anything
>>> wrong?
>>
>> When you say it "returns you nothing", do you mean that you don't get any
>> output at all, or that you're getting "Comment: null" (i.e., that
>> comment.toString() is returning null for some values)?  At any rate, it
>> looks like you're doing a lot more work than you need to.  When you read
>> the remote content (e.g., with model.read(...) or RDFDataMgr.read(model,
>> uri) as Andy mentioned) you get a model back.  Then you can either ask the
>> model for statements directly, or you can get a resource for
>> http://dbpedia.org/resource/Google directly and then ask for properties
>> from it.  E.g.:
>>
>>
>> import com.hp.hpl.jena.rdf.model.Model;
>> import com.hp.hpl.jena.rdf.model.ModelFactory;
>> import com.hp.hpl.jena.rdf.model.RDFNode;
>> import com.hp.hpl.jena.rdf.model.Resource;
>> import com.hp.hpl.jena.rdf.model.ResourceFactory;
>> import com.hp.hpl.jena.rdf.model.StmtIterator;
>> import com.hp.hpl.jena.vocabulary.RDFS;
>>
>> public class DBpediaExample {
>>   final static String DBPEDIA_GOOGLE = "http://dbpedia.org/resource/Google
>> ";
>>   public static void main(String[] args) {
>> // Create a model and read the DBpedia content into it.
>> final Model model = ModelFactory.createDefaultModel().read( DBPEDIA_GOOGLE
>> );
>>   // Create a resource not associate with any model, and ask the model
>> // for statements with it as a subject and rdfs:comment as the property.
>>   // null (for the object) is a wildcard.  You could also use a resource
>> // that is associated with a model (e.g., model.createResource(
>> dbpediaGoogle )).
>>   final Resource google1 = ResourceFactory.createResource( DBPEDIA_GOOGLE
>> );
>> StmtIterator stmts1 = model.listStatements( google1, RDFS.comment,
>> (RDFNode) null );
>>   showObjects( stmts1 );
>>   // Create a resource based on the model. This one is based on the model,
>>   // so when you ask for its properties, you're asking about statements in
>> // the same model.
>> final Resource google2 = model.getResource( DBPEDIA_GOOGLE );
>>   showObjects( google2.listProperties( RDFS.comment ));
>> }
>>   public static void showObjects( final StmtIterator it ) {
>> while ( it.hasNext() ) {
>> System.out.println( it.next().getObject() );
>>   }
>> }
>> }
>>
>> --
>> Joshua Taylor, http://www.cs.rpi.edu/~tayloj/
>>
>


Re: Dereferencing URIs using Jena.

Posted by Luciane Monteiro <lu...@gmail.com>.
I tested this code, it works!   But I have 2 questions:

1- How can I get the comment in a specific language ?

2- You answered me 2 ways of doing that, is there any specific reason to
choose one or the other?

Thank you very much!


2013/11/10 Joshua TAYLOR <jo...@gmail.com>

>
> On Sun, Nov 10, 2013 at 9:28 AM, Luciane Monteiro <lu...@gmail.com>
> wrote:
> > Yes, I did this: ( Here response is a List<DBpediaResource> )
> >
> >  Model model = ModelFactory.createDefaultModel();
> >      List<String> uriList = new ArrayList<String>();
> >
> >
> >         for( DBpediaResource dbResource : response ) {
> >
> >             String uri = dbResource.getFullUri();
> >             uriList.add(uri);
> >         }
> >
> >         for( String uri : uriList ) {
> >
> >             Resource resource = model.createResource(uri);
> >             StmtIterator stmts = resource.listProperties(RDFS.comment);
> >
> >             while( stmts.hasNext() ) {
> >
> >                 Statement stmt = stmts.next();
> >                 RDFNode comment = stmt.getObject();
> >
> >                 System.out.println("Comment: " + comment.toString());
> >             }
> >         }
> >
> > But when I try to print the Comment it returns me nothing. Is there
> anything
> > wrong?
>
> When you say it "returns you nothing", do you mean that you don't get any
> output at all, or that you're getting "Comment: null" (i.e., that
> comment.toString() is returning null for some values)?  At any rate, it
> looks like you're doing a lot more work than you need to.  When you read
> the remote content (e.g., with model.read(...) or RDFDataMgr.read(model,
> uri) as Andy mentioned) you get a model back.  Then you can either ask the
> model for statements directly, or you can get a resource for
> http://dbpedia.org/resource/Google directly and then ask for properties
> from it.  E.g.:
>
>
> import com.hp.hpl.jena.rdf.model.Model;
> import com.hp.hpl.jena.rdf.model.ModelFactory;
> import com.hp.hpl.jena.rdf.model.RDFNode;
> import com.hp.hpl.jena.rdf.model.Resource;
> import com.hp.hpl.jena.rdf.model.ResourceFactory;
> import com.hp.hpl.jena.rdf.model.StmtIterator;
> import com.hp.hpl.jena.vocabulary.RDFS;
>
> public class DBpediaExample {
>  final static String DBPEDIA_GOOGLE = "http://dbpedia.org/resource/Google
> ";
>  public static void main(String[] args) {
> // Create a model and read the DBpedia content into it.
> final Model model = ModelFactory.createDefaultModel().read( DBPEDIA_GOOGLE
> );
>  // Create a resource not associate with any model, and ask the model
> // for statements with it as a subject and rdfs:comment as the property.
>  // null (for the object) is a wildcard.  You could also use a resource
> // that is associated with a model (e.g., model.createResource(
> dbpediaGoogle )).
>  final Resource google1 = ResourceFactory.createResource( DBPEDIA_GOOGLE
> );
> StmtIterator stmts1 = model.listStatements( google1, RDFS.comment,
> (RDFNode) null );
>  showObjects( stmts1 );
>  // Create a resource based on the model. This one is based on the model,
>  // so when you ask for its properties, you're asking about statements in
> // the same model.
> final Resource google2 = model.getResource( DBPEDIA_GOOGLE );
>  showObjects( google2.listProperties( RDFS.comment ));
> }
>  public static void showObjects( final StmtIterator it ) {
> while ( it.hasNext() ) {
> System.out.println( it.next().getObject() );
>  }
> }
> }
>
> --
> Joshua Taylor, http://www.cs.rpi.edu/~tayloj/
>

Re: Dereferencing URIs using Jena.

Posted by Joshua TAYLOR <jo...@gmail.com>.
On Sun, Nov 10, 2013 at 9:28 AM, Luciane Monteiro <lu...@gmail.com>
wrote:
> Yes, I did this: ( Here response is a List<DBpediaResource> )
>
>  Model model = ModelFactory.createDefaultModel();
>      List<String> uriList = new ArrayList<String>();
>
>
>         for( DBpediaResource dbResource : response ) {
>
>             String uri = dbResource.getFullUri();
>             uriList.add(uri);
>         }
>
>         for( String uri : uriList ) {
>
>             Resource resource = model.createResource(uri);
>             StmtIterator stmts = resource.listProperties(RDFS.comment);
>
>             while( stmts.hasNext() ) {
>
>                 Statement stmt = stmts.next();
>                 RDFNode comment = stmt.getObject();
>
>                 System.out.println("Comment: " + comment.toString());
>             }
>         }
>
> But when I try to print the Comment it returns me nothing. Is there
anything
> wrong?

When you say it "returns you nothing", do you mean that you don't get any
output at all, or that you're getting "Comment: null" (i.e., that
comment.toString() is returning null for some values)?  At any rate, it
looks like you're doing a lot more work than you need to.  When you read
the remote content (e.g., with model.read(...) or RDFDataMgr.read(model,
uri) as Andy mentioned) you get a model back.  Then you can either ask the
model for statements directly, or you can get a resource for
http://dbpedia.org/resource/Google directly and then ask for properties
from it.  E.g.:


import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.vocabulary.RDFS;

public class DBpediaExample {
 final static String DBPEDIA_GOOGLE = "http://dbpedia.org/resource/Google";
 public static void main(String[] args) {
// Create a model and read the DBpedia content into it.
final Model model = ModelFactory.createDefaultModel().read( DBPEDIA_GOOGLE
);
 // Create a resource not associate with any model, and ask the model
// for statements with it as a subject and rdfs:comment as the property.
 // null (for the object) is a wildcard.  You could also use a resource
// that is associated with a model (e.g., model.createResource(
dbpediaGoogle )).
 final Resource google1 = ResourceFactory.createResource( DBPEDIA_GOOGLE );
StmtIterator stmts1 = model.listStatements( google1, RDFS.comment,
(RDFNode) null );
 showObjects( stmts1 );
 // Create a resource based on the model. This one is based on the model,
 // so when you ask for its properties, you're asking about statements in
// the same model.
final Resource google2 = model.getResource( DBPEDIA_GOOGLE );
 showObjects( google2.listProperties( RDFS.comment ));
}
 public static void showObjects( final StmtIterator it ) {
while ( it.hasNext() ) {
System.out.println( it.next().getObject() );
 }
}
}

-- 
Joshua Taylor, http://www.cs.rpi.edu/~tayloj/

Re: Dereferencing URIs using Jena.

Posted by Luciane Monteiro <lu...@gmail.com>.
*Yes, I did this: ( Here response is a List<DBpediaResource> )*

 Model model = ModelFactory.createDefaultModel();
     List<String> uriList = new ArrayList<String>();


        for( DBpediaResource dbResource : response ) {

            String uri = dbResource.getFullUri();
            uriList.add(uri);
        }

        for( String uri : uriList ) {

            Resource resource = model.createResource(uri);
            StmtIterator stmts = resource.listProperties(RDFS.comment);

            while( stmts.hasNext() ) {
                Statement stmt = stmts.next();
                RDFNode comment = stmt.getObject();

               * System.out.println("Comment: " + comment.toString());*
            }
        }

*But when I try to print the Comment it returns me nothing. Is there
anything wrong?*


2013/11/9 Joshua TAYLOR <jo...@gmail.com>

> On Fri, Nov 8, 2013 at 12:23 PM, Luciane Monteiro <lu...@gmail.com>
> wrote:
> > Hi!
> >
> > I need to dereference *URIs*, like:
> >
> > http://dbpedia.org/resource/Google
> >
> >  and
> >
> >  http://dbpedia.org/resource/American_Airlines
> >
> >  returned from *Spotlight*, to get their *RDFS:comments* property.
> >
> >
> > Is it something like this? Look:
> >
> > Model model = ModelFactory.createDefaultModel();
> > model.read( "http://dbpedia.org/resource/Google" );
> >
> > But how do I get the property?
>
> When you asked this on the DBpedia mailing list, I did include code in
> my response, in addition to pointing you to the Jena mailing list.  As
> Andy Seaborne mentioned, the Model API (particularly the Model and
> Resource interfaces) will help you out here:
>
> Resource google = model.createResource( "
> http://dbpedia.org/resource/Google" );
> StmtIterator stmts = google.listProperties( RDFS.comment );
> while ( stmts.hasNext() ) {
>   Statement stmt = stmts.next();
>   RDFNode comment = stmt.getObject();
>   /* do something with the comment */
> }
>
>
>
> --
> Joshua Taylor, http://www.cs.rpi.edu/~tayloj/
>

Re: Dereferencing URIs using Jena.

Posted by Joshua TAYLOR <jo...@gmail.com>.
On Fri, Nov 8, 2013 at 12:23 PM, Luciane Monteiro <lu...@gmail.com> wrote:
> Hi!
>
> I need to dereference *URIs*, like:
>
> http://dbpedia.org/resource/Google
>
>  and
>
>  http://dbpedia.org/resource/American_Airlines
>
>  returned from *Spotlight*, to get their *RDFS:comments* property.
>
>
> Is it something like this? Look:
>
> Model model = ModelFactory.createDefaultModel();
> model.read( "http://dbpedia.org/resource/Google" );
>
> But how do I get the property?

When you asked this on the DBpedia mailing list, I did include code in
my response, in addition to pointing you to the Jena mailing list.  As
Andy Seaborne mentioned, the Model API (particularly the Model and
Resource interfaces) will help you out here:

Resource google = model.createResource( "http://dbpedia.org/resource/Google" );
StmtIterator stmts = google.listProperties( RDFS.comment );
while ( stmts.hasNext() ) {
  Statement stmt = stmts.next();
  RDFNode comment = stmt.getObject();
  /* do something with the comment */
}



-- 
Joshua Taylor, http://www.cs.rpi.edu/~tayloj/

Re: Dereferencing URIs using Jena.

Posted by Andy Seaborne <an...@apache.org>.
On 08/11/13 17:23, Luciane Monteiro wrote:
> Hi!
>
> I need to dereference *URIs*, like:
>
> http://dbpedia.org/resource/Google
>
>   and
>
>   http://dbpedia.org/resource/American_Airlines
>
>   returned from *Spotlight*, to get their *RDFS:comments* property.
>
>
> Is it something like this? Look:
>
> Model model = ModelFactory.createDefaultModel();
> model.read( "http://dbpedia.org/resource/Google" );
>
> But how do I get the property?

Use the RDF API of model (see listStatements) or use SPARQL.

	Andy

>
> Thank you!
>