/// <summary> /// Given a condition and the input values, evaluate the condition /// </summary> /// <param name="condition"></param> /// <param name="inputValues"></param> /// <returns></returns> internal static bool EvaluateCondition(string condition, InputValues inputValues) { if (string.IsNullOrWhiteSpace(condition)) { return(true); } Node treeRoot = ConstructExpressionTree(condition); return(EvaluateExpressionTree(treeRoot, inputValues)); }
/// <summary> /// Given an expression tree, evaluate the expression /// </summary> /// <param name="top"></param> /// <param name="inputValues"></param> /// <returns></returns> internal static dynamic EvaluateExpressionTree(Node top, InputValues inputValues) { if (top == null) { return(false); } dynamic leftReturn = false, rightReturn = false; if (top.Left != null) { leftReturn = EvaluateExpressionTree(top.Left, inputValues); } if (top.Right != null) { rightReturn = EvaluateExpressionTree(top.Right, inputValues); } if (top.ValueType == PredefinedType.Custom) { // check if number and return number int num; if (int.TryParse(top.Value, out num)) { return(num); } // check if bool and return bool bool bl = false; if (bool.TryParse(top.Value, out bl)) { return(bl); } } if (!textToTokenHash.ContainsKey(top.Value)) { return(top.Value); } else { switch (textToTokenHash[top.Value]) { case PredefinedTokenEnum.AND: return((leftReturn == null || rightReturn == null) ? false : leftReturn && rightReturn); case PredefinedTokenEnum.NOT: return((rightReturn == null) ? false : !rightReturn); case PredefinedTokenEnum.OR: return((leftReturn == null || rightReturn == null) ? false : leftReturn || rightReturn); case PredefinedTokenEnum.GT: return((leftReturn == null || rightReturn == null) ? false : leftReturn > rightReturn); case PredefinedTokenEnum.LT: return((leftReturn == null || rightReturn == null) ? false : leftReturn < rightReturn); case PredefinedTokenEnum.GE: return((leftReturn == null || rightReturn == null) ? false : leftReturn >= rightReturn); case PredefinedTokenEnum.LE: return((leftReturn == null || rightReturn == null) ? false : leftReturn <= rightReturn); case PredefinedTokenEnum.EQ: return((leftReturn == null || rightReturn == null) ? false : leftReturn == rightReturn); case PredefinedTokenEnum.NE: return((leftReturn == null || rightReturn == null) ? false : leftReturn != rightReturn); case PredefinedTokenEnum.TRUE: return(true); case PredefinedTokenEnum.FALSE: return(false); case PredefinedTokenEnum.OPENPAREN: case PredefinedTokenEnum.CLOSEPAREN: return(true); case PredefinedTokenEnum.DEPTH: return(inputValues.NextDepth); case PredefinedTokenEnum.MAXDEPTH: return(inputValues.MaxDepth); case PredefinedTokenEnum.ISARRAY: return(inputValues.IsArray); case PredefinedTokenEnum.NOMAXDEPTH: return(inputValues.NoMaxDepth); case PredefinedTokenEnum.MINCARDINALITY: return(inputValues.MinCardinality); case PredefinedTokenEnum.MAXCARDINALITY: return(inputValues.MaxCardinality); case PredefinedTokenEnum.NORMALIZED: return(inputValues.Normalized); case PredefinedTokenEnum.REFERENCEONLY: return(inputValues.ReferenceOnly); case PredefinedTokenEnum.STRUCTURED: return(inputValues.Structured); case PredefinedTokenEnum.VIRTUAL: return(inputValues.IsVirtual); case PredefinedTokenEnum.ALWAYS: return(true); default: return(top.Value); } } }