/// <summary> /// we have a method and now we got text for a parameter name -> get the right parameter and set it as the current parameter (we still need a value) /// </summary> /// <param name="parameterSpeechText">the new current parameter name</param> private bool SetCurrentParameter(string parameterSpeechText) { //get the real parameter out of the method with the parameterSpeechText foreach (SpeechParameter speechParameter in this.CurrentSpeechStream.SpeechTuple.Method.Arguments) { if (speechParameter.SpeechNames.Contains(parameterSpeechText)) { this.CurrentParameter = new SpeechParameterStream(parameterSpeechText, this.CurrentSpeechStream.SpeechTuple, new SpeechParameterInfo(speechParameter)); SpeechParameterStream copy = null; //1. look if there is already an old value for this parameter //2. check if the parameter value is empty in this case the user just switched to another parameter for (int i = 0; i < this.CurrentSpeechStream.SpeechParameterStreams.Count; i++) { var speechParameterStream = this.CurrentSpeechStream.SpeechParameterStreams[i]; //check if there is a speech parameter info if (speechParameterStream.SpeechParameterInfo.Parameter.ParameterInfo.Name == speechParameter.ParameterInfo.Name) { copy = speechParameterStream; } if (speechParameterStream.SpeechParameterInfo.Value == null) { this.CurrentSpeechStream.SpeechParameterStreams.RemoveAt(i); i--; } } if (copy != null) { this.CurrentSpeechStream.SpeechParameterStreams.Remove(copy); } this.CurrentSpeechStream.SpeechParameterStreams.Add(this.CurrentParameter); this.State = ExecutingState.ListeningForParameterValue; this._OnParameterRecognized.OnNext(this.CurrentParameter); return(true); } } return(false); }
/// <summary> /// finishes the current parameter (assigns value) and if this was the last param then executes the current method /// </summary> /// <param name="valueText"></param> private bool FinishCurrentParameter(string valueText) { if (this.State == ExecutingState.ListeningForParameters) { //check the first left parameter //parameter name was left out int leftParameterIndex = -1; for (int i = 0; i < this.CurrentSpeechStream.SpeechTuple.Method.Arguments.Count; i++) { var arg = this.CurrentSpeechStream.SpeechTuple.Method.Arguments[i]; if ( this.CurrentSpeechStream.SpeechParameterStreams.Any( p => p.SpeechParameterInfo.Parameter.ParameterInfo.Name == arg.ParameterInfo.Name) == false) { leftParameterIndex = i; break; } } if (leftParameterIndex != -1) { this.CurrentParameter = new SpeechParameterStream("", this.CurrentSpeechStream.SpeechTuple, new SpeechParameterInfo(this.CurrentSpeechStream.SpeechTuple.Method.Arguments[leftParameterIndex])); } } SpeechParameterConverter converter = this.CurrentParameter.SpeechParameterInfo.Parameter.Converter; object erg = null;//OnParameterValueConvert(this.CurrentParameter, valueText); if (converter == null) { throw new Exception("converter was not set so the parameter value: " + valueText + " for parameter " + this.CurrentParameter.RecognizedParameterNameText + "(real name: " + this.CurrentParameter.SpeechParameterInfo.Parameter.ParameterInfo.Name + ") couldnt be converted"); } else { if (converter.MethodInfo.IsStatic == false && converter.InvokingInstance == null) { try { converter.InvokingInstance = Activator.CreateInstance(converter.ExecutingType); } catch (Exception ex) { throw new Exception("tried to create an instance of " + converter.ExecutingType.FullName + " in order to convert the parameter " + this.CurrentParameter.RecognizedParameterNameText + "(real name: " + this.CurrentParameter.SpeechParameterInfo.Parameter.ParameterInfo.Name + ")" + "\nException: " + ex.Message); } } erg = converter.MethodInfo.Invoke(converter.InvokingInstance, new object[] { valueText }); } if (erg != null) { this.CurrentParameter.SpeechParameterInfo.Value = erg; if (this.State == ExecutingState.ListeningForParameters) //only add if the parameter name was left out { this.CurrentSpeechStream.SpeechParameterStreams.Add(this.CurrentParameter); } this._OnParameterFinished.OnNext(this.CurrentParameter); } else { return(false); } //now check if this was the last parameter... if (this.CurrentSpeechStream.SpeechParameterStreams.Count == this.CurrentSpeechStream.SpeechTuple.Method.Arguments.Count) { //we have all parameters... if (this.CurrentSpeechStream.SpeechTuple.Method.MethodInfo.IsStatic == false) { if (this.CurrentSpeechStream.SpeechTuple.InvokeInstance == null) { var obj = Activator.CreateInstance(this.CurrentSpeechStream.SpeechTuple.Method.ExecutingType); this.CurrentSpeechStream.SpeechTuple.InvokeInstance = obj; } } //reorder parameters to fit signature var values = new List <object>(); foreach (var speechParameter in this.CurrentSpeechStream.SpeechTuple.Method.Arguments) { SpeechParameterStream speechParameterInfo = this.CurrentSpeechStream.SpeechParameterStreams.FirstOrDefault( p => p.SpeechParameterInfo.Parameter.ParameterInfo.Name == speechParameter.ParameterInfo.Name); if (speechParameterInfo != null) { values.Add(speechParameterInfo.SpeechParameterInfo.Value); } else { //TODO parameter has no value or parameter info ?? } } this._OnLastParameterFinished.OnNext(this.CurrentSpeechStream); //finally execute the method with the right parameter order ExecuteMethod(this.CurrentSpeechStream.SpeechTuple, this.CurrentSpeechStream.SpeechTuple.InvokeInstance, values.ToArray()); this.AbortListeningForParameters(); this.State = ExecutingState.ListeningForMethods; } else { this.State = ExecutingState.ListeningForParameters; } return(true); }