def model_setup(): rset = ResourceSet() resource = rset.get_resource(URI('metamodel.ecore')) mm_root = resource.contents[0] mm_root.nsURI = 'ghmde' mm_root.nsPrefix = 'model' mm_root.name = 'ghmde' rset.metamodel_registry[mm_root.nsURI] = mm_root print(mm_root.nsURI) MyMetamodel = DynamicEPackage( mm_root) # We create a DynamicEPackage for the loaded root file_instance = MyMetamodel.File() print(file_instance) A = mm_root.getEClassifier('Model') # nuovo instance = A() dictionary = {} dictionary['model'] = instance dictionary['meta-model'] = MyMetamodel return dictionary
def get_infrastructure_model_access(ecore_uri='./infrastructure.ecore', http_uri=False): """Return handler to instanciate classes of a metamodel.""" rset = ResourceSet() uri = URI(ecore_uri) if http_uri: uri = HttpURI(ecore_uri) resource = rset.get_resources(uri) mm_root = resource.contents[0] # We get the root (an EPackage here) return DynamicEPackage(mm_root)
def test_dynamic_removal_eclasses(complexmm): ComplexMM = DynamicEPackage(complexmm) assert ComplexMM.Root complexmm.eClassifiers.remove(ComplexMM.Root) with pytest.raises(AttributeError): ComplexMM.Root assert ComplexMM.A complexmm.eClassifiers[0].delete() with pytest.raises(AttributeError): ComplexMM.A
def test_ecoreinheritance_loading(): rset = ResourceSet() ecore_file = path.join('tests', 'xmi', 'xmi-tests', 'EcoreInheritance.ecore') resource = rset.get_resource(URI(ecore_file)) root = DynamicEPackage(resource.contents[0]) assert Ecore.EModelElement.eAnnotations in root.A.eAllStructuralFeatures() a = root.A() assert isinstance(a, Ecore.EModelElement) assert Ecore.EModelElement.eAnnotations in root.B.eAllStructuralFeatures() b = root.B() assert isinstance(b, Ecore.EModelElement) assert a.eAnnotations == {} assert b.eAnnotations == {}
def load_energy_system(self, name): # create a resourceSet that hold the contents of the esdl.ecore model and the instances we use/create rset = ResourceSet() # Assign files with the .esdl extension to the XMLResource instead of default XMI rset.resource_factory['esdl'] = lambda uri: XMLResource(uri) # Read the lastest esdl.ecore from github esdl_model_resource = rset.get_resource( HttpURI( 'https://raw.githubusercontent.com/EnergyTransition/ESDL/master/esdl/model/esdl.ecore' )) esdl_model = esdl_model_resource.contents[0] # print('Namespace: {}'.format(esdl_model.nsURI)) rset.metamodel_registry[esdl_model.nsURI] = esdl_model # Create a dynamic model from the loaded esdl.ecore model, which we can use to build Energy Systems esdl = DynamicEPackage(esdl_model) # fix python buildin 'from' that is also used in ProfileElement as attribute # use 'start' instead of 'from' when using a ProfileElement alias('start', esdl.ProfileElement.findEStructuralFeature('from')) # have a nice __repr__ for some ESDL classes when printing ESDL objects (includes all Assets and EnergyAssets) esdl.Item.python_class.__repr__ = lambda x: '{}: ({})'.format( x.name, EnergySystemHandler.attr_to_dict(x)) esdl.Carrier.python_class.__repr__ = lambda x: '{}: ({})'.format( x.name, EnergySystemHandler.attr_to_dict(x)) esdl.Geometry.python_class.__repr__ = lambda x: '{}: ({})'.format( x.name, EnergySystemHandler.attr_to_dict(x)) esdl.QuantityAndUnitType.python_class.__repr__ = lambda x: '{}: ({})'.format( x.id, EnergySystemHandler.attr_to_dict(x)) esdl.QuantityAndUnitReference.python_class.__repr__ = lambda x: '{}: ({})'.format( 'QuantityAndUnitReference', EnergySystemHandler.attr_to_dict(x)) esdl.KPI.python_class.__repr__ = lambda x: '{}: ({})'.format( x.name, EnergySystemHandler.attr_to_dict(x)) esdl.ProfileElement.python_class.__repr__ = lambda x: 'ProfileElement ({})'.format( EnergySystemHandler.attr_to_dict(x)) # load the ESDL file resource = rset.get_resource(URI(name)) es = resource.contents[0] # At this point, the model instance is loaded! # get notifications of changes in the EnergySystem model #observer = PrintNotification(es) #observer2 = PrintNotification(es.instance[0].area) # also return the esdlm and rset reference, so we can create esdl classes and store them as strings return es, resource, esdl, rset
def load_metamodel(): """ Load and return a statechart metamodel and its resource set. :return: a tuple (metamodel, resource set) """ filepath = os.path.join(os.path.dirname(__file__), 'amola.ecore') rset = ResourceSet() resource = rset.get_resource(URI(filepath)) metamodel = resource.contents[0] rset.metamodel_registry[metamodel.nsURI] = metamodel return DynamicEPackage(metamodel), rset
def get_energysystem_from_esdl(fname): rset = ResourceSet() # Read the lastest esdl.ecore from github resource = rset.get_resource( HttpURI( 'https://raw.githubusercontent.com/EnergyTransition/ESDL/master/esdl/model/esdl.ecore' )) esdl_model = resource.contents[0] rset.metamodel_registry[esdl_model.nsURI] = esdl_model # Create a dynamic model from the loaded esdl.ecore model, which we can use to build Energy Systems esdl = DynamicEPackage(esdl_model) resource = rset.get_resource(URI(fname)) return resource.contents[0]
def load(): #return model_root rset = ResourceSet() resource = rset.get_resource(URI('metamodel.ecore')) mm_root = resource.contents[0] rset.metamodel_registry[mm_root.nsURI] = mm_root resource = rset.get_resource(URI('model.xmi')) model_root = resource.contents[0] MyMetamodel = DynamicEPackage(mm_root) #main_model_creator() #debug
def test_dynamic_addition_eclasses(complexmm): ComplexMM = DynamicEPackage(complexmm) E = EClass('E') complexmm.eClassifiers.append(E) assert ComplexMM.E F = EClass('F') complexmm.eSubpackages[0].eClassifiers.append(F) assert ComplexMM.inner.F G = EClass('G') H = EClass('H') complexmm.eClassifiers.extend([G, H]) assert ComplexMM.G assert ComplexMM.H
def simplemm(): A = EClass('A') B = EClass('B') A.eStructuralFeatures.append(EAttribute('name', EString)) A.eStructuralFeatures.append(EAttribute('number', EInt)) A.eStructuralFeatures.append(EReference('b', B)) A.eStructuralFeatures.append( EAttribute('names', EString, upper=-1, unique=False)) A.eStructuralFeatures.append( EAttribute('ints', EInt, upper=-1, unique=False)) A.eStructuralFeatures.append( EReference('inners', B, upper=-1, unique=False, containment=True)) A.eStructuralFeatures.append(EReference('cols', B, upper=-1, unique=False)) pack = EPackage('pack', nsURI='http://pack/1.0', nsPrefix='pack') pack.eClassifiers.extend([A, B]) return DynamicEPackage(pack)
def mm(): Root = EClass('Root') A = EClass('A') B = EClass('B') Root.eStructuralFeatures.append( EReference('a_s', A, upper=-1, containment=True)) Root.eStructuralFeatures.append( EReference('bs', B, upper=-1, containment=True)) A.eStructuralFeatures.append(EAttribute('name', EString)) simple_tob = EReference('simple_tob', eType=B, containment=True) A.eStructuralFeatures.append(simple_tob) A.eStructuralFeatures.append( EReference('many_tob', eType=B, containment=True, upper=-1)) B.eStructuralFeatures.append(EReference('toa', A)) B.eStructuralFeatures.append( EReference('inverseA', A, eOpposite=simple_tob)) package = EPackage(name='package') package.eClassifiers.extend([Root, A, B]) return DynamicEPackage(package)
def __init__(self, name=None): # create a resourceSet that hold the contents of the esdl.ecore model and the instances we use/create self.rset = ResourceSet() # Assign files with the .esdl extension to the XMLResource instead of default XMI self.rset.resource_factory['esdl'] = lambda uri: XMLResource(uri) # Read the esdl.ecore from the tmp folder esdl_model_resource = self.rset.get_resource( URI('tmp/esdl/esdl.ecore')) esdl_model = esdl_model_resource.contents[0] # print('Namespace: {}'.format(esdl_model.nsURI)) self.rset.metamodel_registry[esdl_model.nsURI] = esdl_model # Create a dynamic model from the loaded esdl.ecore model, which we can use to build Energy Systems self.esdl = DynamicEPackage(esdl_model) # fix python buildin 'from' that is also used in ProfileElement as attribute # use 'start' instead of 'from' when using a ProfileElement alias('start', self.esdl.ProfileElement.findEStructuralFeature('from')) # have a nice __repr__ for some ESDL classes when printing ESDL objects (includes all Assets and EnergyAssets) self.esdl.Item.python_class.__repr__ = lambda x: '{}: ({})'.format( x.name, EnergySystemHandler.attr_to_dict(x)) self.esdl.Carrier.python_class.__repr__ = lambda x: '{}: ({})'.format( x.name, EnergySystemHandler.attr_to_dict(x)) self.esdl.Geometry.python_class.__repr__ = lambda x: '{}: ({})'.format( x.name, EnergySystemHandler.attr_to_dict(x)) self.esdl.QuantityAndUnitType.python_class.__repr__ = lambda x: '{}: ({})'.format( x.id, EnergySystemHandler.attr_to_dict(x)) self.esdl.QuantityAndUnitReference.python_class.__repr__ = lambda x: '{}: ({})'.format( 'QuantityAndUnitReference', EnergySystemHandler.attr_to_dict(x)) self.esdl.KPI.python_class.__repr__ = lambda x: '{}: ({})'.format( x.name, EnergySystemHandler.attr_to_dict(x)) self.esdl.ProfileElement.python_class.__repr__ = lambda x: 'ProfileElement ({})'.format( EnergySystemHandler.attr_to_dict(x)) if name: self.name = name self.load_energy_system(name)
def test_xmi_with_iD_attribute(tmpdir): mm_file = os.path.join('tests', 'xmi', 'xmi-tests', 'A.ecore') rset = ResourceSet() mm = rset.get_resource(mm_file).contents[0] rset.metamodel_registry[mm.nsURI] = mm mm_dyn = DynamicEPackage(mm) root = mm_dyn.Root() a = mm_dyn.A() b = mm_dyn.B() b.nameID = 'uniqueNameForB' a.tob = b root.a.append(a) root.b.append(b) localdir = tmpdir.mkdir('pyecore-tmp') f1 = localdir.join('model_iD_simple.xmi') resource = rset.create_resource(str(f1)) resource.append(root) resource.save() root2 = mm_dyn.Root() root2.b.append(b) f2 = localdir.join('model_iD.xmi') f3 = localdir.join('model_iD.xmi') resource = rset.create_resource(str(f2)) resource2 = rset.create_resource(str(f3)) resource.append(root) resource2.append(root2) resource2.save() resource.save() rset = ResourceSet() rset.metamodel_registry[mm.nsURI] = mm rset.get_resource(str(f1)) rset.get_resource(str(f2)) rset.get_resource(str(f3))
def _init_metamodel(self): self.rset = ResourceSet() # Assign files with the .esdl extension to the XMLResource instead of default XMI self.rset.resource_factory['esdl'] = lambda uri: XMLResource(uri) # Read esdl.ecore as meta model logger.info('Initalizing ESDL metamodel for documentation from {}'.format(self.esdlEcoreFile)) mm_uri = URI(self.esdlEcoreFile) if self.esdlEcoreFile[:4] == 'http': mm_uri = HttpURI(self.esdlEcoreFile) esdl_model_resource = self.rset.get_resource(mm_uri) esdl_model = esdl_model_resource.contents[0] self.esdl_model = esdl_model # logger.debug('Namespace: {}'.format(esdl_model.nsURI)) self.rset.metamodel_registry[esdl_model.nsURI] = esdl_model # Create a dynamic model from the loaded esdl.ecore model, which we can use to build Energy Systems self.esdl = DynamicEPackage(esdl_model) logger.info("Initialized ESDL version {}".format(self.get_esdl_version()))
def test_xmi_save_none_value_for_containement(tmpdir): rset = ResourceSet() ecore_path = os.path.join('tests', 'xmi', 'xmi-tests', 'root.ecore') resource = rset.get_resource(ecore_path) root = resource.contents[0] rset.metamodel_registry[root.nsURI] = root mm = DynamicEPackage(root) a = mm.A() a.b = None f = tmpdir.mkdir('pyecore-tmp').join('none_in_containment.xmi') resource = rset.create_resource(URI(str(f))) resource.append(a) resource.save() rset = ResourceSet() rset.metamodel_registry[root.nsURI] = root resource = rset.get_resource(URI(str(f))) assert resource.contents[0] is not None assert resource.contents[0].b is None assert len(resource.contents[0]._isset) == 0 # We save with the explicit option fo saving none values resource = rset.create_resource(URI(str(f))) resource.append(a) resource.save() resource.save(options={XMIOptions.SERIALIZE_DEFAULT_VALUES: True}) rset = ResourceSet() rset.metamodel_registry[root.nsURI] = root resource = rset.get_resource(URI(str(f))) assert resource.contents[0] is not None assert resource.contents[0].b is None assert len(resource.contents[0]._isset) == 1
def test_dynamic_access_eclasses(simplemm): SimpleMM = DynamicEPackage(simplemm) assert SimpleMM.A assert SimpleMM.B
def test_static_init_dynamic_epackage_bad_value(generated_library): with pytest.raises(Ecore.BadValueError): DynamicEPackage(generated_library)
def test_static_init_dynamicEPackage_bad_value(): with pytest.raises(BadValueError): DynamicEPackage(library)
def test_static_init_dynamicEPackage_from_static_epackage(): dyn = DynamicEPackage(library) assert library.Writer.eClass is dyn.Writer
def test_static_init_dynamic_epackage(generated_library): package = DynamicEPackage(generated_library) assert package.Book is not None
def importMetaModel(rset, filename): pkg = rset.get_resource(URI(filename)).contents[0] register(rset, pkg) return DynamicEPackage(pkg)
def test_dynamic_access_innerpackage(complexmm): ComplexMM = DynamicEPackage(complexmm) assert ComplexMM.A assert ComplexMM.B assert ComplexMM.inner.C assert ComplexMM.inner.D
from pyecore.resources.resource import HttpURI, ResourceSet from pyecore.utils import DynamicEPackage from pyecore.ecore import EcoreUtils import pyecore.behavior as behavior # Load metamodel rset = ResourceSet() uri = HttpURI('https://raw.githubusercontent.com/gemoc/ale-lang/master/' 'examples/minifsm/model/MiniFsm.ecore') package_root = rset.get_resource(uri).contents[0] rset.metamodel_registry[package_root.nsURI] = package_root fsm = DynamicEPackage(package_root) # Code for each overridden/added method @fsm.Transition.behavior def is_activated(self): return (self.fsm.currentEvent == self.event and self.incoming == self.fsm.currentState) @fsm.State.behavior def execute(self): print('Execute', self.name) @fsm.FSM.behavior def handle(self, event): print('Handle', event) self.currentEvent = event