Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Can I override/specialize an EFeature in a subclass?
icon5.gif  Can I override/specialize an EFeature in a subclass? [message #523641] Sat, 27 March 2010 21:57 Go to next message
Greg Mising name is currently offline Greg Mising nameFriend
Messages: 47
Registered: July 2009
Member
For example, I have class Shape and class Path and a Shape has a 1..* EReference to Path named paths. Now I also have class Rectangle that is a subclass of Shape and class Line that is a subclass of Path. I want to specialize Rectangle so that its paths must be Lines instead of any kind of Path. I am hoping I can do this in the ecore model instead of by writing code. I tried creating an identical EReference from Rectangle to Line also named paths, but when I validated the model it complained that I couldn't have two features named 'paths'. Is there any other way to achieve this type of specialization of EFeatures on subclasses within the ecore model? Annotations perhaps?

Thanks.
Greg
Re: Can I override/specialize an EFeature in a subclass? [message #523643 is a reply to message #523641] Sat, 27 March 2010 23:04 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Greg,

Comments below.

Greg wrote:
> For example, I have class Shape and class Path and a Shape has a 1..*
> EReference to Path named paths. Now I also have class Rectangle that
> is a subclass of Shape and class Line that is a subclass of Path. I
> want to specialize Rectangle so that its paths must be Lines instead
> of any kind of Path.
You can add a constraint, but no, you can't specialize features.
> I am hoping I can do this in the ecore model instead of by writing code.
These days it's possible to specify the implementation of a constraint
as part of an EAnnotation, using OCL for example.
> I tried creating an identical EReference from Rectangle to Line also
> named paths, but when I validated the model it complained that I
> couldn't have two features named 'paths'.
Yes, you can't have more than one feature with the same name.
> Is there any other way to achieve this type of specialization of
> EFeatures on subclasses within the ecore model? Annotations perhaps?
Only via constraints and such constraints aren't enforced immediately;
you need to invoke the validator.

UML2's extended generator supports things like this...
>
> Thanks.
> Greg


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Can I override/specialize an EFeature in a subclass? [message #523715 is a reply to message #523643] Mon, 29 March 2010 00:44 Go to previous messageGo to next message
Greg Mising name is currently offline Greg Mising nameFriend
Messages: 47
Registered: July 2009
Member
Thanks Ed. I know this is certainly doable in Java so the code could be generated, but out of curiosity is there a practical reason why EMF doesn't support this? Are there some internal implementation details that were not designed to support this? I don't see anything in EFactory or EPackage that would have problems supporting it and after a cursory look at EditingDomain, ItemProvider, ItemProviderAdapter and Command nothing stands out there either. I know that there is tons more in the EMF API so I won't be so ignorant as to assume that there isn't some conflict somewhere.

Or is it a matter of priority and time for implementing this feature?

What is UML2's extended generator? Do you know if Rational Rose supports this?

Thanks.
Greg
Re: Can I override/specialize an EFeature in a subclass? [message #523783 is a reply to message #523715] Mon, 29 March 2010 09:42 Go to previous messageGo to next message
Jonas Helming is currently offline Jonas HelmingFriend
Messages: 699
Registered: July 2009
Senior Member
Hi,
yes it would be doable in Java, but I dont think it would be very good
style. If I understood your model correctly I would rather add a
attribute in the subclass called "lines" then overloading the "path"
attribute. Otherwise getPath.add(path) in the subclass would not compile
which is quite confusing isnt it?
Jonas

Greg wrote:
> Thanks Ed. I know this is certainly doable in Java so the code could be
> generated, but out of curiosity is there a practical reason why EMF
> doesn't support this? Are there some internal implementation details
> that were not designed to support this? I don't see anything in
> EFactory or EPackage that would have problems supporting it and after a
> cursory look at EditingDomain, ItemProvider, ItemProviderAdapter and
> Command nothing stands out there either. I know that there is tons more
> in the EMF API so I won't be so ignorant as to assume that there isn't
> some conflict somewhere.
>
> Or is it a matter of priority and time for implementing this feature?
>
> What is UML2's extended generator? Do you know if Rational Rose
> supports this?
>
> Thanks.
> Greg
Re: Can I override/specialize an EFeature in a subclass? [message #523811 is a reply to message #523783] Mon, 29 March 2010 13:16 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Jonas,

Yes, it always bothered me that and instance of the derived class cannot
be used as an instance of the base class blindly because the things you
could do with it violate constraints that aren't apparent in the base
API.

Greg,

In Java, List<B> is not a subtype of List<A> even if B is a subtype of
A, so one could not declare EList<Line> getPaths(). So no matter what,
you end up with something that's effectively an invisible constraint.
If you define a model in UML2 then UML allows this type of thing and
UML's mapping to Ecore uses annotations to capture that information and
generate something that enforces the constraint.


Jonas wrote:
> Hi,
> yes it would be doable in Java, but I dont think it would be very good
> style. If I understood your model correctly I would rather add a
> attribute in the subclass called "lines" then overloading the "path"
> attribute. Otherwise getPath.add(path) in the subclass would not
> compile which is quite confusing isnt it?
> Jonas
>
> Greg wrote:
>> Thanks Ed. I know this is certainly doable in Java so the code could
>> be generated, but out of curiosity is there a practical reason why
>> EMF doesn't support this? Are there some internal implementation
>> details that were not designed to support this? I don't see anything
>> in EFactory or EPackage that would have problems supporting it and
>> after a cursory look at EditingDomain, ItemProvider,
>> ItemProviderAdapter and Command nothing stands out there either. I
>> know that there is tons more in the EMF API so I won't be so ignorant
>> as to assume that there isn't some conflict somewhere.
>>
>> Or is it a matter of priority and time for implementing this feature?
>>
>> What is UML2's extended generator? Do you know if Rational Rose
>> supports this?
>>
>> Thanks.
>> Greg


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Can I override/specialize an EFeature in a subclass? [message #523889 is a reply to message #523783] Mon, 29 March 2010 18:07 Go to previous messageGo to next message
Greg Mising name is currently offline Greg Mising nameFriend
Messages: 47
Registered: July 2009
Member
Hi Jonas.

Maybe it's a matter of opinion. I prefer to enforce model integrity at design-time rather than run-time, and as low in the design stack as possible. I could use the constraint/validator approach but that puts the onus on some downstream code, probably a UI plugin to ensure the validity (integrity) of the model. I don't want my model to ever be in an invalid state.

I think my solution will be along the lines of:
1) Add constraints to the model
2) Create a custom EditingDomain class that will validate all affected objects of all commands it is asked to create before it returns the command. It will also implement a CommandValidationProvider interface so that the UI can listen, receive and process notifications when a command validation fails.
3) For complex constraints that I can't define in the eCore model, I will define custom commands and override CreateCommand in my ItemProviders to make sure that the appropriate custom command is called.

Any thoughts on that? Ed, what do you think?

Thanks.
Greg

Jonas wrote on Mon, 29 March 2010 05:42
Hi,
yes it would be doable in Java, but I dont think it would be very good
style. If I understood your model correctly I would rather add a
attribute in the subclass called "lines" then overloading the "path"
attribute. Otherwise getPath.add(path) in the subclass would not compile
which is quite confusing isnt it?
Jonas

Greg wrote:
> Thanks Ed. I know this is certainly doable in Java so the code could be
> generated, but out of curiosity is there a practical reason why EMF
> doesn't support this? Are there some internal implementation details
> that were not designed to support this? I don't see anything in
> EFactory or EPackage that would have problems supporting it and after a
> cursory look at EditingDomain, ItemProvider, ItemProviderAdapter and
> Command nothing stands out there either. I know that there is tons more
> in the EMF API so I won't be so ignorant as to assume that there isn't
> some conflict somewhere.
>
> Or is it a matter of priority and time for implementing this feature?
>
> What is UML2's extended generator? Do you know if Rational Rose
> supports this?
>
> Thanks.
> Greg

Re: Can I override/specialize an EFeature in a subclass? [message #523894 is a reply to message #523889] Mon, 29 March 2010 13:28 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Greg,

Comments below.

Greg wrote:
> Hi Jonas.
>
> Maybe it's a matter of opinion. I prefer to enforce model integrity
> at design-time rather than run-time, and as low in the design stack as
> possible.
Both approaches for this case require runtime constraints.
> I could use the constraint/validator approach but that puts the onus
> on some downstream code, probably a UI plugin to ensure the validity
> (integrity) of the model.
The validator works without UI or even Eclipse dependencies.
> I don't want my model to ever be in an invalid state.
>
> I think my solution will be along the lines of:
> 1) Add constraints to the model
> 2) Create a custom EditingDomain class that will validate all affected
> objects of all commands it is asked to create before it returns the
> command.
Likely you edit this either by creating children, in which case you can
restrict which choices are offered, or via the properties view, in which
case you can filter the choices offered.
> It will also implement a CommandValidationProvider interface so that
> the UI can listen, receive and process notifications when a command
> validation fails.
Probably the UI should never let you make a choice that would fail.
> 3) For complex constraints that I can't define in the eCore model, I
> will define custom commands and override CreateCommand in my
> ItemProviders to make sure that the appropriate custom command is called.
I think that's generally the right track, though I expect it's even
simpler in that you can specialize the derived class'
collectNewChildDescriptors to filter out inappropriate ones as well as
do something similar for the property descriptor if it's a
non-containment reference.
>
> Any thoughts on that? Ed, what do you think?
>
> Thanks.
> Greg
>
> Jonas wrote on Mon, 29 March 2010 05:42
>> Hi,
>> yes it would be doable in Java, but I dont think it would be very
>> good style. If I understood your model correctly I would rather add a
>> attribute in the subclass called "lines" then overloading the "path"
>> attribute. Otherwise getPath.add(path) in the subclass would not
>> compile which is quite confusing isnt it?
>> Jonas
>>
>> Greg wrote:
>> > Thanks Ed. I know this is certainly doable in Java so the code
>> could be > generated, but out of curiosity is there a practical
>> reason why EMF > doesn't support this? Are there some internal
>> implementation details > that were not designed to support this? I
>> don't see anything in > EFactory or EPackage that would have problems
>> supporting it and after a > cursory look at EditingDomain,
>> ItemProvider, ItemProviderAdapter and > Command nothing stands out
>> there either. I know that there is tons more > in the EMF API so I
>> won't be so ignorant as to assume that there isn't > some conflict
>> somewhere.
>> > > Or is it a matter of priority and time for implementing this
>> feature?
>> > > What is UML2's extended generator? Do you know if Rational Rose
>> > supports this?
>> > > Thanks.
>> > Greg
>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Can I override/specialize an EFeature in a subclass? [message #523912 is a reply to message #523894] Mon, 29 March 2010 19:27 Go to previous messageGo to next message
Greg Mising name is currently offline Greg Mising nameFriend
Messages: 47
Registered: July 2009
Member
Thanks Ed.

You commented:
> Probably the UI should never let you make a choice that would fail.

I totally agree, however ideally the Edit plugin should never let the UI present a choice that would fail. There are some cases that are unavoidable however. For example, if my objects have a name property and I have a rule/constraint (whatever) that dictates that object names within their container must be unique then the UI (PropertySheet in this case) can't prevent the SetCommand from being requested.

I do like the idea of specializing the ItemProviders' collectNewChildDescriptors and getPropertyDescriptors. I'd probably have to use stateful ItemProviders then.

I've actually done this in one case where I have an object with properties: dataType, lowValue, highValue; where the low and high values are instances of the specified dataType, which is a limited list of java object types. I can specialize my getPropertyDescriptors to regenerate when the dataType gets changed, in which case it will create a property descriptor for low and high value suitable for the new datatype. The problem is, what do I do with the current values of those properties. If the old dataType can be cast to the new dataType then it's not a problem. Otherwise I need to clear their values and let the user enter new valid ones. But where in my code do I clear the values? I could do it in the specialized getPropertyDescriptors, but if the user clicks undo then he doesn't get his old values back. I think this is a case where I'd need to specialize getPropertyDescriptors and override the SetCommand for dataType to make sure that the old values are recoverable in an undo.

So I think that I'll hold off on the following for now:
1) Add constraints to the model
2) Create a custom EditingDomain class that will validate all affected
objects of all commands it is asked to create before it returns the
command.

and do the following instead:
1) Specialize the ItemProviders' collectNewChildDescriptors and getPropertyDescriptors and make them stateful
2) For complex constraints that I can't define in the eCore model, I
will define custom commands and override CreateCommand in my
ItemProviders to make sure that the appropriate custom command is called.
3) Combine 1 and 2 where needed

The only thing this doesn't resolve is communicating back to the command requester (e.g. the UI Action or Viewer or Editor) when a command is created that would violate some constraint. I suppose (as in the object name constraint above) this would only apply to those cases where I need to override the command. So I could just have my custom EditingDomain validate the affected objects of my custom commands and send messages back to the caller via a CommandValidationProvider/Listener interface. I suppose I'd have to add constraints to the model to do this then. I could also add a static method to my custom commands, requiresValidation() since it's possible that not all of them will.

So here's my v3 proposal:
1) Add constraints to the model
2) Specialize the ItemProviders' collectNewChildDescriptors and getPropertyDescriptors and make them stateful
3) Create a custom EditingDomain class that will validate all affected objects for only my custom commands that require it that it is asked to create before it returns the command. It will also implement a CommandValidationProvider/Listener interface so that the UI can listen, receive and process notifications when a command validation fails.
4) For complex constraints that I can't define in the eCore model, I will define custom commands and override CreateCommand in my ItemProviders to make sure that the appropriate custom command is called.
5) Combine 2 and 4 where needed

Any other thoughts on this?
BTW, thanks for all your input on this Very Happy

Greg
Re: Can I override/specialize an EFeature in a subclass? [message #523940 is a reply to message #523912] Mon, 29 March 2010 22:39 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Greg,

Comments below.

Greg wrote:
> Thanks Ed.
>
> You commented:
>> Probably the UI should never let you make a choice that would fail.
>
> I totally agree, however ideally the Edit plugin should never let the
> UI present a choice that would fail. There are some cases that are
> unavoidable however. For example, if my objects have a name property
> and I have a rule/constraint (whatever) that dictates that object
> names within their container must be unique then the UI (PropertySheet
> in this case) can't prevent the SetCommand from being requested.
The extended validation framework provide rollback support for this type
of thing. The problem is that constraints are generally expressed based
on the current state of the model, so the model has to be pushed into
that state to check them...
>
> I do like the idea of specializing the ItemProviders'
> collectNewChildDescriptors and getPropertyDescriptors. I'd probably
> have to use stateful ItemProviders then.
Not for these two cases because they depend only on the type of the object.
> I've actually done this in one case where I have an object with
> properties: dataType, lowValue, highValue; where the low and high
> values are instances of the specified dataType, which is a limited
> list of java object types. I can specialize my getPropertyDescriptors
> to regenerate when the dataType gets changed, in which case it will
> create a property descriptor for low and high value suitable for the
> new datatype. The problem is, what do I do with the current values of
> those properties. If the old dataType can be cast to the new dataType
> then it's not a problem. Otherwise I need to clear their values and
> let the user enter new valid ones. But where in my code do I clear
> the values? I could do it in the specialized getPropertyDescriptors,
> but if the user clicks undo then he doesn't get his old values back.
> I think this is a case where I'd need to specialize
> getPropertyDescriptors and override the SetCommand for dataType to
> make sure that the old values are recoverable in an undo.
I see.
>
> So I think that I'll hold off on the following for now:
> 1) Add constraints to the model
> 2) Create a custom EditingDomain class that will validate all affected
> objects of all commands it is asked to create before it returns the
> command.
I think that's challenging as I mentioned earlier, given that validation
is done against the actual state of the model.
>
> and do the following instead:
> 1) Specialize the ItemProviders' collectNewChildDescriptors and
> getPropertyDescriptors and make them stateful
I don't see you need state yet. Remember, there is one created per type.
> 2) For complex constraints that I can't define in the eCore model, I
> will define custom commands and override CreateCommand in my
> ItemProviders to make sure that the appropriate custom command is called.
> 3) Combine 1 and 2 where needed
>
> The only thing this doesn't resolve is communicating back to the
> command requester (e.g. the UI Action or Viewer or Editor) when a
> command is created that would violate some constraint. I suppose (as
> in the object name constraint above) this would only apply to those
> cases where I need to override the command. So I could just have my
> custom EditingDomain validate the affected objects of my custom
> commands and send messages back to the caller via a
> CommandValidationProvider/Listener interface. I suppose I'd have to
> add constraints to the model to do this then. I could also add a
> static method to my custom commands, requiresValidation() since it's
> possible that not all of them will.
You'll likely want to check out the extended validation framework.
>
> So here's my v3 proposal:
> 1) Add constraints to the model
> 2) Specialize the ItemProviders' collectNewChildDescriptors and
> getPropertyDescriptors and make them stateful
> 3) Create a custom EditingDomain class that will validate all affected
> objects for only my custom commands that require it that it is asked
> to create before it returns the command. It will also implement a
> CommandValidationProvider/Listener interface so that the UI can
> listen, receive and process notifications when a command validation
> fails.
> 4) For complex constraints that I can't define in the eCore model, I
> will define custom commands and override CreateCommand in my
> ItemProviders to make sure that the appropriate custom command is called.
> 5) Combine 2 and 4 where needed
>
> Any other thoughts on this? BTW, thanks for all your input on this :d
Sounds good overall.
>
> Greg


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Can I override/specialize an EFeature in a subclass? [message #523975 is a reply to message #523940] Tue, 30 March 2010 06:58 Go to previous messageGo to next message
Greg Mising name is currently offline Greg Mising nameFriend
Messages: 47
Registered: July 2009
Member
Thanks again for your comments on this. I'll definitely check out the extended validation framework.

I also ran across a good article on eCore generics ( http://eclipse.dzone.com/articles/generics-eclipse-modelling -fra?page=0,0), which gives me some insight on how I can model the specialization I'm looking for in the eCore model itself. I just wish that the code generators made better use of them, especially for the ItemProviders.

Thanks again.
Greg
Re: Can I override/specialize an EFeature in a subclass? [message #524056 is a reply to message #523975] Tue, 30 March 2010 08:46 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Greg,

I'm not sure specifically your comment, but if you have a simpler
example of that, that would be good:

https://bugs.eclipse.org/bugs/show_bug.cgi?id=289859



Greg wrote:
> Thanks again for your comments on this. I'll definitely check out the
> extended validation framework.
>
> I also ran across a good article on eCore generics
> ( http://eclipse.dzone.com/articles/generics-eclipse-modelling -fra?page=0,0),
> which gives me some insight on how I can model the specialization I'm
> looking for in the eCore model itself. I just wish that the code
> generators made better use of them, especially for the ItemProviders.
>
> Thanks again.
> Greg


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Can I override/specialize an EFeature in a subclass? [message #524079 is a reply to message #524056] Tue, 30 March 2010 15:22 Go to previous message
Greg Mising name is currently offline Greg Mising nameFriend
Messages: 47
Registered: July 2009
Member
Hi Ed.

I understand Xavier's concerns completely and in fact I spent many hours yesterday trying to find a good solution.

I do have a test case, which I think is pretty simple (based on the article I linked to). I also have a proposed solution for code generation for both of his concerns (type safety of the EList, and ItemProviders exposing inappropriate commands).

I'll post the details on the bug and upload my test case.

Cheers,
Greg
Previous Topic:Turning EMF into XML/XMI without using a resource
Next Topic:eCore generics: runtime values?
Goto Forum:
  


Current Time: Fri Apr 26 23:09:05 GMT 2024

Powered by FUDForum. Page generated in 0.04494 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top