Ejemplo n.º 1
0
        public static Action LoadXml(XElement X)   // TODO add new actions
        {
            switch (X.Name.LocalName)
            {
            case "Assign":
                return(Assign.Parse(X));

            case "Clear":
                return(Clear.Parse(X));

            case "Say":
                return(Say.Parse(X));

            case "Play":
                return(Play.Parse(X, 100));

            case "ShutUp":
                return(ShutUp.Parse(X));

            case "Extension":
                return(Extension.Parse(X));

            case "OneOf":
                return(new OneOf(from z in X.Elements() select Action.LoadXml(z)));

            case "GPIO":
                return(GPIO.Parse(X));

            default:
                throw new RuleEngineException("Unsupported action type");
            }
        }
Ejemplo n.º 2
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);
        }