Expression Pattern Language (eXPL)

eXPL Choice

Choice is a special calculator which provides a solution from two or more alternatives. Choice has a format identical to an axiom declaration with named terms. However, when Choice evaluates, it creates variables from the terms in one row and makes these variables available to what follows. The first term in each row contains an expression for the selection of that row. The remaining terms are constants. The first term is referred to as the "selection term" and the value on which the selection is based is called the "selection value". There are three types of selection term:

  • When the selection term is a string, then a regular expression tests the selection value for a match.
  • When the selection term is a number or a variable, then it is tested for equality with the selection value
  • The remaining option is the selection term is a expression which uses the selection value to produce a boolean result

Choice allows selection based on value ranges. The following script shows an example of a Choice to find the stamp duty applicable to a real estate transaction:

choice bracket
(amount,       threshold,     base, percent)
{amount <  12000,      0,     0.00, 1.00}
{amount <  30000,  12000,   120.00, 2.00}
{amount <  50000,  30000,   480.00, 3.00}
{amount < 100000,  50000,  1080.00, 3.50}
{amount < 200000, 100000,  2830.00, 4.00}
{amount < 250000, 200000,  6830.00, 4.25}
{amount < 300000, 250000,  8955.00, 4.75}
{amount < 500000, 300000, 11330.00, 5.00}
{amount > 500000, 500000, 21330.00, 5.50};

The choice keyword is followed by identifying name "bracket", the term names (set out here as column headings) and the choice alternatives.

There are several important details about how Choice is applied:

  • All the Choice terms are named. These names also identify the Choice variables. All are used for the solution and the first doubles as the selection variable ("amount" in above example).
  • Variables are allowed in the first term in each term set (the selection term)
  • If a default selection term is required, then use keyword "unknown". See example opposite.
  • A Choice can be either chained in a query the same way as a calculator, or can be employed as a calculator term. More details to come.

Stamp Duty Example

This script demonstrates use of the Choice shown opposite as a chain query. It calculates stamp duty payable on amount $123,458. The amount here is passed as a parameter, as indicated by the "parameter" declaration in the first line:

axiom transacton_amount (amount) : parameter;
calc payable(duty = base +
  (amount - threshold) * (percent / 100));
query stamp_duty_query (transacton_amount : bracket) >> (payable);

The tutorial10 StampDuty sample runs this query. The solution is:

payable(duty = 3768.32)

Default Selection Example

The following ColorSwatch script demonstrates a Choice with all selection terms hex constants except one default term with keyword "unknown".

// Choice selects on hex color value to get color name and rgb values
choice swatch (hex, color, red, green, blue)
{0x00FFFF, "aqua", 0, 255, 255}
{0x000000, "black", 0, 0, 0}
{0x0000FF, "blue", 0, 0, 255}
{0xFFFFFF, "white", 255, 255, 255}
{unknown, "unknown", 0, 0, 0};
axiom shade (hex) : parameter;
query color_query (shade : swatch);

The tutorial10ColorSwatch sample runs this query. The solution for unexpected hex value of "0x77ffff" (7,864,319) is:

swatch(rgb = 7864319, color = unknown, red = 0, green = 0, blue = 0)