Managing unknown operators on attributes and elements

XML objects can have unknown attributes or elements. In IRL, you can check for the isknown or unknown operators. In BAL, these operators do not exist.

About this task

In Java™ objects, the isknown attribute returns true even if the attribute or element is equal to null. In contrast, XML objects can have optional attributes or elements that are unknown.

An optional XML attribute or element is unknown in the XML document if the following conditions are met:

  • The value is not given.

  • The value is explicitly set to xsi:nil

In the ILOG® Rule Language (IRL), you can check whether there is a value for optional attributes using the isknown and isunknown operators and, if there is, retrieve it.

The operators isknown and isunknown do not exist in the Business Action Language (BAL). To apply these operators, you need to create a method in the BOM, then use BOM to XOM mapping. The following example shows an example for a lastName attribute.

Procedure

To apply the isknown/isunknown operators:

  1. Create the following method in the BOM
    boolean isKnownLastName();
  2. In the BOM to XOM mapping, implement this method as:
    return lastName isknown;
  3. After verbalizing the isKnownLastName method, you can then protect access to the lastName attribute in the business rules. For example, you can write the following rule condition:
    If the last name of the customer is known and the last name of the customer is ‘Smith”
  4. Finally, if you need to change the attribute value to unknown: for non-primitive types, such as String, just set the attribute to null. In IRL code, you can do this as:
    ?customer.lastName = null;

    If the type of the optional attribute is a primitive type, the XML binding generates the XOM method setUnknown<AttributeName>. You can therefore unset an age attribute of type int using:

    ?customer.setUnknownAge();
    Note:

    The unknown operator is often used to test a nullable field before de-referencing it. This test avoids raising a null pointer exception (NPE) when tests are evaluated against the file in some rule conditions.