Conditional Functions

If-Else

Purpose:

Evaluates an equality and returns one value if true and another value if false.

Type Name: ‘if_else’

Definition Syntax:

[expEquality] ~ [code_if_true] ~ [code_if_false]

where:

  • ‘expEquality’ is a JFunction Equality
  • ‘code_if_true’ is a constant or else the key for which to extract the value from the variable dictionary if the equality is true.
  • ‘code_if_false’ is a constant or else the key for which to extract the value from the variable dictionary if the equality is false
Note

If the value in either ‘code_if_false’ or ‘code_if_true’ is a relatively simple additive equation, you can wrap the additive equation in ‘eq()’. This is a shortcut to use for relatively simple additive equations, for example: ‘eq(a + b * par_rut)’. If the values in ‘code_if_false’ or ‘code_if_true’ are represented by more complex additive equation, we strongly recommend you rather pre-calculate these values and assign them to keys which you then use in your If-Else expression.

Example:

t: par_surf_class = seal ~ f_exp_life_seal_init ~ f_exp_life_ac_init

In this example, the JFunction Equality is defined as:

t: par_surf_class = seal

This equality checks if the value for key ‘par_surf_class’ equals ‘seal’. If this evaluates to 1 (true), then the value returned is that which maps to the key ‘f_exp_life_seal_init’ in the variable dictionary. If the equality evaluates to 0 (false) then then the value returned is that which maps to the key ‘f_exp_life_ac_init’ in the variable dictionary.

Comments

None

Multiple-If

Purpose:

Evaluates multiple equalities and returns the value mapping to a specified key for the FIRST equality that maps to 1 (true). An optional fall-through value can also be provided.

Type Name: ‘multiple_if’

Definition Syntax:

[expEqualityCode1 | returnValueCode1] ~ [expEqualityCode2 | returnValueCode2] ~ etc.

where:

  • ‘expEqualityCode1’ is a JFunction Equality or, in the case of the LAST value, it can be a constant or key (fall-through value) - see note below.
  • ‘returnValueCode1’ is a constant or else the key for which to extract the value from the variable dictionary if the first equality is true.
  • ‘expEqualityCode2’ is a JFunction Equality
  • ‘returnValueCode2’ is a constant or else the key for which to extract the value from the variable dictionary if the second equality is true.
  • Etc. (add further equalities to catch all possible situations)
Note

The last value in the ‘~’ delimited string can be either an valid JFunction Equality or else a constant or a key that maps to a value in the Variable Dictionary. In the latter case, the last value is assumed to be the fall-through value to assign if none of the preceding equality statements return true (1). Note that only the LAST item in the ‘~’ delimited list can be a constant or key. All others need to be valid JFunction Equality codes.

Example 1 - No Fall-Through:

t : this_treatment_cat starts_with rehab | f_reset_rehab_rut ~ n: f_is_seal = 1 | f_reset_seal_rut ~ n: f_is_seal = 0 | f_reset_ac_rut

In this example, the first JFunction Equality is defined as:

t : this_treatment_cat starts_with rehab

This equality checks if the value for key ‘this_treatment_cat’ starts with the phrase ‘rehab’. If this evaluates to 1 (true), then the value returned is that which maps to the key ‘f_reset_rehab_rut’ in the variable dictionary. If the first equality evaluates to 0 (false) then then the evaluation moves to the second equality which is defined as:

n: f_is_seal = 1

This second equality checks if the value for key ‘f_is_seal’ equals 1. If this evaluates to 1 (true), then the value returned is that which maps to the key ‘f_reset_seal_rut’ in the variable dictionary.

If the second equality also evaluates to 0 (false) then then the evaluation moves to the third equality which is defined as:

n: f_is_seal = 0

This third equality checks if the value for key ‘f_is_seal’ equals 0. If this evaluates to 1 (true), then the value returned is that which maps to the key ‘f_reset_ac_rut’ in the variable dictionary.

Comments

The equalities you use in the various components of your Multiple-If function can be either Basic Equalities, AND-compounded equalities or OR-compounded equalities. This combination of possibilities makes the Multiple-If one of the most powerful JFunctions. It does, however, require careful design and testing.

Example 2 - With Fall-Through:

t : this_treatment_cat starts_with rehab | f_reset_rehab_rut ~ n: f_is_seal = 1 | f_reset_seal_rut ~ f_reset_ac_rut

This example is identical to Example 1 above, but in this case the fall-through option is used which simplifies the setup code somewhat. For this case, if the treatment category does not contain ‘rehab’ and the value mapping to ‘f_is_seal’ in the variable dictionary is not 1, then none of the equalities are satisfied and the last value (since it is not a valid equality) will be returned. In this case, if the Variable Dictionary contains a key named ‘f_reset_ac_rut’ then the value mapped to that key will be returned, otherwise, the literal string ‘f_reset_ac_rut’ will be returned.

Important

The optional use of a fall-through value is only available from Version 0-3-5 onwards. For all earlier versions, you should ensure that any possible condition is met by at least one of the equalities in your Multiple-If JFunctions. Thus, for versions before V-0-3-5, if none of the conditions in your Multiple-If Function are met, an error will be thrown at run-time.