def test_get_global_variable(self): """ Test the get_global_variable of the OrderAnalyser Class """ sentence = "i am {{name2}}" variables = { "name": "LaMonf", "name2": "intelora", "name3": u"kalliopé", "name4": 1 } st = Settings(variables=variables) expected_result = "i am intelora" self.assertEqual(BrainLoader._get_global_variable(sentence=sentence, settings=st), expected_result) # test with accent sentence = "i am {{name3}}" expected_result = u"i am kalliopé" self.assertEqual(BrainLoader._get_global_variable(sentence=sentence, settings=st), expected_result) # test with int sentence = "i am {{name4}}" expected_result = "i am 1" self.assertEqual(BrainLoader._get_global_variable(sentence=sentence, settings=st), expected_result)
def test_get_signals(self): signals = [{'order': 'test_order'}] signal = Signal(name="order", parameters="test_order") bl = BrainLoader(file_path=self.brain_to_test) signals_from_brain_loader = bl._get_signals(signals) self.assertEqual([signal], signals_from_brain_loader)
def create_app(self): """ executed once at the beginning of the test """ # be sure that the singleton haven't been loaded before Singleton._instances = {} current_path = os.getcwd() if "/tests" in os.getcwd(): full_path_brain_to_test = current_path + os.sep + "brains/brain_test_api.yml" self.audio_file = "files/bonjour.wav" else: full_path_brain_to_test = current_path + os.sep + "tests/brains/brain_test_api.yml" self.audio_file = "tests/files/bonjour.wav" # rest api config sl = SettingLoader() sl.settings.rest_api.password_protected = False sl.settings.active = True sl.settings.port = 5000 sl.settings.allowed_cors_origin = "*" sl.settings.default_synapse = None sl.settings.hooks["on_order_not_found"] = "order-not-found-synapse" # prepare a test brain brain_to_test = full_path_brain_to_test brain_loader = BrainLoader(file_path=brain_to_test) brain = brain_loader.brain self.app = Flask(__name__) self.app.config['TESTING'] = True self.flask_api = FlaskAPI(self.app, port=5000, brain=brain) self.client = self.app.test_client() return self.flask_api.app
def test_load_brain(self): """ Test the class return a valid brain object """ neuron = Neuron(name='say', parameters={'message': ['test message']}) neuron2 = Neuron(name='sleep', parameters={'seconds': 60}) signal1 = Signal(name="order", parameters="test_order") signal2 = Signal(name="order", parameters="test_order_2") signal3 = Signal(name="order", parameters="test_order_3") signal4 = Signal(name="order", parameters="order_for_int") synapse1 = Synapse(name="test", neurons=[neuron], signals=[signal1]) synapse2 = Synapse(name="test2", neurons=[neuron], signals=[signal2]) synapse3 = Synapse(name="test3", neurons=[neuron], signals=[signal3]) synapse4 = Synapse(name="testint", neurons=[neuron2], signals=[signal4]) synapses = [synapse1, synapse2, synapse4, synapse3] brain = Brain() brain.synapses = synapses brain.brain_file = self.brain_to_test brain.brain_yaml = self.expected_result brain_loader = BrainLoader(file_path=self.brain_to_test) self.assertEqual(brain, brain_loader.brain)
def test_process_synapse_list(self): """ Testing the neuron list from a synapse """ synapse = BrainLoader().brain.get_synapse_by_name("synapse1") order = "enter in synapse 1" matched_synapse = MatchedSynapse(matched_synapse=synapse, user_order=order, matched_order=order) list_matched_synapse = list() list_matched_synapse.append(matched_synapse) with mock.patch("intelora.core.Lifo.LIFOBuffer._process_neuron_list"): self.lifo_buffer._process_synapse_list(list_matched_synapse) expected_response = { 'status': None, 'matched_synapses': [{ 'matched_order': 'enter in synapse 1', 'neuron_module_list': [], 'synapse_name': 'synapse1' }], 'user_order': None } self.assertEqual(expected_response, self.lifo_buffer.api_response.serialize()) self.assertEqual(0, len(self.lifo_buffer.lifo_list))
def run_synapse_by_name(synapse_name, user_order=None, synapse_order=None, high_priority=False, is_api_call=False, overriding_parameter_dict=None, no_voice=False): """ call the lifo for adding a synapse to execute in the list of synapse list to process :param synapse_name: The name of the synapse to run :param user_order: The user order :param synapse_order: The synapse order :param high_priority: If True, the synapse is executed before the end of the current synapse list :param is_api_call: If true, the current call comes from the api :param overriding_parameter_dict: dict of value to add to neuron parameters """ synapse = BrainLoader().brain.get_synapse_by_name(synapse_name) matched_synapse = MatchedSynapse( matched_synapse=synapse, matched_order=synapse_order, user_order=user_order, overriding_parameter=overriding_parameter_dict) list_synapse_to_process = list() list_synapse_to_process.append(matched_synapse) # get the singleton lifo_buffer = LifoManager.get_singleton_lifo() lifo_buffer.add_synapse_list_to_lifo(list_synapse_to_process, high_priority=high_priority) lifo_buffer.execute(is_api_call=is_api_call, no_voice=no_voice)
def __init__(self, **kwargs): super(SignalModule, self).__init__(**kwargs) # get the child who called the class self.signal_name = self.__class__.__name__ Utils.print_info('Init Signal :' + self.signal_name) self.brain = BrainLoader().brain
def start_synapse_by_list_name(cls, list_name, brain=None, overriding_parameter_dict=None, new_lifo=False): """ Start synapses by their name :param list_name: List of name of the synapse to launch :param brain: Brain instance :param overriding_parameter_dict: parameter to pass to neurons :param new_lifo: If True, ask the LifoManager to return a new lifo and not the singleton """ logger.debug( "[SynapseLauncher] start_synapse_by_list_name called with synapse list: %s " % list_name) if list_name: if brain is None: brain = BrainLoader().brain # get all synapse object list_synapse_object_to_start = list() for name in list_name: synapse_to_start = brain.get_synapse_by_name(synapse_name=name) list_synapse_object_to_start.append(synapse_to_start) # run the LIFO with all synapse if new_lifo: lifo_buffer = LifoManager.get_new_lifo() else: lifo_buffer = LifoManager.get_singleton_lifo() list_synapse_to_process = list() for synapse in list_synapse_object_to_start: if synapse is not None: new_matching_synapse = MatchedSynapse( matched_synapse=synapse, matched_order=None, user_order=None, overriding_parameter=overriding_parameter_dict) list_synapse_to_process.append(new_matching_synapse) lifo_buffer.add_synapse_list_to_lifo(list_synapse_to_process) return lifo_buffer.execute(is_api_call=True) return None
def run_synapse_by_name(synapse_name): """ This method will run the synapse """ Utils.print_info("[Event] triggered, running synapse: %s" % synapse_name) # get a brain brain_loader = BrainLoader() brain = brain_loader.brain SynapseLauncher.start_synapse_by_name(synapse_name, brain=brain)
def test_process_neuron_list(self): # Test with a neuron that doesn't wait for an answer synapse = BrainLoader().brain.get_synapse_by_name("synapse5") order = "synapse5" matched_synapse = MatchedSynapse(matched_synapse=synapse, user_order=order, matched_order=order) with mock.patch("intelora.core.TTS.TTSModule.generate_and_play"): self.lifo_buffer.set_api_call(True) self.lifo_buffer._process_neuron_list( matched_synapse=matched_synapse) self.assertEqual("complete", self.lifo_buffer.api_response.status) # test with neuron that wait for an answer LifoManager.clean_saved_lifo() synapse = BrainLoader().brain.get_synapse_by_name("synapse6") order = "synapse6" matched_synapse = MatchedSynapse(matched_synapse=synapse, user_order=order, matched_order=order) self.lifo_buffer.set_api_call(True) with mock.patch("intelora.core.TTS.TTSModule.generate_and_play"): with self.assertRaises(Serialize): self.lifo_buffer._process_neuron_list( matched_synapse=matched_synapse) # test with a neuron that want to add a synapse list to the LIFO LifoManager.clean_saved_lifo() synapse = BrainLoader().brain.get_synapse_by_name("synapse6") order = "synapse6" matched_synapse = MatchedSynapse(matched_synapse=synapse, user_order=order, matched_order=order) self.lifo_buffer.set_api_call(True) self.lifo_buffer.set_answer("synapse 6 answer") with mock.patch("intelora.core.TTS.TTSModule.generate_and_play"): self.assertRaises( SynapseListAddedToLIFO, self.lifo_buffer._process_neuron_list( matched_synapse=matched_synapse))
def test_get_neurons(self): """ Test to get neurons from the brainLoader scenarii: - 1/ get a simple neuron from the brainloader - 2/ get a neuron with global variables as parameters - 3/ get a neuron with int as parameters """ # 1/ get a simple neuron from the brainloader st = Settings() neuron_list = [{'say': {'message': ['test message']}}] neuron = Neuron(name='say', parameters={'message': ['test message']}) bl = BrainLoader(file_path=self.brain_to_test) neurons_from_brain_loader = bl._get_neurons(neuron_list, settings=st) self.assertEqual([neuron], neurons_from_brain_loader) # 2/ get a neuron with global variables as parameters neuron_list = [{'say': {'message': ['bonjour {{name}}']}}] variables = { "author": "Lamonf", "test_number": 60, "name": "intelora" } st = Settings(variables=variables) bl = BrainLoader(file_path=self.brain_to_test) neurons_from_brain_loader = bl._get_neurons(neuron_list, settings=st) neuron = Neuron(name='say', parameters={'message': ['bonjour intelora']}) self.assertEqual([neuron], neurons_from_brain_loader) # 3/ get a neuron with int as parameters st = Settings() neuron_list = [{'sleep': {'seconds': 60}}] neuron = Neuron(name='sleep', parameters={'seconds': 60}) bl = BrainLoader(file_path=self.brain_to_test) neurons_from_brain_loader = bl._get_neurons(neuron_list, settings=st) self.assertEqual([neuron], neurons_from_brain_loader)
def start_synapse_by_name(cls, name, brain=None, overriding_parameter_dict=None, new_lifo=False): """ Start a synapse by it's name :param name: Name (Unique ID) of the synapse to launch :param brain: Brain instance :param overriding_parameter_dict: parameter to pass to neurons :param new_lifo: If True, ask the HookManager to return a new lifo and not the singleton """ logger.debug( "[SynapseLauncher] start_synapse_by_name called with synapse name: %s " % name) if brain is None: brain = BrainLoader().brain # check if we have found and launched the synapse synapse = brain.get_synapse_by_name(synapse_name=name) if not synapse: raise SynapseNameNotFound( "The synapse name \"%s\" does not exist in the brain file" % name) else: if new_lifo: lifo_buffer = LifoManager.get_new_lifo() else: lifo_buffer = LifoManager.get_singleton_lifo() list_synapse_to_process = list() new_matching_synapse = MatchedSynapse( matched_synapse=synapse, matched_order=None, user_order=None, overriding_parameter=overriding_parameter_dict) list_synapse_to_process.append(new_matching_synapse) lifo_buffer.add_synapse_list_to_lifo(list_synapse_to_process) return lifo_buffer.execute(is_api_call=True)
def __init__(self, **kwargs): """ Class used by neuron for talking :param kwargs: Same parameter as the Child. Can contain info about the tts to use instead of the default one """ # get the child who called the class child_name = self.__class__.__name__ self.neuron_name = child_name sl = SettingLoader() self.settings = sl.settings brain_loader = BrainLoader() self.brain = brain_loader.brain # a dict of overridden TTS parameters if provided by the user self.override_tts_parameters = kwargs.get('tts', None) # create the TTS instance self.tts = None if self.override_tts_parameters is None or not isinstance( self.override_tts_parameters, dict): # we get the default TTS self.tts = self._get_tts_object(settings=self.settings) else: for key, value in self.override_tts_parameters.items(): tts_name = key tts_parameters = value self.tts = self._get_tts_object( tts_name=tts_name, override_parameter=tts_parameters, settings=self.settings) # get templates if provided # Check if there is a template associate to the output message self.say_template = kwargs.get('say_template', None) # check if there is a template file associate to the output message self.file_template = kwargs.get('file_template', None) # keep the generated message self.tts_message = None # if the current call is api one self.is_api_call = kwargs.get('is_api_call', False) # if the current call want to mute intelora self.no_voice = kwargs.get('no_voice', False) # boolean to know id the synapse is waiting for an answer self.is_waiting_for_answer = False # the synapse name to add the the buffer self.pending_synapse = None # a dict of parameters the user ask to save in short term memory self.intelora_memory = kwargs.get('intelora_memory', None) # parameters loaded from the order can be save now Cortex.save_parameter_from_order_in_memory(self.intelora_memory)
def test_add_synapse_list_to_lifo(self): """ Testing to add a synapse to the lifo """ synapse = BrainLoader().brain.get_synapse_by_name("synapse1") order = "enter in synapse 1" matched_synapse = MatchedSynapse(matched_synapse=synapse, user_order=order, matched_order=order) list_matched_synapse = list() list_matched_synapse.append(matched_synapse) self.lifo_buffer.add_synapse_list_to_lifo(list_matched_synapse) self.assertEqual(self.lifo_buffer.lifo_list, [list_matched_synapse])
def setUp(self): # be sure the brain haven't been instantiated before Singleton._instances = dict() if "/tests" in os.getcwd(): self.brain_to_test = os.getcwd( ) + os.sep + "brains/lifo_buffer_test_brain.yml" else: self.brain_to_test = os.getcwd( ) + os.sep + "tests/brains/lifo_buffer_test_brain.yml" BrainLoader(file_path=self.brain_to_test) # create a new lifo buffer self.lifo_buffer = LifoManager.get_singleton_lifo() self.lifo_buffer.clean()
def test_clean(self): """ Test the Cleaning of the matched synapses list """ synapse = BrainLoader().brain.get_synapse_by_name("synapse1") order = "enter in synapse 1" matched_synapse = MatchedSynapse(matched_synapse=synapse, user_order=order, matched_order=order) list_matched_synapse = list() list_matched_synapse.append(matched_synapse) self.lifo_buffer.add_synapse_list_to_lifo(list_matched_synapse) self.lifo_buffer.clean() self.assertEqual(0, len(self.lifo_buffer.lifo_list))
def run_synapse_by_name(self, synapse_name): """ Run a synapse by its name test with curl: curl -i --user admin:secret -X POST http://127.0.0.1:5000/synapses/start/id/say-hello-fr run a synapse without making intelora speaking curl -i -H "Content-Type: application/json" --user admin:secret -X POST \ -d '{"no_voice":"true"}' http://127.0.0.1:5000/synapses/start/id/say-hello-fr Run a synapse by its name and pass order's parameters curl -i -H "Content-Type: application/json" --user admin:secret -X POST \ -d '{"no_voice":"true", "parameters": {"parameter1": "value1" }}' \ http://127.0.0.1:5000/synapses/start/id/say-hello-fr :param synapse_name: name(id) of the synapse to execute :return: """ # get a synapse object from the name logger.debug("[FlaskAPI] run_synapse_by_name: synapse name -> %s" % synapse_name) synapse_target = BrainLoader().brain.get_synapse_by_name(synapse_name=synapse_name) # get no_voice_flag if present no_voice = self.get_boolean_flag_from_request(request, boolean_flag_to_find="no_voice") # get parameters parameters = self.get_parameters_from_request(request) if synapse_target is None: data = { "synapse name not found": "%s" % synapse_name } return jsonify(error=data), 404 else: # generate a MatchedSynapse from the synapse matched_synapse = MatchedSynapse(matched_synapse=synapse_target, overriding_parameter=parameters) # get the current LIFO buffer from the singleton lifo_buffer = LifoManager.get_singleton_lifo() # this is a new call we clean up the LIFO lifo_buffer.clean() lifo_buffer.add_synapse_list_to_lifo([matched_synapse]) response = lifo_buffer.execute(is_api_call=True, no_voice=no_voice) data = jsonify(response) return data, 201
def test_singleton(self): bl1 = BrainLoader(file_path=self.brain_to_test) bl2 = BrainLoader(file_path=self.brain_to_test) self.assertTrue(bl1.brain is bl2.brain)
def test_execute(self): """ In this test the brain contains a neurotransmitter """ # -------------------------------------- # Test 1. The user answers correctly to all neurotransmitter # -------------------------------------- # we suppose that the first synapse has matched the first synapse synapse = BrainLoader().brain.get_synapse_by_name("synapse1") order = "enter in synapse 1" matched_synapse = MatchedSynapse(matched_synapse=synapse, user_order=order, matched_order=order) list_matched_synapse = list() list_matched_synapse.append(matched_synapse) self.lifo_buffer.add_synapse_list_to_lifo(list_matched_synapse) self.lifo_buffer.api_response.user_order = order with mock.patch("intelora.core.TTS.TTSModule.generate_and_play"): response = self.lifo_buffer.execute(is_api_call=True) expected_result = { 'status': 'waiting_for_answer', 'matched_synapses': [{ 'matched_order': 'enter in synapse 1', 'neuron_module_list': [{ 'neuron_name': 'Say', 'generated_message': 'question in synapse 1' }], 'synapse_name': 'synapse1' }], 'user_order': 'enter in synapse 1' } self.assertEqual(response, expected_result) # give an answer answer = "answer synapse1" response = self.lifo_buffer.execute(answer=answer, is_api_call=True) expected_result = { 'status': 'waiting_for_answer', 'matched_synapses': [{ 'matched_order': 'enter in synapse 1', 'neuron_module_list': [{ 'neuron_name': 'Say', 'generated_message': 'question in synapse 1' }, { 'neuron_name': 'Neurotransmitter', 'generated_message': None }], 'synapse_name': 'synapse1' }, { 'matched_order': 'answer synapse1', 'neuron_module_list': [{ 'neuron_name': 'Say', 'generated_message': 'enter synapse 2' }], 'synapse_name': 'synapse2' }], 'user_order': None } self.assertEqual(response, expected_result) # give the last answer answer = "synapse5" response = self.lifo_buffer.execute(answer=answer, is_api_call=True) expected_result = { 'status': 'complete', 'matched_synapses': [{ 'matched_order': 'answer synapse1', 'neuron_module_list': [{ 'neuron_name': 'Say', 'generated_message': 'enter synapse 2' }, { 'neuron_name': 'Neurotransmitter', 'generated_message': None }], 'synapse_name': 'synapse2' }, { 'matched_order': 'synapse5', 'neuron_module_list': [{ 'neuron_name': 'Say', 'generated_message': 'execution of synapse 5' }], 'synapse_name': 'synapse5' }, { 'matched_order': 'enter in synapse 1', 'neuron_module_list': [{ 'neuron_name': 'Say', 'generated_message': 'question in synapse 1' }, { 'neuron_name': 'Neurotransmitter', 'generated_message': None }, { 'neuron_name': 'Say', 'generated_message': 'last neuron in synapse 1' }], 'synapse_name': 'synapse1' }], 'user_order': None } self.assertEqual(response, expected_result) # -------------------------------------- # Test 2. The user doesn't answered correctly to the first neurotransmitter # -------------------------------------- # we suppose that the first synapse has matched the first synapse synapse = BrainLoader().brain.get_synapse_by_name("synapse1") order = "enter in synapse 1" matched_synapse = MatchedSynapse(matched_synapse=synapse, user_order=order, matched_order=order) list_matched_synapse = list() list_matched_synapse.append(matched_synapse) self.lifo_buffer.add_synapse_list_to_lifo(list_matched_synapse) self.lifo_buffer.api_response.user_order = order with mock.patch("intelora.core.TTS.TTSModule.generate_and_play"): # fist call to enter in the neurotransmitter self.lifo_buffer.execute(is_api_call=True) wrong_answer = "wrong answer" response = self.lifo_buffer.execute(answer=wrong_answer, is_api_call=True) expected_result = { 'status': 'complete', 'matched_synapses': [{ 'matched_order': 'enter in synapse 1', 'neuron_module_list': [{ 'neuron_name': 'Say', 'generated_message': 'question in synapse 1' }, { 'neuron_name': 'Neurotransmitter', 'generated_message': None }, { 'neuron_name': 'Say', 'generated_message': 'last neuron in synapse 1' }], 'synapse_name': 'synapse1' }, { 'matched_order': None, 'neuron_module_list': [{ 'neuron_name': 'Say', 'generated_message': 'not understood' }], 'synapse_name': 'synapse4' }], 'user_order': None } self.assertEqual(response, expected_result) # -------------------------------------- # Test 3. No synapse matched, we still execute the list # -------------------------------------- list_matched_synapse = list() self.lifo_buffer.add_synapse_list_to_lifo(list_matched_synapse) self.lifo_buffer.api_response.user_order = "this is an order" with mock.patch("intelora.core.TTS.TTSModule.generate_and_play"): # fist call to enter in the neurotransmitter response = self.lifo_buffer.execute(is_api_call=True) expected_result = { 'status': None, 'matched_synapses': [], 'user_order': 'this is an order' } self.assertEqual(response, expected_result)
def test_replace_global_variables(self): """ Testing the _replace_global_variables function from the NeuronLauncher. Scenarii: - 1/ only one global variable - 2/ global variable with string after - 3/ global variable with int after - 4/ multiple global variables - 5/ parameter value is a list - 6/ parameter is a dict """ # 1/ only one global variable parameters = { 'var1': '{{hello}}' } variables = { "hello": "test", "hello2": "test2", } st = Settings(variables=variables) expected_parameters = { 'var1': 'test' } self.assertEqual(BrainLoader._replace_global_variables(parameter=parameters, settings=st), expected_parameters, "Fail to assign a single global variable to parameters") # 2/ global variable with string after parameters = { 'var1': '{{hello}} Sispheor' } variables = { "hello": "test", "hello2": "test2", } st = Settings(variables=variables) expected_parameters = { 'var1': 'test Sispheor' } self.assertEqual(BrainLoader._replace_global_variables(parameter=parameters, settings=st), expected_parameters, "Fail to assign a global variable with string after to parameters") # 3/ global variable with int after parameters = { 'var1': '{{hello}}0' } variables = { "hello": 60, "hello2": "test2", } st = Settings(variables=variables) expected_parameters = { 'var1': '600' } self.assertEqual(BrainLoader._replace_global_variables(parameter=parameters, settings=st), expected_parameters, "Fail to assign global variable with int after to parameters") # 4/ multiple global variables parameters = { 'var1': '{{hello}} {{me}}' } variables = { "hello": "hello", "me": "LaMonf" } st = Settings(variables=variables) expected_parameters = { 'var1': 'hello LaMonf' } self.assertEqual(BrainLoader._replace_global_variables(parameter=parameters, settings=st), expected_parameters, "Fail to assign multiple global variables to parameters") # 5/ parameter value is a list parameters = { 'var1': '[hello {{name}}, bonjour {{name}}]' } variables = { "name": "LaMonf", "hello2": "test2", } st = Settings(variables=variables) expected_parameters = { 'var1': '[hello LaMonf, bonjour LaMonf]' } self.assertEqual(BrainLoader._replace_global_variables(parameter=parameters, settings=st), expected_parameters, "Fail to assign a single global when parameter value is a list to neuron") # 6/ parameter is a dict parameters = {'from_answer_link': [{'synapse': 'synapse2', 'answers': ['absolument', '{{ name }}']}, {'synapse': 'synapse3', 'answers': ['{{ name }}']}], 'default': 'synapse4'} variables = { "name": "nico" } st = Settings(variables=variables) expected_parameters = { 'from_answer_link': [ {'synapse': 'synapse2', 'answers': ['absolument', 'nico']}, {'synapse': 'synapse3', 'answers': ['nico']}], 'default': 'synapse4' } self.assertEqual(BrainLoader._replace_global_variables(parameter=parameters, settings=st), expected_parameters, "Fail to assign a single global when parameter value is a list to neuron")
def test_get_yaml_config(self): """ Test we can get a yaml config from the path """ brain_loader = BrainLoader(file_path=self.brain_to_test) self.assertEqual(brain_loader.yaml_config, self.expected_result)