nico 8 лет назад
Родитель
Сommit
81671002f0
4 измененных файлов с 158 добавлено и 156 удалено
  1. 115 0
      Tests/test_launchers.py
  2. 0 115
      Tests/test_order_analyser.py
  3. 43 0
      kalliope/core/NeuronLauncher.py
  4. 0 41
      kalliope/core/OrderAnalyser.py

+ 115 - 0
Tests/test_launchers.py

@@ -6,6 +6,7 @@ from kalliope.core.NeuronLauncher import NeuronLauncher
 from kalliope.core.SynapseLauncher import SynapseLauncher, SynapseNameNotFound
 from kalliope.core.TriggerLauncher import TriggerLauncher
 from kalliope.core.ConfigurationManager import SettingLoader
+from kalliope.core.Models.Settings import Settings
 
 from kalliope.core.Models.Trigger import Trigger
 from kalliope.core.Models.Neuron import Neuron
@@ -136,3 +137,117 @@ class TestLaunchers(unittest.TestCase):
                                                                  resources_dir=sl.settings.resources.neuron_folder)
             mock_get_class_instantiation.reset_mock()
 
+    def test_replace_global_variables(self):
+        """
+        Testing the _replace_global_variables function from the NeuronLauncher.
+        Scenarii:
+            - 1/ only one global variable
+            - 2/ global variable with string after
+            - 3/ global variable with int after
+            - 4/ multiple global variables
+            - 5/ parameter value is a list
+
+        """
+
+        # 1/ only one global variable
+        neuron1 = Neuron(name='neuron1', parameters={'var1': '{{hello}}'})
+        variables = {
+            "hello": "test",
+            "hello2": "test2",
+        }
+        st = Settings(variables=variables)
+
+        expected_neuron_result = Neuron(name='neuron1', parameters={'var1': 'test'})
+
+        # assign global variable to neuron1
+        NeuronLauncher._replace_global_variables(neuron=neuron1,
+                                                settings=st)
+        self.assertEquals(neuron1,
+                          expected_neuron_result,
+                          "Fail to assign a single global variable to neuron")
+
+        # 2/ global variable with string after
+        neuron1 = Neuron(name='neuron1', parameters={'var1': '{{hello}} Sispheor'})
+        variables = {
+            "hello": "test",
+            "hello2": "test2",
+        }
+        st = Settings(variables=variables)
+
+        expected_neuron_result = Neuron(name='neuron1', parameters={'var1': 'test Sispheor'})
+
+        # assign global variable to neuron1
+        NeuronLauncher._replace_global_variables(neuron=neuron1,
+                                                settings=st)
+        self.assertEquals(neuron1,
+                          expected_neuron_result,
+                          "Fail to assign a global variable with string after to neuron")
+
+        # 3/ global variable with int after
+        neuron1 = Neuron(name='neuron1', parameters={'var1': '{{hello}}0'})
+        variables = {
+            "hello": 60,
+            "hello2": "test2",
+        }
+        st = Settings(variables=variables)
+
+        expected_neuron_result = Neuron(name='neuron1', parameters={'var1': '600'})
+
+        # assign global variable to neuron1
+        NeuronLauncher._replace_global_variables(neuron=neuron1,
+                                                settings=st)
+        self.assertEquals(neuron1,
+                          expected_neuron_result,
+                          "Fail to assign global variable with int after to neuron")
+
+        # 4/ multiple global variables
+        neuron1 = Neuron(name='neuron1', parameters={'var1': '{{hello}} {{me}}'})
+        variables = {
+            "hello": "hello",
+            "me": "LaMonf"
+        }
+        st = Settings(variables=variables)
+
+        expected_neuron_result = Neuron(name='neuron1', parameters={'var1': 'hello LaMonf'})
+
+        # assign global variable to neuron1
+        NeuronLauncher._replace_global_variables(neuron=neuron1,
+                                                settings=st)
+        self.assertEquals(neuron1,
+                          expected_neuron_result,
+                          "Fail to assign multiple global variables to neuron")
+
+        # 5/ parameter value is a list
+        neuron1 = Neuron(name='neuron1', parameters={'var1': '[hello {{name}}, bonjour {{name}}]'})
+        variables = {
+            "name": "LaMonf",
+            "hello2": "test2",
+        }
+        st = Settings(variables=variables)
+
+        expected_neuron_result = Neuron(name='neuron1', parameters={'var1': '[hello LaMonf, bonjour LaMonf]'})
+
+        # assign global variable to neuron1
+        NeuronLauncher._replace_global_variables(neuron=neuron1,
+                                                settings=st)
+        self.assertEquals(neuron1,
+                          expected_neuron_result,
+                          "Fail to assign a single global when parameter value is a list to neuron")
+
+    def test_get_global_variable(self):
+        """
+        Test the get_global_variable of the OrderAnalyser Class
+        """
+        sentence = "i am {{name2}}"
+        variables = {
+            "name": "LaMonf",
+            "name2": "kalliope",
+        }
+        st = Settings(variables=variables)
+
+        expected_result = "i am kalliope"
+
+        self.assertEquals(NeuronLauncher._get_global_variable(sentence=sentence,
+                                                             settings=st),
+                          expected_result,
+                          "Fail to get the global variable from the sentence")

+ 0 - 115
Tests/test_order_analyser.py

@@ -571,121 +571,6 @@ class TestOrderAnalyser(unittest.TestCase):
                           expected_result,
                           "Fail to run the default synapse")
 
-    def test_replace_global_variables(self):
-        """
-        Testing the _replace_global_variables function from the OrderAnalyser.
-        Scenarii:
-            - 1/ only one global variable
-            - 2/ global variable with string after
-            - 3/ global variable with int after
-            - 4/ multiple global variables
-            - 5/ parameter value is a list
-
-        """
-
-        # 1/ only one global variable
-        neuron1 = Neuron(name='neuron1', parameters={'var1': '{{hello}}'})
-        variables = {
-            "hello": "test",
-            "hello2": "test2",
-        }
-        st = Settings(variables=variables)
-
-        expected_neuron_result = Neuron(name='neuron1', parameters={'var1': 'test'})
-
-        # assign global variable to neuron1
-        OrderAnalyser._replace_global_variables(neuron=neuron1,
-                                                settings=st)
-        self.assertEquals(neuron1,
-                          expected_neuron_result,
-                          "Fail to assign a single global variable to neuron")
-
-        # 2/ global variable with string after
-        neuron1 = Neuron(name='neuron1', parameters={'var1': '{{hello}} Sispheor'})
-        variables = {
-            "hello": "test",
-            "hello2": "test2",
-        }
-        st = Settings(variables=variables)
-
-        expected_neuron_result = Neuron(name='neuron1', parameters={'var1': 'test Sispheor'})
-
-        # assign global variable to neuron1
-        OrderAnalyser._replace_global_variables(neuron=neuron1,
-                                                settings=st)
-        self.assertEquals(neuron1,
-                          expected_neuron_result,
-                          "Fail to assign a global variable with string after to neuron")
-
-        # 3/ global variable with int after
-        neuron1 = Neuron(name='neuron1', parameters={'var1': '{{hello}}0'})
-        variables = {
-            "hello": 60,
-            "hello2": "test2",
-        }
-        st = Settings(variables=variables)
-
-        expected_neuron_result = Neuron(name='neuron1', parameters={'var1': '600'})
-
-        # assign global variable to neuron1
-        OrderAnalyser._replace_global_variables(neuron=neuron1,
-                                                settings=st)
-        self.assertEquals(neuron1,
-                          expected_neuron_result,
-                          "Fail to assign global variable with int after to neuron")
-
-        # 4/ multiple global variables
-        neuron1 = Neuron(name='neuron1', parameters={'var1': '{{hello}} {{me}}'})
-        variables = {
-            "hello": "hello",
-            "me": "LaMonf"
-        }
-        st = Settings(variables=variables)
-
-        expected_neuron_result = Neuron(name='neuron1', parameters={'var1': 'hello LaMonf'})
-
-        # assign global variable to neuron1
-        OrderAnalyser._replace_global_variables(neuron=neuron1,
-                                                settings=st)
-        self.assertEquals(neuron1,
-                          expected_neuron_result,
-                          "Fail to assign multiple global variables to neuron")
-
-        # 5/ parameter value is a list
-        neuron1 = Neuron(name='neuron1', parameters={'var1': '[hello {{name}}, bonjour {{name}}]'})
-        variables = {
-            "name": "LaMonf",
-            "hello2": "test2",
-        }
-        st = Settings(variables=variables)
-
-        expected_neuron_result = Neuron(name='neuron1', parameters={'var1': '[hello LaMonf, bonjour LaMonf]'})
-
-        # assign global variable to neuron1
-        OrderAnalyser._replace_global_variables(neuron=neuron1,
-                                                settings=st)
-        self.assertEquals(neuron1,
-                          expected_neuron_result,
-                          "Fail to assign a single global when parameter value is a list to neuron")
-
-    def test_get_global_variable(self):
-        """
-        Test the get_global_variable of the OrderAnalyser Class
-        """
-        sentence = "i am {{name2}}"
-        variables = {
-            "name": "LaMonf",
-            "name2": "kalliope",
-        }
-        st = Settings(variables=variables)
-
-        expected_result = "i am kalliope"
-
-        self.assertEquals(OrderAnalyser._get_global_variable(sentence=sentence,
-                                                             settings=st),
-                          expected_result,
-                          "Fail to get the global variable from the sentence")
-
 
 if __name__ == '__main__':
     unittest.main()

+ 43 - 0
kalliope/core/NeuronLauncher.py

@@ -26,7 +26,50 @@ class NeuronLauncher:
         neuron_folder = None
         if settings.resources:
             neuron_folder = settings.resources.neuron_folder
+
+        cls._replace_global_variables(neuron, settings)
+
         return Utils.get_dynamic_class_instantiation(package_name="neurons",
                                                      module_name=neuron.name,
                                                      parameters=neuron.parameters,
                                                      resources_dir=neuron_folder)
+
+    @classmethod
+    def _replace_global_variables(cls, neuron, settings):
+        """
+        Replace all the parameters with variables with the variable value.
+        :param neuron: the neuron
+        :param settings: the settings
+        """
+        for param in neuron.parameters:
+            if isinstance(neuron.parameters[param], list):
+                list_param_value = list()
+                for sentence in neuron.parameters[param]:
+                    sentence_with_global_variables = cls._get_global_variable(sentence=sentence,
+                                                                              settings=settings)
+                    list_param_value.append(sentence_with_global_variables)
+                neuron.parameters[param] = list_param_value
+
+            else:
+                if Utils.is_containing_bracket(neuron.parameters[param]):
+                    sentence_with_global_variables = cls._get_global_variable(sentence=neuron.parameters[param],
+                                                                              settings=settings)
+                    neuron.parameters[param] = sentence_with_global_variables
+
+    @staticmethod
+    def _get_global_variable(sentence, settings):
+        """
+        Get the global variable from the sentence with brackets
+        :param sentence: the sentence to check
+        :return: the global variable
+        """
+        sentence_no_spaces = Utils.remove_spaces_in_brackets(sentence=sentence)
+        list_of_bracket_params = Utils.find_all_matching_brackets(sentence=sentence_no_spaces)
+        for param_with_bracket in list_of_bracket_params:
+            param_no_brackets = param_with_bracket.replace("{{", "").replace("}}", "")
+            if param_no_brackets in settings.variables:
+                logger.debug("Replacing variable %s with  %s" % (param_with_bracket,
+                                                                 settings.variables[param_no_brackets]))
+                sentence_no_spaces = sentence_no_spaces.replace(param_with_bracket,
+                                                                str(settings.variables[param_no_brackets]))
+        return sentence_no_spaces

+ 0 - 41
kalliope/core/OrderAnalyser.py

@@ -149,49 +149,8 @@ class OrderAnalyser:
     def _start_list_neurons(cls, list_neurons, params, settings):
         # start neurons
         for neuron in list_neurons:
-            cls._replace_global_variables(neuron, settings)
             cls._start_neuron(neuron, params)
 
-    @classmethod
-    def _replace_global_variables(cls, neuron, settings):
-        """
-        Replace all the parameters with variables with the variable value.
-        :param neuron: the neuron
-        :param settings: the settings
-        """
-        for param in neuron.parameters:
-            if isinstance(neuron.parameters[param], list):
-                list_param_value = list()
-                for sentence in neuron.parameters[param]:
-                    sentence_with_global_variables = cls._get_global_variable(sentence=sentence,
-                                                                              settings=settings)
-                    list_param_value.append(sentence_with_global_variables)
-                neuron.parameters[param] = list_param_value
-
-            else:
-                if Utils.is_containing_bracket(neuron.parameters[param]):
-                    sentence_with_global_variables = cls._get_global_variable(sentence=neuron.parameters[param],
-                                                                              settings=settings)
-                    neuron.parameters[param] = sentence_with_global_variables
-
-    @staticmethod
-    def _get_global_variable(sentence, settings):
-        """
-        Get the global variable from the sentence with brackets
-        :param sentence: the sentence to check
-        :return: the global variable
-        """
-        sentence_no_spaces = Utils.remove_spaces_in_brackets(sentence=sentence)
-        list_of_bracket_params = Utils.find_all_matching_brackets(sentence=sentence_no_spaces)
-        for param_with_bracket in list_of_bracket_params:
-            param_no_brackets = param_with_bracket.replace("{{", "").replace("}}", "")
-            if param_no_brackets in settings.variables:
-                logger.debug("Replacing variable %s with  %s" % (param_with_bracket,
-                                                                 settings.variables[param_no_brackets]))
-                sentence_no_spaces = sentence_no_spaces.replace(param_with_bracket,
-                                                                str(settings.variables[param_no_brackets]))
-        return sentence_no_spaces
-
     @staticmethod
     def _start_neuron(neuron, params):
         """