Beispiel #1
0
        private static Action ParseAtomicAction(string actionSequence)
        {
            string ASSIGNEMENT_STRING_REGEX = $"^(?<var>{BracketedConfigProcessor.VARNAME_REGEX_PATTERN})\\s*=\\s*\".*\"$";
            string ASSIGNEMENT_REGEX        = $"^(?<var>{BracketedConfigProcessor.VARNAME_REGEX_PATTERN})\\s*=\\s*(?<value>\\S+)$";
            string CLEAR_REGEX    = $"^clear\\s+\\$(?<var>{BracketedConfigProcessor.VARNAME_REGEX_PATTERN})$";
            string SAY_REGEX      = $"^say\\s+((?<probability>\\d*)\\s+)?\".*\"$";
            string SAY_FAST_REGEX = $"^sayFast\\s+((?<probability>\\d*)\\s+)?\".*\"$";
            string SHUT_UP_REGEX  = $"^shutUp$";
            string GPIO_REGEX     = $"^GPIO\\s+((?<probability>\\d*)\\s+)?(?<signal>([10],)*[10])\\s+(?<time>\\d+)$";
            string EXTERNAL_ACTION_NAME_REGEX_PATTERN = BracketedConfigProcessor.VARNAME_REGEX_PATTERN;
            string EXTERNAL_REGEX        = $"^ext:(?<method>{EXTERNAL_ACTION_NAME_REGEX_PATTERN})\\s+\".*\"$";
            string PLAY_DELAY_REGEX      = $"^play\\s+((?<probability>\\d*)\\s+)?\".*\"(\\s+(?<time>\\d+))?$";
            string STAY_ACTIVE_REGEX     = $"^stayActive$";
            string COMPARE_ANSWERS_REGEX = $"^compareAnswers\\s+(?<goodAnswer>{BracketedConfigProcessor.VARNAME_REGEX_PATTERN})\\s+(?<realAnswer>{BracketedConfigProcessor.VARNAME_REGEX_PATTERN})$";
            string QUIZ_REGEX            = $"^\\s*quiz\\s+(\\\"(?<filename>.*)\\\"\\s*)((?<randomOrder>randomOrder)\\s*)?((?<length>\\d+\\:\\d+))?\\s*$";
            Action action = null;

            string prettyActionSequence = actionSequence.Trim();
            int    probability;

            try {
                if (Regex.IsMatch(prettyActionSequence, ASSIGNEMENT_STRING_REGEX))
                {
                    int    firstQuotePosition = prettyActionSequence.IndexOf("\"");
                    int    lastQuotePosition  = prettyActionSequence.LastIndexOf("\"");
                    int    start          = firstQuotePosition + 1;
                    int    len            = lastQuotePosition - start;
                    string possibleString = prettyActionSequence.Substring(start, len);
                    Match  m = Regex.Match(prettyActionSequence, ASSIGNEMENT_STRING_REGEX);
                    if (BracketedConfigProcessor.AssertValidString(possibleString))
                    {
                        if (m.Length != 0)
                        {
                            action = new Assign(m.Groups["var"].Value, possibleString);
                        }
                    }
                }
                else if (Regex.IsMatch(prettyActionSequence, ASSIGNEMENT_REGEX))
                {
                    Match m = Regex.Match(prettyActionSequence, ASSIGNEMENT_REGEX);
                    if (m.Length != 0)
                    {
                        action = new Assign(m.Groups["var"].Value, m.Groups["value"].Value);
                    }
                }
                else if (Regex.IsMatch(prettyActionSequence, CLEAR_REGEX))
                {
                    Match m = Regex.Match(prettyActionSequence, CLEAR_REGEX);
                    if (m.Length != 0)
                    {
                        action = new Clear(m.Groups["var"].Value);
                    }
                }
                else if (Regex.IsMatch(prettyActionSequence, SAY_REGEX))
                {
                    int    firstQuotePosition = prettyActionSequence.IndexOf("\"");
                    int    lastQuotePosition  = prettyActionSequence.LastIndexOf("\"");
                    int    start          = firstQuotePosition + 1;
                    int    len            = lastQuotePosition - start;
                    string possibleString = prettyActionSequence.Substring(start, len);
                    Match  m = Regex.Match(prettyActionSequence, SAY_REGEX);
                    if (m.Length != 0 && m.Groups["probability"].Value.Length != 0)
                    {
                        probability = Int32.Parse(m.Groups["probability"].Value);
                    }
                    else
                    {
                        probability = 100;
                    }
                    if (BracketedConfigProcessor.AssertValidString(possibleString))
                    {
                        action = new Say(possibleString, probability);
                    }
                }
                else if (Regex.IsMatch(prettyActionSequence, SAY_FAST_REGEX))
                {
                    int    firstQuotePosition = prettyActionSequence.IndexOf("\"");
                    int    lastQuotePosition  = prettyActionSequence.LastIndexOf("\"");
                    int    start          = firstQuotePosition + 1;
                    int    len            = lastQuotePosition - start;
                    string possibleString = prettyActionSequence.Substring(start, len);
                    Match  m = Regex.Match(prettyActionSequence, SAY_REGEX);
                    if (m.Length != 0 && m.Groups["probability"].Value.Length != 0)
                    {
                        probability = Int32.Parse(m.Groups["probability"].Value);
                    }
                    else
                    {
                        probability = 100;
                    }
                    if (BracketedConfigProcessor.AssertValidString(possibleString))
                    {
                        action = new SayFast(possibleString, probability);
                    }
                }
                else if (Regex.IsMatch(prettyActionSequence, SHUT_UP_REGEX))
                {
                    action = new ShutUp();
                }
                else if (Regex.IsMatch(prettyActionSequence, STAY_ACTIVE_REGEX))
                {
                    action = new StayActive();
                }
                else if (Regex.IsMatch(prettyActionSequence, COMPARE_ANSWERS_REGEX))
                {
                    Match m = Regex.Match(prettyActionSequence, COMPARE_ANSWERS_REGEX);
                    if (m.Length != 0)
                    {
                        action = new CompareAnswers(m.Groups["goodAnswer"].Value, m.Groups["realAnswer"].Value);
                    }
                }
                else if (Regex.IsMatch(prettyActionSequence, PLAY_DELAY_REGEX))
                {
                    int    firstQuotePosition = prettyActionSequence.IndexOf("\"");
                    int    lastQuotePosition  = prettyActionSequence.LastIndexOf("\"");
                    int    start          = firstQuotePosition + 1;
                    int    len            = lastQuotePosition - start;
                    string possibleString = prettyActionSequence.Substring(start, len);
                    Match  m = Regex.Match(prettyActionSequence, PLAY_DELAY_REGEX);
                    if (m.Length != 0 && m.Groups["probability"].Value.Length != 0)
                    {
                        probability = Int32.Parse(m.Groups["probability"].Value);
                    }
                    else
                    {
                        probability = 100;
                    }
                    Match m2 = Regex.Match(prettyActionSequence, PLAY_DELAY_REGEX);
                    if (m2.Length != 0 && m2.Groups["time"].Value.Length != 0)
                    {
                        var time = Int32.Parse(m.Groups["time"].Value);
                        if (BracketedConfigProcessor.AssertValidString(possibleString))
                        {
                            action = new Play(possibleString, probability, time);
                        }
                    }
                    else
                    {
                        if (BracketedConfigProcessor.AssertValidString(possibleString))
                        {
                            action = new Play(possibleString, probability);
                        }
                    }
                }
                else if (Regex.IsMatch(prettyActionSequence, QUIZ_REGEX))
                {
                    int firstQuotePosition = prettyActionSequence.IndexOf("\"");
                    int lastQuotePosition  = prettyActionSequence.LastIndexOf("\"");
                    int start = firstQuotePosition + 1;
                    int len   = lastQuotePosition - start;

                    string possibleString = prettyActionSequence.Substring(start, len);
                    if (BracketedConfigProcessor.AssertValidString(possibleString))
                    {
                        action = new Quiz(possibleString);

                        Match m = Regex.Match(prettyActionSequence, QUIZ_REGEX);

                        ((Quiz)action).randomOrdered = m.Length != 0 && m.Groups["randomOrder"].Value.Length != 0;

                        //                    Match m = Regex.Match(prettyActionSequence, QUIZ_REGEX);
                        if (m.Length != 0 && m.Groups["length"].Value.Length != 0)
                        {
                            var lengths = m.Groups["length"].Value.Split(':');
                            ((Quiz)action).lengthLowerBound = int.Parse(lengths[0]);
                            ((Quiz)action).lengthUpperBound = int.Parse(lengths[1]);
                        }
                    }
                }
                else if (Regex.IsMatch(prettyActionSequence, GPIO_REGEX))
                {
                    Match m = Regex.Match(prettyActionSequence, GPIO_REGEX);
                    if (m.Length != 0 && m.Groups["probability"].Value.Length != 0)
                    {
                        probability = Int32.Parse(m.Groups["probability"].Value);
                    }
                    else
                    {
                        probability = 100;
                    }
                    if (m.Length != 0)
                    {
                        action = new GPIO(m.Groups["signal"].Value.Split(',', ' ')
                                          .Select(Int32.Parse).ToList(), Int32.Parse(m.Groups["time"].Value),
                                          probability);
                    }
                }
                else if (Regex.IsMatch(prettyActionSequence, EXTERNAL_REGEX))
                {
                    int    firstQuotePosition = prettyActionSequence.IndexOf("\"");
                    int    lastQuotePosition  = prettyActionSequence.LastIndexOf("\"");
                    int    start          = firstQuotePosition + 1;
                    int    len            = lastQuotePosition - start;
                    string possibleString = prettyActionSequence.Substring(start, len);
                    if (BracketedConfigProcessor.AssertValidString(possibleString))
                    {
                        Match m = Regex.Match(prettyActionSequence, EXTERNAL_REGEX);
                        if (m.Length != 0)
                        {
                            action = new Extension(m.Groups["method"].Value, possibleString);
                        }
                    }
                }
            }
            catch {
                action = null;
            }

            return(action);
        }
Beispiel #2
0
        private static Action ParseAtomicAction(string actionSequence)
        {
            string ASSIGNEMENT_STRING_REGEX = $"^(?<var>{BracketedConfigProcessor.VARNAME_REGEX_PATTERN})\\s*=\\s*\\\"(?<value>\\S+)\\\"$";
            string ASSIGNEMENT_REGEX        =
                $"^(?<var>{BracketedConfigProcessor.VARNAME_REGEX_PATTERN})\\s*=\\s*(?<value>\\S+)$";
            string CLEAR_REGEX = $"^clear\\s+\\$(?<var>{BracketedConfigProcessor.VARNAME_REGEX_PATTERN})$";
            string SAY_REGEX   = $"^say\\s+\".*\"$";
            string GPIO_REGEX  = $"^GPIO\\s+(?<signal>([10],)*[10])\\s+(?<time>\\d+)$";
            string EXTERNAL_ACTION_NAME_REGEX_PATTERN = BracketedConfigProcessor.VARNAME_REGEX_PATTERN;
            string EXTERNAL_REGEX =
                $"^ext:(?<method>{EXTERNAL_ACTION_NAME_REGEX_PATTERN})\\s+\".*\"$";
            Action action = null;

            string prettyActionSequence = actionSequence.Trim();

            try {
                if (Regex.IsMatch(prettyActionSequence, ASSIGNEMENT_STRING_REGEX))
                {
                    Match m = Regex.Match(prettyActionSequence, ASSIGNEMENT_STRING_REGEX);
                    if (m.Length == 0)
                    {
                        throw new ActionParseException();
                    }

                    action = new Assign(m.Groups["var"].Value, m.Groups["value"].Value);
                    //Console.WriteLine($"parsed {actionSequence} as assignement {m.Groups["var"].Value}:={m.Groups["value"].Value}");
                }
                else if (Regex.IsMatch(prettyActionSequence, ASSIGNEMENT_REGEX))
                {
                    Match m = Regex.Match(prettyActionSequence, ASSIGNEMENT_REGEX);
                    if (m.Length == 0)
                    {
                        throw new ActionParseException();
                    }

                    action = new Assign(m.Groups["var"].Value, m.Groups["value"].Value);
                    //Console.WriteLine($"parsed {actionSequence} as assignement {m.Groups["var"].Value}:={m.Groups["value"].Value}");
                }
                else if (Regex.IsMatch(prettyActionSequence, CLEAR_REGEX))
                {
                    Match m = Regex.Match(prettyActionSequence, CLEAR_REGEX);
                    if (m.Length == 0)
                    {
                        throw new ActionParseException();
                    }

                    action = new Clear(m.Groups["var"].Value);
                    //Console.WriteLine($"parsed {actionSequence} as clear {m.Groups["var"].Value}");
                }
                else if (Regex.IsMatch(prettyActionSequence, SAY_REGEX))
                {
                    int    firstQuotePosition = prettyActionSequence.IndexOf("\"");
                    int    lastQuotePosition  = prettyActionSequence.LastIndexOf("\"");
                    int    start          = firstQuotePosition + 1;
                    int    len            = lastQuotePosition - start;
                    string possibleString = prettyActionSequence.Substring(start, len);
                    BracketedConfigProcessor.AssertValidString(possibleString);

                    action = new Say(possibleString);
                    //Console.WriteLine($"parsed {actionSequence} as say {possibleString}");
                }
                else if (Regex.IsMatch(prettyActionSequence, GPIO_REGEX))
                {
                    Match m = Regex.Match(prettyActionSequence, GPIO_REGEX);
                    if (m.Length == 0)
                    {
                        throw new ActionParseException();
                    }
                    action = new GPIO(m.Groups["signal"].Value.Split(',', ' ').Select(Int32.Parse).ToList(),
                                      Int32.Parse(m.Groups["time"].Value));
                }
                else if (Regex.IsMatch(prettyActionSequence, EXTERNAL_REGEX))
                {
                    int    firstQuotePosition = prettyActionSequence.IndexOf("\"");
                    int    lastQuotePosition  = prettyActionSequence.LastIndexOf("\"");
                    int    start          = firstQuotePosition + 1;
                    int    len            = lastQuotePosition - start;
                    string possibleString = prettyActionSequence.Substring(start, len);
                    BracketedConfigProcessor.AssertValidString(possibleString);

                    Match m = Regex.Match(prettyActionSequence, EXTERNAL_REGEX);
                    if (m.Length == 0)
                    {
                        throw new ActionParseException();
                    }

                    action = new Extension(m.Groups["method"].Value, possibleString);
                    //Console.WriteLine($"parsed {actionSequence} as external {m.Groups["method"].Value} {possibleString}");
                }
                else
                {
                    throw new ActionParseException();
                }

                if (action == null)
                {
                    throw new ActionParseException();
                }

                return(action);
            }
            catch {
                throw new ActionParseException();
            }
        }