private static string ComparisonToString(ConditionComparison c)
        {
            switch (c)
            {
            case ConditionComparison.Equal:
                return("==");

            case ConditionComparison.Greater:
                return(">");

            case ConditionComparison.GreaterOrEqual:
                return(">=");

            case ConditionComparison.Less:
                return("<");

            case ConditionComparison.LessOrEqual:
                return("<=");

            case ConditionComparison.NotEqual:
                return("!=");

            default:
                return("?");
            }
        }
 public ConditionTreeLeaf(ConditionType type, string name, ConditionComparison comparison, PropertyUnion compareTo)
 {
     Type         = type;
     VariableName = name;
     Comparison   = comparison;
     CompareTo    = compareTo;
 }
示例#3
0
        public static ComparisonType GetComparisonType(this ConditionComparison conditionComparison)
        {
            switch (conditionComparison)
            {
            case ConditionComparison.Exists:
            case ConditionComparison.NotExists:
                return(ComparisonType.Unary);

            case ConditionComparison.Equals:
            case ConditionComparison.NotEquals:
            case ConditionComparison.Contains:
            case ConditionComparison.StartsWith:
            case ConditionComparison.EndsWith:
            case ConditionComparison.GreaterThan:
            case ConditionComparison.LessThan:
            case ConditionComparison.GreaterThanOrEquals:
            case ConditionComparison.LessThanOrEquals:
            case ConditionComparison.Matches:
            case ConditionComparison.ApproximateTo:
                return(ComparisonType.Binary);

            default:
                throw new ArgumentOutOfRangeException(nameof(conditionComparison));
            }
        }
示例#4
0
        public static Func <string, bool> ToUnaryDelegate(this ConditionComparison conditionComparison)
        {
            switch (conditionComparison)
            {
            case ConditionComparison.Exists:
                return((v) => string.IsNullOrEmpty(v) == false);

            case ConditionComparison.NotExists:
                return((v) => string.IsNullOrEmpty(v));

            case ConditionComparison.Equals:
            case ConditionComparison.NotEquals:
            case ConditionComparison.Contains:
            case ConditionComparison.StartsWith:
            case ConditionComparison.EndsWith:
            case ConditionComparison.Matches:
            case ConditionComparison.ApproximateTo:
            case ConditionComparison.GreaterThan:
            case ConditionComparison.LessThan:
            case ConditionComparison.GreaterThanOrEquals:
            case ConditionComparison.LessThanOrEquals:
                throw new ArgumentException("Not unary comparison condition: ", nameof(conditionComparison));

            default:
                throw new ArgumentOutOfRangeException(nameof(conditionComparison));
            }
        }
 public ConditionTreeLeaf(ConditionType type, string name, ConditionComparison comparison, PropertyUnion compareTo)
 {
     Type = type;
     VariableName = name;
     Comparison = comparison;
     CompareTo = compareTo;
 }
示例#6
0
        public static IConditionTreeItem ReadConditionTree(this System.IO.BinaryReader reader)
        {
            byte type = reader.ReadByte();

            if (type == 0)
            {
                // Empty
                return(ConditionTree.Empty);
            }
            else if (type == 1)
            {
                // Tree
                ConditionTree.ConditionOperator op = (ConditionTree.ConditionOperator)reader.ReadUInt16();
                IConditionTreeItem left            = reader.ReadConditionTree();
                IConditionTreeItem right           = reader.ReadConditionTree();
                return(new ConditionTree(op, left, right));
            }
            else if (type == 2)
            {
                ConditionType       conditionType = (ConditionType)reader.ReadInt32();
                ConditionComparison comparison    = (ConditionComparison)reader.ReadInt32();
                string     variableName           = reader.ReadString();
                BinaryType binType;
                object     compareTo = reader.ReadType(out binType);

                return(new ConditionTreeLeaf(conditionType, variableName, comparison, binType.ToPropertyUnion(compareTo)));
            }
            else
            {
                throw new System.IO.InvalidDataException();
            }
        }
示例#7
0
 private Flow CreateVariableComparisonFlow(ConditionComparison condition, string variableName, string validInputValue, string sentMessageType, string sentMessageContent)
 {
     return(new Flow()
     {
         Id = Guid.NewGuid().ToString(),
         States = new[]
         {
             new State
             {
                 Id = "root",
                 Root = true,
                 Input = new Input
                 {
                     Variable = variableName
                 },
                 Outputs = new Output[]
                 {
                     new Output
                     {
                         Order = 1,
                         Conditions = new Condition[]
                         {
                             new Condition
                             {
                                 Source = ValueSource.Context,
                                 Comparison = condition,
                                 Variable = variableName,
                                 Values = new[] { validInputValue }
                             }
                         },
                         StateId = "success"
                     }
                 }
             },
             new State
             {
                 Id = "success",
                 InputActions = new Action[]
                 {
                     new Action
                     {
                         Type = "SendMessage",
                         Settings = new JObject()
                         {
                             { "type", sentMessageType },
                             { "content", sentMessageContent }
                         }
                     }
                 }
             }
         }
     });
 }
        public Func <string, string, bool> GetBinaryConditionComparator(ConditionComparison conditionComparison)
        {
            switch (conditionComparison)
            {
            case ConditionComparison.Equals:
                return((v1, v2) => string.Compare(v1, v2, CultureInfo.InvariantCulture,
                                                  CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase) == 0);

            case ConditionComparison.NotEquals:
                return((v1, v2) => string.Compare(v1, v2, CultureInfo.InvariantCulture,
                                                  CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase) != 0);

            case ConditionComparison.Contains:
                return((v1, v2) =>
                       v1 != null && v2 != null && v1.Contains(v2, StringComparison.OrdinalIgnoreCase));

            case ConditionComparison.NotContains:
                return((v1, v2) =>
                       v1 != null && v2 != null && !v1.Contains(v2, StringComparison.OrdinalIgnoreCase));

            case ConditionComparison.StartsWith:
                return((v1, v2) =>
                       v1 != null && v2 != null && v1.StartsWith(v2, StringComparison.OrdinalIgnoreCase));

            case ConditionComparison.EndsWith:
                return((v1, v2) => v1 != null && v2 != null && v1.EndsWith(v2, StringComparison.OrdinalIgnoreCase));

            case ConditionComparison.Matches:
                return((v1, v2) => v1 != null && v2 != null && Regex.IsMatch(v1, v2));

            case ConditionComparison.ApproximateTo:
                return((v1, v2) =>
                       v1 != null && v2 != null &&
                       v1.ToLowerInvariant().CalculateLevenshteinDistance(v2.ToLowerInvariant()) <=
                       Math.Ceiling(v1.Length * 0.25));

            case ConditionComparison.GreaterThan:
                return((v1, v2) => decimal.TryParse(v1, out var n1) && decimal.TryParse(v2, out var n2) && n1 > n2);

            case ConditionComparison.LessThan:
                return((v1, v2) => decimal.TryParse(v1, out var n1) && decimal.TryParse(v2, out var n2) && n1 < n2);

            case ConditionComparison.GreaterThanOrEquals:
                return((v1, v2) => decimal.TryParse(v1, out var n1) && decimal.TryParse(v2, out var n2) && n1 >= n2);

            case ConditionComparison.LessThanOrEquals:
                return((v1, v2) => decimal.TryParse(v1, out var n1) && decimal.TryParse(v2, out var n2) && n1 <= n2);

            default:
                throw new ArgumentOutOfRangeException(nameof(conditionComparison));
            }
        }
        public Func <string, bool> GetUnaryConditionComparator(ConditionComparison conditionComparison)
        {
            switch (conditionComparison)
            {
            case ConditionComparison.Exists:
                return(v => !string.IsNullOrEmpty(v));

            case ConditionComparison.NotExists:
                return(v => string.IsNullOrEmpty(v));

            default:
                throw new ArgumentOutOfRangeException(nameof(conditionComparison));
            }
        }
示例#10
0
        public static IConditionTreeItem ReadConditionCollection(this System.IO.BinaryReader reader)
        {
            Stack <ConditionTreeLeaf> andList = new Stack <ConditionTreeLeaf>();
            int numConditions = reader.ReadInt32();

            for (int l = 0; l < numConditions; l++)
            {
                ConditionType       conditionType = (ConditionType)reader.ReadInt32();
                ConditionComparison comparison    = (ConditionComparison)reader.ReadInt32();
                string     variableName           = reader.ReadString();
                BinaryType binType;
                object     compareTo = reader.ReadType(out binType);
                andList.Push(new ConditionTreeLeaf(conditionType, variableName, comparison, binType.ToPropertyUnion(compareTo)));
            }

            return(LegacyConditionParser.AndListToTree(andList));
        }
示例#11
0
        public static Func <string, string, bool> ToBinaryDelegate(this ConditionComparison conditionComparison)
        {
            switch (conditionComparison)
            {
            case ConditionComparison.Equals:
                return((v1, v2) => string.Compare(v1, v2, CultureInfo.InvariantCulture, CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase) == 0);

            case ConditionComparison.NotEquals:
                return((v1, v2) => string.Compare(v1, v2, CultureInfo.InvariantCulture, CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase) != 0);

            case ConditionComparison.Contains:
                return((v1, v2) => v1 != null && v2 != null && v1.IndexOf(v2, StringComparison.OrdinalIgnoreCase) >= 0);

            case ConditionComparison.StartsWith:
                return((v1, v2) => v1 != null && v2 != null && v1.StartsWith(v2, StringComparison.OrdinalIgnoreCase));

            case ConditionComparison.EndsWith:
                return((v1, v2) => v1 != null && v2 != null && v1.EndsWith(v2, StringComparison.OrdinalIgnoreCase));

            case ConditionComparison.Matches:
                return((v1, v2) => v1 != null && v2 != null && Regex.IsMatch(v1, v2));

            case ConditionComparison.ApproximateTo:
                // Allows the difference of 25% of the string.
                return((v1, v2) => v1 != null && v2 != null && v1.ToLowerInvariant().CalculateLevenshteinDistance(v2.ToLowerInvariant()) <= Math.Ceiling(v1.Length * 0.25));

            case ConditionComparison.GreaterThan:
                return((v1, v2) => decimal.TryParse(v1, out var n1) && decimal.TryParse(v2, out var n2) && n1 > n2);

            case ConditionComparison.LessThan:
                return((v1, v2) => decimal.TryParse(v1, out var n1) && decimal.TryParse(v2, out var n2) && n1 < n2);

            case ConditionComparison.GreaterThanOrEquals:
                return((v1, v2) => decimal.TryParse(v1, out var n1) && decimal.TryParse(v2, out var n2) && n1 >= n2);

            case ConditionComparison.LessThanOrEquals:
                return((v1, v2) => decimal.TryParse(v1, out var n1) && decimal.TryParse(v2, out var n2) && n1 <= n2);

            case ConditionComparison.Exists:
            case ConditionComparison.NotExists:
                throw new ArgumentException("Not binary comparison condition: ", nameof(conditionComparison));

            default:
                throw new ArgumentOutOfRangeException(nameof(conditionComparison));
            }
        }
示例#12
0
    public static bool CompareValues <T>(this T actualValue, ConditionComparison condition, T expectedValue)
        where T : IComparable
    {
        switch (condition)
        {
        case ConditionComparison.EqualTo:
        case ConditionComparison.GreaterThan:
        case ConditionComparison.GreaterThanOrEqualTo:
        case ConditionComparison.LessThan:
        case ConditionComparison.LessThanOrEqualTo:
            break;

        default:
            break;
        }

        return(false);
    }
示例#13
0
 public DialogVariableCondition(string variableName, ConditionComparison comparison, string value)
 {
     VariableName = variableName;
     Comparison   = comparison;
     Value        = value;
 }
        private ConditionTreeLeaf ParseLeaf(string value, ParseContext context)
        {
            ConditionType type;

            if (value.IndexOf("_") <= 1 && value.IndexOf("_") != -1)
            {
                type = ConditionType.State;
            }
            else
            {
                type = ConditionType.Property;
            }

            ConditionComparison comparisonType = ConditionComparison.Equal;
            Regex ltCheck    = new Regex("\\(lt_[0-9.]+\\)");
            Match ltMatch    = ltCheck.Match(value);
            Regex gtCheck    = new Regex("\\(gt_[0-9.]+\\)");
            Match gtMatch    = gtCheck.Match(value);
            Regex eqCheck    = new Regex("\\(eq_[a-zA-Z0-9.]+\\)");
            Match eqMatch    = eqCheck.Match(value);
            Regex lteqCheck  = new Regex("\\(lteq_[0-9.]+\\)");
            Match lteqMatch  = lteqCheck.Match(value);
            Regex gteqCheck  = new Regex("\\(gteq_[0-9.]+\\)");
            Match gteqMatch  = gteqCheck.Match(value);
            Regex emptyCheck = new Regex("\\(empty\\)");
            Match emptyMatch = emptyCheck.Match(value);

            string compareTo = "true";

            if (ltMatch.Success)
            {
                comparisonType = ConditionComparison.Less;
                compareTo      = ltMatch.Value.Replace("(lt_", "").Replace(")", "");
            }
            else if (gtMatch.Success)
            {
                comparisonType = ConditionComparison.Greater;
                compareTo      = gtMatch.Value.Replace("(gt_", "").Replace(")", "");
            }
            else if (eqMatch.Success)
            {
                compareTo = eqMatch.Value.Replace("(eq_", "").Replace(")", "");
            }
            else if (lteqMatch.Success)
            {
                comparisonType = ConditionComparison.LessOrEqual;
                compareTo      = lteqMatch.Value.Replace("(lteq_", "").Replace(")", "");
            }
            else if (gteqMatch.Success)
            {
                comparisonType = ConditionComparison.GreaterOrEqual;
                compareTo      = gteqMatch.Value.Replace("(gteq_", "").Replace(")", "");
            }
            else if (emptyMatch.Success)
            {
                comparisonType = ConditionComparison.Empty;
                compareTo      = "";
            }

            if (value.StartsWith("!"))
            {
                if (comparisonType == ConditionComparison.Equal)
                {
                    comparisonType = ConditionComparison.NotEqual;
                }
                else if (comparisonType == ConditionComparison.NotEqual)
                {
                    comparisonType = ConditionComparison.Equal;
                }
                else if (comparisonType == ConditionComparison.Empty)
                {
                    comparisonType = ConditionComparison.NotEmpty;
                }
            }

            string variableName = Regex.Match(value, "\\$[a-zA-Z]+").Value.Replace("$", "").Replace("!", "");

            if (type == ConditionType.State)
            {
                variableName = value.Replace("_", "").Replace("!", "").ToLowerInvariant();
            }

            var propertyType = PropertyUnionType.Boolean;

            if (type == ConditionType.Property && !context.PropertyTypes.TryGetValue(variableName, out propertyType))
            {
                throw new ConditionFormatException(String.Format("Unknown property '{0}'", variableName), 0, 0);
            }

            return(new ConditionTreeLeaf(type, variableName, comparisonType, new PropertyUnion(compareTo, propertyType)));
        }
        private ConditionTreeLeaf ParseLeaf(string value)
        {
            ConditionType type;

            if (value.IndexOf("_") <= 1 && value.IndexOf("_") != -1)
            {
                type = ConditionType.State;
            }
            else
            {
                type = ConditionType.Property;
            }

            ConditionComparison comparisonType = ConditionComparison.Equal;
            Regex ltCheck    = new Regex("\\(lt_[0-9.]+\\)");
            Match ltMatch    = ltCheck.Match(value);
            Regex gtCheck    = new Regex("\\(gt_[0-9.]+\\)");
            Match gtMatch    = gtCheck.Match(value);
            Regex eqCheck    = new Regex("\\(eq_[a-zA-Z0-9.]+\\)");
            Match eqMatch    = eqCheck.Match(value);
            Regex lteqCheck  = new Regex("\\(lteq_[0-9.]+\\)");
            Match lteqMatch  = lteqCheck.Match(value);
            Regex gteqCheck  = new Regex("\\(gteq_[0-9.]+\\)");
            Match gteqMatch  = gteqCheck.Match(value);
            Regex emptyCheck = new Regex("\\(empty\\)");
            Match emptyMatch = emptyCheck.Match(value);

            string compareTo = "true";

            if (ltMatch.Success)
            {
                comparisonType = ConditionComparison.Less;
                compareTo      = ltMatch.Value.Replace("(lt_", "").Replace(")", "");
            }
            else if (gtMatch.Success)
            {
                comparisonType = ConditionComparison.Greater;
                compareTo      = gtMatch.Value.Replace("(gt_", "").Replace(")", "");
            }
            else if (eqMatch.Success)
            {
                compareTo = eqMatch.Value.Replace("(eq_", "").Replace(")", "");
            }
            else if (lteqMatch.Success)
            {
                comparisonType = ConditionComparison.LessOrEqual;
                compareTo      = lteqMatch.Value.Replace("(lteq_", "").Replace(")", "");
            }
            else if (gteqMatch.Success)
            {
                comparisonType = ConditionComparison.GreaterOrEqual;
                compareTo      = gteqMatch.Value.Replace("(gteq_", "").Replace(")", "");
            }
            else if (emptyMatch.Success)
            {
                comparisonType = ConditionComparison.Empty;
                compareTo      = "";
            }

            if (value.StartsWith("!"))
            {
                if (comparisonType == ConditionComparison.Equal)
                {
                    comparisonType = ConditionComparison.NotEqual;
                }
                else if (comparisonType == ConditionComparison.NotEqual)
                {
                    comparisonType = ConditionComparison.Equal;
                }
                else if (comparisonType == ConditionComparison.Empty)
                {
                    comparisonType = ConditionComparison.NotEmpty;
                }
            }

            string            variableName = Regex.Match(value, "\\$[a-zA-Z]+").Value.Replace("$", "").Replace("!", "");
            ComponentProperty property;

            if (type == ConditionType.State)
            {
                variableName = value.Replace("_", "").Replace("!", "").ToLowerInvariant();
                return(new ConditionTreeLeaf(type, variableName, comparisonType, PropertyValue.Parse(compareTo, PropertyValue.Type.Boolean)));
            }
            else if ((property = description.Properties.FirstOrDefault(x => x.Name == variableName)) != null)
            {
                return(new ConditionTreeLeaf(type, variableName, comparisonType, PropertyValue.Parse(compareTo, ConditionParser.ToSimplePropertyType(property.Type))));
            }
            else
            {
                throw new ConditionFormatException(string.Format("Unknown property '{0}'", variableName), 0, 0);
            }
        }
示例#16
0
        /// <summary>
        /// Specifies a query condition.
        /// </summary>
        /// <param name="query">The query.</param>
        /// <param name="attribute">The name of the attribute.</param>
        /// <param name="condition">The relationship type.</param>
        /// <param name="value">The query value.</param>
        public static MaximoResourceSet Where(this MaximoResourceSet query, string attribute, ConditionComparison condition, int value)
        {
            query.Where.Add(new IntegerCondition(attribute, value, condition));

            return(query);
        }
示例#17
0
 private static string ComparisonToString(ConditionComparison c)
 {
     switch (c)
     {
         case ConditionComparison.Equal:
             return "==";
         case ConditionComparison.Greater:
             return ">";
         case ConditionComparison.GreaterOrEqual:
             return ">=";
         case ConditionComparison.Less:
             return "<";
         case ConditionComparison.LessOrEqual:
             return "<=";
         case ConditionComparison.NotEqual:
             return "!=";
         default:
             return "?";
     }
 }
示例#18
0
 /// <summary>
 /// Creates a <see cref="Condition" /> with the specified property name, comparison, and value.
 /// </summary>
 /// <param name="propertyName">The name of the property against which to test when the <see cref="Test" /> method is called passing in a property reference.</param>
 /// <param name="comparison">The type of comparison to use.</param>
 /// <param name="value">The value against which to test when the <see cref="Test" /> method is called.</param>
 public Condition(string propertyName, ConditionComparison comparison, object value)
 {
     mvarPropertyName = propertyName;
     mvarComparison   = comparison;
     mvarValue        = value;
 }
示例#19
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Condition"/> class.
 /// </summary>
 /// <param name="attribute">The name of the attribute.</param>
 /// <param name="compareOperator">The type of comparison to be performed.</param>
 protected Condition(string attribute, ConditionComparison compareOperator)
 {
     _attribute = attribute;
     _operator  = compareOperator;
 }
示例#20
0
 /// <summary>
 /// Initializes a new instance of the <see cref="IntegerCondition" /> class.
 /// </summary>
 /// <param name="attribute">The attribute.</param>
 /// <param name="value">The value.</param>
 /// <param name="op">The operator.</param>
 public IntegerCondition(string attribute, int value, ConditionComparison op = ConditionComparison.Equals) : base(attribute, op)
 {
     _value = value;
 }
示例#21
0
 /// <summary>
 /// Initializes a new instance of the <see cref="StringCondition" /> class.
 /// </summary>
 /// <param name="attribute">The attribute.</param>
 /// <param name="value">The value.</param>
 /// <param name="op">The operator.</param>
 public StringCondition(string attribute, string value, ConditionComparison op = ConditionComparison.Equals) : base(attribute, op)
 {
     _value = value;
 }