poc-mondaine / use-case-renewable-electricity

0 stars 0 forks source link

Get asset(s) from energy system #1

Open redekok opened 5 years ago

redekok commented 5 years ago

In order to retrieve an asset's attribute from the energy system model, my code loops over all assets and checks when the asset name is equal to the one I am looking for. This seems quite cumbersome. Is there yet a get method for assets in ESDL? If not, what would be the best way to compare the assets to the one I'm interested in? Compare by class? Compare by name doesn't seem like the best option..

https://github.com/poc-mondaine/use-case-renewable-electricity/blob/master/energy_system_handler.py#L53-L55

edwinmatthijssen commented 5 years ago

What I often do, is to create a dictionary with the asset id as a key. This speeds up searching and processing enormously. Of course you need to iterate over all assets at least once.

ewoudwerkman commented 5 years ago

You seem to be looking for an asset of a specific type, e.g. a PVParc. I've added some code to show how to do that using isinstance().

Furthermore, I see that KPI's do not have an id, which they should have, as name is used for printing. All things you can have multiples of should contain an id: @edwinmatthijssen can you add an id to a KPI?.

I've added some examples for queying an EnergySystem in your code in a pull request. The code I created also searches for the KPI by id, but this doesn't work in the current version of ESDL (so I added searching for KPI by name, too)

ewoudwerkman commented 5 years ago

I don't seem to have access to the repo, to create a branch with the example code

redekok commented 5 years ago

Great, thanks! I was indeed looking for something like isistance().

@ewoudwerkman I've just added you to the repo's group of collaborators. It seems like the default repository permission is set to read. @arunsub would you be able to adjust that to write?

ewoudwerkman commented 5 years ago

Thanks! See my pull request at #3 Have a look at test.py (which is a partial copy of your code) and energy_system_handler.py for some adaptations

redekok commented 5 years ago

Thanks for thinking (and coding) along! :)

ewoudwerkman commented 5 years ago

Ik heb in de branch getassets een update gedaan met nog wat handige functies:

    # returns a generator of all assets of a specific type. Not only the ones defined in  the main Instance's Area
    # e.g. QuantityAndUnits can be defined in the KPI of an Area or in the EnergySystemInformation object
    # this function returns all of them at once
    def get_all_assets_of_type(self, esdl_type):
        return esdl_type.allInstances()

    # Using this function you can query for objects by ID
    # After loading an ESDL-file, all objects that have an ID defines are stored in resource.uuid_dict automatically
    # Note: If you add things later to the resource, it won't be added automatically to this dictionary though.
    # Use get_by_id_slow() for that
    def get_by_id(self, id):
        return self.resource.uuid_dict[id]

    # This function iterates over all the contents of the Energy System and is much slower than get_by_id()
    def get_by_id_slow(self, id):
        for child in self.es.eAllContents():
            if hasattr(child, 'id'):
                if child.id == id:
                    return child

Daarnaast heb ik twee methodes toegevoegd om het EnergySystem als een string te laden of uit te lezen. Dit is handig om deze straks via HTTP over te sturen (dan is het niet nodig om deze eerst op disk op te slaan bijvoorbeeld).

    # get the energy system as a XML String
    # does not change the 'active' resource
    # so save() will still save as a file
    def get_as_string(self):
      ...

   # load an EnergySystem from a string (using UTF-8 encoding)
    def load_from_string(self, string):
         ...
ewoudwerkman commented 5 years ago

Zie voor voorbeelden pull request #4

redekok commented 5 years ago

@ewoudwerkman Thanks for the examples; those were very helpful! I still have a question about adding the EnergySystemInformation and corresponding QuantityAndUnits. I've included the lines of code from your example in the EnergySystemHandler class (https://github.com/poc-mondaine/use-case-renewable-electricity/blob/master/energy_system_handler.py#L80-L101 and https://github.com/poc-mondaine/use-case-renewable-electricity/blob/master/connect_to_etm.py#L30) but that doesn't seem to result in changes in the ESDL model (mpoc.esdl). Could you help me out here?

ewoudwerkman commented 5 years ago

@redekok There were a few typo's in your code. The reference is called energySystemInformation with a lowercase e. See pull request #5 . To know this relation's name you can or check the generated code in esdl/esdl.py, or check the ecore-model (if you like reading XMI) or the esdl.png (if you like looking at big images)

What I've also observed it that you are using the dynamic variant of pyEcore (in the energy_system_handler.py there is esdl = DynamicEPackage(esdl_model)). The pro's of this is that when @edwinmatthijssen pushes a new ESDL release, you automatically get the changes (e.g. an id for a KPI which he planned for today). The backside of the dynamic variant is that you don't get autocompletion (as the whole model is in memory, so e.g. PyCharm cannot do any autocompletion).

redekok commented 5 years ago

@ewoudwerkman Thanks for identifying the typos! It now works as it should, great! :-)