classicDEVSWrapper.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # Copyright 2014 Modelling, Simulation and Design Lab (MSDL) at
  2. # McGill University and the University of Antwerp (http://msdl.cs.mcgill.ca/)
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """
  16. A wrapper for AtomicDEVS models that are to be interpreted as Classic DEVS models
  17. """
  18. class ClassicDEVSWrapper(object):
  19. """
  20. Wraps around a normal AtomicDEVS model and intercepts the DEVS specific functions. All attribute read/writes need to be redirected to the model itself.
  21. """
  22. def __init__(self, model):
  23. """
  24. Constructor
  25. :param model: the model to wrap around
  26. """
  27. self.model = model
  28. def __getattr__(self, attr):
  29. """
  30. Fetches the attributes of the model. This is a 'magic' function.
  31. :param attr: the attribute to fetch
  32. :returns: the fetched attributed
  33. """
  34. return getattr(self.model, attr)
  35. def __setattr__(self, attr, val):
  36. """
  37. Sets the attribute of the model. This is a 'magic' function. Only the 'model' attribute is not proxied!
  38. :param attr: the attribute to set
  39. :param val: the value to set it to
  40. """
  41. if attr == "model":
  42. object.__setattr__(self, attr, val)
  43. return setattr(self.model, attr, val)
  44. def extTransition(self, inputs):
  45. """
  46. Wrap around the extTransition function by changing the input dictionary
  47. :param inputs: the input dictionary with lists as values
  48. :returns: the new state, as the normal extTransition method would do
  49. """
  50. return self.model.extTransition({i: inputs[i][0] for i in inputs})
  51. def outputFnc(self):
  52. """
  53. Wrap around the outputFnc function by changing the returned dictionary
  54. :returns: the changed dictionary
  55. """
  56. retval = self.model.outputFnc()
  57. return {i: [retval[i]] for i in retval}