test_cycle.py 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # coding: utf-8
  2. from synergine2.config import Config
  3. from synergine2.cycle import CycleManager
  4. from synergine2.log import SynergineLogger
  5. from synergine2.share import shared
  6. from synergine2.simulation import Simulation
  7. from synergine2.simulation import SimulationBehaviour
  8. from synergine2.simulation import SimulationMechanism
  9. from synergine2.simulation import Event
  10. from synergine2.simulation import Subject
  11. from synergine2.simulation import Subjects
  12. from synergine2.simulation import SubjectMechanism
  13. from synergine2.simulation import SubjectBehaviour
  14. from tests import BaseTest
  15. class MyEvent(Event):
  16. def __init__(self, value):
  17. self.value = value
  18. class MySubjectMechanism(SubjectMechanism):
  19. def run(self):
  20. return 42
  21. class MySubjectBehavior(SubjectBehaviour):
  22. use = [MySubjectMechanism]
  23. def run(self, data):
  24. if MySubjectMechanism in data and data[MySubjectMechanism] == 42:
  25. return self.subject.id
  26. def action(self, data) -> [Event]:
  27. return [MyEvent(data * 2)]
  28. class MySubject(Subject):
  29. behaviours_classes = [MySubjectBehavior]
  30. class MySubjects(Subjects):
  31. pass
  32. class MySimulationMechanism(SimulationMechanism):
  33. def run(self, process_number: int = None, process_count: int = None):
  34. return process_number + 1000
  35. class MySimulationBehaviour(SimulationBehaviour):
  36. use = [MySimulationMechanism]
  37. @classmethod
  38. def merge_data(cls, new_data, start_data=None):
  39. start_data = start_data or 0
  40. return start_data + new_data
  41. def run(self, data):
  42. return data[MySimulationMechanism] * 2
  43. def action(self, data) -> [Event]:
  44. return [MyEvent(data)]
  45. class MySimulation(Simulation):
  46. behaviours_classes = [MySimulationBehaviour]
  47. class TestCycle(BaseTest):
  48. def test_subjects_cycle(self):
  49. shared.reset()
  50. config = Config({'core': {'use_x_cores': 2}})
  51. logger = SynergineLogger(name='test')
  52. simulation = Simulation(config)
  53. subjects = MySubjects(simulation=simulation)
  54. simulation.subjects = subjects
  55. for i in range(3):
  56. subjects.append(MySubject(config, simulation=simulation))
  57. cycle_manager = CycleManager(
  58. config=config,
  59. logger=logger,
  60. simulation=simulation,
  61. )
  62. events = cycle_manager.next()
  63. cycle_manager.stop()
  64. assert 3 == len(events)
  65. event_values = [e.value for e in events]
  66. assert all([s.id * 2 in event_values for s in subjects])
  67. def test_new_subject(self):
  68. shared.reset()
  69. config = Config({'core': {'use_x_cores': 1}})
  70. logger = SynergineLogger(name='test')
  71. simulation = Simulation(config)
  72. subjects = MySubjects(simulation=simulation)
  73. simulation.subjects = subjects
  74. for i in range(3):
  75. subjects.append(MySubject(config, simulation=simulation))
  76. cycle_manager = CycleManager(
  77. config=config,
  78. logger=logger,
  79. simulation=simulation,
  80. )
  81. events = cycle_manager.next()
  82. assert 3 == len(events)
  83. event_values = [e.value for e in events]
  84. assert all([s.id * 2 in event_values for s in subjects])
  85. subjects.append(MySubject(config, simulation=simulation))
  86. events = cycle_manager.next()
  87. cycle_manager.stop()
  88. assert 4 == len(events)
  89. event_values = [e.value for e in events]
  90. assert all([s.id * 2 in event_values for s in subjects])
  91. def test_simulation_events(self):
  92. shared.reset()
  93. config = Config({'core': {'use_x_cores': 2}})
  94. logger = SynergineLogger(name='test')
  95. simulation = MySimulation(config)
  96. subjects = MySubjects(simulation=simulation)
  97. simulation.subjects = subjects
  98. cycle_manager = CycleManager(
  99. config=config,
  100. logger=logger,
  101. simulation=simulation,
  102. )
  103. events = cycle_manager.next()
  104. cycle_manager.stop()
  105. assert 1 == len(events)
  106. assert events[0].value == 4002