SynapseLauncher.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import logging
  2. from kalliope.core.NeuronLauncher import NeuronLauncher
  3. from kalliope.core.NeuronParameterLoader import NeuronParameterLoader
  4. from kalliope.core.OrderAnalyser import OrderAnalyser
  5. logging.basicConfig()
  6. logger = logging.getLogger("kalliope")
  7. class SynapseNameNotFound(Exception):
  8. """
  9. The Synapse has not been found
  10. .. seealso: Synapse
  11. """
  12. pass
  13. class SynapseLauncher(object):
  14. def __init__(self):
  15. pass
  16. @classmethod
  17. def start_synapse(cls, name, brain=None):
  18. """
  19. Start a synapse by it's name
  20. :param name: Name (Unique ID) of the synapse to launch
  21. :param brain: Brain instance
  22. """
  23. # check if we have found and launched the synapse
  24. synapse = brain.get_synapse_by_name(synapse_name=name)
  25. if not synapse:
  26. raise SynapseNameNotFound("The synapse name \"%s\" does not exist in the brain file" % name)
  27. else:
  28. cls._run_synapse(synapse=synapse)
  29. return synapse
  30. @classmethod
  31. def _run_synapse(cls, synapse):
  32. """
  33. Start all neurons in the synapse
  34. :param synapse: Synapse for which we run neurons
  35. :return:
  36. """
  37. for neuron in synapse.neurons:
  38. NeuronLauncher.start_neuron(neuron)
  39. return True
  40. @classmethod
  41. def run_matching_synapse_or_default(cls, order_to_process, brain, settings):
  42. """
  43. This method will run all synapse that match the given order "order_to_process"
  44. :param order_to_process: The text order to process in the order analyser
  45. :param brain: Brain instance
  46. :param settings: Settings instance
  47. :return: Return a list of launched synapse
  48. """
  49. no_synapse_match = False
  50. # create a list of launched synapse to return
  51. launched_synapses = list()
  52. if order_to_process is not None: # maybe we have received a null audio from STT engine
  53. launched_synapses_tuple = OrderAnalyser.get_matching_synapse(order=order_to_process, brain=brain)
  54. # oa contains the list Named tuple of synapse to run with the associated order that has matched
  55. # for each synapse, get neurons, et for each neuron, get parameters
  56. if not launched_synapses_tuple:
  57. no_synapse_match = True
  58. else:
  59. # the order match one or more synapses
  60. for tuple_el in launched_synapses_tuple:
  61. launched_synapses.append(tuple_el.synapse)
  62. logger.debug("[SynapseLauncher] Get parameter for %s " % tuple_el.synapse.name)
  63. parameters = NeuronParameterLoader.get_parameters(synapse_order=tuple_el.order,
  64. user_order=order_to_process)
  65. # start the neuron list
  66. NeuronLauncher.start_neuron_list(neuron_list=tuple_el.synapse.neurons,
  67. parameters_dict=parameters)
  68. else:
  69. no_synapse_match = True
  70. if no_synapse_match: # then run the default synapse
  71. if settings.default_synapse is not None:
  72. logger.debug("[SynapseLauncher] No matching Synapse-> running default synapse ")
  73. synapses = SynapseLauncher.start_synapse(name=settings.default_synapse,
  74. brain=brain)
  75. launched_synapses.append(synapses)
  76. # return the launched synapse list
  77. return launched_synapses