1 | from synergine.core.exceptions import UselessMechanism |
||
2 | from synergine.lib.process.tool import get_chunk |
||
3 | |||
4 | |||
5 | class Mechanism(): |
||
6 | """ |
||
7 | Mechanism prepare data for associated events. |
||
8 | The principle: One mechanism compute once what multiple events need. |
||
9 | """ |
||
10 | |||
11 | def __init__(self, events): |
||
12 | self._events = events |
||
13 | self._events_parameters = {} |
||
14 | |||
15 | def run(self, context): |
||
16 | """ |
||
17 | |||
18 | Prepare new mechanism cycle and return Action according to this mechanism events. |
||
19 | |||
20 | :param context: |
||
21 | :return: Actions to run this cycle |
||
22 | :rtype: list (of Action objects) |
||
23 | """ |
||
24 | self._events_parameters = {} |
||
25 | return self._run_events(context) |
||
26 | |||
27 | def _get_object_parameters(self, object_id, context): |
||
28 | """ |
||
29 | |||
30 | Return concerned object parameters for event. |
||
31 | |||
32 | :param object_id: Concerned object if |
||
33 | :param context: The Context |
||
34 | :return: parameters who will be used by event |
||
35 | """ |
||
36 | if object_id not in self._events_parameters: |
||
37 | self._events_parameters[object_id] = self._get_computed_object_event_parameters(object_id, context) |
||
38 | return self._events_parameters[object_id] |
||
39 | |||
40 | def _run_events(self, context): |
||
41 | """ |
||
42 | |||
43 | Return Action according to this mechanism events. |
||
44 | |||
45 | :param context: The Context |
||
46 | :return: list (of Action) |
||
47 | """ |
||
48 | actions = [] |
||
49 | for event in self._events: |
||
50 | if context.get_cycle() % event.get_each_cycle() == 0 \ |
||
51 | or (event.is_first_cycle_force() and context.get_cycle() == 1): |
||
52 | concerned_objects_ids = get_chunk(context.get_total_chunk(), |
||
53 | context.get_current_chunk_position(), |
||
54 | context.metas.collections.get(event.get_concern(), allow_empty=True)) |
||
55 | |||
56 | for object_id in concerned_objects_ids: |
||
57 | try: |
||
58 | event_parameters = self._get_object_parameters(object_id, context) |
||
59 | event_actions = event.observe(object_id, context, event_parameters) |
||
60 | for event_action in event_actions: |
||
61 | actions.append(event_action) |
||
62 | except UselessMechanism: |
||
63 | pass # If mechanism useless on this object, don't observe. |
||
64 | return actions |
||
65 | |||
66 | def _get_computed_object_event_parameters(self, object_id, context): |
||
0 ignored issues
–
show
|
|||
67 | """ |
||
68 | |||
69 | (You must override this method in child-class.) |
||
70 | Compute concerned object event parameters. |
||
71 | |||
72 | |||
73 | :param object_id: Concerned object id |
||
74 | :param context: The Context |
||
75 | :return: Concerned object event parameters |
||
76 | """ |
||
77 | return {} |
||
78 |
This check looks for invalid names for a range of different identifiers.
You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.
If your project includes a Pylint configuration file, the settings contained in that file take precedence.
To find out more about Pylint, please refer to their site.