/// <summary> /// executes a method and sets the state to ListeningForParameters if the method requires parameters /// </summary> /// <param name="speechTuple">the tuple with all infomration</param> /// <param name="recognizedMethodText"></param> private void TryExecuteMethod(SpeechTuple speechTuple, string recognizedMethodText) { if (speechTuple.IsEnabled) //first check if the command is enabled { if (speechTuple.Method.MethodInfo.IsStatic == false) { if (speechTuple.InvokeInstance == null) { var obj = Activator.CreateInstance(speechTuple.Method.ExecutingType); speechTuple.InvokeInstance = obj; } } if (speechTuple.Method.Arguments.Count == 0) { //when the method is static speechTuple.InvokeInstance is already null ExecuteMethod(speechTuple, speechTuple.InvokeInstance, null); } else { this.State = ExecutingState.ListeningForParameters; this.CurrentSpeechStream = new SpeechStream(recognizedMethodText, speechTuple); _OnListeningForParameters.OnNext(CurrentSpeechStream); } } }
/// <summary> /// executes the method and fires all events /// </summary> /// <param name="command">the command to execute</param> /// <param name="instance">the instance to execute the method on</param> /// <param name="args">the args for the method</param> private void ExecuteMethod(SpeechTuple command, object instance, params object[] args) { var token = new SpeechTupleArgs(command); this._OnBeforeMethodInvoked.OnNext(token); if (token.CancelCommand == false) { command.Method.MethodInfo.Invoke(instance, args); //TODO maybe wrap this method in try and error raise event... this._OnAfterMethodInvoked.OnNext(command); } }
public SpeechParameterStream(string recognizedParameterNameText, SpeechTuple speechTuple, SpeechParameterInfo speechParameterInfo) { this.RecognizedParameterNameText = recognizedParameterNameText; this.SpeechTuple = speechTuple; this.SpeechParameterInfo = speechParameterInfo; }
/// <summary> /// creates a new command /// </summary> /// <param name="method">the method</param> /// <param name="invokeInstance">the instance to invoke the method on (or null for static methods)</param> public void AddMethod(SpeechMethod method, object invokeInstance) { Dictionary<string, SpeechGroupTuple> langCommands; //create lang if not exists if (Commands.TryGetValue(method.Lang, out langCommands) == false) { langCommands = new Dictionary<string, SpeechGroupTuple>(); Commands.Add(method.Lang, langCommands); } //add the method to the right speech group SpeechGroupTuple speechGroup; if (langCommands.TryGetValue(method.SpeechGroupKey, out speechGroup) == false) { speechGroup = new SpeechGroupTuple(true); langCommands.Add(method.SpeechGroupKey, speechGroup); } //check all synonyms if (method.SpeechNames.Any(p => speechGroup.Commands.ContainsKey(p))) { //there is a synonyme that is already another speech name in this speech group -> invalid throw new Exception("speech dictionary already contains a method with speech name: " + method.SpeechNames + " and key: " + method.Key); } else { var cmd = new SpeechTuple(true, method, invokeInstance); foreach (var speechName in method.SpeechNames) speechGroup.Commands.Add(speechName, cmd); //same command with different names... } }
public SpeechTupleArgs(SpeechTuple speechTuple) { this.SpeechTuple = speechTuple; }
public SpeechStream(string recognizedText, SpeechTuple speechTuple, params SpeechParameterStream[] speechParameterStreams) { this.RecognizedText = recognizedText; this.SpeechTuple = speechTuple; this.SpeechParameterStreams = speechParameterStreams.ToList(); }