You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Jorge Christodoulou <jo...@hotmail.com> on 2011/05/17 12:53:37 UTC

com.hp.hpl.jena.query.ResultSet nested loop

Hi, I have ResultSet a and ResultSet b (com.hp.hpl.jena.query.ResultSet)
for(; a.hasNext() ;){	String s_a = a.nextSolution().get(commonVars.get(0)).toString();	for(; b.hasNext() ;){		String s_b = b.nextSolution().get(commonVars.get(0)).toString();		if(s_a.equalsIgnoreCase(s_b))			.....	}}
The problem is that it gets in the second loop only for the first iteration of the first loop.Is this because Iterators are single use only and what can i do if so? Any ideas will be helpfull.




 		 	   		  

Re: com.hp.hpl.jena.query.ResultSet remove row

Posted by Andy Seaborne <an...@epimorphics.com>.

On 17/05/11 17:09, Jorge Christodoulou wrote:
>
> Hi, the ResultSet man again :)Is it possible to remove a "row" from a com.hp.hpl.jena.query.ResultSet  during a for loop?
> I tried:
> for(; rSet.hasNext() ;){			if(listCSD.contains(rSet.nextSolution().get(commonVars.get(0)).toString()))				rSet.remove();		}
> but it throws this exception:java.lang.UnsupportedOperationException: ResultSetMem: Attempt to remove an element

No, it's not possible.

Convert to an array (Iter.toList) and manipulate that.

As you want to rerun over the results multiple times, maybe a better 
design is to get the results, transfer to a List<>.

	Andy

Re: com.hp.hpl.jena.query.ResultSet remove row

Posted by Ian Dickinson <ia...@epimorphics.com>.
Hi Jorge,

On 17/05/11 17:09, Jorge Christodoulou wrote:
> Hi, the ResultSet man again :)Is it possible to remove a "row" from a
> com.hp.hpl.jena.query.ResultSet  during a for loop? I tried: for(;
> rSet.hasNext() ;){
> if(listCSD.contains(rSet.nextSolution().get(commonVars.get(0)).toString()))
> rSet.remove();		} but it throws this
> exception:java.lang.UnsupportedOperationException: ResultSetMem:
> Attempt to remove an element

What would it mean to remove a row from a ResultSet? It's a set of
bindings, not a construct in the underlying database. You don't have to
process that row if you don't want to. If you want to avoid any similar
row in future, add the row to a HashSet of ignorable solutions.

Ian

-- 
____________________________________________________________
Ian Dickinson                   Epimorphics Ltd, Bristol, UK
mailto:ian@epimorphics.com        http://www.epimorphics.com
cell: +44-7786-850536              landline: +44-1275-399069
------------------------------------------------------------
Epimorphics Ltd.  is a limited company registered in England
(no. 7016688). Registered address: Court Lodge, 105 High St,
              Portishead, Bristol BS20 6PT, UK


com.hp.hpl.jena.query.ResultSet remove row

Posted by Jorge Christodoulou <jo...@hotmail.com>.
Hi, the ResultSet man again :)Is it possible to remove a "row" from a com.hp.hpl.jena.query.ResultSet  during a for loop?
I tried:
for(; rSet.hasNext() ;){			if(listCSD.contains(rSet.nextSolution().get(commonVars.get(0)).toString()))				rSet.remove();		}
but it throws this exception:java.lang.UnsupportedOperationException: ResultSetMem: Attempt to remove an element

 		 	   		  

Re: com.hp.hpl.jena.query.ResultSet nested loop

Posted by Chris Dollin <ch...@epimorphics.com>.
Jorge said:

> 1)  b = ResultSetFactory.makeRewindable(b); before the loops
> and 2) ((ResultSetRewindable) b).reset(); each time round the `a` loop.
> Thank you very much Chris!!!

Better would be

    ResultSetRewindable rb = ResultSetFactory.makeRewindable(b);

Then you can use rb instead of b and rb.reset() rather than a (Big)cast.
It's clearer what's going on.

Chris
  		 	   		  
-- 
"The wizard seemed quite willing when I talked to him."  /Howl's Moving Castle/

Epimorphics Ltd, http://www.epimorphics.com
Registered address: Court Lodge, 105 High Street, Portishead, Bristol BS20 6PT
Epimorphics Ltd. is a limited company registered in England (number 7016688)

RE: com.hp.hpl.jena.query.ResultSet nested loop

Posted by Jorge Christodoulou <jo...@hotmail.com>.


> You will have to make the `b` iterator rewindable before the loops
> and then reset it each time round the `a` loop.
> 
> Chris

1)  b = ResultSetFactory.makeRewindable(b); before the loops
and 2) ((ResultSetRewindable) b).reset(); each time round the `a` loop.
Thank you very much Chris!!!
 		 	   		  

Re: com.hp.hpl.jena.query.ResultSet nested loop

Posted by Chris Dollin <ch...@epimorphics.com>.
>  Τhe second loop is inside the first one...Sorry, I just show the appearence of the syntax... 		 	   		

You will have to make the `b` iterator rewindable before the loops
and then reset it each time round the `a` loop.

Chris
  
-- 
"I know it was late, but Mountjoy never bothers,                /Archer's Goon/
 so long as it's the full two thousand words."

Epimorphics Ltd, http://www.epimorphics.com
Registered address: Court Lodge, 105 High Street, Portishead, Bristol BS20 6PT
Epimorphics Ltd. is a limited company registered in England (number 7016688)

RE: com.hp.hpl.jena.query.ResultSet nested loop

Posted by Jorge Christodoulou <jo...@hotmail.com>.


> From: jorjecan@hotmail.com
> To: jena-users@incubator.apache.org
> Subject: RE: com.hp.hpl.jena.query.ResultSet nested loop
> Date: Tue, 17 May 2011 11:12:35 +0000
> 
> 
> 
> 
> > Date: Tue, 17 May 2011 12:04:32 +0100
> > From: andy.seaborne@epimorphics.com
> > To: jena-users@incubator.apache.org
> > Subject: Re: com.hp.hpl.jena.query.ResultSet nested loop
> > 
> > 
> > 
> > On 17/05/11 11:53, Jorge Christodoulou wrote:
> > >
> > > Hi, I have ResultSet a and ResultSet b
> > > (com.hp.hpl.jena.query.ResultSet) for(; a.hasNext() ;){	String s_a =
> > > a.nextSolution().get(commonVars.get(0)).toString();	for(; b.hasNext()
> > > ;){		String s_b = b.nextSolution().get(commonVars.get(0)).toString();
> > > if(s_a.equalsIgnoreCase(s_b))			.....	}} The problem is that it gets
> > > in the second loop only for the first iteration of the first loop.Is
> > > this because Iterators are single use only and what can i do if so?
> > > Any ideas will be helpfull.
> > 
> > (unreadable)
> > 
> > If a and b are the same ResultSet (iterator), then yes, ResultSets are 
> > use-once.  See ResultSetFacory.makeRewindable
> > 
> > 	Andy
> 
> a and b are both com.hp.hpl.jena.query.ResultSet but they have different results inside...(if this is what you ask???)Rewindable has only extra method reset() and size() and doesn't seem to help
>  		 	   		  

 Τhe second loop is inside the first one...Sorry, I just show the appearence of the syntax... 		 	   		  

RE: com.hp.hpl.jena.query.ResultSet nested loop

Posted by Jorge Christodoulou <jo...@hotmail.com>.


> Date: Tue, 17 May 2011 12:04:32 +0100
> From: andy.seaborne@epimorphics.com
> To: jena-users@incubator.apache.org
> Subject: Re: com.hp.hpl.jena.query.ResultSet nested loop
> 
> 
> 
> On 17/05/11 11:53, Jorge Christodoulou wrote:
> >
> > Hi, I have ResultSet a and ResultSet b
> > (com.hp.hpl.jena.query.ResultSet) for(; a.hasNext() ;){	String s_a =
> > a.nextSolution().get(commonVars.get(0)).toString();	for(; b.hasNext()
> > ;){		String s_b = b.nextSolution().get(commonVars.get(0)).toString();
> > if(s_a.equalsIgnoreCase(s_b))			.....	}} The problem is that it gets
> > in the second loop only for the first iteration of the first loop.Is
> > this because Iterators are single use only and what can i do if so?
> > Any ideas will be helpfull.
> 
> (unreadable)
> 
> If a and b are the same ResultSet (iterator), then yes, ResultSets are 
> use-once.  See ResultSetFacory.makeRewindable
> 
> 	Andy

a and b are both com.hp.hpl.jena.query.ResultSet but they have different results inside...(if this is what you ask???)Rewindable has only extra method reset() and size() and doesn't seem to help
 		 	   		  

Re: com.hp.hpl.jena.query.ResultSet nested loop

Posted by Andy Seaborne <an...@epimorphics.com>.

On 17/05/11 11:53, Jorge Christodoulou wrote:
>
> Hi, I have ResultSet a and ResultSet b
> (com.hp.hpl.jena.query.ResultSet) for(; a.hasNext() ;){	String s_a =
> a.nextSolution().get(commonVars.get(0)).toString();	for(; b.hasNext()
> ;){		String s_b = b.nextSolution().get(commonVars.get(0)).toString();
> if(s_a.equalsIgnoreCase(s_b))			.....	}} The problem is that it gets
> in the second loop only for the first iteration of the first loop.Is
> this because Iterators are single use only and what can i do if so?
> Any ideas will be helpfull.

(unreadable)

If a and b are the same ResultSet (iterator), then yes, ResultSets are 
use-once.  See ResultSetFacory.makeRewindable

	Andy