浏览代码

update order analyser to use Models

Nicolas Marcq 8 年之前
父节点
当前提交
3e37afcbd8
共有 9 个文件被更改,包括 68 次插入57 次删除
  1. 2 15
      brain.yml
  2. 9 0
      brain_examples/fr/hello_world.yml
  3. 9 0
      brain_examples/fr/say_examples.yml
  4. 6 3
      core/Models/Neurone.py
  5. 17 13
      core/NeuroneLauncher.py
  6. 10 12
      core/OrderAnalyser.py
  7. 1 1
      neurons/say/say.py
  8. 5 5
      test.py
  9. 9 8
      test.yml

+ 2 - 15
brain.yml

@@ -1,17 +1,4 @@
 ---
-  - name: "Say hello"
-    neurons:
-      - say:
-          message:
-            - "Bonjour monsieur"
-            - "Bonjour maitre"
-      - sleep:
-          seconds: 1
-      - say:
-          message: "Je suis Jarvice"
-          tts: "voxygen"
-    when:
-      - order: "dis bonjour"
 
   - name: "Meaning of life"
     neurons:
@@ -25,7 +12,7 @@
   - name: "Run a simple script"
     neurons:
       - script:
-        path: "/home/nico/test.sh"
+          path: "/home/nico/test.sh"
       - say:
           message: "Script lancé, monsieur"
     when:
@@ -37,7 +24,7 @@
           say_template:
             - "Il est {{ hours }} heures et {{ minutes }} minutes"
           tts: "voxygen"
-    when:
+    signals:
       - order: "quelle heure"
 
   - name: "Say local date from template"

+ 9 - 0
brain_examples/fr/hello_world.yml

@@ -0,0 +1,9 @@
+---
+  - name: "Say hello"
+    neurons:
+      - say:
+          message:
+            - "Bonjour monsieur"
+            - "Bonjour maitre"
+    signals:
+      - order: "dit bonjour"

+ 9 - 0
brain_examples/fr/say_examples.yml

@@ -0,0 +1,9 @@
+---
+  - name: "Meaning of life"
+    neurons:
+      - say:
+          message:
+            - "42"
+          tts: "pico2wave"
+    signals:
+      - order: "sens de la vie"

+ 6 - 3
core/Models/Neurone.py

@@ -27,7 +27,7 @@ class TTSNotInstantiable(Exception):
 
 
 class Neurone(object):
-    def __init__(self, name=None, parameters=None):
+    def __init__(self, name=None, parameters=None, **kargs):
         # get the name of the plugin who load Neurone mother class
         # print self.__class__.__name__
         self.name = name
@@ -36,7 +36,10 @@ class Neurone(object):
         # print "Neurone class called with name %s and parameters: %s" % (name, parameters)
 
         # get the tts if is specified otherwise use default
-        tts = self.parameters.get('tts', None)
+        tts = None
+        if self.parameters is not None:
+            tts = self.parameters.get('tts', None)
+
         if tts is not None:
             self.tts = tts
         else:
@@ -48,7 +51,7 @@ class Neurone(object):
         # load the module
         self.tts_instance = self._get_tts_instance()
 
-    def say(self, message, kwargs):
+    def say(self, message, **kwargs):
         # get the tts if is specified otherwise use default
         tts = kwargs.get('tts', None)
         if tts is not None:

+ 17 - 13
core/NeuroneLauncher.py

@@ -35,19 +35,23 @@ class NeuroneLauncher:
     @classmethod
     def start_neurone(cls, neuron):
         """
-        Neuron dict to start. {'neurone_name': {'args1': 'value1',  'args2': 'value2'}}
-        :param neuron: Dict with neuron declaration
-        :type neuron: Dict
+        Start a neuron plugin
+        :param neuron: neuron object
+        :type neuron: Neurone
         :return:
         """
-        if isinstance(neuron, dict):
-            for plugin, parameters in neuron.items():
-                # capitalizes the first letter (because classes have first letter upper case)
-                plugin = plugin.capitalize()
-                _run_plugin(plugin, parameters)
-        else:
-            plugin = neuron
-            # capitalizes the first letter (because classes have first letter upper case)
-            plugin = plugin.capitalize()
-            _run_plugin(plugin)
+        plugin = neuron.name.capitalize()
+        plugin = plugin.capitalize()
+        _run_plugin(plugin, neuron.parameters)
+
+        # if isinstance(neuron, dict):
+        #     for plugin, parameters in neuron.items():
+        #         # capitalizes the first letter (because classes have first letter upper case)
+        #         plugin = plugin.capitalize()
+        #         _run_plugin(plugin, parameters)
+        # else:
+        #     plugin = neuron
+        #     # capitalizes the first letter (because classes have first letter upper case)
+        #     plugin = plugin.capitalize()
+        #     _run_plugin(plugin)
 

+ 10 - 12
core/OrderAnalyser.py

@@ -2,6 +2,7 @@ import re
 
 from core import ConfigurationManager
 from core.ConfigurationManager.BrainLoader import BrainLoader
+from core.Models import Order
 from core.NeuroneLauncher import NeuroneLauncher
 import logging
 
@@ -17,22 +18,19 @@ class OrderAnalyser:
         self.main_controller = main_controller
         self.order = order
         if brain_file is None:
-            self.brain = ConfigurationManager.get_brain()
+            self.brain = BrainLoader.get_brain()
         else:
-            self.brain = BrainLoader(brain_file).get_config()
+            self.brain = BrainLoader(brain_file).get_brain()
         logging.info("Receiver order: %s" % self.order)
 
     def start(self):
-        for el in self.brain:
-            whens = el["when"]
-            for when in whens:
-                brain_order = when["order"]
-                # print "order to test: %s" % brain_order
-                if self._spelt_order_match_brain_order(brain_order):
-                    print "Order found! Run neurons: %s" % el["neurons"]
-                    neurons = el["neurons"]
-                    for neuron in neurons:
-                        NeuroneLauncher.start_neurone(neuron)
+        for synapse in self.brain.synapes:
+            for signal in synapse.signals:
+                if type(signal) == Order:
+                    if self._spelt_order_match_brain_order(signal.sentence):
+                        print "Order found! Run neurons: %s" % synapse.neurons
+                        for neuron in synapse.neurons:
+                            NeuroneLauncher.start_neurone(neuron)
 
         # once we ran all plugin, we can start back jarvis trigger
         if self.main_controller is not None:

+ 1 - 1
neurons/say/say.py

@@ -14,4 +14,4 @@ class Say(Neurone):
         if message is None:
             raise NoMessageException("You must specify a message string or a list of messages as parameter")
         else:
-            self.say(message, kwargs)
+            self.say(**kwargs)

+ 5 - 5
test.py

@@ -13,13 +13,13 @@ import logging
 from core import ShellGui
 from crontab import CronSlices, CronTab
 
-# oa = OrderAnalyser("dis bonjour", brain_file="test.yml")
-#
-# oa.start()
+oa = OrderAnalyser("sens de la vie", brain_file="brain_examples/fr/say_examples.yml")
 
+oa.start()
 
-cron_manager = CrontabManager(brain_file="test.yml")
-cron_manager.load_events_in_crontab()
+#
+# cron_manager = CrontabManager(brain_file="test.yml")
+# cron_manager.load_events_in_crontab()
 
 # command = "/path/to/my/command"
 # comment = "JARVIS"

+ 9 - 8
test.yml

@@ -2,12 +2,13 @@
   - name: "Say hello"
     neurons:
       - say:
-          message: "Script lancé, monsieur"
+          message:
+            - "Bonjour monsieur"
+            - "Bonjour maitre"
+      - sleep:
+          seconds: 1
+      - say:
+          message: "Je suis Jarvice"
+          tts: "voxygen"
     signals:
-      - order: "sens de la vie"
-      - event:
-          id: 1
-          period: "* * 5 5 *"
-      - event:
-          id: 2
-          period: "* * 4 4 *"
+      - order: "dis bonjour"