/// <summary> /// Converts the string representation of a command to a Command object. /// A return value indicates whether the conversion succeded or not. /// </summary> /// <param name="s">A string containing the command to convert</param> /// <param name="blackboard">A blackboard which contains all information about modules</param> /// <param name="cmd">When this method returns, contains the Command equivalent to the command /// contained in s, if the conversion succeeded, or null if the conversion failed. /// The conversion fails if the s parameter is a null reference (Nothing in Visual Basic) or is not of the correct format. /// This parameter is passed uninitialized</param> /// <param name="ex">When this method returns, contains the Exception generated during the parse operation, /// if the conversion failed, or null if the conversion succeeded. The conversion fails if the s parameter /// is a null reference (Nothing in Visual Basic) or is not of the correct format. /// This parameter is passed uninitialized</param> /// <returns>true if conversion was successfull, false otherwise</returns> protected static bool TryParse(string s, Blackboard blackboard, out Command cmd, out Exception ex) { IPrototype proto; //Module source; //Module destination; IModuleClient source; IModuleClient destination; string sCommand; string sParams; string sSrc; string sDest; int result; int id; cmd = null; ex = null; // Extract Data CommandBase.XtractCommandElements(s, out sSrc, out sDest, out sCommand, out sParams, out result, out id); if ((sCommand == null) || (result != -1)) { ex = new ArgumentException("Invalid String", "s"); return false; } // Check if source module is specified if ((sSrc == null) || (sSrc.Length < 1) || !blackboard.Modules.Contains(sSrc)) { ex = new Exception("No source module found for command provided"); return false; } source = blackboard.Modules[sSrc]; // Browse for destination module and prototype if (!blackboard.FindDestinationModule(sCommand, out destination, out proto)) //throw new Exception("No destination module found for command provided"); { // No destination module found. Must check if destination is availiable if ((sDest == null) || (sDest.Length < 1) || !blackboard.Modules.Contains(sDest)) { ex = new Exception("No destination module found for command provided"); return false; } destination = blackboard.Modules[sDest]; proto = null; } // Check if command matchs a prototype if ((proto != null) && proto.ParamsRequired && ((sParams == null) || (sParams.Length < 1))) { ex = new Exception("Invalid string. The Command requires parameters"); return false; } // Create the Command cmd = new Command(source, destination, sCommand, sParams, id); cmd.prototype = proto; cmd.sentTime = DateTime.Now; return true; }
/// <summary> /// Converts the string representation of a response to a Response object. /// A return value indicates whether the conversion succeded or not. /// <param name="s">A string containing the response to convert</param> /// <param name="blackboard">A blackboard which contains all information about modules</param> /// <param name="response">When this method returns, contains the Response equivalent to the response /// contained in s, if the conversion succeeded, or null if the conversion failed. /// The conversion fails if the s parameter is a null reference (Nothing in Visual Basic) or is not of the correct format. /// This parameter is passed uninitialized</param> /// <param name="ex">When this method returns, contains the Exception generated during the parse operation, /// if the conversion failed, or null if the conversion succeeded. The conversion fails if the s parameter /// is a null reference (Nothing in Visual Basic) or is not of the correct format. /// This parameter is passed uninitialized</param> /// </summary> /// <returns>true if conversion was successfull, false otherwise</returns> protected static bool TryParse(string s, Blackboard blackboard, out Response response, out Exception ex) { IPrototype proto; IModuleClient source; IModuleClient destination; string sCommand; string sParams; string sSrc; string sDest; bool result; int iResult; int id; ex = null; response = null; // Extract Data CommandBase.XtractCommandElements(s, out sSrc, out sDest, out sCommand, out sParams, out iResult, out id); if ((sCommand == null) || (iResult == -1)) { ex = new ArgumentException("Invalid String", "s"); return false; } result = iResult == 1; // Check if source module is specified if ((sSrc == null) || (sSrc.Length < 1) || !blackboard.Modules.Contains(sSrc)) { ex = new Exception("No source module found for response provided"); return false; } source = blackboard.Modules[sSrc]; // Browse for destination module and prototype if (!blackboard.FindDestinationModule(sCommand, out source, out proto)) //Since the response arrived from a module which can't generate it a exception is rised { ex = new Exception("Sender module and generator module does not match"); return false; } // if destination module is specified, then it mus be set if ((sDest != null) && (sDest.Length > 0) && source.Parent.Modules.Contains(sDest)) destination = source.Parent.Modules[sDest]; // Else, since there is no way at this point to determine which is destination module for response // and this is no specified, it is set to null, to allow blackboard to find an apropiate one else destination = null; // Check if response matchs a prototype if ((proto != null) && proto.ParamsRequired && ((sParams == null) || (sParams.Length < 1))) { ex = new Exception("Invalid string. The Response requires parameters"); return false; } // Create the Response response = new Response(source, destination, sCommand, sParams, result, id); response.prototype = proto; if (!response.success) response.failReason = ResponseFailReason.ExecutedButNotSucceded; return true; }
/// <summary> /// Converts the string representation of a command to a Command object. /// A return value indicates whether the conversion succeded or not. /// </summary> /// <param name="s">A string containing the command to convert</param> /// <param name="blackboard">A blackboard which contains all information about modules</param> /// <param name="cmd">When this method returns, contains the Command equivalent to the command /// contained in s, if the conversion succeeded, or null if the conversion failed. /// The conversion fails if the s parameter is a null reference (Nothing in Visual Basic) or is not of the correct format. /// This parameter is passed uninitialized</param> /// <param name="ex">When this method returns, contains the Exception generated during the parse operation, /// if the conversion failed, or null if the conversion succeeded. The conversion fails if the s parameter /// is a null reference (Nothing in Visual Basic) or is not of the correct format. /// This parameter is passed uninitialized</param> /// <returns>true if conversion was successfull, false otherwise</returns> protected static bool TryParse(string s, Blackboard blackboard, out Command cmd, out Exception ex) { Regex rx; Match m; Prototype proto; //Module source; //Module destination; IModule source; IModule destination; string sCommand; string sParams; string sSrc; string sDest; string sId; int id; cmd = null; ex = null; // Regular Expresion Generation //rx = new Regex(@"((?<src>[\w\-]+)(\s+(?<dest>[\w\-]+))?\s+)?(?<cmd>[A-Za-z_]+)\s+(""(?<params>[^""]*)"")?\s+(@(?<id>\d+))?"); rx = rxCommandFromBlackboard; m = rx.Match(s); // Check if input string matchs Regular Expression if (!m.Success) { ex = new ArgumentException("Invalid String", "s"); return false; } // Extract Data sCommand = m.Result("${cmd}").ToLower(); sParams = m.Result("${params}"); sId = m.Result("${id}"); sSrc = m.Result("${src}"); sDest = m.Result("${dest}"); if ((sId == null) || (sId.Length < 1)) id = -1; else id = Int32.Parse(sId); // Check if source module is specified if ((sSrc == null) || (sSrc.Length < 1) || !blackboard.Modules.Contains(sSrc)) { ex = new Exception("No source module found for command provided"); return false; } source = blackboard.Modules[sSrc]; // Browse for destination module and prototype if (!blackboard.FindDestinationModule(sCommand, out destination, out proto)) //throw new Exception("No destination module found for command provided"); { // No destination module found. Must check if destination is availiable if ((sDest == null) || (sDest.Length < 1) || !blackboard.Modules.Contains(sDest)) { ex = new Exception("No destination module found for command provided"); return false; } destination = blackboard.Modules[sDest]; proto = null; } // Check if command matchs a prototype if ((proto != null) && proto.ParamsRequired && ((sParams == null) || (sParams.Length < 1))) { ex = new Exception("Invalid string. The Command requires parameters"); return false; } // Create the Command cmd = new Command(source, destination, sCommand, sParams, id); cmd.prototype = proto; cmd.sentTime = DateTime.Now; return true; }
/// <summary> /// Converts the string representation of a response to a Response object. /// A return value indicates whether the conversion succeded or not. /// <param name="s">A string containing the response to convert</param> /// <param name="blackboard">A blackboard which contains all information about modules</param> /// <param name="response">When this method returns, contains the Response equivalent to the response /// contained in s, if the conversion succeeded, or null if the conversion failed. /// The conversion fails if the s parameter is a null reference (Nothing in Visual Basic) or is not of the correct format. /// This parameter is passed uninitialized</param> /// <param name="ex">When this method returns, contains the Exception generated during the parse operation, /// if the conversion failed, or null if the conversion succeeded. The conversion fails if the s parameter /// is a null reference (Nothing in Visual Basic) or is not of the correct format. /// This parameter is passed uninitialized</param> /// </summary> /// <returns>true if conversion was successfull, false otherwise</returns> protected static bool TryParse(string s, Blackboard blackboard, out Response response, out Exception ex) { Regex rx; Match m; Prototype proto; //Module source; IModule source; //Module destination; IModule destination; string sCommand; string sParams; string sSrc; string sDest; string sId; string sResult; bool result; int id; ex = null; response = null; // Regular Expresion Generation //rx = new Regex(@"((?<src>[\w\-]+)(\s+(?<dest>[\w\-]+))?\s+)?(?<cmd>[A-Za-z_]+)\s+(""(?<params>[^""]*)"")?\s+(@(?<id>\d+))?"); rx = rxResponseFromBlackboard; m = rx.Match(s); // Check if input string matchs Regular Expression if (!m.Success) { ex = new ArgumentException("Invalid String", "s"); return false; } // Extract Data sCommand = m.Result("${cmd}").ToLower(); sParams = m.Result("${params}"); sId = m.Result("${id}"); sSrc = m.Result("${src}"); sDest = m.Result("${dest}"); sResult = m.Result("${result}"); if ((sResult == null) || ((sResult != "1") && (sResult != "0"))) { ex = new Exception("Invalid string. No suitable result value found"); return false; } result = (sResult == "1"); if ((sId == null) || (sId.Length < 1)) id = -1; else id = Int32.Parse(sId); // Check if source module is specified if ((sSrc == null) || (sSrc.Length < 1) || !blackboard.Modules.Contains(sSrc)) { ex = new Exception("No source module found for response provided"); return false; } source = blackboard.Modules[sSrc]; // Browse for destination module and prototype if (!blackboard.FindDestinationModule(sCommand, out source, out proto)) //Since the response arrived from a module which can't generate it a exception is rised { ex = new Exception("Sender module and generator module does not match"); return false; } // if destination module is specified, then it mus be set if ((sDest != null) && (sDest.Length > 0) && source.Parent.Modules.Contains(sDest)) destination = source.Parent.Modules[sDest]; // Else, since there is no way at this point to determine which is destination module for response // and this is no specified, it is set to null, to allow blackboard to find an apropiate one else destination = null; // Check if response matchs a prototype if ((proto != null) && proto.ParamsRequired && ((sParams == null) || (sParams.Length < 1))) { ex = new Exception("Invalid string. The Response requires parameters"); return false; } // Create the Response response = new Response(source, destination, sCommand, sParams, result, id); response.prototype = proto; if (!response.success) response.failReason = ResponseFailReason.ExecutedButNotSucceded; return true; }