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, <[email protected]> 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, <[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]:
>>>>>
>>>>>>
>>>>>>  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

Reply via email to