Core   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Importance

Changes 9
Bugs 0 Features 0
Metric Value
c 9
b 0
f 0
dl 0
loc 111
rs 10
wmc 26

13 Methods

Rating   Name   Duplication   Size   Complexity  
A have_to_be_runned_by() 0 2 1
A _initialize_connecteds() 0 3 1
A start_core() 0 8 2
A _wait_for_next_cycle() 0 3 2
A get_terminal() 0 2 1
A _run_connecteds() 0 2 1
A _update_last_cycle_time() 0 2 1
B _load_configuration() 0 10 6
A _initialize_global_parameters() 0 4 2
A get_configuration_manager() 0 3 1
B __init__() 0 26 1
A _end() 0 3 1
B run() 0 23 6
1
from synergine.core.SynergyObjectManager import SynergyObjectManager
2
from synergine.core.CycleCalculator import CycleCalculator
3
from synergine.core.SpaceDataConnector import SpaceDataConnector
4
from synergine.core.config.ConfigurationManager import ConfigurationManager
5
from synergine.core.connection.Connector import Connector
6
from synergine.lib.factory.factory import Factory
7
from synergine.core.cycle.Context import Context
8
from time import time, sleep
9
from os import listdir
10
from os.path import isdir, join as join_path
11
from importlib import import_module
12
from synergine.core.Signals import Signals
13
from random import seed
14
15
16
class Core():
17
    """
18
    Core of Synergine
19
    Manage cycle calculator, terminals, ...
20
    """
21
22
    _configuration_manager = ConfigurationManager()
23
24
    @classmethod
25
    def start_core(cls, config, modules_path=None):
26
        core = cls(config, modules_path)
27
        have_to_be_runned_by = core.have_to_be_runned_by()
28
        if have_to_be_runned_by:
29
            have_to_be_runned_by.encapsulate_run(core.run)
30
        else:
31
            core.run()
32
33
    @classmethod
34
    def get_configuration_manager(cls):
35
        return cls._configuration_manager
36
37
    def __init__(self, config: dict, modules_path=None):
38
        """
39
40
        :param config: dict containing the config. Will be used to instanciate ConfigurationManager)
41
        :param modules_path: path of modules. To retrieve module config
42
        TODO: obsolete ?
43
        :return: Core
44
        """
45
        self._load_configuration(modules_path, config)
46
        self._initialize_global_parameters(config)
47
        self._context = self._configuration_manager.get('app.classes.Context', Context)()
48
        self._context.metas.reset()
49
        Signals.reset()
50
        self._factory = Factory()
51
        self._synergy_object_manager = SynergyObjectManager(self._configuration_manager.get('simulations'),
52
                                                            self._context)
53
        self._cycle_calculator = CycleCalculator(self._context,
54
                                                 self._synergy_object_manager,
55
                                                 self.get_configuration_manager(),
56
                                                 force_main_process=self._configuration_manager.get(
57
                                                     'engine.debug.mainprocess', False))
58
        self._space_data_connector = SpaceDataConnector()
59
        self._last_cycle_time = time()
60
        self._maxfps = self._configuration_manager.get('engine.fpsmax')
61
        self._connector = Connector(self._synergy_object_manager, self._context)
62
        self._initialize_connecteds()
63
64
    def _load_configuration(self, modules_path, app_config):
65
        if modules_path:
66
            modules = [file for file in listdir(modules_path) if isdir(join_path(modules_path, file))]
67
            for module in modules:
68
                try:
69
                    module_config_module = import_module(modules_path + '.' + module + '.config')
70
                    self._configuration_manager.load(module_config_module.config)
71
                except ImportError:
72
                    pass
73
        self._configuration_manager.load(app_config)
74
75
    def _initialize_global_parameters(self, config):
76
        random_seed = self._configuration_manager.get('engine.debug.seed', '__no_set__')
77
        if random_seed != '__no_set__':
78
            seed(random_seed)
79
80
    def run(self, screen=None):
81
        if screen:
82
            self._connector.send_screen_to_connection(screen)
83
        self._run_connecteds()
84
        self._wait_for_next_cycle()
85
86
        cycles = self._configuration_manager.get('engine.debug.cycles', -1)
87
        finish = False
88
        if cycles is 0:
89
            finish = True
90
        while not finish:
91
92
            self._update_last_cycle_time()
93
            # start_time = time()
94
            actions_done = self._cycle_calculator.compute()
95
            self._run_connecteds(actions_done)
96
            # print(time() - start_time)
97
            self._wait_for_next_cycle()
98
99
            if self._cycle_calculator.get_cycle() >= cycles and cycles is not -1:
100
                finish = True
101
102
        self._end()
103
104
    def _end(self):
105
        self._cycle_calculator.end()
106
        self._connector.terminate()
107
108
    def _update_last_cycle_time(self):
109
        self._last_cycle_time = time()
110
111
    def _wait_for_next_cycle(self):
112
        if self._maxfps is not True:
113
            sleep(max(1. / self._maxfps - (time() - self._last_cycle_time), 0))
114
115
    def _initialize_connecteds(self):
116
        self._connector.initialize_terminals(self._configuration_manager.get('connections'),
117
                                             self._configuration_manager)
118
119
    def _run_connecteds(self, actions_done=[]):
120
        self._connector.cycle(actions_done)
121
122
    def have_to_be_runned_by(self):
123
        return self._connector.get_connection_who_have_to_run_core()
124
125
    def get_terminal(self, terminal_name):
126
        return self._connector.get_terminal(terminal_name)