java - how do I implement a model by code in EMF? (Eclipse Modeling Framework) -


i have created model in emf. managed generate code (.genmodel file), looked files, kinda understand implementation of model, edit , editor plugin. yet fail understand, how make own instance of metamodel code. understand way code written, don't know put own stuff called. hope here can me, there no discussed emf thread on stackoverflow. regards, me

short answer: take @ emf book. book understand how use generated code , how construct models (part iv).

there 2 ways create model: programatically or using generated editor. editor allows create model in tree view. use editor need run editor plugin eclipse aplication (right click -> run as). once in nested eclipse should able use editor craete model.

the model itslef not usefull , assume want load model read contents, modify them, query them, etc. first important thing know emf models persisted xmi files (name.xmi). global extension "xmi", in genmodel can oreffered extension (lets assume "soq"). xmi extension xml, if @ xmi file text editor xml. load emf model basic code is:

resourceset resourceset = new resourcesetimpl(); uri uri = uri.createuri("file:/c:/data/model.soq");  resource resource = resourceset.createresource(uri); try  {      resource.load(null);      system.out.println("loaded");  }  catch (ioexception e)  {      system.out.println("failed read " + uri);  }  

the resource contain elements of model. each of them instance of the respective java class genmodel generated. contents of resource (the elements) can loop on resource.getcontents() or on resource.getallcontents(), difference latter iterate on model contents, former on first level. then, example, test of type , stuff:

for (eobject eo : resource.getallcontents()) {     if (eo instanceof myemfclass2) {         system.out.println("found element of myemfclass2");     } } 

the other thing can create instances of metamodel classes , add them resource. example, can generate model reading information source (db, file):

myemfclass2 c2 = soqfactory.createmyemfclass2 (); c2.name = otherdatasource.name; resourceset resourceset = new resourcesetimpl();   uri uri = uri.createuri("file:/c:/data/newm.soq");  resource resource = resourceset.createresource(uri);  resource.getcontents().add(c2);  try  {      resource.save(null);      system.out.println("saved");  }  catch (ioexception e)  {      system.out.println("failed write " + uri);  } 

note specific factory name , methods depend on metamodel. code assumes running eclipse runtime application (that is, metmodel plugins installed in eclipse ide). if want run without runtime eclipse or in standalone environment additional precautions need taken:

ecorepackage.einstance.eclass();    // makes sure emf , running resourceset.getresourcefactoryregistry().getextensiontofactorymap().  put("soq", new xmiresourcefactoryimpl());  // make sure emf knows how load models yourpackage.einstance.eclass();    // makes sure factories registered , classes loaded 

the emf metamodel , generated code "data model". out of box store , manipulate data. additional functionality have coded around it.

on final note, emf forum on eclipse site has active community , emf developers more monitor questions there @ stackoverflow.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -