Effective Calculations [ HP ALLBASE/BRW Reference Manual ] MPE/iX 5.0 Documentation
HP ALLBASE/BRW Reference Manual
Effective Calculations
This section discusses some rules for effective calculations, especially
with more complicated reports. For more details about Calculations,
refer to Chapter 15.
The Calculation Language for HP ALLBASE/BRW is expression-oriented. That
is, every formula has a resulting value -- the value of the calculated
item or function.
Programming languages like COBOL or PASCAL are statement-oriented. You
specify both the control flow and the data flow explicitly. You define
which data goes into which variables and when.
For example in PASCAL you can write:
a := b + c;
e := a / d;
By the sequence in which you write the calculation, you explicitly
specify that a is to be calculated before e.
To do the same calculation in HP ALLBASE/BRW, you define two calculated
items in any sequence:
* item A with the formula: B + C
* item E with the formula: A / D
The formulas are similar to the right side of the assignment statements
in PASCAL, but the difference is that you do not specify the sequence in
which A and E will be calculated. HP ALLBASE/BRW does that for you by
analyzing the formulas of the calculated items and using the most
efficient sequence for the calculation. You don't have to know anything
about HP ALLBASE/BRW's internal control flow.
When you use HP ALLBASE/BRW's calculation language, there are ways to use
it very efficiently and other ways which are not so efficient, in much
the same way as with programming languages. The following gives you some
insight into how HP ALLBASE/BRW calculations work and some hints about
how to define calculations effectively.
How Calculated Items Work
Calculated items are items which get their values by the execution of an
algorithm defined in the calculated item's formula. Their purpose is to
produce values which are not contained in any of the source tables, as
such, but can be derived from other items. A calculated item can be used
in the report like any other item.
In a table every item or every component of an array item has a single
value per record. The same is true for calculated items, too.
Therefore, HP ALLBASE/BRW calculates them only once per record. If you
have defined calculated items which use other calculated items in their
formulas, as in the example above in which E uses A in its formula, HP
ALLBASE/BRW's compiler analyzes these dependencies and automatically
generates code such that A is calculated before E.
If A is used in more than one other calculated item, for example in E, F
and G, A is only calculated once before E, F and G. These items then only
use the result value of A. This means that HP ALLBASE/BRW generates very
effective code if calculated items are used by other calculated items.
HP ALLBASE/BRW does not permit cycles or recursion in calculations. For
example, if you define the calculated items:
item R with the formula: S + 1
item S with the formula: R + 1
where R uses S and S uses R, HP ALLBASE/BRW's compiler could not
determine which item would be calcu lated first. In this case you would
get a message that there is a Cycle in the calculated item formula when
the report is compiled.
How Functions Work
Functions are used to define common formulas. They can only be used in
other formulas and cannot, for example, be printed or used for sorting
like an item. This is because functions need values for their function
arguments which can only be specified in formulas. Function arguments
are like function parameters in PASCAL. They give you a lot of
flexibility when you define function formulas.
On the other hand, function arguments do not allow the calculation of
functions to be optimized in the way HP ALLBASE/BRW optimizes calculated
items. If a function is used in three calculated items, the function is
calculated three times because it may have different function arguments
and therefore three different results. HP ALLBASE/BRW does not determine
if two or more function calls use arguments of the same value.
[REV BEG]
Beware of putting too much into a function formula. Here is an example
of a function that could be made simpler:
function name argument
X (a, b, mode)
The formula for this function is defined as:
IF mode = 1 THEN a + b
ELSE a - b
The function is used in two formulas for calculated items:
item name with the formula:
SUM X (k, l, 1)
DIFFERENCE X (m, n, 2)
Instead of the more complex function (X), it would be better to use two
simpler functions, one for the addition and one for the subtraction:
function and with the formula:
arguments
ADD (a, b) a + b
SUB (a, b) a - b
Then you could use the two functions in a simpler way with the calculated
items:
item name with the formula:
SUM ADD (k, l)
DIFFERENCE SUB (m, n)
[REV END]
Function X is also not efficient in terms of performance, since the IF
THEN ELSE must be evaluated each time the function is calculated. You
can avoid this by using the functions ADD and SUB. If you have functions
with many IF THEN ELSEs, determine whether you can divide them into
simpler, separate functions.
In this discussion about functions, we have talked primarily about using
functions and not about calling functions. This is because there is
really no mechanism to call functions in HP ALLBASE/BRW. Instead, a
function in HP ALLBASE/BRW is more like a macro than like a function in
PASCAL, because HP ALLBASE/BRW's compiler fills in the function's code
every time a function is used. So, if a function is used in 50
calculated items, the function's code is duplicated 50 times!
You can reach HP ALLBASE/BRW's limits, (otherwise not easy to achieve),
by using complex functions in many calculated items or in other
functions. If this occurs, HP ALLBASE/BRW's compiler aborts with the
following message:
Max. number of segments exceeded
which means that too much code will be generated (> 504 KBytes).
Therefore, keep function formulas small if you plan to use the functions
in many places. Better still, when possible use calculated items instead
of functions.
As with calculated items, cycles (recursions), are not allowed in
functions.
Rules For Effective Calculations
In report writing, using effective calculations means achieving good
performance for calculations. Since the calculations may need to be done
very often in a report, depending on the volume of data, calculation
formulas could be performed several thousand or even several hundred
thousand times.
In a programming language you achieve good performance by structuring the
code well. You can do this in HP ALLBASE/BRW too, to get calculations
that have good performance and are easy to maintain.
The most important points are listed below:
* RULE 1
Avoid repeating identical blocks of code.
In a programming language, you would not write code with a block
that is identical, like the following:
a := c * (d - e + 4 * (f / (100 - g)));
b := h * (d - e + 4 * (f / (100 - g)));
Instead, you would use a temporary variable:
i := d - e + 4 * (f / (100 - g));
a := c * i;
b := h * i;
You can also use temporary variables in HP ALLBASE/BRW. You can
define a calculated item, for example:
item name with the formula:
I
D - E + 4 * (F / (100 - G))
Then you can use the new item in the other calculated items:
item name with the formula:
A C * I
B H * I
Note that you do not need to specify that I is to be calculated
before A and B; HP ALLBASE/BRW recognizes this. You also do not
need to print the item I; you can use it just as a temporary
variable.
In a programming language, you can use a function as another way
of structuring, such as:
INTEGER FUNCTION x (y : integer);
BEGIN
x := y * (d - e + 4 * (f / (100 - g)));
END;
You can use this function to calculate the values for a and b:
a := x (c);
b := x (h);
This structuring provides you with easily maintained formulas, but
not with good performance, since the whole function formula is
calculated twice. If you use a function in HP ALLBASE/BRW,
performance will be even worse because not only will the function
be calculated twice, but its code will also be duplicated and
therefore will require more memory.
* RULE 2
Try to use calculated items instead of functions.
Sometimes structuring may not be so easy. For example, if you
have the following calculated items:
item name with the formula:
A
IF r = 3 AND s = 0
OR r = 4 AND s <> 4
OR r = 5 AND s <> 17
OR r > 7 AND s = 3
OR r < 2 AND s <= 4
THEN c + 3 ELSE c - 3
item name with the formula:
B
IF r = 3 AND s = 0
OR r = 4 AND s <> 4
OR r = 5 AND s <> 17
OR r > 7 AND s = 3
OR r < 2 AND s <= 4
THEN h + 2 ELSE h - 2
The largest parts of both formulas, the IF conditions, are
identical.
It is not so simple to avoid the duplicated code in these
formulas, but you can do so by using a calculated item as a
temporary variable. See the following example:
item name with the formula:
COND
IF r = 3 AND s = 0
OR r = 4 AND s <> 4
OR r = 5 AND s <> 17
OR r > 7 AND s = 3
OR r < 2 AND s <= 4
THEN 1 ELSE 0
Then use COND in the formulas of A and B:
item name with the formula:
A
IF cond = 1 THEN c + 3 ELSE c - 3
item name with the formula:
B
IF cond = 1 THEN h + 2 ELSE h - 2
MPE/iX 5.0 Documentation