model.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from DEVS import *
  2. from infinity import INFINITY
  3. class MyAtomicDSModel(AtomicDEVS):
  4. def __init__(self, my_id):
  5. AtomicDEVS.__init__(self, "MyAtomicDSModel[%s]" % my_id)
  6. self.my_id = my_id
  7. self.ctr = 0
  8. self.remaining = 1
  9. self.out_port = self.addOutPort("out_port")
  10. self.in_port = self.addInPort("in_port")
  11. def set_sim(self, sim):
  12. self.sim = sim
  13. def timeAdvance(self):
  14. return self.remaining
  15. def outputFnc(self):
  16. print 'MyAtomicDSModel(%s) outputFnc' % id(self)
  17. return {self.out_port: "MyMessage"}
  18. def intTransition(self):
  19. self.ctr += 1
  20. self.remaining = 1
  21. print 'MyAtomicDSModel(%s:%s) intTransition' % (id(self), self.ctr)
  22. def extTransition(self, inputs):
  23. self.remaining -= self.elapsed
  24. print 'MyAtomicDSModel(%s) extTransition' % id(self)
  25. self.ctr += 1
  26. def modelTransition(self, state):
  27. print 'MyAtomicDSModel(%s) modelTransition %s' % (id(self), self.ctr)
  28. if self.ctr % 2 == 0:
  29. state[self.my_id] = "present"
  30. return True
  31. else:
  32. return False
  33. class Root(CoupledDEVS):
  34. def __init__(self):
  35. CoupledDEVS.__init__(self, "CoupledDEVS")
  36. self.amodel = self.addSubModel(MyAtomicDSModel(0))
  37. self.bmodel = self.addSubModel(MyAtomicDSModel(1))
  38. self.connectPorts(self.amodel.out_port, self.bmodel.in_port)
  39. self.connectPorts(self.bmodel.out_port, self.amodel.in_port)
  40. def modelTransition(self, state):
  41. print 'MyDSModel modelTransition %s' % state