Esempio n. 1
0
        public Action Parse(string command, BackgroundTask task)
        {
            foreach (var c in grammar)
            {
                var parameterRegex = new Regex(@"\{([\w]+)\}");
                var commandPattern = "^" + parameterRegex.Replace(c.Pattern, @"([\w\d': ]+)") + "$";
                var regex          = new Regex(commandPattern, RegexOptions.IgnoreCase);
                var match          = regex.Match(command);
                if (match.Success)
                {
                    var placeholders = GetPlaceholders(c.Pattern);
                    var dictionary   = new Dictionary <string, string>();
                    for (int m = 0; m < match.Groups.Count - 1; m++)
                    {
                        dictionary.Add(placeholders[m], match.Groups[m + 1].Value);
                    }
                    return(() => c.Action(dictionary, task));
                }
            }

            return(null);
        }
Esempio n. 2
0
        public void Execute(string command, bool nak = false, bool echo = false)
        {
            var fragments = command.Split(new string[] { " then " }, StringSplitOptions.RemoveEmptyEntries);

            if (fragments.Any(x => IsWaitFor(x)))
            {
                var taskID = context.Tasks.Count + 1;
                var source = new CancellationTokenSource();
                var token  = source.Token;
                var task   = new BackgroundTask
                {
                    TaskID             = taskID,
                    Command            = command,
                    CanncelationSource = source,
                    CancellationToken  = token
                };
                context.Tasks.Add(task);

                Task.Run(() =>
                {
                    for (int f = 0; f < fragments.Length; f++)
                    {
                        if (!token.IsCancellationRequested)
                        {
                            var fragment = fragments[f];
                            if (fragment == "repeat")
                            {
                                f        = 0;
                                fragment = fragments[f];
                            }

                            var ok = ExecuteFragment(fragment, task, echo: echo);
                            if (ok && !IsWaitFor(fragment))
                            {
                                Console.WriteLine($"Task {task.TaskID} executed \"{fragment}\"");
                            }
                            if (!ok)
                            {
                                Console.WriteLine($"Task {task.TaskID} contains a command I don't understand: \"{fragment}\"");
                            }
                        }
                    }
                    task.Cancelled = token.IsCancellationRequested;
                    task.Completed = !token.IsCancellationRequested;
                    if (!token.IsCancellationRequested)
                    {
                        Console.WriteLine($"Task {taskID} has completed");
                    }
                }, token);

                if (!nak)
                {
                    Console.WriteLine($"Task {taskID} started");
                }
            }
            else
            {
                var oks = new List <bool>();
                foreach (var fragment in fragments)
                {
                    var ok = ExecuteFragment(fragment, echo: echo);
                    if (!ok)
                    {
                        Console.WriteLine($"Sorry, I don't understand \"{fragment}\"");
                    }
                    oks.Add(ok);
                }
                if (!nak && oks.All(x => x))
                {
                    Console.WriteLine("OK");
                }
            }
        }