BrainLoader.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import inspect
  2. import logging
  3. import os
  4. from YAMLLoader import YAMLLoader
  5. from core.ConfigurationManager.ConfigurationChecker import ConfigurationChecker
  6. from core.Models import Singleton
  7. from core.Models.Brain import Brain
  8. from core.Models.Event import Event
  9. from core.Models.Neuron import Neuron
  10. from core.Models.Order import Order
  11. from core.Models.Synapse import Synapse
  12. logging.basicConfig()
  13. logger = logging.getLogger("kalliope")
  14. @Singleton
  15. class BrainLoader(object):
  16. """
  17. This Class is used to get the brain YAML and the Brain as an object
  18. """
  19. def __init__(self, file_path=None):
  20. logger.debug("Loading brain with file path: %s" % file_path)
  21. self.file_path = file_path
  22. self.yaml_config = self.get_yaml_config()
  23. self.brain = self.get_brain()
  24. def get_yaml_config(self):
  25. """
  26. Class Methods which loads default or the provided YAML file and return it as a String
  27. :return: The loaded brain YAML
  28. :rtype: String
  29. :Example:
  30. brain_yaml = BrainLoader.get_yaml_config(/var/tmp/brain.yml)
  31. .. warnings:: Class Method
  32. """
  33. if self.file_path is None:
  34. brain_file_path = self._get_root_brain_path()
  35. else:
  36. brain_file_path = self.file_path
  37. return YAMLLoader.get_config(brain_file_path)
  38. def get_brain(self):
  39. """
  40. Class Methods which loads default or the provided YAML file and return a Brain
  41. :return: The loaded Brain
  42. :rtype: Brain
  43. :Example:
  44. brain = BrainLoader.get_brain(file_path="/var/tmp/brain.yml")
  45. .. seealso:: Brain
  46. .. warnings:: Class Method
  47. """
  48. # Instantiate a brain
  49. brain = Brain()
  50. # get the brain with dict
  51. dict_brain = self.get_yaml_config()
  52. brain.brain_yaml = dict_brain
  53. # create list of Synapse
  54. synapses = list()
  55. for synapses_dict in dict_brain:
  56. if "includes" not in synapses_dict: # we don't need to check includes as it's not a synapse
  57. if ConfigurationChecker().check_synape_dict(synapses_dict):
  58. # print "synapses_dict ok"
  59. name = synapses_dict["name"]
  60. neurons = self._get_neurons(synapses_dict["neurons"])
  61. signals = self._get_signals(synapses_dict["signals"])
  62. new_synapse = Synapse(name=name, neurons=neurons, signals=signals)
  63. synapses.append(new_synapse)
  64. brain.synapses = synapses
  65. if self.file_path is None:
  66. brain.brain_file = self._get_root_brain_path()
  67. else:
  68. brain.brain_file = self.file_path
  69. # check that no synapse have the same name than another
  70. if not ConfigurationChecker().check_synapes(synapses):
  71. brain = None
  72. return brain
  73. @staticmethod
  74. def _get_neurons(neurons_dict):
  75. """
  76. Get a list of Neuron object from a neuron dict
  77. :param neurons_dict: Neuron name or dictionary of Neuron_name/Neuron_parameters
  78. :type neurons_dict: String or dict
  79. :return: A list of Neurons
  80. :rtype: List
  81. :Example:
  82. neurons = cls._get_neurons(synapses_dict["neurons"])
  83. .. seealso:: Neuron
  84. .. warnings:: Static and Private
  85. """
  86. neurons = list()
  87. for neuron_dict in neurons_dict:
  88. if isinstance(neuron_dict, dict):
  89. if ConfigurationChecker().check_neuron_dict(neuron_dict):
  90. # print "Neurons dict ok"
  91. for neuron_name in neuron_dict:
  92. name = neuron_name
  93. parameters = neuron_dict[name]
  94. # print parameters
  95. new_neuron = Neuron(name=name, parameters=parameters)
  96. neurons.append(new_neuron)
  97. else:
  98. # the neuron does not have parameter
  99. if ConfigurationChecker().check_neuron_dict(neuron_dict):
  100. new_neuron = Neuron(name=neuron_dict)
  101. neurons.append(new_neuron)
  102. return neurons
  103. @classmethod
  104. def _get_signals(cls, signals_dict):
  105. """
  106. Get a list of Signal object from a signals dict
  107. :param signals_dict: Signal name or dictionary of Signal_name/Signal_parameters
  108. :type signals_dict: String or dict
  109. :return: A list of Event and/or Order
  110. :rtype: List
  111. :Example:
  112. signals = cls._get_signals(synapses_dict["signals"])
  113. .. seealso:: Event, Order
  114. .. warnings:: Class method and Private
  115. """
  116. # print signals_dict
  117. signals = list()
  118. for signal_dict in signals_dict:
  119. if ConfigurationChecker().check_signal_dict(signal_dict):
  120. # print "Signals dict ok"
  121. event_or_order = cls._get_event_or_order_from_dict(signal_dict)
  122. signals.append(event_or_order)
  123. return signals
  124. @staticmethod
  125. def _get_event_or_order_from_dict(signal_or_event_dict):
  126. """
  127. The signal is either an Event or an Order
  128. :param signal_or_event_dict: A dict of event or signal
  129. :type signal_or_event_dict: dict
  130. :return: The object corresponding to An Order or an Event
  131. :rtype: An Order or an Event
  132. :Example:
  133. event_or_order = cls._get_event_or_order_from_dict(signal_dict)
  134. .. seealso:: Event, Order
  135. .. warnings:: Static method and Private
  136. """
  137. if 'event' in signal_or_event_dict:
  138. # print "is event"
  139. event = signal_or_event_dict["event"]
  140. if ConfigurationChecker.check_event_dict(event):
  141. return Event(period=event)
  142. if 'order' in signal_or_event_dict:
  143. order = signal_or_event_dict["order"]
  144. if ConfigurationChecker.check_order_dict(order):
  145. return Order(sentence=order)
  146. @staticmethod
  147. def _get_root_brain_path():
  148. """
  149. Return the full path of the default brain file
  150. :Example:
  151. brain.brain_file = cls._get_root_brain_path()
  152. .. raises:: IOError
  153. .. warnings:: Static method and Private
  154. """
  155. # get current script directory path. We are in /an/unknown/path/kalliope/core/ConfigurationManager
  156. cur_script_directory = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
  157. # get parent dir. Now we are in /an/unknown/path/kalliope
  158. parent_dir = os.path.normpath(cur_script_directory + os.sep + os.pardir + os.sep + os.pardir)
  159. brain_path = parent_dir + os.sep + "brain.yml"
  160. logger.debug("Real brain.yml path: %s" % brain_path)
  161. if os.path.isfile(brain_path):
  162. return brain_path
  163. raise IOError("Default brain.yml file not found")