def add_asset_and_emit(self, esh: EnergySystemHandler, es_id: str, asset: EnergyAsset, area_bld_id: str): with self.flask_app.app_context(): asset_to_be_added_list = list() port_list = self.calculate_port_list(asset) message = self.create_asset_discription_message(asset, port_list) asset_to_be_added_list.append(message) if not ESDLAsset.add_object_to_area(esh.get_energy_system(es_id), asset, area_bld_id): ESDLAsset.add_object_to_building(esh.get_energy_system(es_id), asset, area_bld_id) emit('add_esdl_objects', {'es_id': es_id, 'asset_pot_list': asset_to_be_added_list, 'zoom': False}, namespace='/esdl')
def get_or_create_esi_qau(esh: EnergySystemHandler, active_es_id: str) -> esdl.QuantityAndUnits: es: esdl.EnergySystem = esh.get_energy_system(active_es_id) if not es.energySystemInformation: esi: esdl.EnergySystemInformation = esdl.EnergySystemInformation( id=str(uuid4())) es.energySystemInformation = esi esh.add_object_to_dict(active_es_id, esi) if not es.energySystemInformation.quantityAndUnits: qaus = esdl.QuantityAndUnits(id=str(uuid4())) es.energySystemInformation.quantityAndUnits = qaus esh.add_object_to_dict(active_es_id, qaus) return es.energySystemInformation.quantityAndUnits
def test(): esh = EnergySystemHandler() #left = esh.load_file('Right1_with_ESI.esdl') left = esh.load_file('Left.esdl') #right = esh.import_file('Right1_with_ESI.esdl') right = esh.import_file('Right1_with_ESI_and_connectedTo_and_carrier.esdl') #right = esh.import_file('Left.esdl') merge = ESDLMerge() merge.config(forceCombineMainArea=True) es = merge.merge(left, right) print(esh.to_string())
def get_handler(): global managed_sessions client_id = session['client_id'] if client_id in managed_sessions: if ESH_KEY in managed_sessions[client_id]: esh = managed_sessions[client_id][ESH_KEY] logger.debug('Retrieve ESH client_id={}, es.name={}'.format( client_id, esh.get_energy_system().name)) else: logger.warning( 'No EnergySystemHandler in session. Returning empty energy system' ) esh = EnergySystemHandler() esh.create_empty_energy_system('Untitled EnergySystem', '', 'Untitled Instance', 'Untitled Area') set_handler(esh) return esh else: logger.warning('Session has timed-out. Returning empty energy system') esh = EnergySystemHandler() esh.create_empty_energy_system('Untitled EnergySystem', '', 'Untitled Instance', 'Untitled Area') set_handler(esh) return esh
def _create_cost_qau(cost_unit_string): qau = esdl.QuantityAndUnitType( id=str(uuid4()), physicalQuantity=esdl.PhysicalQuantityEnum.COST, description='Cost in ' + cost_unit_string) _change_cost_unit(qau, cost_unit_string) print('new qau', EnergySystemHandler.attr_to_dict(qau)) return qau
def duplicate_energy_asset(esh: EnergySystemHandler, es_id, energy_asset_id: str): original_asset = esh.get_by_id(es_id, energy_asset_id) duplicate_asset = original_asset.deepcopy() # reset all id's for c in duplicate_asset.eContents: if c.eClass.findEStructuralFeature('id'): if c.eGet('id'): c.eSet('id', str(uuid4())) duplicate_asset.id = str(uuid4()) name = original_asset.name + '_ret' if original_asset.name.endswith('_ret'): name = original_asset.name[:-4] + '_sup' if original_asset.name.endswith('_sup'): name = original_asset.name[:-4] + '_ret' if not isinstance( duplicate_asset, Pipe): # do different naming for other conductors then pipes name = '{}_{}'.format(original_asset.name, 'copy') duplicate_asset.name = name geometry = duplicate_asset.geometry if isinstance(geometry, Line): line: Line = geometry # geometry.clone() rev_point = list(reversed(line.point)) # reverse coordinates line.point.clear() line.point.extend(rev_point) for p in line.point: _shift_point(p) if isinstance(geometry, Point): _shift_point(geometry) # disconnect the ports as also the connectedTo has been duplicated, we need to remove this reference for port in duplicate_asset.port: port.connectedTo.clear() esh.add_object_to_dict(es_id, duplicate_asset, recursive=True) # add to UUID registry return duplicate_asset
def duplicate_energy_asset(esh: EnergySystemHandler, es_id, energy_asset_id: str): original_asset = esh.get_by_id(es_id, energy_asset_id) duplicate_asset = original_asset.clone() duplicate_asset.id = str(uuid4()) name = original_asset.name + '_ret' if original_asset.name.endswith('_ret'): name = original_asset.name[:-4] + '_sup' if original_asset.name.endswith('_sup'): name = original_asset.name[:-4] + '_ret' if not isinstance(duplicate_asset, Pipe): # do different naming for other pipes name = '{}_{}'.format(original_asset.name, 'copy') duplicate_asset.name = name geometry = original_asset.geometry if isinstance(geometry, Line): line = geometry.clone() for p in geometry.point: point_clone = p.clone() _shift_point(point_clone) line.point.insert(0, point_clone) # reverse the list duplicate_asset.geometry = line if isinstance(geometry, Point): point_clone = geometry.clone() _shift_point(point_clone) duplicate_asset.geometry = point_clone for port in original_asset.port: newport = port.clone() newport.id = str(uuid4()) duplicate_asset.port.append(newport) esh.add_object_to_dict(es_id, newport) # add to UUID registry esh.add_object_to_dict(es_id, duplicate_asset) # add to UUID registry return duplicate_asset
def esdl_file_io(): with self.flask_app.app_context(): esh = EnergySystemHandler() set_handler(esh) # len(request.values) seems to be a multiple of 2 for i in range(int(len(request.values)/2)): base64_file_contents = request.values.get(f'file_info[{i}][file]') # base64_file_contents now contains something like # "data:application/octet-stream;base64,PD94bWwgdmVyc..." # Only take the part after the comma base64_file_contents = base64_file_contents.split(',')[1] filename = request.values.get(f'file_info[{i}][filename]') if base64_file_contents: esdlstr_base64_bytes = base64_file_contents.encode('utf-8') esdlstr_bytes = base64.b64decode(esdlstr_base64_bytes) esdl_str = esdlstr_bytes.decode('utf-8') self.load_esdl_in_esh(filename, esdl_str) return {'message': 'all ESDL(s) received'}, 201
def load_external_ref(): rset = ResourceSet() rset.resource_factory['esdl'] = XMLResource rset.resource_factory['*'] = XMLResource #car_rs = rset.get_resource(HttpURI('https://edr.hesi.energy/store/esdl/80e3ac6a-94b1-4a85-a0d1-a68de4251243?format=xml')) #units_rs = rset.get_resource(HttpURI('http://localhost:9080/store/resource/public/QuantityAndUnits.esdl')) #es_rs = rset.get_resource(URI('ES with external ref.esdl')) es_rs = rset.get_resource( ESDLDriveHttpURI( 'http://localhost:9080/store/resource/public/ES%20with%20external%20ref.esdl' )) print('initial rset', rset.resources) es = es_rs.contents[0] # uri = StringURI('http://test.esdl') # es_rs.save(uri) # string_esdl = uri.getvalue() # rset2 = ResourceSet() # rset2.resource_factory['esdl'] = XMLResource # rset2.resource_factory['*'] = XMLResource # # uri2 = StringURI('string://test2.esdl', text=string_esdl) # es_rs2 = rset2.create_resource(uri2) # es_rs2.load() # #es_rs2 = rset2.get_resource(uri=uri2) # es2 = es_rs2.contents[0] #uri = StringURI('to_string_' + 'es_ref' + '.esdl') #es_rs.save(uri) #print(uri.getvalue()) print('before access, new rset:', rset.resources) ref = es.energySystemInformation.carriers.carrier[ 0].energyContentUnit.reference print(qau_to_string(ref)) print(EnergySystemHandler.attr_to_dict(ref)) print(list(rset.resources.values())[1].contents[0].id) print(rset.resources)
# This work is based on original code developed and copyrighted by TNO 2020. # Subsequent contributions are licensed to you by the developers of such code and are # made available to the Project under one or several contributor license agreements. # # This work is licensed to you under the Apache License, Version 2.0. # You may obtain a copy of the license at # # http://www.apache.org/licenses/LICENSE-2.0 # # Contributors: # TNO - Initial implementation # Manager: # TNO from esdl.esdl_handler import EnergySystemHandler from esdl import esdl from pyecore.ecore import EStructuralFeature, EReference if __name__ == '__main__': esh = EnergySystemHandler() esdl.EnergySystem.__repr__ = lambda x: '{}(name={})'.format(x.eClass.name, x.name) es, _ = esh.load_file("C:\\Users\\werkmane\\OneDrive - TNO\\Documents\\ESDL\\EnergySystems\\ECW_with_carriers.esdl") es:esdl.EnergySystem = es carrier: esdl.Carrier = es.energySystemInformation.carriers.carrier[0] print(carrier) print(es.instance[0].area.asset[0].port[0].carrier) p:esdl.Port = es.instance[0].area.asset[0].port[0] r:EReference = p.eClass.findEStructuralFeature('carrier') print(r) # carrier relatie van port naar carrier moet bi-directional zijn.
from esdl import esdl from esdl.processing.ESDLQuantityAndUnits import * from esdl.esdl_handler import EnergySystemHandler esdl.QuantityAndUnitType.__repr__ = \ lambda x: '{}'.format(EnergySystemHandler.attr_to_dict(x)) def test_qau(descr, qau): print("{}:".format(descr)) qau.description = descr qau_str = qau_to_string(qau) print("- {}".format(qau_str)) print("- Building unit from the QaU sting:") unit_str = unit_to_string(qau) unit_qau = build_qau_from_unit_string(unit_str) qau_dict = esh.attr_to_dict(unit_qau) print(" Unit: {}".format(unit_to_string(unit_qau))) print(" {}".format(qau_dict)) if __name__ == "__main__": esh = EnergySystemHandler() qau = esdl.QuantityAndUnitType(id=str(uuid4())) qau.physicalQuantity = esdl.PhysicalQuantityEnum.DIRECTION qau.unit = esdl.UnitEnum.DEGREES test_qau("Wind direction in degrees", qau) qau = esdl.QuantityAndUnitType(id=str(uuid4()))
from src.shape import Shape from esdl.esdl_handler import EnergySystemHandler esh = EnergySystemHandler() esh.load_file('Nesselande contour warmtenet.esdl') # esh.load_file('Arnhem Schuytgraaf contour warmtenet.esdl') es = esh.get_energy_system() polygon = es.instance[0].area.area[0].geometry shape = Shape.create(polygon) print(shape.get_wkt())
# # http://www.apache.org/licenses/LICENSE-2.0 # # Contributors: # TNO - Initial implementation # Manager: # TNO from extensions.vue_backend.cost_information import _change_cost_unit from esdl.esdl_handler import EnergySystemHandler from uuid import uuid4 from esdl import esdl import re cost_unit_string = "EUR/kWh/yr" qau = esdl.QuantityAndUnitType(id=str(uuid4()), physicalQuantity=esdl.PhysicalQuantityEnum.COST, description='Cost in '+cost_unit_string) _change_cost_unit(qau, cost_unit_string) print(EnergySystemHandler.attr_to_dict(qau)) if re.match(r"\w+/kWh", cost_unit_string): print('yes') else: print('no') if re.match(r".+/yr$", cost_unit_string): print("\yr") qau.perTimeUnit = esdl.TimeUnitEnum.YEAR else: print('no /yr')
def main(): esh = EnergySystemHandler() es, _ = esh.load_file('esdl/Left.esdl') tracker = esh.change_tracker stack = tracker.get_tracker_stack(es) es: esdl.EnergySystem = esh.energy_system stack.start_recording(combineCommands=True, label="add pipes") pipe1 = esdl.Pipe(name="TestPipe1", id='TestPipe1') pipe2 = esdl.Pipe(name="TestPipe2", id='TestPipe2') ip1 = esdl.InPort(name="TestIP1", id="Pipe1IP1") op1 = esdl.OutPort(name="TestOP1", id="Pipe2OP1") pipe1.port.append(ip1) pipe2.port.append(op1) es.instance[0].area.asset.append(pipe1) es.instance[0].area.asset.append(pipe2) stack.add_undo_step("add connected to") ip1.connectedTo.append(op1) print('Inport 1 connected to:', ip1.connectedTo) stack.stop_recording() pprint(stack.stack) stack.undo() print('Inport 1 connected to after undo:', ip1.connectedTo) stack.redo() print('Inport 1 connected to after redo:', ip1.connectedTo) print("\n====================================== TEST 2: delete InPort 1 of Pipe1 ===========================") stack.start_recording(combineCommands=True, label="Delete Port1") print("Ports of Pipe1 before port delete:", pipe1.port) print("Ports of Pipe1 before connectedTo:", pipe1.port[0].connectedTo) ip1.delete(recursive=True) print("Ports of Pipe1 after port delete:", pipe1.port) stack.stop_recording() stack.undo() print("Ports of Pipe1 after undo:", pipe1.port) #print("Pipe1 after undo connectedTo:", pipe1.port[0].connectedTo) print("\n====================================== TEST 3: delete all of Pipe1 ===========================") stack.start_recording(combineCommands=True, label="Delete Pipe1") print('Before deletion assets:', [a.name for a in es.instance[0].area.asset]) pipe1.delete(recursive=True) print('After deletion assets:', [a.name for a in es.instance[0].area.asset]) stack.stop_recording() stack.undo() print('After undo deletion of Pipe1 assets:', [a.name for a in es.instance[0].area.asset]) stack.stop_recording() """ self.references = {<esdl.esdl.Pipe object at 0x000000000469FE08>: [(<EReference area: <class 'esdl.esdl.Area'>>, <esdl.esdl.Area object at 0x000000000469F2C8>), (<EReference controlStrategy: <class 'esdl.esdl.ControlStrategy'>>, None), (<EReference behaviour: <class 'esdl.esdl.AbstractBehaviour'>>, EOrderedSet()), (<EReference isOwnedBy: <class 'esdl.esdl.Party'>>, None), (<EReference containingBuilding: <class 'esdl.esdl.AbstractBuilding'>>, None), (<EReference geometry: <class 'esdl.esdl.Geometry'>>, None), (<EReference costInformation: <class 'esdl.esdl.CostInformation'>>, None), (<EReference KPIs: <class 'esdl.esdl.KPIs'>>, None), (<EReference material: <class 'esdl.esdl.AbstractMatter'>>, None), (<EReference dataSource: <class 'esdl.esdl.AbstractDataSource'>>, None), (<EReference port: <class 'esdl.esdl.Port'>>, EOrderedSet()), (<EReference sector: <class 'esdl.esdl.Sector'>>, None)], <esdl.esdl.OutPort object at 0x000000000469FA88>: [(<EReference energyasset: <class 'esdl.esdl.EnergyAsset'>>, <esdl.esdl.Pipe object at 0x000000000469FE08>), (<EReference connectedTo: <class 'esdl.esdl.InPort'>>, EOrderedSet()), (<EReference carrier: <class 'esdl.esdl.Carrier'>>, None), (<EReference profile: <class 'esdl.esdl.GenericProfile'>>, EOrderedSet())]} self.inverse_references = {<esdl.esdl.Pipe object at 0x000000000469FE08>: [], <esdl.esdl.OutPort object at 0x000000000469FA88>: []} """ ip1.connectedTo.append(op1) d = Delete(owner=pipe2) print('Before delete Pipe2:', [a.name for a in es.instance[0].area.asset]) if d.can_execute: d.do_execute() print('After delete Pipe2:', [a.name for a in es.instance[0].area.asset]) print(d.references) print(d.inverse_references) d.undo() print('After undo delete Pipe2:', [a.name for a in es.instance[0].area.asset]) print('ports of Pipe2: ', [p.name for p in d.owner.port]) print('ports connectedTo of Pipe2: ', [[q.name +'-'+p.name for p in q.connectedTo] for q in d.owner.port])
def test(): stack = UndoRedoCommandStack( ) # keep track of all undo-redo stuff using notifications #monitor_esdl_changes(stack) esh = EnergySystemHandler() esdl.EnergySystem.__repr__ = lambda x: '{}(name={})'.format( x.eClass.name, x.name) es, _ = esh.load_file('esdl\Left.esdl') es: esdl.EnergySystem = es es2, _ = esh.import_file('esdl\Right1.esdl') print(esh.rset.resources) resource = esh.get_resource(es.id) print(resource.uri.plain) from esdl.undo import ResourceObserver ro = ResourceObserver(command_stack=stack) ro.observe(resource) stack.start_recording(combineCommands=True) #changeESName = Set(owner=es, feature='name', value="Please undo me!") #stack.execute(changeESName) print('Initial value:', es.name, es.description) #stack.undo() es.name = "Please undo me!" print('Updated value:', es.name, es.description) stack.add_undo_step() es.description = "cool" stack.stop_recording() print('Updated value2:', es.name, es.description) stack.undo() print('Undone:', es.name, es.description) stack.redo() print('Redo:', es.name, es.description) print(stack.stack) stack.undo() print('Undo again:', es.name, es.description) print(stack.stack) stack.undo() print('Undo again:', es.name, es.description) print(stack.stack) print("---- next test ----") stack.start_recording(combineCommands=True) area: esdl.Area = es.instance[0].area #observer.observe(area) #addAsset = Add(owner=area, feature='asset', value=chp) #stack.execute(addAsset) print('Area name:' + area.name) area.name = 'test area' print('Area name after change:' + area.name) #stack.undo() #print('Area name after undo:' + area.name) #print('Area assets', area.asset) chp = esdl.CHP(id="CHP", name="CHP", fuelType=esdl.PowerPlantFuelEnum.NATURAL_GAS) area.asset.append(chp) print('Area assets after adding CHP', area.asset) stack.stop_recording() stack.undo() print('Area assets after undo', area.asset) print('Area name after undo:' + area.name) r2Stack = UndoRedoCommandStack() ResourceObserver(r2Stack).observe(esh.get_resource(es_id=es2.id)) r2Stack.start_recording() stack.start_recording(combineCommands=True) es2.name = 'test' stack.stop_recording() r2Stack.stop_recording() r2Stack.undo() print(es2.name)