Vision Tutorial 3: Single Object Access


Reminder!
To run these examples, you should first start a new session then load the sample database using:
  "/localvision/samples/general/sample.load" asFileContents evaluate ; 
Any other files referenced can be read from the /localvision/samples/general/ directory.

Note: The sample.load file runs by default on a Unix environment. If you are using a Windows NT platform, this location may be prefixed by a drive and optional path (e.g. d:/visiondb/localvision/samples/general/sample.load). Check with your Vision Administrator for further details.



Basic Access

There are many objects in Vision that you need to access by name. Because Vision allows you to maintain many different classes of information, certain conventions have been established to help keep the names straight. For example, suppose you manage a portfolio for General Motors, analyze the company General Motors, and own the common stock of General Motors. These would represent three distinct objects in Vision. The variable name GM could only be associated with one of these objects. To address this situation, a separate dictionary of names is set up for each entity class. You would access a specific entity object using an expression of the form:

    Named Company GM
to access the Company object or
    Named Account GM
to access the Account object. For example, the sample database includes a sector named RETAIL and an industry named RETAIL. The expression:
    Named Industry RETAIL
accesses the industry and the expression:
    Named Sector RETAIL
accesses the sector.

Type and execute the expression:

    Named Company GM
You should see:
    GM
Now try accessing a company that does not exist:
    Named Company XYZ
You should see:
    >>> Selector 'XYZ' Not Found <<< 
           NA 
Because XYZ is not defined as a Company, Vision displays a not found error and returns the value NA (not available) as the result.

To access the latest sales value for GM, execute the expression:

    Named Company GM sales
You should see:
    101781.00
To access GM's price, execute:
    Named Company GM price
You should see:
    32.12
If you request information that does not exist, you will receive a Selector Not Found message as shown below:
    Named Company GM xyz 
You should see
    >>> Selector 'xyz' Not Found <<< 
          NA 
This message informs you that the company GM does not respond to the message xyz. To determine what messages the company does respond to, send the displayMessages message:
    Named Company GM displayMessages
You will see a list of the messages that have been defined for the Company class. Because all instances of a class respond to the same messages, the expression:
    Company displayMessages
is identical to the previous expression. To access the earnings per share value for GM, execute:
    Named Company GM earningsPerShare
producing the result:
    5.03
Change GM to IBM as shown below:
    Named Company IBM earningsPerShare
You should see:
    8.72
To better identify what information is being printed, modify the previous program as shown below:
    "IBM Earnings Per Share: " print; 
    Named Company IBM earningsPerShare
You should see:
    IBM Earnings Per Share: 8.72
You can do calculations with the information you access. For example, to compute GM's return on equity, execute the following:
    Named Company GM netIncome / Named Company GM commonEquity * 100
You should see:
    10.76
Suppose you want to display several pieces of information for GM in the same program. For example, to look at the components of return on equity followed by the computation, execute:
    Named Company GM netIncome print: 10.3 ; 
    Named Company GM commonEquity print: 10.3 ; 
    Named Company GM netIncome / Named Company GM commonEquity * 100 
You should see:
    3550.900  32988.910  10.76
To make the results of the last program more presentable, try the following modifications:
    "Net Income" print: 30 ; 
    Named Company GM netIncome printNL: 10.3 ; 
    "Common Equity" print: 30 ; 
    Named Company GM commonEquity printNL: 10.3 ; 
    "Return On Equity" print: 30 ; 
    Named Company GM netIncome / Named Company GM commonEquity 
    * 100 printNL: 10.3 ; 
Your should see:
    Net Income              3550.900
    Common Equity          32988.910
    Return On Equity          10.764
The print: 30 message to the strings causes them to be padded with blank spaces so that the labels are all the same length. The printNL: message is used to separate each item into its own row.

Access Shortcuts

By this time, you have probably become tired of typing Named Company GM over and over again. Several techniques are available in Vision to help streamline your programs.

You can create a variable that represents the company GM. To do this type:

    !gm <- Named Company GM
The variable gm will remain available until you terminate your session. The expression:
    gm sales
can now be used to access GM's latest sales value and the program:
    "Net Income" print: 30 ; 
    gm netIncome printNL: 10.3 ; 
    "Common Equity" print: 30 ; 
    gm commonEquity printNL: 10.3 ; 
    "Return On Equity" print: 30 ; 
    gm netIncome / gm commonEquity * 100 printNL: 10.3 ;
can be used to compute and display GM's return on equity calculation. Variables can be used to save the results of calculations. For example, a variable can be created to store the results of the roe calculation:
    !roeGM <- gm netIncome / gm commonEquity * 100 ;
The previous program could then be rewritten as:
    gm netIncome print: 10.3 ; 
    gm commonEquity print: 10.3 ; 
    roeGM 
By creating the variables gm and roeGM you were able to eliminate some typing in the previous examples. Unfortunately, this approach is only useful if you are working the company GM. The do: message provides a more general way to streamline your program.

Type the following:

    gm 
    do: [ name print: 30 ; 
          sales print: 10 ; 
          netIncome print: 10 ; 
        ] ;
You should see:
    General Motors Corp          101781.00   3550.90
The do: message can be used to "factor out" the repeated expression in the body of your program, in this case the gm. The do: message requires one parameter. This parameter is enclosed by square brackets [ ]. All expressions inside the brackets are evaluated for the company GM. Any valid Vision expression can be included inside the brackets. The previous example is identical to:
    Named Company GM 
    do: [ name print: 30 ; 
          sales print: 10 ; 
          netIncome print: 10 ; 
        ] ; 
In addition to sales, suppose that you wished to calculate the price-earnings ratio for GM by dividing the price by the earnings per share value. The program:
    Named Company GM 
    do: [ name print: 30 ; 
          sales print: 10 ; 
          price / earningsPerShare print:10 ; 
        ] ; 
produces the result:
    General Motors Corp        101781.00   6.39
To find the same information for IBM, change GM to IBM as shown below:
    Named Company IBM 
    do: [ name print: 30 ; 
          sales print: 10 ; 
          price / earningsPerShare print:10 ; 
        ] ; 
You should see:
    IBM Corp     54217.02    9.26
To find the same information for Exxon, change IBM to XON as shown below:
    Named Company XON 
    do: [ name print: 30 ; 
          sales print: 10 ; 
          price / earningsPerShare print:10 ; 
        ] ; 
You should see:
    Exxon Corp       76416.00       18.62


Developing Programs

You saw in the previous example that the do: message allows you to develop a program for one company and easily modify it to work with another one. By combining this streamlined access mechanism with various formatting techniques, you are ready to build substantial programs.

Start by labeling the items displayed in the previous report:

Named Company GM 
do: [ 
     "Sales" print: 30 ; 
      sales printNL: 10 ; 
     "Price-Earning Ratio" print: 30 ; 
     price / earningsPerShare printNL: 10 ; 
    ] ; 
producing the result:
    Sales                101781.00
    Price-Earning Ratio       6.39

To display net income as the second line item in this report, modify the previous program as follows:

    Named Company GM 
    do: [ "Sales" print: 30 ; 
          sales printNL: 10 ; 
          "Net Income" print: 30 ; 
          netIncome printNL: 10 ; 
          "Price-Earning Ratio" print: 30 ; 
          price /earningsPerShare printNL:10 ; 
        ] ; 
producing the result:
    Sales               101781.00
    Net Income            3550.90
    Price-Earning Ratio      6.39
To display the large numbers with commas to make them more readable, modify the program as shown below:
    Named Company GM 
    do: [ "Sales" print: 30 ; 
          sales printWithCommasNL: 10 ; 
          "Net Income" print: 30 ; 
          netIncome printWithCommasNL: 10 ; 
          "Price-Earning Ratio" print: 30 ; 
          price /earningsPerShare printNL:10 ; 
        ] ; 
producing the result:
    Sales                 101,781.00
    Net Income              3,550.90
    Price-Earning Ratio         6.39
Now modify the program to print the company name centered over the forty columns of the report:
    Named Company GM 
    do: [ name centerNL: 40 . print; 
          "Sales" print: 30 ; 
          sales printWithCommasNL: 10 ; 
          "Net Income" print: 30 ; 
          netIncome printWithCommasNL: 10 ; 
          "Price-Earning Ratio" print: 30 ; 
          price /earningsPerShare printNL: 10 ; 
        ] ;
producing the result:
             General Motors  Corp 
    Sales                              101,781.00
    Net Income                           3,550.90
    Price-Earning Ratio                      6.39
Skip an extra line after the company name. As a finishing touch, place a note at the bottom of the output, indicating that you have just completed your first major report:
    Named Company GM 
    do: [ name centerNL: 40 . print; 
          newLine print ; 
          "Sales" print: 30 ; 
          sales printWithCommasNL: 10 ; 
          "Net Income" print: 30; 
          netIncome printWithCommasNL: 10 ; 
          "Price-Earning Ratio" print: 30 ; 
          price /earningsPerShare printNL: 10 ; 
        ] ; 
    newLine print ; 
    "--------------------" printNL ; 
    "My First Report" printNL ; 
producing the result:
            General Motors Corp
    
    Sales                                 101,781.00
    Net Income                              3,550.90
    Price-Earning Ratio                         6.39
    
    -------------------- 
    My First Report
To prepare this report for a different company, just change the GM to the company of your choice.

Creating A Method

It would now be useful to turn this program into a method that you can run without recalling the program each time. To create a method called "firstReport", execute the following:

    Company 
    defineMethod: 
    [ | firstReport | 
      name centerNL: 40 . print; 
      newLine print; 
      "Sales" print: 30 ; 
      sales printWithCommasNL: 10 ; 
      "Net Income" print: 30 ; 
      netIncome printWithCommasNL: 10 ; 
      "Price-Earning Ratio" print: 30 ; 
      price / earningsPerShare printNL: 10 ; 
      newLine print; 
      "--------------------" printNL ; 
      "My First Report Method" printNL ; 
    ] ; 
To run this method, execute:
    Named Company IBM firstReport
You should see:
        IBM Corp
    
    Sales                      54,217.02
    Net Income                  5,258.00
    Price-Earning Ratio             9.26
    
    -------------------- 
    My First Report Method
To run this report for GM, execute:
    Named Company GM firstReport
You should see:
           General Motors Corp 
    
    Sales                                 101,781.00
    Net Income                              3,550.90
    Price-Earning Ratio                         6.39
    
    -------------------- 
    My First Report Method 


Basic Navigations

In the examples so far, you have printed numeric and textual information defined specifically for each company such as name and sales. In Vision, there is no restriction on the type of data you can store for a property. For example, the value for the company GM's industry was defined using the expression:

    Named Company GM :industry <- Named Industry AUTO ;
In a traditional database, the value of industry would probably be stored as a string or number in a company table. This value would then be used to query an industry table in order to access information about a company's industry. In Vision, the object representing the company's industry is stored directly as the value of the industry property. As a result, the simple expression:
    Named Company GM industry
returns an object of the class Industry that responds all the Industry messages including name. The expression:
    Named Company GM industry name
accesses the name of GM's industry. The expression:
    Named Company GM industry whatAmI
informs you that the object is of the class Industry and the expression:
    Named Company GM industry displayMessages
displays the list of messages that are defined for industry objects. Notice that one of the messages is the sector message. In the sample database, the value for the AUTO industry's sector was defined using:
    Named Industry AUTO :sector <- Named Sector DUR ;
This expression assigns the object representing the Durable Goods sector to the AUTO industry's property. Since all sectors respond to the message name, the expression:
    Named Company GM industry sector name
displays the result:
    Durables

Related Topics