Collection remove issue

For questions and postings not covered by the other forums
ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Collection remove issue

Postby ConvertFromOldNGs » Fri Aug 07, 2009 12:34 pm

by Jade Starter >> Mon, 5 Apr 2004 22:19:16 GMT

Hi,

I am running into a problem deleting certain references from a collection class. My collection class is called Games and it contains classes of type Game. In the collection class I created a method "deleteSome" which will remove some of the entries from the collection.

Here is the code I am using:

vars
game : Game;begin

foreach game in self do
if condition = true then
self.remove(game);
end if;
endforeach;
end;

When I ran it I got an error msg saying "entry not found in collection". This does not make any sense to me as I am traversing the collection and all entries have to exist. So I decided to take out the condition and try to remove all game instances from the collection. This gave me the same error.

I then tried to play around with the collection and unchecked "duplicates allowed". It required me to do a reorg. I ran it again and it worked. After further testing I noticed that doing a reorg fixes the problem.

Furtherone, I noticed that self.indexOf(loc) returns the correct number, but when I call self.includes(loc) it says false. So for some reasons it finds the entry sometimes, but not all the times.

Does anybody know why I am getting this error? Is there a way in my code to force a reorg before I execute the remove command?

Thanks for all your help in advance!
By the way, I am using Jade 6.0.16.

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Collection remove issue

Postby ConvertFromOldNGs » Fri Aug 07, 2009 12:34 pm

by dcooper@jade.co.nz >> Mon, 5 Apr 2004 23:59:47 GMT

Is your Games collection a dictionary (ie: a subclass of MemberKeyDictionary)?

And after adding a Game to the dictionary, do you ever change properties on the Game that are keys in the dictionary?

If so and (as Rob mentioned in his post) you don't have an inverse relationship between Game and the class that owns the dictionary (ie: the class on which you've defined a property - allGames, for example - of the dictionary type), then you'll be creating a mismatch between the key properties on the Game and the key values at which the Game was added to the dictionary.

In this case, the "remove" method won't be able to find the Game in the dictionary because it will be looking it up based on the wrong keys.

For example, if you have a no-inverse dictionary of Games (keyed by "name", for example) and do this:

dict.add(game);
write dict.includes(game); // true
// change key, but if no inverse game is still in dict at old key game.name := "changed";
write dict.includes(game); // false
dict.remove(game); // exception

However, a foreach loop will still find the game (whereas the "includes" above won't) because it traverses the dictionary without accessing the members by their key(s).

As Rob's suggested, adding an inverse relationship is the best way to address this. Then JADE will take care of the dictionary key maintenance for you.

If you don't want to do that, wherever you change a key value on Game, you need to change your code to be (using the above example);

// remove game before changing key
dict.remove(game);
// change key
game.name := "changed";
// add game back at new key value
dict.add(game);

You can remove members from dictionaries and sets (but not arrays) while you're iterating them, but as Rob points out, it's often clearer/easier to use a transient collection to separate the iteration and the removal/deletion.

Cheers,
Dean.

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Collection remove issue

Postby ConvertFromOldNGs » Fri Aug 07, 2009 12:34 pm

by Patwos >> Tue, 6 Apr 2004 9:40:10 GMT

Further to Rob's and Dean's messages, this is also the subject of FAQ 11959 - including some sample code that you can use to help identify and fix entries that have a mismatched key.

As they have both already said, the best option would be to add an inverse and let automatic inverse maintenance take care of everything for you to avoid this situation.

Hope that helps,
Pat.

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Collection remove issue

Postby ConvertFromOldNGs » Fri Aug 07, 2009 12:34 pm

by allistar >> Tue, 6 Apr 2004 9:42:06 GMT

As others have mentioned it sounds like you have a collection with no inverse. In general it is wise to make *all* collections and references have inverses. This way JADE will automatically maintain the other side of the inverse if you change the "manual" side (or if you change a key, which I suspect has caused your issue).

Allistar.
--
------------------------------------------------------------------
Allistar Melville
Software Developer, Analyst allistar@silvermoon.co.nz
Auckland, NEW ZEALAND

Silvermoon Software
Specialising in JADE development and consulting
Visit us at: http://www.silvermoon.co.nz
*NEW* Simple web access to Jade at: www.silvermoon.co.nz/jhp.html ------------------------------------------------------------------


Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 27 guests