previous up next
Go backward to 2.1 Executing the Program
Go up to 2 Using the Toolkit
Go forward to 2.3 Asserting Conditions
RISC-Linz logo

2.2 Writing the Program

The program described in the previous section has been implemented by the following pieces of source code compiled with the classes of the DAJ toolkit.

import daj.*;

public class Main extends Application
{
  public static void main(String[] args)
  {
    new Main().run();
  }

  public Main()
  {
    super("A Trivial Program", 400, 300);
  }

  public void construct()
  {
    Node node0 = node(new Prog(0), "0", 100, 100);
    Node node1 = node(new Prog(1), "1", 150, 200);
    Node node2 = node(new Prog(2), "2", 300, 150);
    link(node0, node1);
    link(node1, node2);
    link(node2, node0);
    link(node0, node2);
    link(node2, node1);
    link(node1, node0);
  }

  public String getText()
  {
    return "A Trivial Program\n \n" +
      "2 messages circling bidirectionally\n" +
      "through a ring of three nodes";
  }
}

The DAJ toolkit is contained in a Java package daj; this package has to be imported in the beginning. An application is represented by an object of a subclass of the DAJ class Application. The programmer has to provide in this class a method construct that creates the nodes of the network and defines the corresponding channel connections.

Each network node is created by a call of the function node that takes as its arguments the program executed by the node, the node label shown by the visualizer and the coordinates of the node in the visualization (measured in pixels from the left upper corner of the visualization area).

The default constructor of the class determines the appearance of the visualizer by the title displayed in its frame and by the size of the visualization area. If the program shall be also executable in standalone (non-applet) mode, the class must also contain a method main() that creates the application object and starts its execution by invoking the method run.

The method getText returns the text (a sequence of lines separated by newlines) that it displayed in a window when the user selects the item "About Algorithm" in menu "Help".

Having defined the network, we now are going to implement the program executed by each network node:

class Prog extends Program
{
  int number;

  public Prog(int i)
  {
    number = i; 
  }
 
  public String getText()
  {
    String msgString;
    if (msg == null)
      msgString = "(null)";
    else
      msgString = msg.getText();
    return 
      "sent: " + String.valueOf(sent) +
      "\nmsg: " + msgString;
  }

  public void main()
  {
    if (number == 0)
      {
        out(0).send(new Msg(0));
        out(1).send(new Msg(1));
      }
    for (int i = 0; i < 5; i++)
      {
        int index = in().select();
        Message msg = in(index).receive();
        out(index).send(msg);
      }
  }
}

A node program is an object inheriting from DAJ class Program. In above example, we define a constructor that initializes the program with its index (in order to let the the program exhibit different behaviors on different nodes). The method main represents the code actually executed by the network node. In our example, node 0 sends two messages to its output channels with predefined names out(0) and out(1). Each node runs through three iterations in which it

  1. determines which of its input channels is not empty
    index = in().select()
    
  2. receives the message contained in this channel
    msg = in(index).receive()
    
  3. and sends it to the opposite output channel.
    out(index).send(msg)
    

A node type should override the method getText to exhibit the internal state of each node; the visualizer calls this method when displaying the node state in a pop-up window.

What now remains to be defined is the type of messages sent respectively received via the channels of the network:

class Msg extends Message
{
  int val;
  public Msg(int i)
  {
    val = i;
  }
  public int value()
  {
    return val;
  }
  public String getText()
  {
    return "content = " + String.valueOf(val);
  }
}

A message type must inherit from DAJ class Message and should provide a method getText that returns the content of the message in a single line. This method is invoked by the visualizer when it displays the contents of a channel in a pop-up window.


Maintainer: Wolfgang Schreiner
Last Modification: November 14, 1997

previous up next