Source code for synergine.synergy.event.Event
from synergine.core.exceptions import NotConcernedEvent
from synergine.core.simulation.mechanism.Mechanism import Mechanism
from synergine.cst import COL_ALL
[docs]class Event():
"""
Event are called by mechanisms and trig associated actions if conditions matches.
"""
_mechanism = Mechanism
"""Mechanism class who run this event with prepared parameters"""
_concern = COL_ALL
"""The COL id of concerned synergies objects"""
_each_cycle = 1
"""Event ca be executed each x cycle if needed"""
_first_cycle_force = False
"""Event will be executed at first cycle regardless of _each_cycle"""
def __init__(self, actions):
self._actions = actions
@classmethod
[docs] def get_mechanism(cls):
"""
:return: Mechanism class who will run this event
:rtype: Mechanism
"""
return cls._mechanism
@classmethod
[docs] def get_concern(cls):
"""
:return: COL name if concerned synergies objects
"""
return cls._concern
@classmethod
[docs] def get_each_cycle(cls):
"""
:return: The number of each cycle where execute this event
"""
return cls._each_cycle
@classmethod
[docs] def is_first_cycle_force(cls):
return cls._first_cycle_force
[docs] def observe(self, object_id, context, parameters={}):
"""
Return actions who have to be executed.
:param object_id: The id of observed synergy object
:param context: The Context
:param parameters: Mechanism prepared dict of parameters
:return: list of actions
:rtype: list (of Action)
"""
active_actions = []
try:
parameters = self._prepare(object_id, context, parameters)
for action in self._actions:
action_object = action(object_id, parameters)
active_actions.append(action_object)
except NotConcernedEvent:
pass # Object not concerned by this event
return active_actions
[docs] def _prepare(self, object_id, context, parameters={}):
"""
Return dict with parameters for actions
:param object_id: The id of observed synergy object
:param context: The Context
:param parameters: Mechanism prepared dict of parameters
:raise: NotConcernedEvent
:return:
"""
raise NotImplementedError()