20-CS-4003-001 Organization of Programming Languages Fall 2017
Streams

Lambda calculus, Type theory, Formal semantics, Program analysis

    Previous     Next     All lectures        Code    

Topological Sort

import javax.swing.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.math.*;
import java.util.*;
import java.io.*;
import java.net.*;

// Stream declaration 
class StreamO {
   public StreamO () { isNull = true; }
   public boolean isNull;
   public Object first;
   public StreamO rest () { return null; }
}

// Note: this does not detect cycles
class Node extends StreamO {
   Vector <Node> depends;               // Pointers to dependent objects
   int ndepends,                        // Number of dependent objects
       idx;                             // Index into depends array
   String ident;                        // String identifying this object
   StreamO s;                           // Used in StreamO rest().

   public Node (String id) {
      ident = id;                       // Save identity
      idx = 0;                          // Initial index into depends
      depends = new Vector <Node> ();   // Initialize depends
      ndepends = 0;                     // Number of dependencies is 0
      isNull = false;
   }

   // Set the dependencies - the list of dependencies has been
   // constructed from an input file in "void set_topo_dependencies"
   void requires (Node dependency) {  
      depends.add(dependency); 
      ndepends++; 
   }

   // As long as there are tokens in the current Stream, output them.
   // When the current Stream is closed (rest() returns NULL) switch to
   // the next Stream, in order.  When all Streams are closed, send
   // this object into the stream.
   public StreamO rest () {
      if (isNull) return null;
      while (idx < ndepends && 
             (s = depends.get(idx).rest()) == null) idx++;
      if (idx < ndepends) return s;
      isNull = true;
      return this;
   }
}

class Stream_7Frame extends JFrame implements ActionListener {
   JTextArea text;
   JButton button;
   JComboBox combo;

   public Stream_7Frame () {
      setLayout(new FlowLayout());
      add(new JScrollPane(text = new JTextArea(30,20)));
      text.setFont(new Font("TimesRoman", Font.PLAIN, 18));
      JPanel p = new JPanel();
      p.setLayout(new GridLayout(1,2));
      p.add(combo = new JComboBox());
      p.add(button = new JButton("Press Me"));
      add(p);
      combo.addItem("topo.dat");
      combo.addItem("topo.1.dat");
      combo.addItem("topo.0.dat");
      button.addActionListener(this);
   }

   public void actionPerformed (ActionEvent evt) {
      int count = 0;
      Vector <Node> nodes = new Vector <Node> ();
      Object s;

      text.setText("");

      // Read data from file      
      // Assume format of input file:                        
      //   name1 name2 name3 ...                             
      //   name1_depend1 name1_depend2 ... name1_dependk1    
      //   name2_depend1 name2_depend2 ... name2_dependk2    
      //   ...                                               
      //   namen_depend1 namen_depend2 ... name2n_dependkn   
      try {
         String file = (String)combo.getSelectedItem();
         String dir = 
            "http://gauss.ececs.uc.edu/Courses/c4003/java/";
         String line = null;
         
         URL url = new URL(dir+"DataDriven/Topological/"+file);
         InputStream is = (InputStream)url.getContent();
         ViewFrame vf = new ViewFrame(file,this);
         BufferedReader br = 
            new BufferedReader(new InputStreamReader(is));
         try {
            line = br.readLine();
            vf.append(line);
            StringTokenizer t = new StringTokenizer(line," ");
            while (true) {
               String token = t.nextToken();
               nodes.add(new Node(token));
            }
         } catch (Exception e) { }
         
         count = 0;
         try {
            while (true) {
               line  = br.readLine();
               vf.append(line);
               StringTokenizer t = new StringTokenizer(line," ");
               try {
                  while (true) {
                     int n = Integer.parseInt(t.nextToken());
                     ((Node)(nodes.get(count))).requires(nodes.get(n));
                  }
               } catch (Exception f) { }
               count++;
            }
         }  catch (Exception e) { }
      } catch (Exception e) { 
         text.append("Error: "+e.toString()+"\n");
      }

      // Done reading from file.  Do the topological sort.
      for (int i=0 ; i < count ; i++) {
         while ((s=((Node)(nodes.get(i))).rest()) != null) {
            text.append(((Node)s).ident+"\n");
         }
      }
   }
}

class ViewFrame extends JFrame {
   Stream_7Frame topo;
   JTextArea text;
   
   public ViewFrame (String file, Stream_7Frame t) {
      super(file);
      topo = t;
      add(new JScrollPane(text = new JTextArea(80,24)));
      setSize(500,500);
      setVisible(true);
   }
   
   public void append(String str) {
      text.append(str+"\n");
   }
}

public class stream_7 
   extends Applet implements ActionListener {
   JButton go;

   public void init () {
      setLayout(new BorderLayout());
      setBackground(new Color(255,255,223));
      add("Center", go = new JButton("Applet"));
      go.addActionListener(this);
   }

   public void actionPerformed (ActionEvent evt) {
      Stream_7Frame sf = new Stream_7Frame();
      sf.setSize(400,750);
      sf.setVisible(true);
   }
}
 -  The class Node to the left computes a topological sort of a given partial order of elements. Comments are in blue because there are many of them. The definition of Stream has changed slightly to allow any object to be a member of a stream. This example is rather deep because the number of procedures through with a token must travel to reach the point of demand could be huge. In additon, the rest() procedure is the most complex in this series. The red lines on the left show where the output stream is referenced by s. Walking through s is done as in the previous slide.