Problem deleting model element [message #552305] |
Wed, 11 August 2010 12:11  |
Eclipse User |
|
|
|
I am trying to delete an element from my target model using the following in an ETL file,
var subroutines := tgtIL1!`Subroutine`.allInstances();
if(s.`temporary` = true){
deleteElement(s);
}
any ideas why I get the following error: Method 'deleteElement' not found.
cheers, Andy
|
|
|
|
|
|
Re: Problem deleting model element [message #552505 is a reply to message #552480] |
Thu, 12 August 2010 08:08   |
Eclipse User |
|
|
|
Hi Louis,
On 12/08/2010 11:58, Louis Rose wrote:
> Hi Andy,
>
> That ConcurrentModificationException is raised because, in Java (and
> hence in EOL), you can't modify a list while iterating over it with a
> foreach. It's daft, and I don't like it either :)
There's a good reason for this: Iterators need to maintain a state
indicating where they are in a specific iteration. Depending on the
semantics of a collection and the semantics of the specific iterator
(which may not necessarily be a simple sequential iteration), it may be
very difficult to reconstruct or adapt this state when the underlying
collection is modified except through the iterator itself. To avoid
unintended problems with this (such as programs assuming they have
actually seen every element in a collection, when really they haven't),
the Java Collection API opts for a fail-fast policy using the
ConcurrentModificationException.
Having said that, I am not sure why this problem needs to be reflected
into EOL, though. I assume, that you are currently translating an EOL
for loop like this:
EOL:
for a in B {
....
}
Java:
for (BElemType a : B) {
...
}
This will cause ConcurrentModificationExceptions if B is modified in the
loop body.
Alternatively, you could translate this to
for (BElemType a : B.clone()) {
...
}
which creates a copy of the original collection to be iterated, and thus
separates iteration and modification, meaning that no
ConcurrentModificationExceptions occur.
What do you think?
Steffen
>
> The workaround is to use a while rather than a foreach loop:
>
> while (not subroutines.isEmpty()) {
> var s = subroutines.first;
> if(s.`temporary` = true){
> delete s;
> }
> }
>
> Cheers,
> Louis.
>
|
|
|
|
|
|
|
|
Re: Problem deleting model element [message #592974 is a reply to message #552480] |
Thu, 12 August 2010 08:08   |
Eclipse User |
|
|
|
Hi Louis,
On 12/08/2010 11:58, Louis Rose wrote:
> Hi Andy,
>
> That ConcurrentModificationException is raised because, in Java (and
> hence in EOL), you can't modify a list while iterating over it with a
> foreach. It's daft, and I don't like it either :)
There's a good reason for this: Iterators need to maintain a state
indicating where they are in a specific iteration. Depending on the
semantics of a collection and the semantics of the specific iterator
(which may not necessarily be a simple sequential iteration), it may be
very difficult to reconstruct or adapt this state when the underlying
collection is modified except through the iterator itself. To avoid
unintended problems with this (such as programs assuming they have
actually seen every element in a collection, when really they haven't),
the Java Collection API opts for a fail-fast policy using the
ConcurrentModificationException.
Having said that, I am not sure why this problem needs to be reflected
into EOL, though. I assume, that you are currently translating an EOL
for loop like this:
EOL:
for a in B {
....
}
Java:
for (BElemType a : B) {
...
}
This will cause ConcurrentModificationExceptions if B is modified in the
loop body.
Alternatively, you could translate this to
for (BElemType a : B.clone()) {
...
}
which creates a copy of the original collection to be iterated, and thus
separates iteration and modification, meaning that no
ConcurrentModificationExceptions occur.
What do you think?
Steffen
>
> The workaround is to use a while rather than a foreach loop:
>
> while (not subroutines.isEmpty()) {
> var s = subroutines.first;
> if(s.`temporary` = true){
> delete s;
> }
> }
>
> Cheers,
> Louis.
>
|
|
|
|
|
|
Re: Problem deleting model element [message #593149 is a reply to message #552710] |
Tue, 24 August 2010 12:07  |
Eclipse User |
|
|
|
As discussed on the bug report, the delete statement can also be used with collections. This means that the following code:
var subroutines := tgtIL1!`Subroutine`.allInstances();
while (not subroutines.isEmpty()) {
var s = subroutines.first;
if(s.`temporary` = true){
delete s;
}
}
Could be rewritten as:
var subroutines := tgtIL1!`Subroutine`.allInstances();
delete subroutines.select(s|s.`temporary` = true);
Cheers,
Louis.
|
|
|
Powered by
FUDForum. Page generated in 0.27436 seconds