Пример #1
0
    def test_get_default_synapse_from_sysnapses_list(self):
        # Init
        neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
        neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
        neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'})
        neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'})

        signal1 = Order(sentence="this is the sentence")
        signal2 = Order(sentence="this is the second sentence")
        signal3 = Order(sentence="that is part of the third sentence")

        synapse1 = Synapse(name="Synapse1",
                           neurons=[neuron1, neuron2],
                           signals=[signal1])
        synapse2 = Synapse(name="Synapse2",
                           neurons=[neuron3, neuron4],
                           signals=[signal2])
        synapse3 = Synapse(name="Synapse3",
                           neurons=[neuron2, neuron4],
                           signals=[signal3])

        default_synapse_name = "Synapse2"
        all_synapse_list = [synapse1, synapse2, synapse3]
        expected_result = synapse2

        # Assert equals
        self.assertEquals(
            OrderAnalyser._get_default_synapse_from_sysnapses_list(
                all_synapses_list=all_synapse_list,
                default_synapse_name=default_synapse_name), expected_result,
            "Fail to match the expected default Synapse")
Пример #2
0
    def test_get_matching_synapse_list(self):
        # Init
        neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
        neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
        neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'})
        neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'})

        signal1 = Order(sentence="this is the sentence")
        signal2 = Order(sentence="this is the second sentence")
        signal3 = Order(sentence="this is the third sentence")

        synapse1 = Synapse(name="Synapse1",
                           neurons=[neuron1, neuron2],
                           signals=[signal1])
        synapse2 = Synapse(name="Synapse2",
                           neurons=[neuron3, neuron4],
                           signals=[signal2])
        synapse3 = Synapse(name="Synapse3",
                           neurons=[neuron2, neuron4],
                           signals=[signal3])

        order_to_match = "this is the sentence"
        all_synapse_list = [synapse1, synapse2, synapse3]

        expected_result = [synapse1]

        # Success
        self.assertEquals(
            OrderAnalyser._get_matching_synapse_list(
                all_synapses_list=all_synapse_list,
                order_to_match=order_to_match), expected_result,
            "Fail matching the expected synapse from the complete synapse list and the order"
        )
Пример #3
0
    def test_start_synapse(self):
        """
        Test the Synapse launcher trying to start synapse
        """
        # Init
        neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
        neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
        neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'})
        neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'})

        signal1 = Order(sentence="this is the sentence")
        signal2 = Order(sentence="this is the second sentence")
        signal3 = Order(sentence="that is part of the third sentence")

        synapse1 = Synapse(name="Synapse1",
                           neurons=[neuron1, neuron2],
                           signals=[signal1])
        synapse2 = Synapse(name="Synapse2",
                           neurons=[neuron3, neuron4],
                           signals=[signal2])
        synapse3 = Synapse(name="Synapse3",
                           neurons=[neuron2, neuron4],
                           signals=[signal3])

        all_synapse_list = [synapse1, synapse2, synapse3]

        br = Brain(synapses=all_synapse_list)

        sl = SettingLoader()
        r = Resources(neuron_folder="/var/tmp/test/resources")
        sl.settings.resources = r
        with mock.patch("kalliope.core.Utils.get_dynamic_class_instantiation"
                        ) as mock_get_class_instantiation:
            # Success
            SynapseLauncher.start_synapse("Synapse1", brain=br)

            calls = [
                mock.call(package_name="neurons",
                          module_name=neuron1.name,
                          parameters=neuron1.parameters,
                          resources_dir='/var/tmp/test/resources'),
                mock.call(package_name="neurons",
                          module_name=neuron2.name,
                          parameters=neuron2.parameters,
                          resources_dir='/var/tmp/test/resources')
            ]
            mock_get_class_instantiation.assert_has_calls(calls=calls)
            mock_get_class_instantiation.reset_mock()

            # Fail
            with self.assertRaises(SynapseNameNotFound):
                SynapseLauncher.start_synapse("Synapse4", brain=br)
Пример #4
0
    def test_run_synapse(self):
        """
        Test to run a Synapse
        """
        neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
        neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
        signal1 = Order(sentence="this is the sentence")
        synapse1 = Synapse(name="Synapse1",
                           neurons=[neuron1, neuron2],
                           signals=[signal1])
        synapse_empty = Synapse(name="Synapse_empty",
                                neurons=[],
                                signals=[signal1])
        sl = SettingLoader()
        resources = Resources(neuron_folder='/var/tmp/test/resources')
        sl.settings.resources = resources
        with mock.patch("kalliope.core.Utils.get_dynamic_class_instantiation"
                        ) as mock_get_class_instantiation:
            SynapseLauncher._run_synapse(synapse=synapse1)

            calls = [
                mock.call(package_name="neurons",
                          module_name=neuron1.name,
                          parameters=neuron1.parameters,
                          resources_dir="/var/tmp/test/resources"),
                mock.call(package_name="neurons",
                          module_name=neuron2.name,
                          parameters=neuron2.parameters,
                          resources_dir="/var/tmp/test/resources")
            ]
            mock_get_class_instantiation.assert_has_calls(calls=calls)
            mock_get_class_instantiation.reset_mock()

            # Do not any Neurons
            SynapseLauncher._run_synapse(synapse=synapse_empty)
            mock_get_class_instantiation.assert_not_called()
            mock_get_class_instantiation.reset_mock()
Пример #5
0
    def load_brain(self):
        """
        Class Methods which loads default or the provided YAML file and return a Brain
        :return: The loaded Brain
        :rtype: Brain

        :Example:

            brain = BrainLoader.load_brain(file_path="/var/tmp/brain.yml")

        .. seealso:: Brain
        .. warnings:: Class Method
        """

        # Instantiate a brain
        brain = Brain()

        # get the brain with dict
        dict_brain = self.get_yaml_config()

        brain.brain_yaml = dict_brain
        # create list of Synapse
        synapses = list()
        for synapses_dict in dict_brain:
            if "includes" not in synapses_dict:  # we don't need to check includes as it's not a synapse
                if ConfigurationChecker().check_synape_dict(synapses_dict):
                    name = synapses_dict["name"]
                    neurons = self._get_neurons(synapses_dict["neurons"],
                                                self.settings)
                    signals = self._get_signals(synapses_dict["signals"])
                    new_synapse = Synapse(name=name,
                                          neurons=neurons,
                                          signals=signals)
                    synapses.append(new_synapse)
        brain.synapses = synapses
        if self.file_path is None:
            brain.brain_file = self._get_root_brain_path()
        else:
            brain.brain_file = self.file_path
        # check that no synapse have the same name than another
        if not ConfigurationChecker().check_synapes(synapses):
            brain = None

        return brain
Пример #6
0
    def test_get_synapse_params(self):
        # Init
        neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
        neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})

        signal1 = Order(sentence="this is the {{ sentence }}")

        synapse1 = Synapse(name="Synapse1",
                           neurons=[neuron1, neuron2],
                           signals=[signal1])

        order_to_check = "this is the value"
        expected_result = {'sentence': 'value'}

        self.assertEquals(
            OrderAnalyser._get_synapse_params(synapse=synapse1,
                                              order_to_check=order_to_check),
            expected_result,
            "Fail to retrieve the params of the synapse from the order")
Пример #7
0
    def test_find_synapse_to_run(self):
        """
        Test to find the good synapse to run
        Scenarii:
            - Find the synapse
            - No synpase found, no default synapse
            - No synapse found, run the default synapse
        """
        # Init
        neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
        neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
        neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'})
        neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'})

        signal1 = Order(sentence="this is the sentence")
        signal2 = Order(sentence="this is the second sentence")
        signal3 = Order(sentence="that is part of the third sentence")

        synapse1 = Synapse(name="Synapse1",
                           neurons=[neuron1, neuron2],
                           signals=[signal1])
        synapse2 = Synapse(name="Synapse2",
                           neurons=[neuron3, neuron4],
                           signals=[signal2])
        synapse3 = Synapse(name="Synapse3",
                           neurons=[neuron2, neuron4],
                           signals=[signal3])

        all_synapse_list = [synapse1, synapse2, synapse3]

        br = Brain(synapses=all_synapse_list)
        st = Settings()
        # Find synapse
        order = "this is the sentence"
        expected_result = synapse1
        oa_tuple_list = OrderAnalyser._find_synapse_to_run(brain=br,
                                                           settings=st,
                                                           order=order)
        self.assertEquals(oa_tuple_list[0].synapse, expected_result,
                          "Fail to run the proper synapse matching the order")

        expected_result = signal1.sentence
        self.assertEquals(oa_tuple_list[0].order, expected_result,
                          "Fail to run the proper synapse matching the order")
        # No Default synapse
        order = "No default synapse"
        expected_result = []
        self.assertEquals(
            OrderAnalyser._find_synapse_to_run(brain=br,
                                               settings=st,
                                               order=order), expected_result,
            "Fail to run no synapse, when no default is defined")

        # Default synapse
        st = Settings(default_synapse="Synapse2")
        order = "default synapse"
        expected_result = synapse2
        oa_tuple_list = OrderAnalyser._find_synapse_to_run(brain=br,
                                                           settings=st,
                                                           order=order)
        self.assertEquals(oa_tuple_list[0].synapse, expected_result,
                          "Fail to run the default synapse")
Пример #8
0
    def test_get_matching_synapse_list(self):
        # Init
        neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
        neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
        neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'})
        neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'})

        signal1 = Order(sentence="this is the sentence")
        signal2 = Order(sentence="this is the second sentence")
        signal3 = Order(sentence="that is part of the third sentence")

        synapse1 = Synapse(name="Synapse1",
                           neurons=[neuron1, neuron2],
                           signals=[signal1])
        synapse2 = Synapse(name="Synapse2",
                           neurons=[neuron3, neuron4],
                           signals=[signal2])
        synapse3 = Synapse(name="Synapse3",
                           neurons=[neuron2, neuron4],
                           signals=[signal3])

        order_to_match = "this is the sentence"
        all_synapse_list = [synapse1, synapse2, synapse3]

        # Success
        expected_result = synapse1
        oa_tuple_list = OrderAnalyser._get_matching_synapse_list(
            all_synapses_list=all_synapse_list, order_to_match=order_to_match)
        self.assertEquals(
            oa_tuple_list[0].synapse, expected_result,
            "Fail matching 'the expected synapse' from the complete synapse list and the order"
        )

        # Multiple Matching synapses
        signal2 = Order(sentence="this is the sentence")

        synapse2 = Synapse(name="Synapse2",
                           neurons=[neuron3, neuron4],
                           signals=[signal2])
        order_to_match = "this is the sentence"

        all_synapse_list = [synapse1, synapse2, synapse3]

        expected_result = [synapse1, synapse2]
        oa_tuple_list = OrderAnalyser._get_matching_synapse_list(
            all_synapses_list=all_synapse_list, order_to_match=order_to_match)
        self.assertEquals(
            oa_tuple_list[0].synapse, expected_result[0],
            "Fail 'Multiple Matching synapses' from the complete synapse list and the order (first element)"
        )
        self.assertEquals(
            oa_tuple_list[1].synapse, expected_result[1],
            "Fail 'Multiple Matching synapses' from the complete synapse list and the order (second element)"
        )

        # matching no synapses
        order_to_match = "this is not the correct word"

        all_synapse_list = [synapse1, synapse2, synapse3]

        expected_result = []

        self.assertEquals(
            OrderAnalyser._get_matching_synapse_list(
                all_synapses_list=all_synapse_list,
                order_to_match=order_to_match), expected_result,
            "Fail matching 'no synapses' from the complete synapse list and the order"
        )

        # matching synapse with all key worlds
        # /!\ Some words in the order are matching all words in synapses signals !
        order_to_match = "this is not the correct sentence"
        all_synapse_list = [synapse1, synapse2, synapse3]

        expected_result = [synapse1, synapse2]

        oa_tuple_list = OrderAnalyser._get_matching_synapse_list(
            all_synapses_list=all_synapse_list, order_to_match=order_to_match)

        self.assertEquals(
            oa_tuple_list[0].synapse, expected_result[0],
            "Fail matching 'synapse with all key worlds' from the complete synapse list and the order (first element)"
        )
        self.assertEquals(
            oa_tuple_list[1].synapse, expected_result[1],
            "Fail matching 'synapse with all key worlds' from the complete synapse list and the order (second element)"
        )
Пример #9
0
    def test_start(self):
        """
        Testing if the matches from the incoming messages and the signals/order sentences.
        Scenarii :
            - Order matchs a synapse and the synapse has been launched.
            - Order does not match but have a default synapse.
            - Order does not match and does not have default synapse.
            - Provide synapse without any external orders
            - Provide synapse with any external orders
        """
        # Init
        neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
        neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
        neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'})
        neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'})

        signal1 = Order(sentence="this is the sentence")
        signal2 = Order(sentence="this is the second sentence")
        signal3 = Order(sentence="that is part of the third sentence")

        synapse1 = Synapse(name="Synapse1",
                           neurons=[neuron1, neuron2],
                           signals=[signal1])
        synapse2 = Synapse(name="Synapse2",
                           neurons=[neuron3, neuron4],
                           signals=[signal2])
        synapse3 = Synapse(name="Synapse3",
                           neurons=[neuron2, neuron4],
                           signals=[signal3])

        all_synapse_list = [synapse1, synapse2, synapse3]

        br = Brain(synapses=all_synapse_list)

        with mock.patch("kalliope.core.OrderAnalyser._start_neuron"
                        ) as mock_start_neuron_method:
            # assert synapses have been launched
            order_to_match = "this is the sentence"
            oa = OrderAnalyser(order=order_to_match, brain=br)
            expected_result = [synapse1]

            self.assertEquals(
                oa.start(), expected_result,
                "Fail to run the expected Synapse matching the order")

            calls = [mock.call(neuron1, {}), mock.call(neuron2, {})]
            mock_start_neuron_method.assert_has_calls(calls=calls)
            mock_start_neuron_method.reset_mock()

            # No order matching Default Synapse to run
            order_to_match = "random sentence"
            oa = OrderAnalyser(order=order_to_match, brain=br)
            oa.settings = mock.MagicMock(default_synapse="Synapse3")
            expected_result = [synapse3]
            self.assertEquals(
                oa.start(), expected_result,
                "Fail to run the default Synapse because no other synapses match the order"
            )

            # No order matching no Default Synapse
            order_to_match = "random sentence"
            oa = OrderAnalyser(order=order_to_match, brain=br)
            oa.settings = mock.MagicMock()
            expected_result = []
            self.assertEquals(
                oa.start(), expected_result,
                "Fail to no synapse because no synapse matchs and no default defined"
            )

            # Provide synapse to run
            order_to_match = "this is the sentence"
            oa = OrderAnalyser(order=order_to_match, brain=br)
            expected_result = [synapse1]
            synapses_to_run = [synapse1]

            self.assertEquals(oa.start(synapses_to_run=synapses_to_run),
                              expected_result,
                              "Fail to run the provided synapse to run")
            calls = [mock.call(neuron1, {}), mock.call(neuron2, {})]
            mock_start_neuron_method.assert_has_calls(calls=calls)
            mock_start_neuron_method.reset_mock()

            # Provide synapse and external orders
            order_to_match = "this is an external sentence"
            oa = OrderAnalyser(order=order_to_match, brain=br)
            external_orders = "this is an external {{ order }}"
            synapses_to_run = [synapse2]
            expected_result = [synapse2]

            self.assertEquals(
                oa.start(synapses_to_run=synapses_to_run,
                         external_order=external_orders), expected_result,
                "Fail to run a provided synapse with external order")
            calls = [
                mock.call(neuron3, {"order": u"sentence"}),
                mock.call(neuron4, {"order": u"sentence"})
            ]
            mock_start_neuron_method.assert_has_calls(calls=calls)
            mock_start_neuron_method.reset_mock()
Пример #10
0
    def test_run_synapse_by_name_with_order(self):
        """
        Test to start a synapse with a specific given order
        Scenarii :
            - Neuron has been found and launched
            - Neuron has not been found
        """

        # Init
        neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
        neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
        neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'})
        neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'})

        signal1 = Order(sentence="the sentence")
        signal2 = Order(sentence="the second sentence")
        signal3 = Order(sentence="part of the third sentence")

        synapse1 = Synapse(name="Synapse1",
                           neurons=[neuron1, neuron2],
                           signals=[signal1])
        synapse2 = Synapse(name="Synapse2",
                           neurons=[neuron3, neuron4],
                           signals=[signal2])
        synapse3 = Synapse(name="Synapse3",
                           neurons=[neuron2, neuron4],
                           signals=[signal3])

        all_synapse_list = [synapse1, synapse2, synapse3]

        br = Brain(synapses=all_synapse_list)

        order = "This is the order"
        synapse_name = "Synapse2"
        answer = "This is the {{ answer }}"

        with mock.patch("kalliope.core.OrderAnalyser.start"
                        ) as mock_orderAnalyser_start:
            neuron_mod = NeuronModule()
            neuron_mod.brain = br

            # Success
            self.assertTrue(
                neuron_mod.run_synapse_by_name_with_order(
                    order=order,
                    synapse_name=synapse_name,
                    order_template=answer), "fail to find the proper synapse")

            # mock_orderAnalyser_start.assert_called_once()
            mock_orderAnalyser_start.assert_called_once_with(
                synapses_to_run=[synapse2], external_order=answer)
            mock_orderAnalyser_start.reset_mock()

            # Fail
            synapse_name = "Synapse5"
            self.assertFalse(
                neuron_mod.run_synapse_by_name_with_order(
                    order=order,
                    synapse_name=synapse_name,
                    order_template=answer), "fail to NOT find the synapse")

            mock_orderAnalyser_start.assert_not_called()
            mock_orderAnalyser_start.reset_mock()