On 12/09/13 08:38, Claude Warren wrote:
something like

protected void writeRDFStatements( Model model, PrintWriter writer )
         {
                 Set<Resource> subjects = new TreeSet<Resource>();
                 subjects.addAll( model.listSubjects().asSet() );
Iterator<Resource> rIter = subjects.iterator()
while (rIter.hasNext()) writeRDFStatements( model, rIter.nex(), writer );
}

Might do the trick, assuming that you want the statements in "natural"
order and that you don't have so many as to overflow memory.

If you want a different order you could implement a comparator and pass it
to the TreeSet constructor.

Claude

Yes - if you care about specific order then you can't reply on listStatement or Graph.find (even, in theory, across different calls to to same operation). Internally, there are hash tables, and any format that involves pretty-printing is reordering the triples. For example, Turtle (pretty form or in blocks) is a by-subject-then-by-predicate sort).

Diving into the output internals ... and dropping to the triple level ...

There are low level utility operations

RDFDataMgr.writeTriples or .writeQuads

that writes an iterator in N-Triples or N-Quads format but a little better is "flat Turtle".

http://jena.apache.org/documentation/io/rdf-output.html#line-printed-formats

which is produced by streaming the triples into the object obtained by

StreamRDFLib.writer

for writing a StreamRDF (which is primarily the output of a parser but it is a sink of triple, quads, prefixes and base declarations).

        Andy



On Thu, Sep 12, 2013 at 8:09 AM, <[email protected]> wrote:

Hi Claude,

It's close as this code at least group the triples
per subject basis and the "model.write(stream, lang)"
prints out in that manner. Still, there's no explicit
ordering in the subjects (other than the iterator's
own implicit one). Looks like I need to write my own.

Thanks

Chan


  The code you are looking for is probably in
com.hp.hpl.jena.xmloutput.**impl.Basic.java


  protected void writeRDFStatements( Model model, PrintWriter writer )
         {
ResIterator rIter = model.listSubjects();
while (rIter.hasNext()) writeRDFStatements( model, rIter.nextResource(),
writer );
}

  protected void writeRDFStatements
         ( Model model, Resource subject, PrintWriter writer )
     {
StmtIterator sIter = model.listStatements( subject, null, (RDFNode) null
);
writeDescriptionHeader( subject, writer );
while (sIter.hasNext()) writePredicate( sIter.nextStatement(), writer );
writeDescriptionTrailer( subject, writer );
     }

However, it just prints the statements out in the order the model returns
them by listSubjects() and listStatements() for each subject returned. So
as Chris said the order is model implementation specific.

Claude


On Thu, Sep 12, 2013 at 7:01 AM, <[email protected]> wrote:

  On 2013-09-11 23:15, Chris_Dollin wrote:

  On Wednesday, September 11, 2013 07:28:33 PM [email protected] wrote:

  Is there a Jena stmt/triple iterator feeding items in order ?

I looked around the Jena API list to find, but no luck. However,
the 'Model.write(outputStream, lang)' method produces triple
listing in hierarchy with indentation - wonder how the method
produces such listing.


Is Model.listStatements() what you're looking for?

[Note: there is no "in order"; the statements come out in some
  unspecified implementation-specific order.]

Chris


What I'm looking for is another iterator (or a Jena utility ?)
which can order the triples in hierarchy - not in random sequence
from 'listStatements()'. I just need to traverse a resource from
the top to the bottom, and it'd be nice to have an iterator to feed
the triples that way. The model.write() produces triples in sequence
and I just want to utilize that ordering mechanism.

Chan





Reply via email to