Version 3 of flow-based programming

Updated 2002-06-06 08:35:41

Flow-based Programming, J. Paul Morrison, Van Nostrand Reinhold, NY 1994

Roy Terry recommends this as not only illuminating in its own right, but "amenable to Tcl implementation".

Arjen Markus I was inspired by a book on software architecture (Mary Shaw and David Garlan: Software Architecture, perspectives on an emerging discipline) to start thinking about implementing "pipelines" in Tcl. Consider the following example: we want to remove all comment lines from a C source file. One way to do this, is to read in the file, filter out those lines and pieces of text that belong to a comment and write out the remainder.

In one line:

   output [nocomments [readfile $input_name]] $output_name


   set content          [readfile $input_name]
   set filtered_content [nocomments $content]
   output $filtered_content $output_name

So, you get a more or less complicated chain of commands, the one taking the output from the previous one.

The drawbacks of this approach are:

  • You need to keep the whole file in memory and possibly one or more processed copies.
  • You need to wait for the whole file to be read or processed before the next step can proceed.

What, on the other hand, if we do it like this:

   set infile  [open "$input_name"  "r"]
   set outfile [open "$output_name" "w"]
   while {[readline $infile line] >= 0 } {
      set filtered_line [nocomments $line]
      outputline $filtered_line $outfile
   close $infile
   close $outfile

Well, this solves the first objection to the previous method, but the second is changed into:

  • You need to wait for output to become available, if the input file is the output from a separate process or a socket

Two new disadvantages are added, which may even be more serious:

  • How do you handle the case where [nocomments] filters out the entire line? This requires additional logic, probably in the outer loop.
  • What if one line of input causes multiple lines of output from [nocomments] (or some other command, say a pretty printer)?

The book on flow-based programming by J. Paul Morrison describes a method of thinking about programs and designing them that deals with precisely the above difficulties. The solution is to have the input items (lines in this case) generate events that are then treated in order of generation by other tasks.

How can we achieve this in Tcl? Let us keep it simple - we want to show the principle, so the filtering is: convert everything to upper case.

(To be continued ...)

RS still wonders how this differs from Streams � la SICP...