EMFScript
Models Scripting with EMF & Javascript
About
News
Documentation
Tutorial
Script Examples
Download & Installation
Project Info
A first script showing the power of emfscript
/ M3 Level : The meta-meta-model is 'ecore' var ecore = importModelDef( 'org.eclipse.emf.ecore', 'import.org.eclipse.emf.ecore.EcorePackage' ); var expr = xmiLoadModelElement( 'platform:/resource/fr.enic.emfshell/models/expr.ecore' ); // M2 Level : The meta-model 'lang' has the Program and Variable concepts var lang = xmiLoadModelElement( 'lang.ecore' ); // Decorate the 'lang' metamodel : // add an EOperation to the lang.Program class var eop_run = ecore.EOperation.create(); eop_run.name = 'run'; lang.Program.eOperations.add( eop_run ); lang.Program.run._body = function() { var vars = this.variables; function Context() { var known = []; this.add = function( v ) { var i; for (i in known) { var k = known[i]; if (k.name == v.name) return k; } known = known.concat( v ); return null; } } var cx = new Context(); var i; for (i in vars) { var v = vars[i]; var k = cx.add(v); if (k) print( k.name+' : '+k.value ); } } var eops = lang.Program.eOperations; // provide a JavaScript implementation for the HUTN EOperation var eop_hutn1 = eops.select(function(e){ return e.name=='asHUTN' && e.eParameters.size()==0;} ).first(); eop_hutn1._body = function() { // the '_' underscore indicates that body is not an ecore concept print( 'Program '+this.name+';' ); var vars = this.variables; for (i in vars) { var v = vars[i]; print( ' '+v.name+(v.value ? ' = '+v.value : '')+';' ); } print( 'end.' ); } // multiple eOperations with the same name var eop_hutn2 = eops.select(function(e){ return e.name=='asHUTN' && e.eParameters.size()==1 && e.eParameters[0].eType==ecore.EIntegerObject;} ).first(); eop_hutn2._body = function() { print( this.name+'.asHUTN(EIntegerObject)' ); } // multiple eOperations with the same name var eop_hutn3 = eops.select(function(e){ return e.name=='asHUTN' && e.eParameters.size()==1 && e.eParameters[0].eType==ecore.EString;} ).first(); eop_hutn3._body = function() { print( this.name+'.asHUTN(EString)' ); } // M1 Level : The 'HelloWorld' model which is an instance of 'lang' metamodel var prog = lang.Program.create(); prog.name = 'HelloWorld'; var x = lang.Variable.create(); prog.variables.add( x ); x.name = 'x'; x.value = 5.1; var y = lang.Variable.create(); prog.variables.add( y ); y.name = 'y'; y.value = 3.0; x = lang.Variable.create(); prog.variables.add( x ); x.name = 'x'; y = lang.Variable.create(); prog.variables.add( y ); y.name = 'y'; // display the program HUTN prog.asHUTN(); // run the 'prog' Program instance prog.run(); // discrimination on arguments prog.asHUTN( 'Hello' ); prog.asHUTN( new java.lang.Integer( 5 ) );
About
>
Documentation
>
Script Examples
>