|
@@ -8,8 +8,19 @@ logging.basicConfig()
|
|
logger = logging.getLogger("kalliope")
|
|
logger = logging.getLogger("kalliope")
|
|
|
|
|
|
|
|
|
|
-class Serialize(Exception): pass
|
|
+class Serialize(Exception):
|
|
-class Synapse_LIFO(Exception): pass
|
|
+ """
|
|
|
|
+ When raised, the LIFO class return the current API response to the caller
|
|
|
|
+ """
|
|
|
|
+ pass
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+class SynapseListAddedToLIFO(Exception):
|
|
|
|
+ """
|
|
|
|
+ When raised, a synapse list to process has been added to the LIFO list.
|
|
|
|
+ The LIFO must start over and process the last synapse list added
|
|
|
|
+ """
|
|
|
|
+ pass
|
|
|
|
|
|
|
|
|
|
class LIFOBuffer(object):
|
|
class LIFOBuffer(object):
|
|
@@ -26,6 +37,8 @@ class LIFOBuffer(object):
|
|
api_response = APIResponse()
|
|
api_response = APIResponse()
|
|
lifo_list = list()
|
|
lifo_list = list()
|
|
logger.debug("[LIFOBuffer] LIFO buffer created")
|
|
logger.debug("[LIFOBuffer] LIFO buffer created")
|
|
|
|
+ answer = None
|
|
|
|
+ is_api_call = False
|
|
|
|
|
|
@classmethod
|
|
@classmethod
|
|
def add_synapse_list_to_lifo(cls, matched_synapse_list):
|
|
def add_synapse_list_to_lifo(cls, matched_synapse_list):
|
|
@@ -37,6 +50,21 @@ class LIFOBuffer(object):
|
|
logger.debug("[LIFOBuffer] Add a new synapse list to process to the LIFO")
|
|
logger.debug("[LIFOBuffer] Add a new synapse list to process to the LIFO")
|
|
cls.lifo_list.append(matched_synapse_list)
|
|
cls.lifo_list.append(matched_synapse_list)
|
|
|
|
|
|
|
|
+ @classmethod
|
|
|
|
+ def clean(cls):
|
|
|
|
+ """
|
|
|
|
+ Clean the LIFO by creating a new list
|
|
|
|
+ """
|
|
|
|
+ cls.lifo_list = list()
|
|
|
|
+
|
|
|
|
+ @classmethod
|
|
|
|
+ def _return_serialized_api_response(cls):
|
|
|
|
+
|
|
|
|
+ returned_api_response = cls.api_response.serialize()
|
|
|
|
+
|
|
|
|
+ cls.api_response = APIResponse()
|
|
|
|
+ return returned_api_response
|
|
|
|
+
|
|
@classmethod
|
|
@classmethod
|
|
def execute(cls, answer=None, is_api_call=False):
|
|
def execute(cls, answer=None, is_api_call=False):
|
|
"""
|
|
"""
|
|
@@ -52,79 +80,79 @@ class LIFOBuffer(object):
|
|
:param is_api_call: Boolean passed to all neuron in order to let them know if the current call comes from API
|
|
:param is_api_call: Boolean passed to all neuron in order to let them know if the current call comes from API
|
|
:return: serialized APIResponse object
|
|
:return: serialized APIResponse object
|
|
"""
|
|
"""
|
|
|
|
+
|
|
|
|
+ cls.answer = answer
|
|
|
|
+ cls.is_api_call = is_api_call
|
|
|
|
+
|
|
try:
|
|
try:
|
|
|
|
|
|
while cls.lifo_list:
|
|
while cls.lifo_list:
|
|
|
|
+ logger.debug("[LIFOBuffer] number of synapse list to process: %s" % len(cls.lifo_list))
|
|
try:
|
|
try:
|
|
- logger.debug("[LIFOBuffer] number of synapse list to process: %s" % len(cls.lifo_list))
|
|
+ cls._process_synapse_list()
|
|
-
|
|
+ except SynapseListAddedToLIFO:
|
|
-
|
|
|
|
- last_synapse_fifo_list = cls.lifo_list[-1]
|
|
|
|
-
|
|
|
|
- while last_synapse_fifo_list:
|
|
|
|
-
|
|
|
|
- matched_synapse = last_synapse_fifo_list[0]
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- if matched_synapse not in cls.api_response.list_processed_matched_synapse:
|
|
|
|
- cls.api_response.list_processed_matched_synapse.append(matched_synapse)
|
|
|
|
-
|
|
|
|
- while matched_synapse.neuron_fifo_list:
|
|
|
|
-
|
|
|
|
- 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"] = is_api_call
|
|
|
|
-
|
|
|
|
- instantiated_neuron = NeuronLauncher.start_neuron(neuron=neuron,
|
|
|
|
- parameters_dict=matched_synapse.parameters)
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- cls.api_response.status = "complete"
|
|
|
|
- if instantiated_neuron.is_waiting_for_answer:
|
|
|
|
- logger.debug("[LIFOBuffer] Wait for answer mode")
|
|
|
|
- cls.api_response.status = "waiting_for_answer"
|
|
|
|
- raise Serialize
|
|
|
|
- else:
|
|
|
|
- logger.debug("[LIFOBuffer] complete mode")
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- matched_synapse.neuron_module_list.append(instantiated_neuron)
|
|
|
|
-
|
|
|
|
- matched_synapse.neuron_fifo_list.remove(neuron)
|
|
|
|
-
|
|
|
|
- if instantiated_neuron.pending_synapse:
|
|
|
|
-
|
|
|
|
- cls.add_synapse_list_to_lifo([instantiated_neuron.pending_synapse])
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- raise Synapse_LIFO
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- last_synapse_fifo_list .remove(matched_synapse)
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- cls.lifo_list.remove(last_synapse_fifo_list)
|
|
|
|
- except Synapse_LIFO:
|
|
|
|
continue
|
|
continue
|
|
raise Serialize
|
|
raise Serialize
|
|
|
|
|
|
except Serialize:
|
|
except Serialize:
|
|
-
|
|
+ cls._return_serialized_api_response()
|
|
- will_be_returned = cls.api_response.serialize()
|
|
|
|
-
|
|
|
|
- cls.api_response = APIResponse()
|
|
|
|
- return will_be_returned
|
|
|
|
|
|
|
|
@classmethod
|
|
@classmethod
|
|
- def clean(cls):
|
|
+ def _process_synapse_list(cls):
|
|
- """
|
|
+
|
|
- Clean the LIFO by creating a new list
|
|
+
|
|
- """
|
|
+ last_synapse_fifo_list = cls.lifo_list[-1]
|
|
- cls.lifo_list = list()
|
|
+
|
|
|
|
+ while last_synapse_fifo_list:
|
|
|
|
+
|
|
|
|
+ matched_synapse = last_synapse_fifo_list[0]
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if matched_synapse not in cls.api_response.list_processed_matched_synapse:
|
|
|
|
+ cls.api_response.list_processed_matched_synapse.append(matched_synapse)
|
|
|
|
+
|
|
|
|
+ while matched_synapse.neuron_fifo_list:
|
|
|
|
+ cls._process_neuron_list(matched_synapse=matched_synapse)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ last_synapse_fifo_list.remove(matched_synapse)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ cls.lifo_list.remove(last_synapse_fifo_list)
|
|
|
|
+
|
|
|
|
+ @classmethod
|
|
|
|
+ def _process_neuron_list(cls, matched_synapse):
|
|
|
|
+ logger.debug("[LIFOBuffer] number of neuron to process: %s" % len(matched_synapse.neuron_fifo_list))
|
|
|
|
+
|
|
|
|
+ neuron = matched_synapse.neuron_fifo_list[0]
|
|
|
|
+
|
|
|
|
+ if cls.answer is not None:
|
|
|
|
+ neuron.parameters["answer"] = cls.answer
|
|
|
|
+
|
|
|
|
+ cls.answer = None
|
|
|
|
+
|
|
|
|
+ neuron.parameters["is_api_call"] = cls.is_api_call
|
|
|
|
+
|
|
|
|
+ instantiated_neuron = NeuronLauncher.start_neuron(neuron=neuron,
|
|
|
|
+ parameters_dict=matched_synapse.parameters)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ cls.api_response.status = "complete"
|
|
|
|
+ if instantiated_neuron.is_waiting_for_answer:
|
|
|
|
+ logger.debug("[LIFOBuffer] Wait for answer mode")
|
|
|
|
+ cls.api_response.status = "waiting_for_answer"
|
|
|
|
+ raise Serialize
|
|
|
|
+ else:
|
|
|
|
+ logger.debug("[LIFOBuffer] complete mode")
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ matched_synapse.neuron_module_list.append(instantiated_neuron)
|
|
|
|
+
|
|
|
|
+ matched_synapse.neuron_fifo_list.remove(neuron)
|
|
|
|
+
|
|
|
|
+ if instantiated_neuron.pending_synapse:
|
|
|
|
+
|
|
|
|
+ cls.add_synapse_list_to_lifo([instantiated_neuron.pending_synapse])
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ raise SynapseListAddedToLIFO
|