Chapter 13: Requirements-based Vocabulary

Requirements-based vocabulary enables you to define new COBOL verbs and functions for use with the objects you have created. This chapter explains the syntax for defining these new verbs and functions.

Requirements-based vocabulary is a Micro Focus extension. All the code shown in this chapter uses the Micro Focus alternatives to the ISO 2002 syntax.

Overview

As an alternative message-sending mechanism to the INVOKE verb, you can create your own new COBOL verbs and functions. For example, you could create a new verb, CREDIT, enabling you to write:

credit aBankAccount with amount

To use this syntax in a program, you have to include a class interface definition at the start of your program or class. Interface definitions are declared through external classes. An external class has the structure of a class, and defines all of the class and instance methods of the class, but does not contain any of the code for the methods.

By convention, each external class is held in a .if file, and programs using vocabularies use COPY statements to include the class definition. The COPY statements must appear before the Class-ID header.

There are three steps to using requirements-based vocabularies:

  1. Define a vocabulary for an object (or for a set of objects). The vocabulary for each object is defined in an external class.
  2. Include the external classes at the start of each program (or class) which invokes objects through a vocabulary.
  3. Use the verbs and functions defined by the vocabulary to send messages to objects.

In many cases, step 1 will be the responsibility of different programmers to steps 2 and 3. One group of programmers is given the task of designing objects for an organization's own class library, while other groups use the objects from the class library as the building blocks to create business applications.

The class library designers would take charge of defining the vocabulary (step 1 above) for objects in the class library, ensuring that it was consistent and readable. The application builders would use the vocabularies (steps 2 and 3) to simplify their own development work, and to enhance the readability of their applications.

Defining a Vocabulary

You define vocabularies on a class basis. Each vocabulary is defined by an external class. An external class is a program that defines a set of method interfaces, but does not include any code.

External Class Structure

External classes have the same basic structure as any OO COBOL class. The outline structure for the BankAccount external class is shown below:

 class-id. BankAccount is external
           inherits from Base 
 object section.
 class-control.
     BankAccount is class "account"
     Base is class "Base"
     .
    
* Class methods.
 class-id. "new"
* Method interface definition. 
 end program "new".
  ...
    
* Object program. 
    
 object.
    
* Instance methods
     
 end object.
    
 end class BankAccount.

The only differences between the external class definition for BankAccount, and the BankAccount class itself, are:

Method Interface

A method program interface definition has the following elements:

No storage is declared, and there is no code in the Procedure Division. The method interface for the credit method looks like this:

 method-id. credit is method.
     linkage section.
     01 lnkAmount        pic 9(10)v99.
 
 procedure division using lnkAmount
    INVOKED AS ==Credit <self> [with] <lnkAmount> ==
    OR AS ==deposit <lnkAmount> [in] <self> ==
    . 
 
 end method credit.

The elements of the new syntax definition are:

Delimiters The INVOKED AS phrase indicates the start of the verb or function signature syntax. Each signature is bracketed by double equals "==". You can specify multiple definitions for a method by using the OR AS phrase.
Parameters Appear inside angle brackets "<>". All parameters must be passed by reference in the PROCEDURE DIVISION USING statement.

<SELF> is a special parameter which marks the point where the sender puts the object reference for the method. <SELF> must always appear once (and only once) in the interface definition. You can also use <THIS> and <OBJECT> as synonyms for <SELF>.

Compulsory words Appear exactly as they are to be used.
Noise words Appear inside square brackets "[ ]". Noise words enable you to make your new verbs and functions more readable. In the example above, you can use both these ways to invoke the credit method:
credit anAccount with aSumOfMoney
credit anaccount aSumOfMoney

By default, the following are disallowed in the verb signatures of method interface definitions:

You can enable the use of both these in verb signatures by setting compiler directive OOCTRL(-Q). Setting this directive disables the use of qualification in a verb signature which invokes a method. The default setting is OOCTRL(+Q).

User-defined Functions

You can also create new functions with user vocabularies. The new functions look like COBOL intrinsic functions when used in a program.

For example, you could code:

 compute interest = .06 * function balance (thisBankAccount)

You can omit the keyword function, so you could also write:

 compute interest = .06 * balance (thisBankAccount)

The definition of function balance looks like this:

 method-id. "getBalance".
 linkage section.
 01 lsAmount pic S9(10)v99.

 procedure division returning lsAmount
    INVOKED AS FUNCTION == Balance ( <self> ) ==.
 end method "getbalance".

Using Vocabulary-defined Verbs and Functions

To use vocabulary defined verbs or functions:

  1. Include the external class defining the new syntax in your program as a copyfile, using a COPY statement. By convention these are stored in .if files.

    If you are using several objects with their own vocabulary definitions, you must include the external class for each one. Put external classes at the top of the source code, before the Identification Division.

  2. Use the vocabulary defined for the object in place of an INVOKE verb and method name when you want to send the object a message.

The Micro Focus class libraries supplied with your COBOL system include external classes with vocabularies for the following types of manipulation:.

base.if Component handling verbs
chararray.if String manipulation verbs
symbtab.if SymbolTable manipulation verbs
guibase.if Event mapping verbs
sdiframe.if GUI application framework verbs

One advantage that vocabularies give you is that data values are cast into the format expected by the target method. For example, you can code:

     credit anAccount with 5

The interface definition at the start of the program enables the Compiler to convert the number 5 into the numeric format expected by the method. The equivalent using standard OO COBOL requires two statements:

     move 5 to anAmount
     invoke anAccount "credit" using anAmount

For an example that uses requirements-based vocabulary, see the chapter Requirements-based Vocabulary Tutorial.


Copyright © 2009 Micro Focus (IP) Ltd. All rights reserved.