Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Epsilon » Create new element from a type using variable by EOL(Create new element from a type using variable by EOL)
Create new element from a type using variable by EOL [message #1780552] Tue, 23 January 2018 20:54 Go to next message
taghreed altamimi is currently offline taghreed altamimiFriend
Messages: 184
Registered: October 2014
Senior Member
Hi ,
I have the following code :
var vprocessor= lqnmodel!processor;
 var p: new vprocessor;
 p.name="Processor1";


processor is a type in my model which is lqnmodel
My question is : is there any trick to use the type as variable to create a new element type processor as in the code above var p: new vprocessor;
instead of using it directly meaning
var p: new  lqnmodel!processor;


Thanks,
Taghreed
Re: Create new element from a type using variable by EOL [message #1780554 is a reply to message #1780552] Tue, 23 January 2018 21:06 Go to previous messageGo to next message
Dimitris Kolovos is currently offline Dimitris KolovosFriend
Messages: 2154
Registered: July 2009
Location: York, UK
Senior Member

Hi Taghreed,

I'm not sure I fully understand what you're asking but in case it helps, you can also create an instance of processor as follows:

lqnmodel.createInstance("processor");

Does this help?

Cheers,
Dimitris
Re: Create new element from a type using variable by EOL [message #1780557 is a reply to message #1780554] Tue, 23 January 2018 21:42 Go to previous messageGo to next message
taghreed altamimi is currently offline taghreed altamimiFriend
Messages: 184
Registered: October 2014
Senior Member
i want to save type "processor" in a variable then create new instance using that variable because in the code that i am writing type received from an operation called getType().

vType =getType() that returns for example type lqnmodel!processor then according to that i want to create an instance from that type like var p: new vType.
So i do not want to use the type directly i want to save it in a variable then using it.

Hope its more clear now .

Thanks,
Taghreed
Re: Create new element from a type using variable by EOL [message #1780558 is a reply to message #1780557] Tue, 23 January 2018 21:44 Go to previous messageGo to next message
Dimitris Kolovos is currently offline Dimitris KolovosFriend
Messages: 2154
Registered: July 2009
Location: York, UK
Senior Member

Hi Taghreed,

If you've stored the name of the type in question in a variable called vType, you can create an instance using lqnmodel.createInstance(vType).

Cheers,
Dimitris
Re: Create new element from a type using variable by EOL [message #1780559 is a reply to message #1780558] Tue, 23 January 2018 21:52 Go to previous messageGo to next message
taghreed altamimi is currently offline taghreed altamimiFriend
Messages: 184
Registered: October 2014
Senior Member
Hi Dimitris,
How can i use this way to create an instance?? i tried and did not work
var vtype= "lqnmodel!processor";
var p: new vtype;
Re: Create new element from a type using variable by EOL [message #1780560 is a reply to message #1780559] Tue, 23 January 2018 21:53 Go to previous messageGo to next message
Dimitris Kolovos is currently offline Dimitris KolovosFriend
Messages: 2154
Registered: July 2009
Location: York, UK
Senior Member

Hi Taghreed,

The following should do the trick:

var vtype = "processor";
var p = lqnmodel.createInstance(vtype);


Cheers,
Dimitris
Re: Create new element from a type using variable by EOL [message #1780561 is a reply to message #1780560] Tue, 23 January 2018 21:56 Go to previous messageGo to next message
taghreed altamimi is currently offline taghreed altamimiFriend
Messages: 184
Registered: October 2014
Senior Member
Hi Dimitris,
Ok thanks very much,

Cheers,
Taghreed
Re: Create new element from a type using variable by EOL [message #1780562 is a reply to message #1780561] Tue, 23 January 2018 22:03 Go to previous messageGo to next message
taghreed altamimi is currently offline taghreed altamimiFriend
Messages: 184
Registered: October 2014
Senior Member
Hi Dimitris,
lqnmodel is the identification for the model that i gave in run configuration,Can i get that name programmatically ?? using builtin operation in EOL for example??
Re: Create new element from a type using variable by EOL [message #1780623 is a reply to message #1780562] Wed, 24 January 2018 22:25 Go to previous messageGo to next message
Dimitris Kolovos is currently offline Dimitris KolovosFriend
Messages: 2154
Registered: July 2009
Location: York, UK
Senior Member

Hi Taghreed,

System.context.modelRepository.models.get(0).createInstance(vtype) should do the trick.

Cheers,
Dimitris
Re: Create new element from a type using variable by EOL [message #1780675 is a reply to message #1780623] Thu, 25 January 2018 16:18 Go to previous messageGo to next message
taghreed altamimi is currently offline taghreed altamimiFriend
Messages: 184
Registered: October 2014
Senior Member
Thanks very much Dimitris :)
Re: Create new element from a type using variable by EOL [message #1780689 is a reply to message #1780675] Thu, 25 January 2018 18:02 Go to previous messageGo to next message
taghreed altamimi is currently offline taghreed altamimiFriend
Messages: 184
Registered: October 2014
Senior Member
Hi Dimitris ;
System.context.modelRepository.models.get(0) returns the model name.How can i return model element type in the same way???
for example if i want to query any type in my emf model such as getting the name for lqnmodel!processor i can write the following code:
for (p in lqnmodel!processor){
   if ( p.name="D1"){
   p.name.println();}

How can i return the model element type with its model name and then stored them in variable and use that variable in my code above instead of using them directly.

Cheers,
Taghreed.
Re: Create new element from a type using variable by EOL [message #1780720 is a reply to message #1780689] Fri, 26 January 2018 09:33 Go to previous messageGo to next message
Dimitris Kolovos is currently offline Dimitris KolovosFriend
Messages: 2154
Registered: July 2009
Location: York, UK
Senior Member

Hi Taghreed,

System.context.modelRepository.models.get(0) returns an instance of EmfModel [1] - not the name of the model. EmfModel doesn't provide an API for retrieving all type names but you can work around this by querying the underlying EMF resource directly as follows:

var resource = System.context.modelRepository.models.get(0).resource;

for (ePackage in resource.resourceSet.packageRegistry.values) {
  for (eClassifier in ePackage.eClassifiers) {
    eClassifier.name.println();
  }
}


Cheers,
Dimitris



[1] http://download.eclipse.org/epsilon/javadoc/org/eclipse/epsilon/emc/emf/EmfModel.html

[Updated on: Fri, 26 January 2018 09:33]

Report message to a moderator

Re: Create new element from a type using variable by EOL [message #1780772 is a reply to message #1780720] Fri, 26 January 2018 20:37 Go to previous messageGo to next message
taghreed altamimi is currently offline taghreed altamimiFriend
Messages: 184
Registered: October 2014
Senior Member
Hi Dimitris,
I tried the code in your previous reply and it works fine.
var vtype = "processor";
var p = System.context.modelRepository.models.get(0).createInstance(vtype);

My question is how can i get EReference (in this example called (processors) ) and store it in a variable and then use it to add the new instance to the correct containment reference ??
as below but without using its name using variable:
System.context.modelRepository.models.get(0).processors.add(p)

I tried for hours to get the correct code in order to save your time but i failed:(
I do really appreciate your help,

Thanks,
Taghreed
Re: Create new element from a type using variable by EOL [message #1780798 is a reply to message #1780772] Sat, 27 January 2018 15:54 Go to previous messageGo to next message
Dimitris Kolovos is currently offline Dimitris KolovosFriend
Messages: 2154
Registered: July 2009
Location: York, UK
Senior Member

Hi Taghreed,

You have to locate the element in your model under which you need to add p first. I don't have your complete metamodel but if it looks something like this

class Computer {
  val Processor[*] processors;
} 
class Processor {
}


off the top of my head you will need to do something like

var processors = lqnmodel!Computer.all.first().processors; // or
var processors = System.context.modelRepository.models.get(0).getAllOfType("Computer").first().processors;
processors.add(p);


Cheers,
Dimitris
Re: Create new element from a type using variable by EOL [message #1780803 is a reply to message #1780798] Sat, 27 January 2018 21:25 Go to previous messageGo to next message
taghreed altamimi is currently offline taghreed altamimiFriend
Messages: 184
Registered: October 2014
Senior Member
Hi Dimitris,
Sorry I think I did not explain what I want in a clear way.I am writing operations to change EMF model and I want them to be general so I don't want to use the exact names from the model.
I need to return Ereference from the model and store it in a variable then use that variable to add p to its container. As I did when I created the new instance without using the instances names.
var processors = System.context.modelRepository.models.get(0).getAllOfType("Computer").first().processors;
processors.add(p);


in the above example you used "processors" which is ereference name but I need a way to return ereferenece name in a variable then use it to add p.
Re: Create new element from a type using variable by EOL [message #1780810 is a reply to message #1780803] Sun, 28 January 2018 12:17 Go to previous message
Dimitris Kolovos is currently offline Dimitris KolovosFriend
Messages: 2154
Registered: July 2009
Location: York, UK
Senior Member

Hi Taghreed,

To learn how you can set/get the values of model element properties in a reflective way, I'd suggest looking at articles on dynamic EMF [1-3] or consulting the respective chapter of the EMF book.

Cheers,
Dimitris

[1] https://www.ibm.com/developerworks/library/os-eclipse-dynamicemf/
[2] http://www.devx.com/Java/Article/29093/0/page/1
[3] https://www.eclipse.org/modeling/emf/docs/
Previous Topic:generating .etl model by with errors
Next Topic:"EMC - JSON" for JSON object
Goto Forum:
  


Current Time: Fri Mar 29 07:27:06 GMT 2024

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

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

Back to the top