Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Epsilon » Problem deleting model element
Problem deleting model element [message #552305] Wed, 11 August 2010 12:11 Go to next message
Andy Ed is currently offline Andy Ed
Messages: 41
Registered: December 2009
Member
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 #552343 is a reply to message #552305] Wed, 11 August 2010 15:24 Go to previous messageGo to next message
Louis Rose is currently offline Louis Rose
Messages: 439
Registered: July 2009
Location: York, United Kingdom
Senior Member
Hi Andy,

Does the following work?

delete s;


Epsilon provides a built-in delete keyword, but not a deleteElements method.

Cheers,
Louis.
Re: Problem deleting model element [message #552460 is a reply to message #552305] Thu, 12 August 2010 05:57 Go to previous messageGo to next message
Andy Ed is currently offline Andy Ed
Messages: 41
Registered: December 2009
Member
using delete s I get an exception,

Internal error: java.util.ConcurrentModificationException

the trace points to the line,

for(s in subroutines){ ....

in a loop,

for(s in subroutines){
if(s.`temporary` = true){
delete s;
}
}

How should I overcome this?
Re: Problem deleting model element [message #552480 is a reply to message #552460] Thu, 12 August 2010 06:58 Go to previous messageGo to next message
Louis Rose is currently offline Louis Rose
Messages: 439
Registered: July 2009
Location: York, United Kingdom
Senior Member
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 Smile

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 #552505 is a reply to message #552480] Thu, 12 August 2010 08:08 Go to previous messageGo to next message
Steffen Zschaler is currently offline Steffen Zschaler
Messages: 228
Registered: July 2009
Senior Member
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 #552536 is a reply to message #552480] Thu, 12 August 2010 10:55 Go to previous messageGo to next message
Andy Ed is currently offline Andy Ed
Messages: 41
Registered: December 2009
Member
I have used something similar, and it worked - thanks.

>while (not subroutines.isEmpty()) {
>var s = subroutines.first;
> if(s.`temporary` = true){
> delete s;
> }
>}

[Updated on: Thu, 12 August 2010 10:59]

Report message to a moderator

Re: Problem deleting model element [message #552579 is a reply to message #552536] Thu, 12 August 2010 13:05 Go to previous messageGo to next message
Louis Rose is currently offline Louis Rose
Messages: 439
Registered: July 2009
Location: York, United Kingdom
Senior Member
Andy: Glad it worked!

Steffan: Thanks for the explanation, and for the suggestion. My guess would be that cloning each element might impact negatively on performance, but we'd have to run some tests. Perhaps we could use a different approach though - I agree that it'd be good for EOL not to throw concurrent modification exceptions if possible.

I've filled the following bug report, and I'll discuss a solution with Dimitris when we're both back from vacation:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=322560

Thanks for the feedback guys.

Cheers,
Louis.
Re: Problem deleting model element [message #552710 is a reply to message #552579] Fri, 13 August 2010 04:57 Go to previous messageGo to next message
Steffen Zschaler is currently offline Steffen Zschaler
Messages: 228
Registered: July 2009
Senior Member
Cool. I've added a few comments on the bug report.

Steffen

On 12/08/2010 18:05, Louis Rose wrote:
> Andy: Glad it worked!
>
> Steffan: Thanks for the explanation, and for the suggestion. My guess
> would be that cloning each element might impact negatively on
> performance, but we'd have to run some tests. Perhaps we could use a
> different approach though - I agree that it'd be good for EOL not to
> throw concurrent modification exceptions if possible.
>
> I've filled the following bug report, and I'll discuss a solution with
> Dimitris when we're both back from vacation:
> https://bugs.eclipse.org/bugs/show_bug.cgi?id=322560
>
> Thanks for the feedback guys.
>
> Cheers,
> Louis.
>
Re: Problem deleting model element [message #554892 is a reply to message #552710] Tue, 24 August 2010 12:07 Go to previous messageGo to next message
Louis Rose is currently offline Louis Rose
Messages: 439
Registered: July 2009
Location: York, United Kingdom
Senior Member
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.
Re: Problem deleting model element [message #592941 is a reply to message #552460] Thu, 12 August 2010 06:58 Go to previous messageGo to next message
Louis Rose is currently offline Louis Rose
Messages: 439
Registered: July 2009
Location: York, United Kingdom
Senior Member
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 :)

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 Go to previous messageGo to next message
Steffen Zschaler is currently offline Steffen Zschaler
Messages: 228
Registered: July 2009
Senior Member
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 #593005 is a reply to message #552480] Thu, 12 August 2010 10:55 Go to previous messageGo to next message
Andy Ed is currently offline Andy Ed
Messages: 41
Registered: December 2009
Member
I have used something similar, and it worked - thanks.
Re: Problem deleting model element [message #593013 is a reply to message #552536] Thu, 12 August 2010 13:05 Go to previous messageGo to next message
Louis Rose is currently offline Louis Rose
Messages: 439
Registered: July 2009
Location: York, United Kingdom
Senior Member
Andy: Glad it worked!

Steffan: Thanks for the explanation, and for the suggestion. My guess would be that cloning each element might impact negatively on performance, but we'd have to run some tests. Perhaps we could use a different approach though - I agree that it'd be good for EOL not to throw concurrent modification exceptions if possible.

I've filled the following bug report, and I'll discuss a solution with Dimitris when we're both back from vacation:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=322560

Thanks for the feedback guys.

Cheers,
Louis.
Re: Problem deleting model element [message #593066 is a reply to message #593013] Fri, 13 August 2010 04:57 Go to previous messageGo to next message
Steffen Zschaler is currently offline Steffen Zschaler
Messages: 228
Registered: July 2009
Senior Member
Cool. I've added a few comments on the bug report.

Steffen

On 12/08/2010 18:05, Louis Rose wrote:
> Andy: Glad it worked!
>
> Steffan: Thanks for the explanation, and for the suggestion. My guess
> would be that cloning each element might impact negatively on
> performance, but we'd have to run some tests. Perhaps we could use a
> different approach though - I agree that it'd be good for EOL not to
> throw concurrent modification exceptions if possible.
>
> I've filled the following bug report, and I'll discuss a solution with
> Dimitris when we're both back from vacation:
> https://bugs.eclipse.org/bugs/show_bug.cgi?id=322560
>
> Thanks for the feedback guys.
>
> Cheers,
> Louis.
>
Re: Problem deleting model element [message #593149 is a reply to message #552710] Tue, 24 August 2010 12:07 Go to previous message
Louis Rose is currently offline Louis Rose
Messages: 439
Registered: July 2009
Location: York, United Kingdom
Senior Member
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.
Previous Topic:[EOL] Help with GmfGraph Navigation.
Next Topic:Modify diagram dynamically.
Goto Forum:
  


Current Time: Mon Sep 01 09:53:42 EDT 2014

Powered by FUDForum. Page generated in 0.02155 seconds