home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Environments / Prograph Classic 2.6.1 / Prograph Tutorial Manual / Prograph Tutorial / Prograph Tutorial.rsrc / TEXT_157.txt < prev    next >
Encoding:
Text File  |  1995-10-16  |  13.7 KB  |  182 lines

  1.  
  2. t    Method Super Operations
  3.  
  4. Sometimes you want to have it both ways. You want a subclass to exhibit the behavior of its parent, but you want to add something new. The*215* super annotation  *1025*of method operations gives you this ability to inherit method behavior and then add to it. 
  5.  
  6. Assume that dogs sleep like the rest of the barnyard‚Äôs animals. In addition, dogs enter a dream state in which they sniff out and chase imagined rabbits. A Super annotation in an overshadowing version of the sleep method in the Dog class lets Dog instances dream when they sleep.
  7.  
  8. u    Copy the sleep method from the Animal class and paste it into the Dog Methods window.
  9.  
  10. u    Open the Dog/sleep method and select all the operations except the input and output bars. Select Opers to Local from the Opers menu. Name the resulting local method ‚Ķand then dreams. Before editing this local to implement the dream state, you must first instruct the dog to sleep.
  11.  
  12. u    Create an operation and label it //sleep. Attach its left terminal to the left root of the input bar and its right terminal to the right root of the input bar. If you asked a Dog instance to sleep with this method in its current state, an infinite loop would result from the sleep method calling itself over and over until you issue a Break and an Abort command.
  13.  
  14. ___________________________________________________________
  15. NOTE:  You may have to move aside the comments on the two terminals of the sleep operation in order to see the terminals. Alternatively, Command-click the terminals to toggle their comments from being shown to being hidden.
  16. -----------------------------------------------------------
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26. u    Click on the //sleep operation and then select the*1026**592* Super item in the Controls menu. Your Dog/sleep method should now look like this:
  27.  
  28. The Super annotation tells Prograph to look for the sleep method, not in the Dog class but in the Animal class, the parent of the Dog subclass. If Prograph does not find the method in the parent of the current class, it continues up the inheritance line until it finds a method by the indicated name.
  29.  
  30. The Super annotated //sleep method operation produces an entry such as "Rover is sleeping." which is added to the DiaryEntry record of events. To complete the Dog/sleep method:
  31.  
  32. u    Select the //sleep operation, and then Option-click the ‚Ķand then dreams local operation to create a synchro link. This ensures that the Dog instance sleeps before it dreams.
  33.  
  34. u    Open the ‚Ķand then dreams local operation. Open a Change Value dialog on the string constant that is fed into the "join" operation. Change the " is sleeping." value to " enters a dream state and twitches as it sniffs and chases an imaginary rabbit."
  35.  
  36. u    Switch the order of the joined strings in the second "join" operation so the dream sequence is appended to the entry in the diary that notes that the Dog instance is sleeping.
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60. u    After editing the local, close its case window and turn Step/Show On for the Dog/sleep method.
  61.  
  62. u    To test the Dog/sleep method without waiting for a random sleeping Dog event in the Barnyard Simulation, create a Dog/testSleep method. Note the input to the Dog instance generator is a list of (attributeName value) lists‚Äîthus the double parentheses, ((name Rover)), as this list only has one list element. Be sure to include the synchro so the /sleep operation executes before the what happened attribute is retrieved from the DiaryEntry for display.
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78. u    Execute the Dog/testSleep method. An execution window on Dog/sleep opens as soon as Dog/testSleep calls it. Press Return to step execution to the Super annotated //sleep operation. Press Shift-Return to ‚Äústep into‚Äù the call to Animal/sleep. Open the Stack window to confirm the progression of method executions.
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104. u    Continue pressing Return to follow the flow of execution. After both Animal/sleep and Dog/sleep are resolved, Dog/testSleep finishes execution with the display of a Value window on the what happened attribute of the active DiaryEntry. The entry reads "Rover is sleeping. Rover enters a dream state and twitches as it sniffs and chases an imaginary rabbit."
  105.  
  106. ____________________________________________________
  107. NOTE:  Super annotations are most often used to incorporate the behavior of a parent‚Äôs version of a method within the overshadowing version of the method in the parent‚Äôs subclasses. A typical example is the use of the Super annotation to incorporate the behavior of the Animal/sleep method into the behavior of Dog/sleep.
  108.  
  109. Super annotations, however, can be used in a wide range of situations. A Super annotation can be used any time an instance should behave in a manner consistent with one of its parent‚Äôs methods rather than behave as it would if its own overshadowed version of a method were executed. 
  110.  
  111. An example is an object-oriented expert system that understands the domain of rocks and minerals. A subclass of mineral can behave one way at room temperature but behave like an instance of its parent class when heated. A method to assay this subclass of mineral can use case structure to test for temperature. The first case, which executes when the mineral is at room temperature, can use a method call, /shatterPressure, within the mineral subclass‚Äôs own collection of methods. A second case of the assaying method can execute when high temperatures are detected. This second case uses a Super annotated call to //shatterPressure, the parent‚Äôs version of this method. The range of the possible uses of the super annotation is beyond the scope of this tutorial. The mechanism itself and how you annotate a method-calling operation in the editor is the same for all such applications.
  112. -----------------------------------------------------------
  113.  
  114. u    Before running the Barnyard simulation, copy and paste the Dog/sleep method to the Cat class and change the chase to mouse from rabbit.
  115.  
  116. As you run the Barnyard Simulation, you see increasingly diverse and descriptive entries in the diary.
  117.  
  118. t    Initialization, Get, and Set Methods
  119.  
  120. In addition to the basic attribute and method varieties, there are three special cases of overshadowing:  Instance generation, Get attribute, and Set attribute overshadowing. Reflecting the special nature of these situations, visually distinctive icons*410* *411**420*are used in the Methods windows of classes to identify methods that implement these features.
  121.  
  122.  
  123.  
  124.  
  125. The shapes of these special method icons*977**388* are complementary to the shapes of the primitive operation icons that these methods overshadow. These unique icons are created by selecting the Instance, Get, or Set items in the Opers menu while a method icon is selected in a Methods window. 
  126.  
  127.  Overshadowing Instance Creation
  128.  
  129. An overshadowing Instance generator is assigned the special name, <<>>, automatically by the editor. You give overshadowed*386* Get and *975*Set attribute methods the name of the attribute that they are to overshadow.
  130.  
  131. The best way to further explain what these special methods are and how they work is by example. Let‚Äôs assume the research project landed some additional funding from the International Cattle Association (ICA). The ICA requires the researchers to report on the size of the cattle herds in the Barnyard they are studying. The reporting requirement is simply that the researchers telephone ICA headquarters once a year to report the number of cows, by species, owned by Farmer Brown.
  132.  
  133. To avoid having to sort through all the animals inhabiting the Barnyard at reporting time, use the class attribute herdMembers and overshadow the instance generation of Cow and, by inheritance, its subclass instances.
  134.  
  135. u    Open the Cow Methods window and Command-click to create a new method icon*482*. Select Instance from the Opers menu to transform the unnamed method into the instance-generation overshadowing*817* (*481**688*Initialization) method, which the editor automatically names <<>> .
  136.  
  137. ___________________________________________________________
  138. NOTE:  The*6* <<>> method (it is convenient to read and think of this as the ‚Äúinitialization‚Äù method) can be thought of as a user-defined automatic complement to the Instance primitive operation. That is why the <<>> method has a single root and terminal on its input and output bar. Once this method is defined in a class, <<>> is called anywhere there is an Instance primitive operation that generates an instance of the class in which the <<>> method is defined. Since the Instance primitive has a terminal and root on its operation icon, the <<>> method must maintain both the arity (the same number of roots and terminals) and the functionality of the Instance primitive operation that it overrides. Like its primitive counterpart, you can pass a list of (attributeName value) lists into the <<>> method to set the values of attributes of the instance that is being created. Once inside the <<>> method, your overshadowing method can do any additional processing you want to tailor the act of instance creation. It is then up to you to provide the datalink to output the massaged instance through the terminal of the <<>> output bar. If the newly created instance is not passed out the <<>> method‚Äôs output bar, the <<>> method produces a NONE data value along any datalink attached to its output root. You rarely want this to occur.
  139.  
  140. Instance generation overshadowing by means of an initialization method is typically used to do type checking on values that are to be set for attributes. You might want to make sure that an integer has been entered to a prompt for the wingspan of a Bird instance. Or you might want to check for capitalization of the name attributes of Animal instances, and so on.
  141. -----------------------------------------------------------
  142.  
  143. When an instance of Cow or, by method inheritance, an instance of one of its subclasses, is generated, the new Cow instance joins the herd by being added to the list maintained in the class‚Äôs herdMembers class attribute. The object that flows into the <<>> method of Cow or its subclass is the new instance being created. Its name, age, and favorite food attribute values are assigned based on user input to the prompts that appear when the farmer goes to the auction every fourth iteration of the Barnyard/action method.
  144.  
  145. u    Open the Cow attribute window and click to select the herdMembers class attribute. Open a Value window on this attribute and set its default data type to list. Dismiss the Value window and, with the herdMembers attribute icon still selected, choose Propagate Attribute from the Edit menu. This sets the herdMembers attribute default value of the Cow subclasses to an empty list like their parent. 
  146.  
  147. u    Define the Cow/<<>> method so it looks like the following:
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161. This <<>> method simply passes the newly created instance through from the input root to the output terminal. But before it does that, <<>> gets the current value of the new instance‚Äôs herdMembers attribute and attaches the new instance to the end of the list before setting herdMembers to this updated list value. This means that a separate herd is maintained in the Cow class and its subclasses.
  162.  
  163. u    To bring the Barnyard cows together in a herd‚Äîthat is, to copy any Cow and its subclass instances from the Barnyard/inhabitants attribute to the herdMembers attribute of each of the Cow classes‚Äîselectively load the cowRoundUp universal method from the Chapter 6 Helpers, in the Ch 6 Δí folder (inside the Tutorial Examples Δí folder). Execute cowRoundUp.
  164.  
  165. u    Open a Value window on the herdMembers attribute in the Cow, Guernsey, and Black Angus classes to confirm the successful roundup. If no Cow or its subclass instances currently exist in your current Barnyard instance, open the inhabitants attribute in the Barnyard instance in Barnyard/activeYards and add a couple of Guernsey and Black Angus instances before going further. Execute cowRoundUp again and confirm that you now have some cows in your herdMember attributes. (You can delete cowRoundUp when you are done with this preliminary step, since Cow/<<>> updates the herd incrementally as new instances are created.)
  166.  
  167. With the Initialization method in the Cow class keeping an up-to-date list of members of the herds of the various species of cow, you now only need to add a reporting method to satisfy the ICA-funded program:
  168.  
  169. u    Open the Universal Methods window and then selectively load the herd report method from the Chapter 6 Helpers file.
  170.  
  171. The herd report method accesses the herdMembers attribute of the Cow class and its subclasses to determine the number of cows in each species herd. If you poke around a bit in this method you discover that case structure and various control annotations are used to prepare a grammatically appropriate report to ICA headquarters. For example, herd report produces such syntactic constructions as the phrases "There are no cows‚Ķ", "There is 1‚Ķ", and "There are 3‚Ķ".
  172.  
  173. The researchers decide to schedule the annual ICA telephone report as an additional activity during the annual birthday celebration and trip to the animal auction.
  174.  
  175. u    To incorporate the ICA report into the annual birthday and auction attendance event, create a herd report operation in the birthday & buy? local method deeply nested in the Barnyard/action method.
  176.  
  177. u    Run the Barnyard Simulation for several time periods. Buy a Guernsey and Black Angus cow at your next two auctions.
  178.  
  179. Your annual birthday party and trip to the auction is accompanied by an ICA report display.
  180.  
  181. Certainly a simple integer-count attribute equal to the number of instances of each of the Cow classes would be more efficient for storage and reporting. The researchers, however, implemented the herdMembers instance list design with future reporting requirements in mind. As an informal challenge, consider extending the herd report method to compute an average age summary in addition to the total herd counts currently reported.
  182.