You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Chris_Dollin <eh...@gmail.com> on 2013/09/12 07:15:33 UTC

Re: an iterator for serialized stmt/triple listing ?

On Wednesday, September 11, 2013 07:28:33 PM chan@info-cast.com 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

Re: an iterator for serialized stmt/triple listing ?

Posted by Andy Seaborne <an...@apache.org>.
> Hi Andy,
>
> This is very informative as I wasn't even aware that Jena document covers
> the I/O separately. BTW, what're the package names for these StreamRDFLib
> and RDFDataMgr ? I tried to find them in the Jena JavaDoc, but no luck.

Because the I/O supports datasets as well as graphs, it's in ARQ. 
History at work.

(But I find it easier to attach as much as of the Jena source code in 
Eclipse as  makes sense and look for classes that way.  maven makes it 
easy.)

	Andy

>
> Thanks
>
> Chan


Re: an iterator for serialized stmt/triple listing?

Posted by ch...@info-cast.com.
On 2013-09-12 01:54, Andy Seaborne wrote:
> 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
> 
Hi Andy,

This is very informative as I wasn't even aware that Jena document 
covers
the I/O separately. BTW, what're the package names for these 
StreamRDFLib
and RDFDataMgr ? I tried to find them in the Jena JavaDoc, but no luck.

Thanks

Chan

Re: an iterator for serialized stmt/triple listing ?

Posted by Andy Seaborne <an...@apache.org>.
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, <ch...@info-cast.com> 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, <ch...@info-cast.com> wrote:
>>>
>>>   On 2013-09-11 23:15, Chris_Dollin wrote:
>>>>
>>>>   On Wednesday, September 11, 2013 07:28:33 PM chan@info-cast.com 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
>>>>
>>>>
>
>


Re: an iterator for serialized stmt/triple listing?

Posted by ch...@info-cast.com.
Hi Claude,

Thanks for the advice - your Comparator-based solution is cleaner, 
compact
and powerful. We understand utilizing the Comparator is always better 
way
to deal with any sorting needs, as one can customize the detail 
process.

BTW, the HashMap is for table-lookup to find a Resource with the 
toString()
name, and to provide the set of Resource names to the TreeSet. The 
hashing
mechanism doesn't have anything to do with the actual name sorting to 
order
the Resources.

Best

Chan

> Chan,
> 
> You are using a HashMap, this means that the objects will be retrieved 
> in
> hashCode order.  Not what you want.
> 
> Look at the java.util.Comparator interface.  Implement one of those 
> that
> extracts the name for comparison and returns the results in the order 
> you
> want.
> 
> something like:
> 
>     new Comparator<Resource>(){
>          public int compare(Resource o1, Resource o2) {
>             return -1 * o1.toString().compareTo( o2.toString() );
>         }
>     };
> 
> You then pass that implementation to the TreeSet constructor.  This 
> will
> yield a sorted map with the items in the order you want.   Then just 
> call
> the iterator.
> 
> Claude
> 
> 
> On Fri, Sep 13, 2013 at 3:21 AM, <ch...@info-cast.com> wrote:
> 
>> Hi Claude,
>> 
>> Thanks for your guidance - it worked nicely.
>> I made a couple of changes to make it as I wanted:
>> 
>> 1) Need to sort using the name of the subject resources,
>>    not the resource itself. Have to build a map to do that
>> 2) Need to take iterator in descending order after sort,
>>    to have the named resources come up first
>> 
>> The changed & tested code:
>> ------------------------------**------------------------------**-------
>>   public void writeRDFStatements(Model model, PrintWriter writer) {
>>     Map<String, Resource> subjMap = new HashMap<String, Resource>();
>>     ResIterator subjIter = model.listSubjects();
>>     while (subjIter.hasNext()) {
>>       Resource subj = subjIter.next();
>>       Resource typeDefn = subj.getPropertyResourceValue(**RDF.type);
>>       subjMap.put(subj.toString(), subj);
>>     }
>> 
>>     TreeSet<String> subjs = new TreeSet<String>(subjMap.**keySet());
>>     Iterator<String> rIter = subjs.descendingIterator();
>>     while (rIter.hasNext())
>>       writeRDFStatements(model, subjMap.get(rIter.next()), writer);
>>   }
>> ------------------------------**------------------------------**-------
>> Chan
>> 
>>  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
>>> 
>>> 
>>> On Thu, Sep 12, 2013 at 8:09 AM, <ch...@info-cast.com> 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, <ch...@info-cast.com> wrote:
>>>>> 
>>>>>  On 2013-09-11 23:15, Chris_Dollin wrote:
>>>>> 
>>>>>> 
>>>>>>  On Wednesday, September 11, 2013 07:28:33 PM 
>>>>>> chan@info-cast.comwrote:
>>>>>> 
>>>>>>> 
>>>>>>>  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
>>>>>> 
>>>>>> 
>>>>>> 

Re: an iterator for serialized stmt/triple listing ?

Posted by Claude Warren <cl...@xenei.com>.
Chan,

You are using a HashMap, this means that the objects will be retrieved in
hashCode order.  Not what you want.

Look at the java.util.Comparator interface.  Implement one of those that
extracts the name for comparison and returns the results in the order you
want.

something like:

    new Comparator<Resource>(){
         public int compare(Resource o1, Resource o2) {
            return -1 * o1.toString().compareTo( o2.toString() );
        }
    };

You then pass that implementation to the TreeSet constructor.  This will
yield a sorted map with the items in the order you want.   Then just call
the iterator.

Claude


On Fri, Sep 13, 2013 at 3:21 AM, <ch...@info-cast.com> wrote:

> Hi Claude,
>
> Thanks for your guidance - it worked nicely.
> I made a couple of changes to make it as I wanted:
>
> 1) Need to sort using the name of the subject resources,
>    not the resource itself. Have to build a map to do that
> 2) Need to take iterator in descending order after sort,
>    to have the named resources come up first
>
> The changed & tested code:
> ------------------------------**------------------------------**-------
>   public void writeRDFStatements(Model model, PrintWriter writer) {
>     Map<String, Resource> subjMap = new HashMap<String, Resource>();
>     ResIterator subjIter = model.listSubjects();
>     while (subjIter.hasNext()) {
>       Resource subj = subjIter.next();
>       Resource typeDefn = subj.getPropertyResourceValue(**RDF.type);
>       subjMap.put(subj.toString(), subj);
>     }
>
>     TreeSet<String> subjs = new TreeSet<String>(subjMap.**keySet());
>     Iterator<String> rIter = subjs.descendingIterator();
>     while (rIter.hasNext())
>       writeRDFStatements(model, subjMap.get(rIter.next()), writer);
>   }
> ------------------------------**------------------------------**-------
> Chan
>
>  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
>>
>>
>> On Thu, Sep 12, 2013 at 8:09 AM, <ch...@info-cast.com> 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, <ch...@info-cast.com> wrote:
>>>>
>>>>  On 2013-09-11 23:15, Chris_Dollin wrote:
>>>>
>>>>>
>>>>>  On Wednesday, September 11, 2013 07:28:33 PM chan@info-cast.comwrote:
>>>>>
>>>>>>
>>>>>>  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
>>>>>
>>>>>
>>>>>


-- 
I like: Like Like - The likeliest place on the web<http://like-like.xenei.com>
LinkedIn: http://www.linkedin.com/in/claudewarren

Re: an iterator for serialized stmt/triple listing?

Posted by ch...@info-cast.com.
Hi Claude,

Thanks for your guidance - it worked nicely.
I made a couple of changes to make it as I wanted:

1) Need to sort using the name of the subject resources,
    not the resource itself. Have to build a map to do that
2) Need to take iterator in descending order after sort,
    to have the named resources come up first

The changed & tested code:
-------------------------------------------------------------------
   public void writeRDFStatements(Model model, PrintWriter writer) {
     Map<String, Resource> subjMap = new HashMap<String, Resource>();
     ResIterator subjIter = model.listSubjects();
     while (subjIter.hasNext()) {
       Resource subj = subjIter.next();
       Resource typeDefn = subj.getPropertyResourceValue(RDF.type);
       subjMap.put(subj.toString(), subj);
     }

     TreeSet<String> subjs = new TreeSet<String>(subjMap.keySet());
     Iterator<String> rIter = subjs.descendingIterator();
     while (rIter.hasNext())
       writeRDFStatements(model, subjMap.get(rIter.next()), writer);
   }
-------------------------------------------------------------------
Chan

> 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
> 
> 
> On Thu, Sep 12, 2013 at 8:09 AM, <ch...@info-cast.com> 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, <ch...@info-cast.com> wrote:
>>> 
>>>  On 2013-09-11 23:15, Chris_Dollin wrote:
>>>> 
>>>>  On Wednesday, September 11, 2013 07:28:33 PM chan@info-cast.com 
>>>> 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
>>>> 
>>>> 

Re: an iterator for serialized stmt/triple listing ?

Posted by Claude Warren <cl...@xenei.com>.
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


On Thu, Sep 12, 2013 at 8:09 AM, <ch...@info-cast.com> 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, <ch...@info-cast.com> wrote:
>>
>>  On 2013-09-11 23:15, Chris_Dollin wrote:
>>>
>>>  On Wednesday, September 11, 2013 07:28:33 PM chan@info-cast.com 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
>>>
>>>


-- 
I like: Like Like - The likeliest place on the web<http://like-like.xenei.com>
LinkedIn: http://www.linkedin.com/in/claudewarren

Re: an iterator for serialized stmt/triple listing?

Posted by ch...@info-cast.com.
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, <ch...@info-cast.com> wrote:
> 
>> On 2013-09-11 23:15, Chris_Dollin wrote:
>> 
>>> On Wednesday, September 11, 2013 07:28:33 PM chan@info-cast.com 
>>> 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
>> 

Re: an iterator for serialized stmt/triple listing ?

Posted by Claude Warren <cl...@xenei.com>.
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, <ch...@info-cast.com> wrote:

> On 2013-09-11 23:15, Chris_Dollin wrote:
>
>> On Wednesday, September 11, 2013 07:28:33 PM chan@info-cast.com 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
>



-- 
I like: Like Like - The likeliest place on the web<http://like-like.xenei.com>
LinkedIn: http://www.linkedin.com/in/claudewarren

Re: an iterator for serialized stmt/triple listing?

Posted by ch...@info-cast.com.
On 2013-09-11 23:15, Chris_Dollin wrote:
> On Wednesday, September 11, 2013 07:28:33 PM chan@info-cast.com 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