BrainLoader.py 4.9 KB

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