/// <summary>
        /// Verifies if a comparison is true or false.
        /// </summary>
        /// <param name="left">The left-hand term in the comparison</param>
        /// <param name="condition">The condition of the comparison</param>
        /// <param name="right">The right-hand term in the comparison</param>
        /// <param name="data">The BotData used for variable replacement</param>
        /// <returns>Whether the comparison is true or false</returns>
        public static bool Verify(string left, Condition condition, string right, BotData data)
        {
            var style    = NumberStyles.Number | NumberStyles.AllowCurrencySymbol; // Needed when comparing values with a currency symbol
            var provider = new CultureInfo("en-US");
            var L        = BlockBase.ReplaceValuesRecursive(left, data);           // The left-hand term can accept recursive values like <LIST[*]>
            var r        = BlockBase.ReplaceValues(right, data);                   // The right-hand term cannot

            switch (condition)
            {
            case Condition.EqualTo:
                return(L.Any(l => l == r));

            case Condition.NotEqualTo:
                return(L.Any(l => l != r));

            case Condition.GreaterThan:
                return(L.Any(l => decimal.Parse(l.Replace(',', '.'), style, provider) > decimal.Parse(r, style, provider)));

            case Condition.LessThan:
                return(L.Any(l => decimal.Parse(l.Replace(',', '.'), style, provider) < decimal.Parse(r, style, provider)));

            case Condition.Contains:
                return(L.Any(l => l.Contains(r)));

            case Condition.DoesNotContain:
                return(L.Any(l => !l.Contains(r)));

            case Condition.Exists:
                return(L.Any(l => l != left));    // Returns true if any replacement took place

            case Condition.DoesNotExist:
                return(L.All(l => l == left));    // Returns true if no replacement took place

            case Condition.MatchesRegex:
                return(L.Any(l => Regex.Match(l, r).Success));

            case Condition.DoesNotMatchRegex:
                return(L.Any(l => !Regex.Match(l, r).Success));

            default:
                return(false);
            }
        }
Пример #2
0
 /// <summary>
 /// Serializes a block to a JSON string.
 /// </summary>
 /// <param name="block">The block to serialize</param>
 /// <returns>The JSON-encoded BlockBase object with TypeNameHandling on</returns>
 public static string SerializeBlock(BlockBase block)
 {
     return(JsonConvert.SerializeObject(block, Formatting.None, settings));
 }
Пример #3
0
 /// <summary>
 /// Clones a BlockBase object by serializing and deserializing it.
 /// </summary>
 /// <param name="block">The object to clone</param>
 /// <returns>The cloned BlockBase object</returns>
 public static BlockBase CloneBlock(BlockBase block)
 {
     return(DeserializeBlock(SerializeBlock(block)));
 }