Previous Topic: Action Blocks Used in SET ... USING ActionsNext Topic: Exit States


Derivation Algorithms

An attribute's derivation algorithm executes whenever an entity of the type to which the attribute belongs is READ, provided the entity action view includes a view of that attribute.

A derivation algorithm must return a single export attribute view of the attribute being derived. Additionally, it can have only a single import view, and that is a view of the entity type to which the derived attribute belongs. All non-derived attributes for the entity occurrence have already been populated before the derivation algorithm is invoked.

The import view of a derivation algorithm has a special property: it can be referenced as the equivalent of a CURRENT view (a distinction normally reserved for entity action views) in READ statements appearing in the derivation algorithm.

The derivation algorithm in the following sample code calculates a value for the attribute Total of the entity type ORDER, which is the sum of the value of all its ORDER ITEMs.

Example Derivation Algorithm

DERIVE_ORDER_TOTAL
     IMPORTS:         Entity View provided order
     EXPORTS:         Entity View derived order
     ENTITY ACTIONS:  Entity View product
                      Entity View order_item

READ EACH order_line
     product
     WHERE DESIRED order_item is on CURRENT provided order
     AND DESIRED product appears on DESIRED order_item
SET derived order total TO derived order total +
     (product price * order_item_quantity)

Composite attributes can be simulated by using a derivation algorithm that includes string functions. For example, assume that a composite attribute Phone Number is built from the elementary attributes Area Code, Exchange, and Station. This is a reasonable need for a phone company.

To simulate the composite attribute, Phone Number can be defined as a derived attribute with the derivation algorithm shown in the following sample code.

Simulating a Composite Attribute

DERIVE_PHONE_NUMBER
     IMPORTS:    Entity View provided customer
     EXPORTS:    Entity View derived customer
SET derived customer phone_number TO CONCAT
     (provided customer exchange, provided customer station)