Value Expressions

Relational Operators and Value Expressions

Set expressions are powerful and general, but because of their generality it can be difficult for the language to know enough about them to combine them in interesting ways. Therefore, BBQ has special support for a particular subset known as value expressions, which are guaranteed to result in a single value--rather than a set--when evaluated within a particular context. In BBQ, constants are value expressions, some operators (i.e. count, &) produce value expressions, and value operators can only be used with value expression. Value expressions, in turn, can be used as part of set expressions or transforms.

There are three ways that value expressions can be used to produce a value within set expressions: they can have a value independent of the rest of the expression; they can have a value for each member of a set to which they are applied; or they can have a single value when applied to a set as a whole. More formally, each of these ways to evaluate a value expression is referred to as a value context. Most value expressions are only meaningful in one of the contexts. We'll provide a more complete definition of each of the contexts mentions above:

Scalar Context

In a scalar context, a value expression is evaluated without reference to any other objects or expressions. A constant is an example of a value expression that may be evaluated in a scalar context.

If a value expression may be evaluated in a scalar context, it also can be used as a complete set expression.

Count-Preserving Context

A value expression in a count-preserving context results in a single value when evaluated for a single object.

When evaluated in a set expression, a count-preserving value expression functions as a stateless transform.

Group Context

A value expression in a group context results in a single value when evaluated for an entire set. The canonical example is the count expression, which returns the number of members in a set.

Value expressions of the same context can be freely combined with the value operators to produce a compound expression with that context. Scalar expressions may be combined with count-preserving expressions to produce another count-preserving expression, and scalar expressions may be combined with group expressions to produce another group expression. You can't combine a group expression and a count-preserving expression to get a value expression.

Value expressions have a result type, and non-scalar value expressions have a type to which they apply. Combining value expressions with value operators is subject to the same type resolution rules applicable to sets and transforms.

Relational Operators and Value Expressions

BBQ supports comparing value expressions with the full set of relational operators (< <= = != >= >). The result of such a comparison is a filter applied to the expression that produced the value expressions.

The = and != operators apply to all types; the other relational operators only apply to the types with a pre-defined ordering, integer and string. Therefore these other operators are most likely to be useful in conjunction with the count expression. For example, to find all the methods in the analyzed system with more than 3 arguments, you might use the following set expression:

( count of ( arguments of ) > 3 ) methods in all classes

Note how in this example the relational operator expression that acts as a filter is enclosed in parentheses because the relational operators have lower precedence than the default rule, which is to apply transforms and value expressions to the following set expression.

browse-by-query home antlersoft free software sourceforge project page