/// <summary>
        /// Reads as object.
        /// </summary>
        /// <param name="expressionString">The expression string.</param>
        /// <returns>IBooleanComputable.</returns>
        public IBooleanComputable ReadAsObject(string expressionString)
        {
            IBooleanComputable result = null;

            if (!string.IsNullOrWhiteSpace(expressionString))
            {
                int parenthesesDepth = 0;
                int position         = 0;
                result = ExpectExpression(expressionString, ref parenthesesDepth, ref position);

                if (parenthesesDepth != 0)
                {
                    throw new InvalidExpressiontException(nameof(rightParentheses), data: expressionString, position: expressionString.Length - 1);
                }
            }

            return(result);
        }
        /// <summary>
        /// Computes the specified computable object.
        /// </summary>
        /// <param name="computableObject">The computable object.</param>
        /// <param name="json">The json.</param>
        /// <returns></returns>
        public static bool Compute(IBooleanComputable computableObject, JObject json)
        {
            if (json != null)
            {
                ICriteriaOperatorComputable operatorComputable = computableObject as ICriteriaOperatorComputable;

                if (operatorComputable != null)
                {
                    return(BooleanCompute(operatorComputable, json));
                }

                IRelationshipOperatorComputable relationshipComputable = computableObject as IRelationshipOperatorComputable;

                if (relationshipComputable != null)
                {
                    return(BooleanCompute(relationshipComputable, json));
                }
            }

            return(false);
        }
        /// <summary>
        /// Expects the expression.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <param name="parenthesesDepth">The parentheses depth.</param>
        /// <param name="position">The position.</param>
        /// <returns>IBooleanComputable.</returns>
        /// <exception cref="Beyova.Diagnostic.InvalidExpressiontException">
        /// </exception>
        private IBooleanComputable ExpectExpression(string input, ref int parenthesesDepth, ref int position)
        {
            IBooleanComputable   pendingExpression           = null;
            RelationshipOperator?pendingRelationshipOperator = null;

            TrimStartSpaces(input, ref position);

            while (position < input.Length)
            {
                switch (input[position])
                {
                case leftParentheses:
                    if (pendingExpression == null)
                    {
                        parenthesesDepth++;
                        position++;
                        pendingExpression = ExpectExpression(input, ref parenthesesDepth, ref position);
                    }
                    else
                    {
                        if (pendingRelationshipOperator.HasValue)
                        {
                            parenthesesDepth++;
                            position++;
                            pendingExpression = new RelationshipOperatorComputable
                            {
                                ItemLeft  = pendingExpression,
                                ItemRight = ExpectExpression(input, ref parenthesesDepth, ref position),
                                Operator  = pendingRelationshipOperator.Value
                            };

                            //Clean tmp status
                            pendingRelationshipOperator = null;
                        }
                        else
                        {
                            throw new InvalidExpressiontException(nameof(RelationshipOperator), data: input, position: position);
                        }
                    }
                    break;

                case rightParentheses:
                    if (parenthesesDepth > 0)
                    {
                        if (pendingRelationshipOperator.HasValue)
                        {
                            throw new InvalidExpressiontException("Expression", data: input, position: position);
                        }

                        parenthesesDepth--;
                        position++;
                        return(pendingExpression);
                    }
                    else
                    {
                        throw new InvalidExpressiontException(nameof(leftParentheses), data: input, position: position);
                    }

                //break;
                case space:
                    position++;
                    break;

                default:
                    if (pendingExpression == null)
                    {
                        pendingExpression = ExpectPureKeyValueExpression(input, ref parenthesesDepth, ref position);
                    }
                    else
                    {
                        if (pendingRelationshipOperator.HasValue)
                        {
                            pendingExpression = new RelationshipOperatorComputable
                            {
                                ItemLeft  = pendingExpression,
                                ItemRight = ExpectExpression(input, ref parenthesesDepth, ref position),
                                Operator  = pendingRelationshipOperator.Value
                            };

                            //Clean tmp status
                            pendingRelationshipOperator = null;
                        }
                        else
                        {
                            var relationshipOperatorString = GetNextValidSection(input, ref position, false, relationshipOperatorInterruptChars);
                            RelationshipOperator relationshipOperator;
                            if (!IsRelationshipOperator(relationshipOperatorString, out relationshipOperator))
                            {
                                throw new InvalidExpressiontException(nameof(RelationshipOperator), data: input, position: position);
                            }
                            else
                            {
                                pendingRelationshipOperator = relationshipOperator;
                            }
                        }
                    }

                    break;
                }
            }

            return(pendingExpression);
        }
Example #4
0
 /// <summary>
 /// Creates an instance of the XOr computable with two boolean computables as inputs.
 /// </summary>
 /// <param name="inputA">The input computable A.</param>
 /// <param name="inputB">The input computable B.</param>
 public XOr(IBooleanComputable inputA, IBooleanComputable inputB)
 {
     InputA = inputA;
     InputB = inputB;
 }
Example #5
0
 /// <summary>
 /// Creates an instance of the inverter with the required input node.
 /// </summary>
 /// <param name="input">The input node.</param>
 public Not(IBooleanComputable input)
 {
     Input = input;
 }
Example #6
0
 /// <summary>
 /// Creates an instance of the if-then-else-computable with the condition-, then- and else-computable.
 /// </summary>
 /// <param name="condition">The condition-computable.</param>
 /// <param name="thenComputable">The then-computable.</param>
 /// <param name="elseComputable">The else-computable.</param>
 public If(IBooleanComputable condition, IComputable <T> thenComputable, IComputable <T> elseComputable)
 {
     Condition      = condition;
     ThenComputable = thenComputable;
     ElseComputable = elseComputable;
 }