Эх сурвалжийг харах

update rest api to use the lifo and update doc

nico 8 жил өмнө
parent
commit
4b6e764fc1

+ 79 - 46
Docs/rest_api.md

@@ -130,23 +130,20 @@ curl -i --user admin:secret -X POST  http://localhost:5000/synapses/start/id/say
 Output example:
 ```JSON
 {
-  "synapses": {
-    "name": "say-hello",
-    "neurons": [
-      {
-        "say": {
-          "message": [
-            "Bonjour monsieur"
-          ]
+  "matched_synapses": [
+    {
+      "matched_order": null,
+      "neuron_module_list": [
+        {
+          "generated_message": "Bonjour monsieur",
+          "neuron_name": "Say"
         }
-      }
-    ],
-    "signals": [
-      {
-        "order": "bonjour"
-      }
-    ]
-  }
+      ],
+      "synapse_name": "say-hello-fr"
+    }
+  ],
+  "status": "complete",
+  "user_order": null
 }
 ```
 
@@ -174,31 +171,49 @@ curl -i --user admin:secret -H "Content-Type: application/json" -X POST --data @
 Output example if the order have matched and so launched synapses:
 ```JSON
 {
-  "synapses": [
+  "matched_synapses": [
     {
-      "name": "Say-hello", 
-      "neurons": [
+      "matched_order": "Bonjour",
+      "neuron_module_list": [
         {
-          "name": "say", 
-          "parameters": "{'message': ['Hello sir']}"
+          "generated_message": "Bonjour monsieur",
+          "neuron_name": "Say"
         }
-      ], 
-      "signals": [
+      ],
+      "synapse_name": "say-hello-fr"
+    }
+  ],
+  "status": "complete",
+  "user_order": "bonjour"
+}
+```
+
+If the order haven't match any synapses it will try to run the default synapse if this lat exist in your settings:
+```JSON
+{
+  "matched_synapses": [
+    {
+      "matched_order": null,
+      "neuron_module_list": [
         {
-          "order": "hello"
+          "generated_message": "Je n'ai pas compris vôtre ordre",
+          "neuron_name": "Say"
         }
-      ]
+      ],
+      "synapse_name": "default-synapse"
     }
-  ]
+  ],
+  "status": "complete",
+  "user_order": "not existing order"
 }
 ```
 
-If the order haven't match any synapses:
-```JSON
+Or return an empty list of matched synapse
+```
 {
-  "error": {
-    "error": "The given order doesn't match any synapses"
-  }
+  "matched_synapses": [],
+  "status": null,
+  "user_order": "not existing order"
 }
 ```
 
@@ -217,30 +232,48 @@ curl -i --user admin:secret -X POST  http://localhost:5000/synapses/start/audio
 Output example if the order inside the audio have matched and so launched synapses:
 ```JSON
 {
-  "synapses": [
+  "matched_synapses": [
     {
-      "name": "Say-hello", 
-      "neurons": [
+      "matched_order": "Bonjour",
+      "neuron_module_list": [
         {
-          "name": "say", 
-          "parameters": "{'message': ['Hello sir']}"
+          "generated_message": "Bonjour monsieur",
+          "neuron_name": "Say"
         }
-      ], 
-      "signals": [
+      ],
+      "synapse_name": "say-hello-fr"
+    }
+  ],
+  "status": "complete",
+  "user_order": "bonjour"
+}
+```
+
+If the order haven't match any synapses it will try to run the default synapse if this lat exist in your settings:
+```JSON
+{
+  "matched_synapses": [
+    {
+      "matched_order": null,
+      "neuron_module_list": [
         {
-          "order": "hello"
+          "generated_message": "Je n'ai pas compris vôtre ordre",
+          "neuron_name": "Say"
         }
-      ]
+      ],
+      "synapse_name": "default-synapse"
     }
-  ]
+  ],
+  "status": "complete",
+  "user_order": "not existing order"
 }
 ```
 
-If the order haven't match any synapses:
-```JSON
+Or return an empty list of matched synapse
+```
 {
-  "error": {
-    "error": "The given order doesn't match any synapses"
-  }
+  "matched_synapses": [],
+  "status": null,
+  "user_order": "not existing order"
 }
 ```

+ 20 - 0
kalliope/core/LIFOBuffer.py

@@ -36,6 +36,19 @@ class LIFOBuffer(object):
 
     @classmethod
     def execute(cls, answer=None, is_api_call=False):
+        """
+        Process the LIFO list.
+        
+        The LIFO list contains multiple list of matched synapses.        
+        For each list of matched synapse we process synapses inside        
+        For each synapses we process neurons.        
+        If a neuron add a Synapse list to the lifo, this synapse list is processed before executing the first list 
+        in which we were in.
+        
+        :param answer: String answer to give the the last neuron which whas waiting for an answer
+        :param is_api_call: Boolean passed to all neuron in order to let them know if the current call comes from API
+        :return: serialized APIResponse object
+        """
 
         # we keep looping over the LIFO til we have synapse list to process in it
         while cls.lifo_list:
@@ -113,3 +126,10 @@ class LIFOBuffer(object):
         # we clean up the API response object for the next call
         cls.api_response = APIResponse()
         return will_be_returned
+
+    @classmethod
+    def clean(cls):
+        """
+        Clean the LIFO by creating a new list
+        """
+        cls.lifo_list = list()

+ 1 - 1
kalliope/core/NeuronModule.py

@@ -113,7 +113,7 @@ class NeuronModule(object):
         """
         return {
             'neuron_name': self.neuron_name,
-            'tts_message': self.tts_message
+            'generated_message': self.tts_message
         }
 
     def say(self, message):

+ 16 - 7
kalliope/core/RestAPI/FlaskAPI.py

@@ -4,9 +4,11 @@ import threading
 
 import time
 
+from kalliope.core.LIFOBuffer import LIFOBuffer
+from kalliope.core.Models.MatchedSynapse import MatchedSynapse
 from kalliope.core.Utils.FileManager import FileManager
 
-from kalliope.core.ConfigurationManager import SettingLoader
+from kalliope.core.ConfigurationManager import SettingLoader, BrainLoader
 from kalliope.core.OrderListener import OrderListener
 from werkzeug.utils import secure_filename
 
@@ -136,18 +138,25 @@ class FlaskAPI(threading.Thread):
         :param synapse_name:
         :return:
         """
-        synapse_target = self._get_synapse_by_name(synapse_name)
+        # get a synapse object from the name
+        synapse_target = BrainLoader().get_brain().get_synapse_by_name(synapse_name=synapse_name)
 
         if synapse_target is None:
             data = {
                 "synapse name not found": "%s" % synapse_name
             }
             return jsonify(error=data), 404
-
-        # run the synapse
-        SynapseLauncher.start_synapse(synapse_name, brain=self.brain)
-        data = jsonify(synapses=synapse_target.serialize())
-        return data, 201
+        else:
+            # generate a MatchedSynapse from the synapse
+            matched_synapse = MatchedSynapse(matched_synapse=synapse_target)
+            # get the current LIFO buffer
+            lifo_buffer = LIFOBuffer()
+            # 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)
+            data = jsonify(response)
+            return data, 201
 
     @requires_auth
     def run_synapse_by_order(self):