Пример #1
0
        /// <summary>
        /// Get all commands that match this command.
        /// </summary>
        /// <param name="command"></param>
        /// <param name="validateStructure"></param>
        /// <param name="validateParam"></param>
        /// <param name="foundCommands"></param>
        public void TryGetCommands(List <string> command, bool validateStructure, bool validateParam, ref List <YCommand> foundCommands)
        {
            for (int i = 0; i < commands.Count; i++)
            {
                YCommand cmd = commands[i];

                if (ValidateCommand(command, validateParam, cmd) > -1 || !validateStructure)
                {
                    foundCommands.Add(cmd);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Try get the cosest match to the command input.
        /// </summary>
        /// <param name="command"></param>
        /// <param name="yCommand"></param>
        /// <returns></returns>
        public int TryGetCommand(List <string> command, out YCommand yCommand)
        {
            int      pLength     = command.Count - StructureLength;
            int      best        = -1;
            YCommand bestCommand = null;

            for (int i = 0; i < commands.Count; i++)
            {
                YCommand cmd   = commands[i];
                int      match = ValidateCommand(command, true, cmd);

                if (match > best)
                {
                    bestCommand = cmd;
                    best        = match;
                }
            }

            yCommand = bestCommand;
            return(best);
        }
Пример #3
0
        /// <summary>
        /// Validate if a YCommand matches a command structure and parameters.
        /// </summary>
        /// <param name="command">Command structure.</param>
        /// <param name="validateParam">If parameter shall be validated too.</param>
        /// <param name="yCommand"></param>
        /// <returns></returns>
        int ValidateCommand(List <string> command, bool validateParam, YCommand yCommand)
        {
            if (!validateParam && StructureLength < command.Count)
            {
                return(-1);
            }

            for (int i = 0; i < StructureLength; i++)
            {
                if (i >= command.Count)
                {
                    return(-1);
                }

                if (command[i] != yCommand.Structure[i])
                {
                    return(-1);
                }
            }

            if (!validateParam)
            {
                return(StructureLength * 2);
            }

            int paramMatch = ValidateParams(command, yCommand);

            if (paramMatch > -1)
            {
                return((StructureLength * 2) + paramMatch);
            }
            else
            {
                return(-1);
            }
        }
Пример #4
0
        /// <summary>
        /// Check if parameters is matching the YCommand.
        /// </summary>
        /// <param name="command"></param>
        /// <param name="yCommand"></param>
        /// <returns></returns>
        int ValidateParams(List <string> command, YCommand yCommand)
        {
            for (int i = 0; i < yCommand.Parameters.Count; i++)
            {
                YParameter parameter    = yCommand.Parameters[i];
                Type       expectedType = parameter.Type;

                if (i + StructureLength >= command.Count)
                {
                    // Check the last params parameters here
                    if (parameter.IsParam)
                    {
                        return(yCommand.Parameters.Count);
                    }

                    if (parameter.IsOptional)
                    {
                        continue;
                    }

                    return(-1);
                }

                if (parameter.IsParam)
                {
                    return(yCommand.Parameters.Count);
                }

                if (!IsOfType(command[StructureLength + i], expectedType))
                {
                    return(-1);
                }
            }

            return(yCommand.Parameters.Count);
        }
Пример #5
0
 public void Add(YCommand command)
 {
     commands.Add(command);
 }