SynapseLauncher.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import logging
  2. from kalliope.core.ConfigurationManager import BrainLoader
  3. from kalliope.core.LIFOBuffer import LIFOBuffer
  4. from kalliope.core.Models.MatchedSynapse import MatchedSynapse
  5. from kalliope.core.NeuronLauncher import NeuronLauncher
  6. from kalliope.core.OrderAnalyser import OrderAnalyser
  7. logging.basicConfig()
  8. logger = logging.getLogger("kalliope")
  9. class SynapseNameNotFound(Exception):
  10. """
  11. The Synapse has not been found
  12. .. seealso: Synapse
  13. """
  14. pass
  15. class SynapseLauncher(object):
  16. def __init__(self):
  17. pass
  18. @classmethod
  19. def start_synapse(cls, name, brain=None):
  20. """
  21. Start a synapse by it's name
  22. :param name: Name (Unique ID) of the synapse to launch
  23. :param brain: Brain instance
  24. """
  25. # check if we have found and launched the synapse
  26. synapse = brain.get_synapse_by_name(synapse_name=name)
  27. if not synapse:
  28. raise SynapseNameNotFound("The synapse name \"%s\" does not exist in the brain file" % name)
  29. else:
  30. return cls._run_synapse(synapse=synapse)
  31. @classmethod
  32. def _run_synapse(cls, synapse):
  33. """
  34. Start all neurons in the synapse
  35. :param synapse: Synapse for which we run neurons
  36. :return:
  37. """
  38. for neuron in synapse.neurons:
  39. instantiated_neuron = NeuronLauncher.start_neuron(neuron)
  40. # add the generated message to the synapse
  41. synapse.answers.append(instantiated_neuron.tts_message)
  42. return synapse
  43. @classmethod
  44. def run_matching_synapse_from_order(cls, order_to_process, brain, settings, is_api_call=False):
  45. """
  46. :param order_to_process: the spoken order sent by the user
  47. :param brain: Brain object
  48. :param settings: Settings object
  49. :param is_api_call: if True, the current call come from the API. This info must be known by launched Neuron
  50. :return: list of matched synapse
  51. """
  52. # get our singleton LIFO
  53. lifo_buffer = LIFOBuffer()
  54. # if the LIFO is not empty, so, the current order is passed to the current processing synapse as an answer
  55. if len(lifo_buffer.lifo_list) > 0:
  56. # the LIFO is not empty, this is an answer to a previous call
  57. return lifo_buffer.execute(answer=order_to_process, is_api_call=is_api_call)
  58. else: # the LIFO is empty, this is a new call
  59. # get a list of matched synapse from the order
  60. list_synapse_to_process = OrderAnalyser.get_matching_synapse(order=order_to_process, brain=brain)
  61. if not list_synapse_to_process: # the order analyser returned us an empty list
  62. # add the default synapse if exist into the lifo
  63. if settings.default_synapse:
  64. logger.debug("[SynapseLauncher] No matching Synapse-> running default synapse ")
  65. # get the default synapse
  66. default_synapse = BrainLoader().get_brain().get_synapse_by_name(settings.default_synapse)
  67. new_matching_synapse = MatchedSynapse(matched_synapse=default_synapse,
  68. matched_order=None,
  69. user_order=order_to_process)
  70. list_synapse_to_process.append(new_matching_synapse)
  71. else:
  72. logger.debug("[SynapseLauncher] No matching Synapse and no default synapse ")
  73. lifo_buffer.add_synapse_list_to_lifo(list_synapse_to_process)
  74. lifo_buffer.api_response.user_order = order_to_process
  75. return lifo_buffer.execute(is_api_call=is_api_call)