Skip to content

Instantly share code, notes, and snippets.

@aranega
Last active January 14, 2017 14:00
Show Gist options
  • Save aranega/89761454a232106a20ef4184bd9198cf to your computer and use it in GitHub Desktop.
Save aranega/89761454a232106a20ef4184bd9198cf to your computer and use it in GitHub Desktop.
Demo of loading the Geppetto metamodel and models, modication and XMI serialization using PyEcore
# Ecore core and PyEcore runtime core
import pyecore.ecore as Ecore
# global_registry for metamodels registration
from pyecore.resources import global_registry
# Resource for load/save data, URI for file location model/metamodel
# ResourceSet to create a resource container and isolate the loaded metamodel
from pyecore.resources import ResourceSet, Resource, URI
# To fetch URI from an Http address
from pyecore.resources.resource import HttpURI
# Only for display purposes
def tree_display(root, level=''):
prefix = '+-- ' if root.eContainer() else '/ '
if hasattr(root, 'name'):
display = '{0} [{1}]'.format(root.name, root.eClass.name)
else:
display = root.eClass.name
if root.eContainmentFeature():
display += ' {{{0}}}'.format(root.eURIFragment())
print(level + prefix + display)
for eobject in root.eContents:
tree_display(eobject, level + ' ')
if __name__ == '__main__':
# Register static Ecore metamodel
global_registry[Ecore.nsURI] = Ecore
# Create a new ResourceSet
rset = ResourceSet()
# We create the XMLType resource
# (required regarding the way the metamodel is built)
int_conversion = lambda x: int(x)
String = Ecore.EDataType('String', str)
Double = Ecore.EDataType('Double', int, 0, from_string=int_conversion)
Int = Ecore.EDataType('Int', int, 0, from_string=int_conversion)
IntObject = Ecore.EDataType('IntObject', int, None,
from_string=int_conversion)
Boolean = Ecore.EDataType('Boolean', bool, False,
from_string=lambda x: x in ['True', 'true'])
EJavaObject = Ecore.EDataType('EJavaObject', object)
Long = Ecore.EDataType('Long', int, 0, from_string=int_conversion)
xmltype = Ecore.EPackage() # XMLType resource root
xmltype.eClassifiers.extend([String,
Double,
Int,
EJavaObject,
Long,
Boolean,
IntObject])
xmltype.nsURI = 'http://www.eclipse.org/emf/2003/XMLType'
xmltype.nsPrefix = 'xmltype'
xmltype.name = 'xmltype'
# We register it in the ResourceSet as a metamodel
# at the 'http://www.eclipse.org/emf/2003/XMLType' URI
rset.metamodel_registry[xmltype.nsURI] = xmltype
# Load the Geppetto metamodel from:
# https://raw.githubusercontent.com/openworm/org.geppetto.model/development/src/main/resources/geppettoModel.ecore
metamodel_url = ('https://raw.githubusercontent.com/openworm/'
'org.geppetto.model/development/src/main/resources/'
'geppettoModel.ecore')
resource = rset.get_resource(HttpURI(metamodel_url))
mm_root = resource.contents[0] # At this point, mm_root is the Geppetto metamodel root
# Register all the EPackages inside the ResourceSet
rset.metamodel_registry[mm_root.nsURI] = mm_root
for subpack in mm_root.eSubpackages:
rset.metamodel_registry[subpack.nsURI] = subpack
# Load a Geppetto test model from:
# https://raw.githubusercontent.com/openworm/org.geppetto.model/development/src/test/resources/GeppettoModelTest.xmi
model_url = ('https://raw.githubusercontent.com/openworm/'
'org.geppetto.model/development/src/test/resources/'
'GeppettoModelTest2.xmi')
resource = rset.get_resource(HttpURI(model_url))
root = resource.contents[0]
# We display the model
tree_display(root)
# We add a new variable (playing with reflection stuffs)
var1 = root.variables[0] # We get the first variable
Variable = var1.eClass # We get its metaclasse
var2 = Variable() # We create a new instance
var2.types.append(var1.types[0])
var2.name = 'myvar'
var2.id = var2.name + '_id'
root.variables.append(var2) # We attach the new instance to the model
tree_display(root)
# We save the new instance in a file resource
resource.save(output=URI('test_demo.xmi'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment