Creating a DataType Instance in ETL [message #1850538] |
Tue, 08 March 2022 05:37 |
|
I have a simple EOL Program which attempts to assign an attribute that has in EDataType based on String. When I assign a String, the program gives me an error.
The value of type 'class java.lang.String' must be of type 'org.eclipse.emf.ecore.impl.EDataTypeImpl@769d9f42 (name: StringAsDatatype) (instanceClassName: String) (serializable: true)'
I would like to know how I can make this assignment. I have a feeling this has worked in the past. Something like this:
datatype StringAsDatatype: String;
class Entity {
attr StringAsDatatype attribute;
}
[Updated on: Tue, 08 March 2022 05:46] Report message to a moderator
|
|
|
Re: Creating a DataType Instance in ETL [message #1850540 is a reply to message #1850538] |
Tue, 08 March 2022 08:18 |
|
Hi Jörn,
Instances of EDataTypes need to be created explicitly before they can be set as EAttribute values. There should be an easier way to do this but at the moment you'd need to use something like the following
var event : new Event;
event.setDate("2022-03-08");
event.date.println();
operation Event setDate(date : String) {
var feature = event.eClass().getEStructuralFeature("date");
var dt = feature.getEType();
event.eSet(feature, dt.getEPackage().getEFactoryInstance().createFromString(dt, date));
}
to set the date of an Event conforming to the metamodel below
@namespace(uri="events")
package events;
class Event {
attr EDate ~date;
}
Best,
Dimitris
|
|
|
Re: Creating a DataType Instance in ETL [message #1850580 is a reply to message #1850540] |
Wed, 09 March 2022 00:18 |
|
Thanks. I had seen a similar approach somewhere in the forum but could not use it, as it seems to require
being implemented, which is not the case for models loaded reflectively.
I will have a look if I can construct the underlying instance object and then supply it.
The approach provided exposes a lot Ecore internals. That does not feel consistent with EMC aiming to keep those things out of view. Is that simply a feature that is missing in the EMF EMC provider?
It seems that for subtypes of standard eTypes, it should be possible to check the Java instance type for and then make a reflective assignment. In fact, this approach should be generally applicable if users create assignment values.
Opinion is very welcome. I think this a common use case, given that a lot of the bigger MOF-derived metamodels published by the OMG contain datatypes a lot. This makes application of ETL to such models quite complex and adds a fair bit of boilerplate.
[Updated on: Wed, 09 March 2022 00:24] Report message to a moderator
|
|
|
|
Re: Creating a DataType Instance in ETL [message #1850587 is a reply to message #1850581] |
Wed, 09 March 2022 08:32 |
|
I agree that the workaround I proposed is not ideal. To better support this scenario, I'd be inclined to override IModel's createInstance(type, parameters) method in AbstractEmfModel so that it creates instances of EDataTypes too. In this way, we could write code like below and avoid an implicit conversion upon assignment (which e.g. wouldn't work as well for multi-valued attributes of custom EDataTypes).
var event : new Event;
event.date = new EDate("2022-03-08");
event.date.println();
Best,
Dimitris
|
|
|
Re: Creating a DataType Instance in ETL [message #1850593 is a reply to message #1850587] |
Wed, 09 March 2022 11:59 |
|
That looks really intuitive and does not break the properties of the EMC layer either. Is that a big job? I am worried what happens with types that have larger-arity constructors? Can this deal with something like this? What parameters can you pass?
var lectureTheater : new Building;
lectureTheater.dimensions = new EBox(3,7,4);
[Updated on: Wed, 09 March 2022 12:01] Report message to a moderator
|
|
|
Re: Creating a DataType Instance in ETL [message #1850599 is a reply to message #1850593] |
Wed, 09 March 2022 14:50 |
|
Multiple/complex parameters shouldn't be a problem as IModel's createInstance(...) receives a collection of objects as parameters. It's not a huge job but it changes AbstractEmfModel's core assumption that only EClasses are exposed as EMC types so there are a few more methods that we'd have to update accordingly e.g.
new EDate("2022-03-08").isTypeOf(EDate); // Should now return true
EDate.getAllOfType(); // Should this throw an unsupported operation exception, or attempt to collect all EDates anywhere in the model?
// Possibly more methods that don't come to mind immediately
Best,
Dimitris
[Updated on: Wed, 09 March 2022 16:41] Report message to a moderator
|
|
|
Re: Creating a DataType Instance in ETL [message #1850609 is a reply to message #1850599] |
Wed, 09 March 2022 20:49 |
|
Good to hear about parameters. I would expect that fragment to return a set, over all values used, given that datatypes do not have identity. So we need to assume that if their underlying java equals and hashcode indicate they are the same, there should not be a duplicate in the result.
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03817 seconds