The ConstraintOperator class is used internally by a ConstraintBuilder to represent an operator that modifies or combines constraints. Constraint operators use left and right precedence values to determine whether the top operator on the stack should be reduced before pushing a new operator.
Example #1
0
        /// <summary>
        /// Appends the specified operator to the expression by first
        /// reducing the operator stack and then pushing the new
        /// operator on the stack.
        /// </summary>
        /// <param name="op">The operator to push.</param>
        public void Append(ConstraintOperator op)
        {
            op.LeftContext = lastPushed;
            if (lastPushed is ConstraintOperator)
                SetTopOperatorRightContext(op);

            // Reduce any lower precedence operators
            ReduceOperatorStack(op.LeftPrecedence);

            ops.Push(op);
            lastPushed = op;
        }
Example #2
0
        /// <summary>
        /// Appends the specified operator to the expression by first
        /// reducing the operator stack and then pushing the new
        /// operator on the stack.
        /// </summary>
        /// <param name="op">The operator to push.</param>
        public void Append(ConstraintOperator op)
        {
            op.LeftContext = lastPushed;
            if (lastPushed is ConstraintOperator)
            {
                SetTopOperatorRightContext(op);
            }

            // Reduce any lower precedence operators
            ReduceOperatorStack(op.LeftPrecedence);

            ops.Push(op);
            lastPushed = op;
        }
Example #3
0
        /// <summary>
        /// Resolves this instance, returning a Constraint. If the builder
        /// is not currently in a resolvable state, an exception is thrown.
        /// </summary>
        /// <returns>The resolved constraint</returns>
        public Constraint Resolve()
        {
            if (!IsResolvable)
            {
                throw new InvalidOperationException("A partial expression may not be resolved");
            }

            while (!ops.Empty)
            {
                ConstraintOperator op = ops.Pop();
                op.Reduce(constraints);
            }

            return(constraints.Pop());
        }
Example #4
0
        /// <summary>
        /// Sets the top operator right context.
        /// </summary>
        /// <param name="rightContext">The right context.</param>
        private void SetTopOperatorRightContext(object rightContext)
        {
            // Some operators change their precedence based on
            // the right context - save current precedence.
            int oldPrecedence = ops.Top.LeftPrecedence;

            ops.Top.RightContext = rightContext;

            // If the precedence increased, we may be able to
            // reduce the region of the stack below the operator
            if (ops.Top.LeftPrecedence > oldPrecedence)
            {
                ConstraintOperator changedOp = ops.Pop();
                ReduceOperatorStack(changedOp.LeftPrecedence);
                ops.Push(changedOp);
            }
        }
Example #5
0
 /// <summary>
 /// Pushes the specified operator onto the stack.
 /// </summary>
 /// <param name="op">The op.</param>
 public void Push(ConstraintOperator op)
 {
     stack.Add(op);
 }
Example #6
0
 /// <summary>
 /// Pushes the specified operator onto the stack.
 /// </summary>
 /// <param name="op">The op.</param>
 public void Push(ConstraintOperator op)
 {
     stack.Add(op);
 }