5
1

I want to serialise resources individually with Jena. I've retrieved a list of resources of the type ont:Artist, and I would like to generate individual serialiations for each one, but which also contain the full descriptions of the resource's objects. I'm currently copying each resource into a new blank model and serialising it. For example, I am currently getting:

<ont:Artist rdf:about="http://example.com/artist/77">
    <ont:hasAlbum rdf:resource="http://example.com/album/75"/>
</ont:Artist>

With the album only represented by its URI. I would like all of the details about album to be included in this serialisation, like so:

<ont:Artist rdf:about="http://example.com/artist/77">
     <iweb:hasAlbum>
         <iweb:Album rdf:about="http://example.com/artist/77">
             <iweb:hasTitle>This is a title</iweb:hasTitle>
         </iweb:Album>
    </iweb:hasAlbum>
</ont:Artist>

How can I do this for an individual resource?

asked 20 May '11, 12:38

blork's gravatar image

blork
1135
accept rate: 0%


I'm no expert on the Jena API, but I assume you can simply fire a SPARQL CONSTRUCT query and treat/process the result as a new Model. The following SPARQL query will give you back all artists, their album, and all the properties of each album:

CONSTRUCT {
   ?artist a ont:Artist ;
           iweb:hasAlbum ?album . 
   ?album ?P ?Y .
}
WHERE {
   ?artist a ont:Artist ;
           iweb:hasAlbum ?album . 
   ?album ?P ?Y .
}

If you want to do this for an invidual artist, replace ?artist with the URI of the individual (or add a filter condition).

permanent link

answered 22 May '11, 22:15

Jeen%20Broekstra's gravatar image

Jeen Broekstra ♦
11.6k412
accept rate: 37%

edited 22 May '11, 22:16

Resource individual = yourModel.createResource(individualURI);
Model individualModel = ModelFactory.createDefaultModel();
traverseModel(yourModel, individualModel, individual);
individualModel.write(outputStream, "RDF/XML-ABBREV");

public void traverseModel (Model completeModel, Model newModel, Resource node) {
    StmtIterator subNodes = completeModel.listStatements(node, null, null);
    while (subNodes.hasNext()) {
        Statement subStmt = subNodes.next();
        newModel.add(subStmt);
        if (subStmt.getObject().isResource() && ! newModel.listStatements(subStmt.getObject(), null, null).hasNext()) traverseModel(completeModel, newModel, subStmt.getObject());
    }
}
permanent link

answered 23 May '11, 03:51

William%20Greenly's gravatar image

William Greenly
5.1k412
accept rate: 13%

edited 23 May '11, 14:19

1

Nice piece of code! I take it that the last code-line should call 'traverseModel' instead of 'traverse', right?

(23 May '11, 09:28) Ryan Kohl Ryan%20Kohl's gravatar image

Yes, thanks Ryan. Will make appropriate edit.

(23 May '11, 09:34) William Greenly William%20Greenly's gravatar image
2

You might want to add something to guard against infinite loops where you have cycles in the RDF graph.

(23 May '11, 10:12) mhgrove mhgrove's gravatar image

Yeah - that would be a good idea (cringe). Will make the appropriate edit.

(23 May '11, 14:03) William Greenly William%20Greenly's gravatar image
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×615
×177
×15

question asked: 20 May '11, 12:38

question was seen: 1,337 times

last updated: 23 May '11, 14:19