|
@@ -0,0 +1,110 @@
|
|
|
+import logging
|
|
|
+
|
|
|
+from kalliope.core.NeuronLauncher import NeuronLauncher
|
|
|
+from kalliope.core.Models import Singleton
|
|
|
+from kalliope.core.Models.APIResponse import APIResponse
|
|
|
+
|
|
|
+logging.basicConfig()
|
|
|
+logger = logging.getLogger("kalliope")
|
|
|
+
|
|
|
+
|
|
|
+class LIFOBuffer(object):
|
|
|
+ """
|
|
|
+ This class is a LIFO list of synapse to process where the last synapse list to enter will be the first synapse
|
|
|
+ list to be processed.
|
|
|
+ This design is needed in order to use Kalliope from the API.
|
|
|
+ Because we want to return an information when a Neuron is still processing and waiting for an answer from the user
|
|
|
+ like with the Neurotransmitter neuron.
|
|
|
+
|
|
|
+ """
|
|
|
+ __metaclass__ = Singleton
|
|
|
+
|
|
|
+ api_response = APIResponse()
|
|
|
+ lifo_list = list()
|
|
|
+ synapse_list_added_to_lifo = False
|
|
|
+ logger.debug("[LIFOBuffer] LIFO buffer created")
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def add_synapse_list_to_lifo(cls, matched_synapse_list):
|
|
|
+ """
|
|
|
+ Add a synapse list to process to the lifo
|
|
|
+ :param matched_synapse_list: List of Matched Synapse
|
|
|
+ :return:
|
|
|
+ """
|
|
|
+ logger.debug("[LIFOBuffer] Add a new synapse list to process to the LIFO")
|
|
|
+ cls.lifo_list.append(matched_synapse_list)
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def execute(cls, answer=None):
|
|
|
+
|
|
|
+
|
|
|
+ while cls.lifo_list:
|
|
|
+ logger.debug("[LIFOBuffer] number of synapse list to process: %s" % len(cls.lifo_list))
|
|
|
+
|
|
|
+
|
|
|
+ cls.synapse_list_added_to_lifo = False
|
|
|
+
|
|
|
+ last_synapse_fifo_list = cls.lifo_list[-1]
|
|
|
+
|
|
|
+ while last_synapse_fifo_list:
|
|
|
+
|
|
|
+ if cls.synapse_list_added_to_lifo:
|
|
|
+ break
|
|
|
+
|
|
|
+ matched_synapse = last_synapse_fifo_list[0]
|
|
|
+
|
|
|
+ cls.api_response.list_processed_matched_synapse.append(matched_synapse)
|
|
|
+
|
|
|
+ while matched_synapse.neuron_fifo_list:
|
|
|
+ if cls.synapse_list_added_to_lifo:
|
|
|
+ break
|
|
|
+ logger.debug("[LIFOBuffer] number of neuron to process: %s" % len(matched_synapse.neuron_fifo_list))
|
|
|
+
|
|
|
+ neuron = matched_synapse.neuron_fifo_list[0]
|
|
|
+
|
|
|
+ if answer is not None:
|
|
|
+ neuron.parameters["answer"] = answer
|
|
|
+
|
|
|
+ answer = None
|
|
|
+
|
|
|
+ neuron.parameters["is_api_call"] = True
|
|
|
+
|
|
|
+ instantiated_neuron = NeuronLauncher.start_neuron_list_refacto(
|
|
|
+ neuron=neuron,
|
|
|
+ parameters_dict=matched_synapse.parameters)
|
|
|
+
|
|
|
+
|
|
|
+ cls.api_response.status = "complete"
|
|
|
+ if not instantiated_neuron.is_waiting_for_answer:
|
|
|
+
|
|
|
+
|
|
|
+ matched_synapse.neuron_module_list.append(instantiated_neuron)
|
|
|
+
|
|
|
+ matched_synapse.neuron_fifo_list.remove(neuron)
|
|
|
+ else:
|
|
|
+ print "Wait for answer mode"
|
|
|
+ cls.api_response.status = "waiting_for_answer"
|
|
|
+
|
|
|
+ will_be_returned = cls.api_response.serialize()
|
|
|
+
|
|
|
+ cls.api_response = APIResponse()
|
|
|
+ return will_be_returned
|
|
|
+
|
|
|
+ if instantiated_neuron.pending_synapse:
|
|
|
+
|
|
|
+ cls.add_synapse_list_to_lifo([instantiated_neuron.pending_synapse])
|
|
|
+
|
|
|
+
|
|
|
+ cls.synapse_list_added_to_lifo = True
|
|
|
+
|
|
|
+
|
|
|
+ if not cls.synapse_list_added_to_lifo:
|
|
|
+
|
|
|
+ last_synapse_fifo_list .remove(matched_synapse)
|
|
|
+
|
|
|
+
|
|
|
+ if not cls.synapse_list_added_to_lifo:
|
|
|
+
|
|
|
+ cls.lifo_list.remove(last_synapse_fifo_list)
|
|
|
+
|
|
|
+ return cls.api_response.serialize()
|