Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » How to implement an interface in Ecore Model Editor
How to implement an interface in Ecore Model Editor [message #1006635] Fri, 01 February 2013 09:45 Go to next message
Anders  Jönsson is currently offline Anders Jönsson
Messages: 33
Registered: January 2012
Member
Hi,
How can I make an EMF class implement an external interface using the "Sample Ecore Model Editor"?

I have a modeled class Event and an external interface Time. I want Event to implement Time.
Using the "Sample Ecore Model Editor" I have added an EAnnotation (GenModel) with the Source attribute set to http://www.eclipse.org/emf/2002/GenModel. It has got a Details entry with Key = documentation and Value = @extends com.riiplan.time.Time (also tried @implements com.riiplan.time.Time).

This results in the following lines in the .ecore file:
<eClassifiers xsi:type="ecore:EClass" name="Event">
<eAnnotations source="http://www.eclipse.org/emf/2002/GenModel">
<details key="documentation" value="@extends com.riiplan.time.Time"/>
</eAnnotations>

Which, in turn, results in the following generated java interface:
/**
* <!-- begin-user-doc -->
* A representation of the model object '<em><b>Event</b></em>'.
* <!-- end-user-doc -->
*
* <!-- begin-model-doc -->
* @extends com.riiplan.time.Time
* <!-- end-model-doc -->
*
... some stuff deleted here...
* @generated
*/
public interface Event<T extends Time> extends CDOObject {

BUT I was expecting:
public interface Event<T extends Time> extends CDOObject, Time {

///Anders
Re: How to implement an interface in Ecore Model Editor [message #1006648 is a reply to message #1006635] Fri, 01 February 2013 10:20 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26141
Registered: July 2009
Senior Member
Anders,

Comments below.

On 01/02/2013 10:45 AM, Anders Jönsson wrote:
> Hi,
> How can I make an EMF class implement an external interface using the
> "Sample Ecore Model Editor"?
>
> I have a modeled class Event and an external interface Time. I want
> Event to implement Time.
Is it okay that Even itself extends Time?
> Using the "Sample Ecore Model Editor" I have added an EAnnotation
> (GenModel) with the Source attribute set to
> http://www.eclipse.org/emf/2002/GenModel. It has got a Details entry
> with Key = documentation and Value = @extends com.riiplan.time.Time
> (also tried @implements com.riiplan.time.Time).
>
> This results in the following lines in the .ecore file:
> <eClassifiers xsi:type="ecore:EClass" name="Event">
> <eAnnotations source="http://www.eclipse.org/emf/2002/GenModel">
> <details key="documentation" value="@extends
> com.riiplan.time.Time"/>
> </eAnnotations>
>
> Which, in turn, results in the following generated java interface:
> /**
> * <!-- begin-user-doc -->
> * A representation of the model object '<em><b>Event</b></em>'.
> * <!-- end-user-doc -->
> *
> * <!-- begin-model-doc -->
> * @extends com.riiplan.time.Time
> * <!-- end-model-doc -->
> *
> .. some stuff deleted here...
> * @generated
> */
> public interface Event<T extends Time> extends CDOObject {
>
> BUT I was expecting:
> public interface Event<T extends Time> extends CDOObject, Time {
I don't think the merger looks at the Javadoc after the substitution
merging but before.

A different approach would be to define an EClass that wraps (has
instance type name) com.riiplan.time.Time, setting it to
interface/abstract true, and then using that EClass in your super types
for Event. You might also want to explicitly model all the methods on
Time as EOperations so that the generator will generate stubs for them.
>
> ///Anders
Re: How to implement an interface in Ecore Model Editor [message #1006718 is a reply to message #1006648] Fri, 01 February 2013 14:47 Go to previous messageGo to next message
Anders  Jönsson is currently offline Anders Jönsson
Messages: 33
Registered: January 2012
Member
I created an EMF interface with instance type name com.riiplan.time.Time, as advised. But, as you can see from my class:

public interface Event<T extends Time> extends CDOObject, Time {

I also use Time as a bounds for a generic type T. Therefore Time was already modeled as a DataType. There cannot be two entities with name Time so I deleted the DataType-Time and substituted it for the new interface-Time.

But this fails. I have an attribute in Event, called timePoint, typed as T. This gives an exception in the get method of EventImpl:
public T getTimePoint() {
return (T)eGet(EventPackage.Literals.EVENT__TIME_POINT, true);
}

The Exception is:
java.lang.ClassCastException: com.riiplan.time.impl.ImmutableTimeOfDay cannot be cast to org.eclipse.emf.ecore.InternalEObject
at org.eclipse.emf.ecore.impl.EStructuralFeatureImpl$InternalSettingDelegateSingleEObject.dynamicGet(EStructuralFeatureImpl.java:2556)
at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eGet(BasicEObjectImpl.java:1027)
at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eGet(BasicEObjectImpl.java:1011)
at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eGet(BasicEObjectImpl.java:1003)
at com.riiplan.mdl.event.impl.EventImpl.getTimePoint(EventImpl.java:162)
at com.riiplan.mdl.event.impl.EventImpl.getTime(EventImpl.java:180)

I am confused. Will backtrack to Time being a DataType and make Event implement Time by hand.


>Is it okay that Event itself extends Time?
No!

///Anders
Re: How to implement an interface in Ecore Model Editor [message #1006723 is a reply to message #1006718] Fri, 01 February 2013 15:09 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26141
Registered: July 2009
Senior Member
Anders,

Comments below.

On 01/02/2013 3:47 PM, Anders Jönsson wrote:
> I created an EMF interface with instance type name
> com.riiplan.time.Time, as advised. But, as you can see from my class:
>
> public interface Event<T extends Time> extends CDOObject, Time {
>
> I also use Time as a bounds for a generic type T. Therefore Time was
> already modeled as a DataType.
You could model it either way, or both ways...
> There cannot be two entities with name Time
No, but the data type name doesn't matter, nor does the name for the
wrapper EClass.
> so I deleted the DataType-Time and substituted it for the new
> interface-Time.
>
> But this fails. I have an attribute in Event, called timePoint, typed
> as T.
Yes, you'd want an EDataType for that. You can have both an EDataType
wrapper and an EClass wrapper; they must have different names but can
wrap the same instance type.
> This gives an exception in the get method of EventImpl:
> public T getTimePoint() {
> return (T)eGet(EventPackage.Literals.EVENT__TIME_POINT, true);
> }
>
> The Exception is:
> java.lang.ClassCastException: com.riiplan.time.impl.ImmutableTimeOfDay
> cannot be cast to org.eclipse.emf.ecore.InternalEObject
> at
> org.eclipse.emf.ecore.impl.EStructuralFeatureImpl$InternalSettingDelegateSingleEObject.dynamicGet(EStructuralFeatureImpl.java:2556)
> at
> org.eclipse.emf.ecore.impl.BasicEObjectImpl.eGet(BasicEObjectImpl.java:1027)
> at
> org.eclipse.emf.ecore.impl.BasicEObjectImpl.eGet(BasicEObjectImpl.java:1011)
> at
> org.eclipse.emf.ecore.impl.BasicEObjectImpl.eGet(BasicEObjectImpl.java:1003)
> at
> com.riiplan.mdl.event.impl.EventImpl.getTimePoint(EventImpl.java:162)
> at com.riiplan.mdl.event.impl.EventImpl.getTime(EventImpl.java:180)
>
> I am confused. Will backtrack to Time being a DataType and make Event
> implement Time by hand.
I wonder now if what you're doing makes sense. Does time define a
meaning for equals?
>
>
>> Is it okay that Event itself extends Time?
> No!
What's what you're showing though:

public interface Event<T extends Time> extends CDOObject, Time {

I would imagine that an Event should have a Time, not be a Time,
especially given that you want Time to be a data value and likely want
to define hashCode and equals for it, which are methods you should never
override in your modeled object implementation classes.
>
> ///Anders
Re: How to implement an interface in Ecore Model Editor [message #1007085 is a reply to message #1006723] Mon, 04 February 2013 11:50 Go to previous messageGo to next message
Anders  Jönsson is currently offline Anders Jönsson
Messages: 33
Registered: January 2012
Member
Ed,

Modelling com.riiplan.time.Time both as a data type and as an interface but with different names solves my original problem. Thanks a lot!

Thanks also for commenting on the design. I will consider your input carefully. One note though; Time IS NOT a data type. It is an interface that is implemented by Event and also by other data types (e.g. TimePoint) in order to simplify reuse of other time related structures. It is (now) modeled as a data type only because <T extends Time> didn't work for Time as an interface.
(careful readers of this thread please disregard from the fact that Time was a data type even before I knew it couldn't be modeled as an interface).

Regards
///Anders



Re: How to implement an interface in Ecore Model Editor [message #1007087 is a reply to message #1007085] Mon, 04 February 2013 12:02 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26141
Registered: July 2009
Senior Member
Anders,

Note that if you are using the EClass wrapper for Time in T extends Time
you'd want to use T do define an EReference rather than an EAttribute.
I'm not sure if that's what you want, but if you have modeled objects
that implement Time and you want to be able to serialize references to
those, you really do want an EReference for that...


On 04/02/2013 12:50 PM, Anders Jönsson wrote:
> Ed,
>
> Modelling com.riiplan.time.Time both as a data type and as an
> interface but with different names solves my original problem. Thanks
> a lot!
>
> Thanks also for commenting on the design. I will consider your input
> carefully. One note though; Time IS NOT a data type. It is an
> interface that is implemented by Event and also by other data types
> (e.g. TimePoint) in order to simplify reuse of other time related
> structures. It is (now) modeled as a data type only because <T extends
> Time> didn't work for Time as an interface.
> (careful readers of this thread please disregard from the fact that
> Time was a data type even before I knew it couldn't be modeled as an
> interface).
>
> Regards ///Anders
>
>
>
>
Re: How to implement an interface in Ecore Model Editor [message #1007094 is a reply to message #1007087] Mon, 04 February 2013 12:41 Go to previous messageGo to next message
Anders  Jönsson is currently offline Anders Jönsson
Messages: 33
Registered: January 2012
Member
I see...

1. I will probably change my design so that there is a clear distinction between my data types (like TimePoint) and my "classes" like Events. Will probably let Events implement something like TimeEvent instead. I REALLY appreciate you taking the time to point these things out.

2. Just to see if I got it right: When it comes to persistens/serialization you cannot have an interface that is implemented by both an EClass and an EDataType because:
- An EClass cannot be serialized as an EAttribute?
- You cannot have an EReference to an EDataType at all?

///Anders
Re: How to implement an interface in Ecore Model Editor [message #1007108 is a reply to message #1007094] Mon, 04 February 2013 13:36 Go to previous message
Ed Merks is currently offline Ed Merks
Messages: 26141
Registered: July 2009
Senior Member
Andrers,

Comments below.

On 04/02/2013 1:41 PM, Anders Jönsson wrote:
> I see...
>
> 1. I will probably change my design so that there is a clear
> distinction between my data types (like TimePoint) and my "classes"
> like Events.
Yes, you want to think carefully about that. Instances of EDataTypes
should generally be immutable value objects; they'll typically
specialize hashCode and equals. Instances of EClass are mutable and
notify when they mutate; they should not specialize hashCode and equals.
> Will probably let Events implement something like TimeEvent instead. I
> REALLY appreciate you taking the time to point these things out.
>
> 2. Just to see if I got it right: When it comes to
> persistens/serialization you cannot have an interface that is
> implemented by both an EClass and an EDataType because:
You can, but it probably means you're doing something wrong or at least
very unusual.
> - An EClass cannot be serialized as an EAttribute?
It could, but you lose "identity" and it only works if you write the
createFromString/convertToString logic. It doesn't make a lot of sense
and is most likely wrong.
> - You cannot have an EReference to an EDataType at all?
No. The generic type of an EReference must erase to an EClass and the
generic type of an EAttribute must erase to an EDataType. In some
places where you want either (the ChangeModel's FeatureChange is a good
example) you'd have to model both an attribute and a reference...
>
> ///Anders
>
Previous Topic:Client-/Server Architecture with Server-Side EMF Model
Next Topic:Copy EObject with eOpposite refs
Goto Forum:
  


Current Time: Sat Oct 25 17:31:31 GMT 2014

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

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