Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » M2M (model-to-model transformation) » [QVTO] object creation, using EMF reflection API.
[QVTO] object creation, using EMF reflection API. [message #546566] Tue, 13 July 2010 11:31 Go to next message
Christophe Bouhier is currently offline Christophe BouhierFriend
Messages: 937
Registered: July 2009
Senior Member
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;
}
Re: [QVTO] object creation, using EMF reflection API. [message #547096 is a reply to message #546566] Thu, 15 July 2010 11:49 Go to previous messageGo to next message
Sergey Boyko is currently offline Sergey BoykoFriend
Messages: 171
Registered: July 2009
Senior Member
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;
> }
Re: [QVTO] object creation, using EMF reflection API. [message #547350 is a reply to message #547096] Fri, 16 July 2010 12:30 Go to previous messageGo to next message
Christophe Bouhier is currently offline Christophe BouhierFriend
Messages: 937
Registered: July 2009
Senior Member
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;
>> }
Re: [QVTO] object creation, using EMF reflection API. [message #547376 is a reply to message #547350] Fri, 16 July 2010 13:38 Go to previous messageGo to next message
Christophe Bouhier is currently offline Christophe BouhierFriend
Messages: 937
Registered: July 2009
Senior Member
Sergey,

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.


<extension point="org.eclipse.m2m.qvt.oml.javaBlackboxUnits">
<unit name="ExampleJavaLib" namespace="m2m.qvt.oml">
<library name="UtilLib"

class="org.eclipse.m2m.qvt.oml.examples.blackbox.UtilitiesLibrary ">
<metamodel nsURI="http://www.eclipse.org/emf/2002/Ecore"/>
</library>
</unit>
</extension>
"


So where do

m2m.qvt.oml.ExampleJavaLib

and

UtilLib

go in the .qvto file?

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;
>>> }
Re: [QVTO] object creation, using EMF reflection API. [message #547743 is a reply to message #547096] Mon, 19 July 2010 14:07 Go to previous messageGo to next message
Christophe Bouhier is currently offline Christophe BouhierFriend
Messages: 937
Registered: July 2009
Senior Member
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();


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;
>> }
Re: [QVTO] object creation, using EMF reflection API. [message #547753 is a reply to message #547743] Mon, 19 July 2010 14:22 Go to previous messageGo to next message
Sergey Boyko is currently offline Sergey BoykoFriend
Messages: 171
Registered: July 2009
Senior Member
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
>
>
Re: [QVTO] object creation, using EMF reflection API. [message #547987 is a reply to message #547753] Tue, 20 July 2010 10:29 Go to previous message
Christophe Bouhier is currently offline Christophe BouhierFriend
Messages: 937
Registered: July 2009
Senior Member
Hi Sergey,


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;

storeIndex := storeIndex + 1;
pos := pos + 1;
};
}

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
>>
>>
Previous Topic:[QVTO] ClassNotFound in blackbox
Next Topic:[ATL] How to right such a rule down (see picture)
Goto Forum:
  


Current Time: Tue Mar 19 10:57:41 GMT 2024

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

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

Back to the top