Parcourir la source

fix usage of neurotransmitter + neurotimer

nico il y a 7 ans
Parent
commit
dc95dcb3e4

+ 15 - 3
kalliope/core/LIFOBuffer.py

@@ -41,6 +41,8 @@ class LIFOBuffer(object):
     answer = None
     is_api_call = False
     no_voice = False
+    is_running = False
+    reset_lifo = False
 
     @classmethod
     def set_answer(cls, value):
@@ -51,14 +53,20 @@ class LIFOBuffer(object):
         cls.is_api_call = value
 
     @classmethod
-    def add_synapse_list_to_lifo(cls, matched_synapse_list):
+    def add_synapse_list_to_lifo(cls, matched_synapse_list, high_priority=False):
         """
         Add a synapse list to process to the lifo
         :param matched_synapse_list: List of Matched Synapse
+        :param high_priority: If True, the synapse list added is executed directly
         :return: 
         """
         logger.debug("[LIFOBuffer] Add a new synapse list to process to the LIFO")
         cls.lifo_list.append(matched_synapse_list)
+        if high_priority:
+            cls.reset_lifo = True
+
+        if not cls.is_running:
+            return cls.execute()
 
     @classmethod
     def clean(cls):
@@ -102,6 +110,8 @@ class LIFOBuffer(object):
         cls.is_api_call = is_api_call
         cls.no_voice = no_voice
 
+        cls.is_running = True
+
         try:
             # we keep looping over the LIFO til we have synapse list to process in it
             while cls.lifo_list:
@@ -116,6 +126,7 @@ class LIFOBuffer(object):
                 cls.lifo_list.remove(last_synapse_fifo_list)
                 # clean the cortex from value loaded from order as all synapses have been processed
                 Cortex.clean_parameter_from_order()
+            cls.is_running = False
             raise Serialize
 
         except Serialize:
@@ -193,12 +204,13 @@ class LIFOBuffer(object):
                     # the neuron is fully processed we can remove it from the list
                     matched_synapse.neuron_fifo_list.remove(neuron)
 
-                if instantiated_neuron.pending_synapse:  # the last executed neuron want to run a synapse
+                if cls.reset_lifo:  # the last executed neuron want to run a synapse
                     logger.debug("[LIFOBuffer] Last executed neuron want to run a synapse. Restart the LIFO")
                     # add the synapse to the lifo (inside a list as expected by the lifo)
-                    cls.add_synapse_list_to_lifo([instantiated_neuron.pending_synapse])
+                    # cls.add_synapse_list_to_lifo([instantiated_neuron.pending_synapse])
                     # we have added a list of synapse to the LIFO ! this one must start over.
                     # break all while loop until the execution is back to the LIFO loop
+                    cls.reset_lifo = False
                     raise SynapseListAddedToLIFO
             else:
                 raise Serialize

+ 8 - 17
kalliope/core/NeuronModule.py

@@ -5,6 +5,7 @@ import sys
 
 import six
 from jinja2 import Template
+from kalliope.core.LIFOBuffer import LIFOBuffer
 
 from kalliope.core import OrderListener
 from kalliope.core.ConfigurationManager import SettingLoader, BrainLoader
@@ -230,18 +231,23 @@ class NeuronModule(object):
 
         return returned_message
 
-    def run_synapse_by_name(self, synapse_name, user_order=None, synapse_order=None):
+    @staticmethod
+    def run_synapse_by_name(synapse_name, user_order=None, synapse_order=None, high_priority=False):
         """
         call the lifo for adding a synapse to execute in the list of synapse list to process
         :param synapse_name: The name of the synapse to run
         :param user_order: The user order
         :param synapse_order: The synapse order
+        :param high_priority: If True, the synapse is executed before the end of the current synapse list
         """
         synapse = BrainLoader().get_brain().get_synapse_by_name(synapse_name)
         matched_synapse = MatchedSynapse(matched_synapse=synapse,
                                          matched_order=synapse_order,
                                          user_order=user_order)
-        self.pending_synapse = matched_synapse
+
+        list_synapse_to_process = list()
+        list_synapse_to_process.append(matched_synapse)
+        LIFOBuffer.add_synapse_list_to_lifo(list_synapse_to_process, high_priority=high_priority)
 
     @staticmethod
     def is_order_matching(order_said, order_match):
@@ -323,18 +329,3 @@ class NeuronModule(object):
                     RpiUtils.switch_pin_to_on(rpi_settings.pin_led_talking)
                 else:
                     RpiUtils.switch_pin_to_off(rpi_settings.pin_led_talking)
-
-    def start_synapse_by_name(self, synapse_name, overriding_parameter_dict=None):
-        """
-        Used to run a synapse by name by calling directly the SynapseLauncher class.
-        The Lifo buffer is not aware of this call and so the user cannot get the result
-        :param synapse_name: name of the synapse to run
-        :param overriding_parameter_dict: dict of parameter to pass to the synapse
-        """
-        # received parameters are not coded with utf-8 on python 2 by default.
-        if sys.version_info[0] == 2:
-            reload(sys)
-            sys.setdefaultencoding('utf-8')
-        SynapseLauncher.start_synapse_by_name(synapse_name,
-                                              brain=self.brain,
-                                              overriding_parameter_dict=overriding_parameter_dict)

+ 1 - 1
kalliope/neurons/neurotimer/neurotimer.py

@@ -117,4 +117,4 @@ class Neurotimer(NeuronModule):
         """
         logger.debug("[Neurotimer] waiting time is over, start the synapse %s" % self.synapse)
 
-        self.start_synapse_by_name(synapse_name=self.synapse)
+        self.run_synapse_by_name(synapse_name=self.synapse, high_priority=False)

+ 5 - 4
kalliope/neurons/neurotransmitter/neurotransmitter.py

@@ -21,7 +21,7 @@ class Neurotransmitter(NeuronModule):
         if self._is_parameters_ok():
             if self.direct_link is not None:
                 logger.debug("Neurotransmitter directly call to the synapse name: %s" % self.direct_link)
-                self.run_synapse_by_name(self.direct_link)
+                self.run_synapse_by_name(self.direct_link, high_priority=True)
             else:
                 if self.is_api_call:
                     if self.answer is not None:
@@ -42,7 +42,7 @@ class Neurotransmitter(NeuronModule):
         # print self.links
         # set a bool to know if we have found a valid answer
         if audio is None:
-            self.run_synapse_by_name(self.default)
+            self.run_synapse_by_name(self.default, high_priority=True)
         else:
             found = False
             for el in self.from_answer_link:
@@ -51,11 +51,12 @@ class Neurotransmitter(NeuronModule):
                         logger.debug("Neurotransmitter: match answer: %s" % answer)
                         self.run_synapse_by_name(synapse_name=el["synapse"],
                                                  user_order=audio,
-                                                 synapse_order=answer)
+                                                 synapse_order=answer,
+                                                 high_priority=True)
                         found = True
                         break
             if not found:  # the answer do not correspond to any answer. We run the default synapse
-                self.run_synapse_by_name(self.default)
+                self.run_synapse_by_name(self.default, high_priority=True)
 
     def _is_parameters_ok(self):
         """