transferfunction.rst 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. ..
  2. Copyright 2014 Modelling, Simulation and Design Lab (MSDL) at
  3. McGill University and the University of Antwerp (http://msdl.cs.mcgill.ca/)
  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. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. Transfer functions
  14. ==================
  15. The DEVS formalism (both Classic and Parallel) allows transfer functions for:
  16. * input to input couplings
  17. * output to input couplings
  18. * output to output couplings
  19. The couplings can thus be annotated with functions that translate the actual message on it to another one.
  20. The syntax for this annotation is by passing a function to the *connectPorts* method.
  21. That function will be called for every event seperatly (for Parallel DEVS: every element of the bag; for Classic DEVS: every element).
  22. Using this functionality does not require any additional configurations.
  23. It is important to note that using such a function will have a severe performance impact for events that are send on this connection.
  24. Before calling the function, the event itself will already be coupled. After this, the translated event will again be copied.
  25. The first copy will *always* be using the pickle method, the second copy will be the one specified by the user.
  26. If the function does not return anything, default Python semantics will cause the function to return *None*.
  27. Such a function can be passed to every call to *connectPorts*, be it an input-to-input, output-to-input or output-to-output connection.
  28. The 'total translation function' will be constructed only once at the start and this composite function will be called each time.
  29. Couplings with no translation function on them will simply copy the output to the input.
  30. Thus ommitting the parameter or passing *None* will have no effect.
  31. Example
  32. -------
  33. A simple example is a function which translates *OutputEvent* events to *InputEvent* events::
  34. def translate_inputevent_to_outputevent(inputEvent):
  35. # For simplicity, we assume that the OutputEvent constructor
  36. # takes an InputEvent as its argument
  37. return OutputEvent(inputEvent)
  38. To use this function on a connection::
  39. class MyModel(CoupledDEVS):
  40. def __init__(self, name):
  41. ...
  42. self.connectPorts(self.model1.outport, self.model2.inport, translate_inputevent_to_outputevent)
  43. ...