Expression Pattern Language (eXPL)

Axiom Choice

Axiom choice is one of two selection artifacts, the second being term choice. A term choice selects a single term value, while an axiom choice selects one axiom from a collection. An axiom choice is a hybrid template and axiom collection combination which is used to select a data set for use in generating a solution based on query criteria.

Format

The axiom choice format is similar to that of a literal axiom collection, except keyword axiom is replaced with choice, anonymous terms are not allowed and each row contains a hybrid combination of a match term followed by one or more literals:

choice name( term-names ) { match-term, literals } ...

The match term is unique to axiom choice and it provides the logic on which a selection is made. A match term can be one of two types, both of which involve a match variable indicated by the first name in the term names header:

  1. Literal value which is compared to the match variable
  2. Boolean expression containing the match variable which is evaluated at the time the selection is made

Usage

Axiom choice is versatile in that it can be placed in a query chain or used in a calculator like a conditional branch. There are subtle differences in behaviour to be pointed out later. The format for using an axiom choice in a calculator is simple. The axiom choice is referenced by name and an optional comma-delimited parameter list can follow:

choice name [( parameters )]

An axiom choice declaration may be several lines long, so is not allowed to be attached to a template as this would be confusing. However, when referenced by a calculator, the axiom choice is automatically incorporated in the calculator and a set of variables is provided to hold the selection when it is made. As the axiom choice is in a term position, it must return a value, which in this case is a selection index. Values range from zero upwards, and if no match is found, the returned value is -1.

Axiom choice allows selection based on value ranges. The following example shows an axiom choice named "bracket" used 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};

There are several important details about how the bracket axiom choice is applied:

  • All the choice terms are named. The first, "amount", identifies the match variable.
  • Each match term contains a boolean expression which compares the match variable to a literal value
  • There is no default selection as the last option is open ended.

If a default selection is needed as a "catch-all" last option, then the keyword unknown is used as the last match term.

Choice as a Query Chain

The selection term is set by unifying with the incoming axiom. The result of the axiom choice is placed in the solution. If no match occurs, then the query step is short circuited. This allows cases to be filtered from the query results based on certain criteria. Note that when a match occurs, the selection index is also placed int the solution as a term with the same name as the choice.

It is also possible to append to the axiom choice heading, the names of any terms from the incoming axiom to pass through to the solution. An example of this can be seen in the following example.

Stamp Duty Example

Application StampDuty of tutorial10 demonstrates use of the "bracket" axiom choice as a chain query. In this case, a term name of "id" has been added to allow the "id" axiom value to transfer to the following chained calculator. The application determines the stamp duty payable on $123,458 for transaction id 100078:

choice bracket
(amount,       threshold,     base, percent, id)
...;

axiom transacton_amount (id, amount) : parameter;

calc payable(id, duty = base +
  (amount - threshold) * (percent / 100));

query<term> stamp_duty_query (transacton_amount : bracket)
  -> (payable);

The solution is:

stamp_duty(id=100078, duty=3768.32)

Axiom Choice in Calculator

When used in a calculator, an axiom choice has features not available in a query chain:

  • Choice variables can be declared in front of the choice for type conversion
  • Choice variables can also be declared in front of the choice to assign a default value for the case of no match
  • The selection index value of -1 is available in the case no match occurs and can be used to trigger a default condition

The first two of these features can be observed in the next example.

Stamp Duty Calculator

The StampDuty2 Application of tutorial10 is similar to the previous chain query example, but the first difference to notice is that the tax scales are reversed, so the $500,000 and above range is the first alternative. This has been done so a floor amount of $5,000 can be set below which a fixed duty of $20 applies, and for which no match will occur. The next thing of note is that there is a declaration of 2 variables used by the choice and a default value set for the duty amount:

  • Variable amount is declared as a $US currency type so the amount will be formatted accordingly
  • Variable percent is assigned a value 0.0
  • Variable duty is assigned a default value of 20.0 which applies when the percent value after the choice is 0.0

The application runs with 4 different transaction amounts, the first being below the floor amount and which is charged USD20.00 as expected. The index value for each transaction is printed out and you can see that the first transaction has a value of -1 for "no match"