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
    def test_Synapse(self):
        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 = Signal(name="order", parameters="this is the sentence")
        signal2 = Signal(name="order",
                         parameters="this is the second sentence")

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

        expected_result_serialize = {
            'signals': [{
                'name': 'order',
                'parameters': 'this is the sentence'
            }],
            'neurons': [{
                'name': 'neurone1',
                'parameters': {
                    'var1': 'val1'
                }
            }, {
                'name': 'neurone2',
                'parameters': {
                    'var2': 'val2'
                }
            }],
            'name':
            'Synapse1',
            'enabled':
            True
        }

        self.assertDictEqual(expected_result_serialize, synapse1.serialize())

        self.assertTrue(synapse1.__eq__(synapse3))
        self.assertFalse(synapse1.__eq__(synapse2))
Exemple #3
0
    def test_Synapse(self):
        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 = Signal(name="order", parameters="this is the sentence")
        signal2 = Signal(name="order", parameters="this is the second sentence")

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

        expected_result_serialize = {
            'signals': [
                {
                    'name': 'order',
                    'parameters': 'this is the sentence'
                }
            ],
            'neurons': [
                {
                    'name': 'neurone1',
                    'parameters': {
                         'var1': 'val1'
                     }
                },
                {
                    'name': 'neurone2',
                    'parameters':
                        {
                            'var2': 'val2'
                        }
                }
            ],
            'name': 'Synapse1',
            'enabled': True
        }

        self.assertDictEqual(expected_result_serialize, synapse1.serialize())

        self.assertTrue(synapse1.__eq__(synapse3))
        self.assertFalse(synapse1.__eq__(synapse2))