GRIDAPPSD / gridappsd-python

Python connector to gridappsd
5 stars 6 forks source link

distributed agent child class area attribute can be undefined #116

Closed afisher1 closed 1 year ago

afisher1 commented 1 year ago

Describe the bug This is an issue in FeederAgent, SwitchAreaAgent, and SecondaryAreaAgent classes in agents.py. I will use the FeederAgent class to illustrate. self.feeder_area is only defined in the FeederAgent.init() function if self.agent_area_dict is not None. self.agent_area_dict is None if the FeederAgent instance was created with the argument, feeder_dict = None. This is allowed by design as you can call the parent class DistributedAgent.connect() and that will query the proper area context manager to populate self.agent_area_dict. However, feeder_dict is an designed as an attribute of the child class and thus never gets created in this scenario. So an error gets thrown if you try to use it.

To solve this I defined the feeder_dict as None by default in the init function and then created a connect function that calls the parent class's connect function and then proceeds to properly assign feeder_dict.

here is the code example of my proposed solution:

class FeederAgent(DistributedAgent):

    def __init__(self,
                 upstream_message_bus_def: MessageBusDefinition,
                 downstream_message_bus_def: MessageBusDefinition,
                 agent_config: Dict,
                 feeder_dict=None,
                 simulation_id=None):
        super(FeederAgent,
              self).__init__(upstream_message_bus_def,
                             downstream_message_bus_def, agent_config,
                             feeder_dict, simulation_id)
        **self.feeder_area = None
        self.downstream_message_bus_def = downstream_message_bus_def**
        if self.agent_area_dict is not None:
            feeder = cim.Feeder(mRID=self.downstream_message_bus_def.id)
            self.feeder_area = DistributedModel(connection=self.connection,
                                                feeder=feeder,
                                                topology=self.agent_area_dict)

    **def connect(self):
        super().connect()
        if self.feeder_area is None:
            feeder = cim.Feeder(mRID=self.downstream_message_bus_def.id)
            self.feeder_area = DistributedModel(connection=self.connection,
                                                feeder=feeder,
                                                topology=self.agent_area_dict)**

To Reproduce Steps to reproduce the behavior:

  1. create an instance of FeederAgent with feeder_dict = None.
  2. call the instance's connect function.
  3. try and print the instances feeder_area attribute

Expected behavior after connect is called feeder_area should be properly defined.

afisher1 commented 1 year ago

I have this code in my local after_test branch and can commit it if you would like.

poorva1209 commented 1 year ago

@afisher1 @craig8 Looks like the solution for this is merged in develop? Should I close this issue?

afisher1 commented 1 year ago

Yes Craig and I went ahead and made a PR while we were testing his splitting of field_bus and gridappsd-python core stuff. This issue can be closed.