Using Default Values of Variables

There are situations where the configuration files will not contain the value of a certain connection string if it is equal to some default value. For example, when defining an HTTP URL, a port value of 80 is probably not specifically mentioned in the URL. This means that the configuration file is more likely to contain http://www.hp.com/ than http://www.hp.com:80/, even though both are correct. This might cause a problem when writing a search condition, since if the PORT variable contains the value 80 and the condition requires such value to exist, the test for the URL will fail if the value is not specified.

To solve this, each variable can, optionally, have a default value. Search expressions can be altered in case the variable’s value is the same as its default value.

There are two ways to alter a search expression:

  • Ignore a condition if a variable's value is the same as its default value

    Use this option to completely ignore a condition if one or more of the variables in the search condition has the same value as its default value. If a condition is ignored, it does not return True or False, it is ignored so the result of the logical operator depends on the operator and the other operands. For example:

    • In the case of a condition having the form A AND B, where A and B are other expressions, if A is ignored, the results will be the same as the result of B.
    • For the expression: A AND B AND C, if A is ignored, the result will be the same as B AND C.

    • For the expression: A OR B OR C, if A is ignored, the result will be the same as B OR C.

    In the rare case where all sub-conditions were ignored, meaning that the entire condition was ignored, the result of the configuration document condition will be False.

    To ignore a condition, add the ignoreIfDefaultValue attribute to the condition, following a list of variables that will trigger this condition to be ignored. For example:

    <KeyCondition key="serverName" ignoreIfDefaultValue="IPADDRESS">
  • Use a different condition if a variable's value is the same as its default

    Let's continue with the example with PORT 80 and the URL. Use the following regular expression to test for the URL:

    http://${DOMAIN}:${PORT}/

    It is clear that this regular expression will never evaluate to True for strings such as http://www.hp.com/ since the colon (:) does not exist in the string. Therefore, we would also want to test for the following regular expression if the PORT variable has the value of 80:

    http://${DOMAIN}/

    In these kinds of scenarios, you can use alternative expressions. To define an alternative expression, define multiple search expressions under the same conditions, and state which one should be used when a variable has its default value. For example:

    <KeyCondition key="url">
       <RegExp>http://${DOMAIN}:${PORT}/</RegExp>
       <RegExp alternativeFor=”PORT”>http://${DOMAIN}/</RegExp>
    </KeyCondition>
    

    Using the alternativeFor attribute, state that in case the variable has its default value, the alternative expression will be evaluated, instead of the original expression. The original expression is the one without the alternativeFor attribute or with an empty alternativeFor attribute.

    For all variables that appear in an expression and have a default value, you must define alternative expression to cover any combination of those variables. Otherwise, you will get a compilation error. In case there is just one variable, you need only one alternative expression, as in the example above. If there are multiple variables with default values, you will need multiple alternative expressions.

    If, for example, the DOMAIN variable also had a default value, using an alternativeFor statement means having all the following alternative conditions:

    <KeyCondition key="url">
       <RegExp>http://${DOMAIN}:${PORT}/</RegExp>
       <RegExp alternativeFor=”PORT”>http://${DOMAIN}/</RegExp>
       <RegExp alternativeFor=”DOMAIN”>http://www.hp.com:${PORT}/</RegExp>
       <RegExp alternativeFor=”PORT, DOMAIN”>http://www.hp.com/</RegExp>
    </KeyCondition>
    

    Only one of those combinations will be evaluated during runtime.

For more information about specifying default values for variables, see Default Values.