home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / viscobv6.zip / vac22os2 / ibmcobol / samples / toolkit / rexx / pipe.cmd < prev    next >
OS/2 REXX Batch file  |  1996-11-19  |  10KB  |  129 lines

  1. /******************************************************************************/
  2. /*  pipe                     Object REXX Samples                              */
  3. /*                                                                            */
  4. /*  A pipeline implementation                                                 */
  5. /*                                                                            */
  6. /*  This program demonstrates the use of ::class and ::method directives to   */
  7. /*  create a simple implementation of a CMS-like pipeline function.           */
  8. /******************************************************************************/
  9. ::class filter public                       /* base filter class              */
  10.                                                                                    
  11. ::method '[]' class                         /* create a "chain" of filters    */
  12. start = self~new                            /* create a new filter            */
  13. current = start                             /* current is the starting filter */
  14. do i = 1 to arg()                           /* loop through the other filters */
  15.   next = arg(i)                             /* get the next filter            */
  16.   next = next~new                           /* create a new instance (maybe)  */
  17.   current~next = next                       /* chain this up                  */
  18.   current = next                            /* this is now the current one    */
  19. end                                                                                
  20. return start                                /* return the chain start         */
  21.                                                                                    
  22. ::method go                                 /* execute using a provided object*/
  23. expose source                               /* get the source supplier        */
  24. use arg source                              /* set to the supplied object     */
  25. self~begin                                  /* now go feed the pipeline       */
  26.                                                                                    
  27. ::method next attribute                     /* next stage of the filter       */
  28. ::method source attribute                   /* source of the initial data     */
  29.                                             /* that they are class objects for*/
  30. ::method new                                /* the filter chaining process    */
  31. return self                                 /* just return ourself            */
  32.                                                                                    
  33. ::method begin                              /* start pumping the pipeline     */
  34. expose source next                          /* access the data and next chain */
  35.                                                                                    
  36. engine = source~supplier                    /* get a data supplier            */
  37. do while engine~available                   /* while more data                */
  38.   next~process(engine~item)                 /* pump this down the pipe        */
  39.   engine~next                               /* get the next data item         */
  40. end                                                                                
  41. next~done                                   /* signal that we're finished     */
  42.                                                                                    
  43. ::method process                            /* default data processing        */
  44. use arg value                               /* get the data item              */
  45. self~next~process(value)                    /* send this down the line        */
  46.                                                                                    
  47. ::method done                               /* process "end-of-pipe" condition*/
  48. use arg value                               /* get the data item              */
  49. self~next~done                              /* send this down the line        */
  50.                                                                                    
  51. ::class sorter public subclass filter       /* sort piped data                */
  52. ::method init                               /* sorter initialization method   */
  53. expose items                                /* list of sorted items           */
  54. items = .list~new                           /* create a new list              */
  55. self~init:super                             /* forward the initialization     */
  56.                                                                                    
  57. ::method process                            /* process sorter piped data item */
  58. expose items                                /* access internal state data     */
  59. use arg value                               /* access the passed value        */
  60. index = items~first                         /* get the first list item        */
  61. do while .nil <> index                      /* while we still have an index   */
  62.   if items[index] > value then do           /* found the insertion point?     */
  63.                                             /* insert this                    */
  64.     items~insert(value, items~previous(index))
  65.     return                                  /* we're finished                 */
  66.   end  
  67.   index = items~next(index)                 /* step to the next item          */
  68. end                                                                                
  69. items~insert(value)                         /* add this item to the end       */
  70.                                                                                    
  71. ::method done                               /* process the "end-of-pipe"      */
  72. expose items                                /* expose the list                */
  73. index = items~first                         /* get the first item             */
  74. do while .nil <> index                      /* while we still have items      */
  75.   self~next~process(items[index])           /* send along this item           */
  76.   index = items~next(index)                 /* step to the next item          */
  77. end                                                                             
  78.                                                                                    
  79. ::class reverser public subclass filter     /* a string reversal filter       */
  80. ::method process                            /* filter processing item         */
  81. use arg value                               /* get the data item              */
  82. self~next~process(value~reverse)            /* send it along in reversed form */
  83.                                                                                 
  84.                                             /* terminal filter item           */
  85. ::class displayer subclass filter public    
  86. ::method process                            /* process a data item            */ 
  87. use arg value                               /* get the data value             */ 
  88. say value                                   /* display this item              */ 
  89.                                                                                     
  90. ::method done                               /* all finished                   */ 
  91. return                                      /* this is a terminal node        */ 
  92.                                                                                    
  93. ::class selector public subclass filter     /* a string selector filter       */ 
  94. ::method init                               /* process initial strings        */ 
  95. expose patterns                             /* access the exposed item        */ 
  96. patterns = arg(1,'a')                       /* get the patterns list          */ 
  97. self~init:super                             /* forward the initialization     */ 
  98.                                                                                     
  99. ::method '[]' class                         /* initialize a selector filter   */ 
  100. use arg a                                   /* just create a new item         */ 
  101. return self~new(a)                
  102.  
  103. ::method process                            /* process a selection filter     */
  104. expose patterns                             /* expose the pattern list        */
  105. use arg value                               /* access the data item           */
  106. do i = 1 to patterns~size                   /* loop through all the patterns  */
  107.                                             /* this pattern in the data?      */
  108.   if (value~pos(patterns[i]) <> 0) then do
  109.     self~next~process(value)                /* send it along                  */
  110.     leave                                   /* stop the loop                  */
  111.   end
  112. end
  113.                                             
  114. ::class stem subclass filter public         /* collect items in a stem        */                                       
  115. ::method init                               /* initialize a collector         */
  116. expose stem.                                /* expose target stem             */
  117. use arg stem.                               /* get the stem variable target   */
  118. stem.0 = 0                                  /* start with zero items          */
  119. self~init:super                             /* forward the initialization     */
  120.                                                                                    
  121. ::method process                            /* process a stem filter item     */
  122. expose stem.                                /* expose the stem                */
  123. use arg value                               /* get the data item              */
  124. stem.0 = stem.0 + 1                         /* stem the item count            */
  125. stem.[stem.0] = value                       /* save the value                 */
  126.                                                                                    
  127. ::method done                               /* all finished                   */
  128. return                                      /* this is a terminal node        */
  129.