BrainLoader.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.Brain import Brain
  7. from core.Models.Event import Event
  8. from core.Models.Neuron import Neuron
  9. from core.Models.Order import Order
  10. from core.Models.Synapse import Synapse
  11. class BrainLoader(YAMLLoader):
  12. def __init__(self, filepath=None):
  13. self.brain_file_path = filepath
  14. if filepath is None:
  15. self.brain_file_path = self._get_root_brain_path()
  16. # self.filePath = "../../" + self.fileName
  17. YAMLLoader.__init__(self, self.brain_file_path)
  18. def get_config(self):
  19. return YAMLLoader.get_config(self)
  20. def get_brain(self):
  21. """
  22. return a brain object from YAML settings
  23. :return: Brain object
  24. :rtype: Brain
  25. """
  26. # get the brain with dict
  27. dict_brain = self.get_config()
  28. # create a new brain
  29. brain = Brain()
  30. # create list of Synapse
  31. synapses = list()
  32. for synapes_dict in dict_brain:
  33. # print synapes_dict
  34. if ConfigurationChecker().check_synape_dict(synapes_dict):
  35. # print "synapes_dict ok"
  36. name = synapes_dict["name"]
  37. neurons = self._get_neurons(synapes_dict["neurons"])
  38. signals = self._get_signals(synapes_dict["signals"])
  39. new_synapse = Synapse(name=name, neurons=neurons, signals=signals)
  40. synapses.append(new_synapse)
  41. brain.synapes = synapses
  42. brain.brain_file = self.brain_file_path
  43. # check that no synapse have the same name than another
  44. if ConfigurationChecker().check_synapes(synapses):
  45. return brain
  46. return None
  47. @staticmethod
  48. def _get_neurons(neurons_dict):
  49. """
  50. Get a list of Neuron object from a neuron dict
  51. :param neurons_dict:
  52. :return:
  53. """
  54. neurons = list()
  55. for neuron_dict in neurons_dict:
  56. if isinstance(neuron_dict, dict):
  57. if ConfigurationChecker().check_neuron_dict(neuron_dict):
  58. # print "Neurons dict ok"
  59. for neuron_name in neuron_dict:
  60. name = neuron_name
  61. parameters = neuron_dict[name]
  62. # print parameters
  63. new_neuron = Neuron(name=name, parameters=parameters)
  64. neurons.append(new_neuron)
  65. else:
  66. # the neuron does not have parameter
  67. if ConfigurationChecker().check_neuron_dict(neuron_dict):
  68. new_neuron = Neuron(name=neuron_dict)
  69. neurons.append(new_neuron)
  70. return neurons
  71. def _get_signals(self, signals_dict):
  72. # print signals_dict
  73. signals = list()
  74. for signal_dict in signals_dict:
  75. if ConfigurationChecker().check_signal_dict(signal_dict):
  76. # print "Signals dict ok"
  77. event_or_order = self._get_event_or_order_from_dict(signal_dict)
  78. signals.append(event_or_order)
  79. return signals
  80. @staticmethod
  81. def _get_event_or_order_from_dict(signal_or_event_dict):
  82. if 'event' in signal_or_event_dict:
  83. # print "is event"
  84. event = signal_or_event_dict["event"]
  85. if ConfigurationChecker.check_event_dict(event):
  86. return Event(period=event)
  87. if 'order' in signal_or_event_dict:
  88. order = signal_or_event_dict["order"]
  89. if ConfigurationChecker.check_order_dict(order):
  90. return Order(sentence=order)
  91. @staticmethod
  92. def _get_root_brain_path():
  93. """
  94. Return the full path of the default brain file
  95. :return:
  96. """
  97. # get current script directory path. We are in /an/unknown/path/jarvis/core/ConfigurationManager
  98. cur_script_directory = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
  99. # get parent dir. Now we are in /an/unknown/path/jarvis
  100. parent_dir = os.path.normpath(cur_script_directory + os.sep + os.pardir + os.sep + os.pardir)
  101. brain_path = parent_dir + os.sep + "brain.yml"
  102. print brain_path
  103. logging.debug("Real brain.yml path: %s" % brain_path)
  104. if os.path.isfile(brain_path):
  105. return brain_path
  106. raise IOError("Default brain.yml file not found")