nesting.rst 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. Nested simulation
  14. =================
  15. .. versionchanged:: 2.1.3
  16. Nested simulation is only possible if **both** the nested simulation and the invoking simulation are **local** simulations.
  17. .. versionchanged:: 2.2.0
  18. Allow nested local simulations in distributed simulations.
  19. Nested simulation allows a simulation to be influenced by the results of another simulation. This functionality is very simple to use, as it just works as expected. In one of the user-defined functions of the model, it is thus possible to create another model and another simulator and simulate that new simulator. The only difference is that some specific configurations do not work on the nested simulation, such as using a logging server.
  20. An example of nesting is provided in the remainder of this subsection.
  21. Suppose we want to create a *Processor* which has a state of how many messages it has already processed. This amount of messages is then used to determine the *timeAdvance*, but in a very specific way that is determined by another DEVS simulation. This example is somewhat contrived, though the example is supposed to be as small as possible to only show how it works, not the utility of the feature.
  22. The following code implements such behaviour::
  23. class State(object):
  24. def __init__(self):
  25. self.processed = 0
  26. self.processing = False
  27. class NestedProcessor(AtomicDEVS):
  28. def __init__(self):
  29. AtomicDEVS.__init__(self, "Nested")
  30. self.state = State()
  31. self.inport = self.addInPort("inport")
  32. self.outport = self.addOutPort("outport")
  33. def extTransition(self, inputs):
  34. self.state.processing = True
  35. return self.state
  36. def intTransition(self):
  37. self.state.processed += 1
  38. return self.state
  39. def outputFnc(self):
  40. return {self.outport: [1]}
  41. def timeAdvance(self):
  42. if self.state.processing:
  43. # Determine the time based on another simulation
  44. from simulator import Simulator
  45. from myqueue import CQueue
  46. model = CQueue()
  47. # The processed attribute of the state is used to determine the processing time in our example
  48. model.queue.processing_time = self.state.processed
  49. sim = Simulator(model)
  50. sim.setTerminationTime(5.0)
  51. sim.simulate()
  52. return max(1, model.queue.state)
  53. else:
  54. return INFINITY
  55. In our example, we only used nested simulation in the *timeAdvance* function, though it is possible everywhere.