Beispiel #1
0
    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 brackets
            - 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}}']}}]

        st = Settings()
        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 {{name}}']})

        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 create_synapses(self):
        """
        curl -i -H "Content-Type: application/json" \
        --user admin:secret \
        -X POST \
        -d '{
          "name": "Say-hello",
          "signals": [
            {
              "order": "je suis nicolas"
            }
          ],
          "neurons": [
            {
              "say": {
                "message": "je sais"
              }
            }
          ]
        }' \
        http://127.0.0.1:5000/synapses
        :return:
        """
        if not request.get_json() or 'name' not in request.get_json():
            data = {
                "Error": "Wrong parameters, 'name' not set"
            }
            return jsonify(error=data), 400

        new_synapse = request.get_json()
        try:
            ConfigurationChecker().check_synape_dict(new_synapse)
        except (InvalidSynapeName, NoSynapeNeurons, NoSynapeSignals) as e:
            data = {
                "error": "%s" % e
            }
            return jsonify(data), 400

        try:
            name = new_synapse["name"]
            neurons = BrainLoader.get_neurons(new_synapse["neurons"], self.settings)
            signals = BrainLoader.get_signals(new_synapse["signals"])
            new_synapse_instance = Synapse(name=name, neurons=neurons, signals=signals)
            self.brain.synapses.append(new_synapse_instance)
            # TODO save the brain in yaml
            return jsonify(new_synapse_instance.serialize()), 201
        except KalliopeModuleNotFoundError as e:
            data = {
                "error": "%s" % e
            }
            return jsonify(data), 400