Eclipse Community Forums - RDF feed
https://www.eclipse.org/forums/
Eclipse Community Forums[QVTO] object creation, using EMF reflection API.
https://www.eclipse.org/forums/index.php/mv/msg/171821/546566/#msg_546566
I am wondering if somehow in QVTO, I can create an object in a
Dynamic/reflective fashion.
So, Can I substitute this:
var c : Company;
object c: Company {
name := self.name;
}
with
var c : [somecommand]"string representing the type";
object c: Company {
name := self.name;
}]]>Christophe Bouhier2010-07-13T11:31:45-00:00Re: [QVTO] object creation, using EMF reflection API.
https://www.eclipse.org/forums/index.php/mv/msg/171821/547096/#msg_547096
Directly this is not supported by QVTo.
You can simulate such behavior by means of Java black-box. For example,
provided in black-box library the following method:
main() {
var instance : UML::Class := map
instantiateUmlElement("Class").oclAsType(UML::Class);
}
mapping instantiateUmlElement(in metaclass : String) : UML::Element {
init {
var umlMetaclass := map
umlPackage().getEClassifier(metaclass).oclAsType(ECORE::ECla ss);
if (umlMetaclass.oclIsUndefined()) then {
result := null;
}
else {
result := map
umlPackage().eFactoryInstance.create(umlMetaclass).oclAsType (UML::Element);
} endif;
}
}
-- uml package access singleton, a mapping executed only once
mapping umlPackage() : ECORE::EPackage {
init {
var helperPackage := object UML::Package {};
result := helperPackage.getEClass().ePackage;
e.removeElement(helperPackage);
}
}
Regards,
Sergey
Christophe Bouhier wrote:
> Hi,
>
> I am wondering if somehow in QVTO, I can create an object in a
> Dynamic/reflective fashion.
>
>
> So, Can I substitute this:
>
> var c : Company;
> object c: Company {
> name := self.name;
> }
>
>
> with
>
> var c : [somecommand]"string representing the type";
> object c: Company {
> name := self.name;
> }]]>Sergey Boyko2010-07-15T11:49:50-00:00Re: [QVTO] object creation, using EMF reflection API.
https://www.eclipse.org/forums/index.php/mv/msg/171821/547350/#msg_547350
Thank you, I my recent two posts I discover using black-box.
Thank you for showing, that it is possible to use the EFactory.create
and instantiate an object, this is really cool.
Cheers Christophe
Sergey Boyko wrote:
> Hi Christophe ,
>
> Directly this is not supported by QVTo.
>
> You can simulate such behavior by means of Java black-box. For example,
> provided in black-box library the following method:
>
> @Operation (kind=Kind.OPERATION, contextual = true)
> public static EClass getEClass(EModelElement object) {
> return object.eClass();
> }
>
> it can be used in script like follows:
>
> modeltype UML uses "http://www.eclipse.org/uml2/3.0.0/UML";
> modeltype ECORE uses "http://www.eclipse.org/emf/2002/Ecore";
>
> transformation Main(inout e:UML);
>
> main() {
> var cls : EClass := object UML::Class{}.getEClass();
> var concreteClass : UML::Class :=
> cls.ePackage.eFactoryInstance.create(cls).oclAsType(UML::Cla ss);
> }
>
>
>
> More sophisticated script example (using provided getEClass() black-box
> method):
>
> modeltype UML uses "http://www.eclipse.org/uml2/3.0.0/UML";
> modeltype ECORE uses "http://www.eclipse.org/emf/2002/Ecore";
>
> transformation Main(inout e:UML);
>
> main() {
> var instance : UML::Class := map
> instantiateUmlElement("Class").oclAsType(UML::Class);
> }
>
> mapping instantiateUmlElement(in metaclass : String) : UML::Element {
> init {
> var umlMetaclass := map
> umlPackage().getEClassifier(metaclass).oclAsType(ECORE::ECla ss);
> if (umlMetaclass.oclIsUndefined()) then {
> result := null;
> }
> else {
> result := map
> umlPackage().eFactoryInstance.create(umlMetaclass).oclAsType (UML::Element);
> } endif;
> }
> }
>
> -- uml package access singleton, a mapping executed only once
> mapping umlPackage() : ECORE::EPackage {
> init {
> var helperPackage := object UML::Package {};
> result := helperPackage.getEClass().ePackage;
> e.removeElement(helperPackage);
> }
> }
>
>
> Regards,
> Sergey
>
>
> Christophe Bouhier wrote:
>> Hi,
>>
>> I am wondering if somehow in QVTO, I can create an object in a
>> Dynamic/reflective fashion.
>>
>>
>> So, Can I substitute this:
>>
>> var c : Company;
>> object c: Company {
>> name := self.name;
>> }
>>
>>
>> with
>>
>> var c : [somecommand]"string representing the type";
>> object c: Company {
>> name := self.name;
>> }]]>Christophe Bouhier2010-07-16T12:30:22-00:00Re: [QVTO] object creation, using EMF reflection API.
https://www.eclipse.org/forums/index.php/mv/msg/171821/547376/#msg_547376
I am struggling to make the java library available in my QVTo.
What would be the syntax to import the library in qvto?
(I see in the extension description this, but not sure how it works).
"Examples:
The following examples registers the ExampleJavaLib black-box unit,
which is resolvable as m2m.qvt.oml.ExampleJavaLib from importing QVT
modules.
It defines the QVT library resolveable by name UtilLib, implemented in
the org.eclipse.m2m.qvt.oml.examples.blackbox.UtilitiesLibrary class.
The library operation reference types defined in the Ecore metamodel.
I also pressume the plugin containing the library and javaBlackboxUnits,
should be in runtime to be resolved correct?
rgds Christophe
Christophe Bouhier wrote:
> Hi Sergey,
>
> Thank you, I my recent two posts I discover using black-box.
>
> Thank you for showing, that it is possible to use the EFactory.create
> and instantiate an object, this is really cool.
>
> Cheers Christophe
>
>
>
> Sergey Boyko wrote:
>> Hi Christophe ,
>>
>> Directly this is not supported by QVTo.
>>
>> You can simulate such behavior by means of Java black-box. For
>> example, provided in black-box library the following method:
>>
>> @Operation (kind=Kind.OPERATION, contextual = true)
>> public static EClass getEClass(EModelElement object) {
>> return object.eClass();
>> }
>>
>> it can be used in script like follows:
>>
>> modeltype UML uses "http://www.eclipse.org/uml2/3.0.0/UML";
>> modeltype ECORE uses "http://www.eclipse.org/emf/2002/Ecore";
>>
>> transformation Main(inout e:UML);
>>
>> main() {
>> var cls : EClass := object UML::Class{}.getEClass();
>> var concreteClass : UML::Class :=
>> cls.ePackage.eFactoryInstance.create(cls).oclAsType(UML::Cla ss);
>> }
>>
>>
>>
>> More sophisticated script example (using provided getEClass()
>> black-box method):
>>
>> modeltype UML uses "http://www.eclipse.org/uml2/3.0.0/UML";
>> modeltype ECORE uses "http://www.eclipse.org/emf/2002/Ecore";
>>
>> transformation Main(inout e:UML);
>>
>> main() {
>> var instance : UML::Class := map
>> instantiateUmlElement("Class").oclAsType(UML::Class);
>> }
>>
>> mapping instantiateUmlElement(in metaclass : String) : UML::Element {
>> init {
>> var umlMetaclass := map
>> umlPackage().getEClassifier(metaclass).oclAsType(ECORE::ECla ss);
>> if (umlMetaclass.oclIsUndefined()) then {
>> result := null;
>> }
>> else {
>> result := map
>> umlPackage().eFactoryInstance.create(umlMetaclass).oclAsType (UML::Element);
>>
>> } endif;
>> }
>> }
>>
>> -- uml package access singleton, a mapping executed only once
>> mapping umlPackage() : ECORE::EPackage {
>> init {
>> var helperPackage := object UML::Package {};
>> result := helperPackage.getEClass().ePackage;
>> e.removeElement(helperPackage);
>> }
>> }
>>
>>
>> Regards,
>> Sergey
>>
>>
>> Christophe Bouhier wrote:
>>> Hi,
>>>
>>> I am wondering if somehow in QVTO, I can create an object in a
>>> Dynamic/reflective fashion.
>>>
>>>
>>> So, Can I substitute this:
>>>
>>> var c : Company;
>>> object c: Company {
>>> name := self.name;
>>> }
>>>
>>>
>>> with
>>>
>>> var c : [somecommand]"string representing the type";
>>> object c: Company {
>>> name := self.name;
>>> }]]>Christophe Bouhier2010-07-16T13:38:29-00:00Re: [QVTO] object creation, using EMF reflection API.
https://www.eclipse.org/forums/index.php/mv/msg/171821/547743/#msg_547743
In my case any arbitrary EClass object from my ecore model should be
recognized (Casted down to EModelElement) by the blackbox contextual
operation. However it doesn't,
so I can't call getEClass from a created object like this, I get a
syntax error.
var cls : EClass := object NETW::Company{}.getEclass();
I don't understand why this would work for the UML example you provide,
as it's just another ecore model.
Am I doing anything wrong here?
cheers Christophe
Sergey Boyko wrote:
> Hi Christophe ,
>
> Directly this is not supported by QVTo.
>
> You can simulate such behavior by means of Java black-box. For example,
> provided in black-box library the following method:
>
> @Operation (kind=Kind.OPERATION, contextual = true)
> public static EClass getEClass(EModelElement object) {
> return object.eClass();
> }
>
> it can be used in script like follows:
>
> modeltype UML uses "http://www.eclipse.org/uml2/3.0.0/UML";
> modeltype ECORE uses "http://www.eclipse.org/emf/2002/Ecore";
>
> transformation Main(inout e:UML);
>
> main() {
> var cls : EClass := object UML::Class{}.getEClass();
> var concreteClass : UML::Class :=
> cls.ePackage.eFactoryInstance.create(cls).oclAsType(UML::Cla ss);
> }
>
>
>
> More sophisticated script example (using provided getEClass() black-box
> method):
>
> modeltype UML uses "http://www.eclipse.org/uml2/3.0.0/UML";
> modeltype ECORE uses "http://www.eclipse.org/emf/2002/Ecore";
>
> transformation Main(inout e:UML);
>
> main() {
> var instance : UML::Class := map
> instantiateUmlElement("Class").oclAsType(UML::Class);
> }
>
> mapping instantiateUmlElement(in metaclass : String) : UML::Element {
> init {
> var umlMetaclass := map
> umlPackage().getEClassifier(metaclass).oclAsType(ECORE::ECla ss);
> if (umlMetaclass.oclIsUndefined()) then {
> result := null;
> }
> else {
> result := map
> umlPackage().eFactoryInstance.create(umlMetaclass).oclAsType (UML::Element);
> } endif;
> }
> }
>
> -- uml package access singleton, a mapping executed only once
> mapping umlPackage() : ECORE::EPackage {
> init {
> var helperPackage := object UML::Package {};
> result := helperPackage.getEClass().ePackage;
> e.removeElement(helperPackage);
> }
> }
>
>
> Regards,
> Sergey
>
>
> Christophe Bouhier wrote:
>> Hi,
>>
>> I am wondering if somehow in QVTO, I can create an object in a
>> Dynamic/reflective fashion.
>>
>>
>> So, Can I substitute this:
>>
>> var c : Company;
>> object c: Company {
>> name := self.name;
>> }
>>
>>
>> with
>>
>> var c : [somecommand]"string representing the type";
>> object c: Company {
>> name := self.name;
>> }]]>Christophe Bouhier2010-07-19T14:07:15-00:00Re: [QVTO] object creation, using EMF reflection API.
https://www.eclipse.org/forums/index.php/mv/msg/171821/547753/#msg_547753
Some replies are in-lined below.
Regards,
Sergey
Christophe Bouhier wrote:
> Hi Sergey,
>
>
> In my case any arbitrary EClass object from my ecore model should be
> recognized (Casted down to EModelElement) by the blackbox contextual
> operation. However it doesn't,
> so I can't call getEClass from a created object like this, I get a
> syntax error.
>
>
> var cls : EClass := object NETW::Company{}.getEclass();
The reason is that UML root metaclass (UML::Element) extends from
EModelElement. You should provide your own context type.
Excuse me for providing such metamodel-sensitive example.
>
>
> I don't understand why this would work for the UML example you provide,
> as it's just another ecore model.
>
> Am I doing anything wrong here?
> cheers Christophe
>
>]]>Sergey Boyko2010-07-19T14:22:42-00:00Re: [QVTO] object creation, using EMF reflection API.
https://www.eclipse.org/forums/index.php/mv/msg/171821/547987/#msg_547987
I managed to get the meta object for my DSL objects.
What I didn't managed to do is to set the attributes values and have an
instantiated object with the attribute values set. I have tried with
blackbox passing my object and setting the attributes, but without
success. Getting a bit frustrated, I decided to code it manually and
post it here, and see how this could be translated using the meta class
to set attributes:
// Map to a Company object, set the attributes.
mapping XLS::RowType::toCompany() : NETW::Company {
var values : Sequence(String) = self.cell->toCell();
log(' MAPPING: VALUES', values);
// For each cell we should check the index.
var set : Sequence(CellType) = self.cell->asSequence();
var pos : Integer := 1;
var storeIndex : Integer := 1;
while(pos <= set->size()){
var c : CellType = set->at(pos);
if( not c.index.oclIsUndefined() ) then {
storeIndex := c.index; // find the relevant attribute from the index.
log(' START CELL INDEX:', storeIndex);
} else {
log(' NEXT CELL INDEX:', storeIndex);
}
endif;
var featureName : String := this.currentHeaders->at(storeIndex);
log(' FEATURE: ', featureName);
if(storeIndex = 1) then { result.rSSurl := values->at(pos) } endif;
if(storeIndex = 2) then { result.website := values->at(pos) } endif;
if(storeIndex = 3) then { result.name := values->at(pos) } endif;
if(storeIndex = 4) then { result.shortName := values->at(pos) } endif;
if(storeIndex = 5) then { result.symbol := values->at(pos) } endif;
As you can see, I have an index, which tells me which attribute position
I should look for. I was hoping to somehow do this reflectively.
cheers Christophe
Sergey Boyko wrote:
> Hi Christophe ,
>
> Some replies are in-lined below.
>
> Regards,
> Sergey
>
>
> Christophe Bouhier wrote:
>> Hi Sergey,
>>
>>
>> In my case any arbitrary EClass object from my ecore model should be
>> recognized (Casted down to EModelElement) by the blackbox contextual
>> operation. However it doesn't,
>> so I can't call getEClass from a created object like this, I get a
>> syntax error.
>>
>>
>> var cls : EClass := object NETW::Company{}.getEclass();
>
> The reason is that UML root metaclass (UML::Element) extends from
> EModelElement. You should provide your own context type.
>
> Excuse me for providing such metamodel-sensitive example.
>
>>
>>
>> I don't understand why this would work for the UML example you
>> provide, as it's just another ecore model.
>>
>> Am I doing anything wrong here?
>> cheers Christophe
>>
>>]]>Christophe Bouhier2010-07-20T10:29:30-00:00