Graph Workbench SPI :: The Basics

Writing algorithm plugins using the Graph Workbench Service Provider Interface.

If you've got this far, it probably means that you want to create your own graphing algorithms using the Graph Workbench application. Hopefully this series of tutorials will explain it well enough to get started.

The first thing you need to do is get your head around the idea that the Graph Workbench application needs to plot a certain color for every coordinate on the graph that maps to a screen pixel. You only need to provide some code that tells it what color to use for that pixel. This is done by creating a class that implements the Algorithm interface, which has a single calculate method.

That's pretty much all there is to it. You don't need to worry about the process of iterating over pixels, calculating graph coordinates or gathering input parameters.

The Basics

Let's start with a simple example, and what can be simpler than drawing a square? Using basic 2D algorithms you would draw as series of lines connecting four points and possibly fill the interior.

But that's not how this works. Remember, the Graph Workbench will call your algorithm once for each pixel, requiring it to return with a color for that pixel. The approach required therefore is to determine whether or not the coordinate is inside or outside the square. The mathematics behind this is trivial - do the x and y coordinate fall within the bounds defined by each corner?

import com.stimware.graphworkbench.spi.Algorithm;
import java.awt.Color;

public class Tutorial_1_1 implements Algorithm {

    public Color calculate(double x, double y) {

        // Is this point within the square?
        if (x > -0.5 && x < 0.5) {
            if (y > -0.5 && y < 0.5) {
                return Color.WHITE;
            }
        }

        return Color.BLACK;
    }
}

You need to do a couple of things to get this code running in the Graph Workbench. Let's break it down into some easy steps:

  1. Compile you source code - This will require the GraphWorkBenchSPI.jar file to be in the classpath.
  2. Package the resulting classes - They need to go into a JAR file in order to be loaded by the Graph Workbench's Plugin Registry. You don't need to create any manifest or config files for this process.
  3. Load the plugin - Within Graph Workbench, open the File menu and select New Graph. This will first open the Plugin Registry to allow you to select an Algorithm plugin. Below the list of Algorithms is a button labeled Load Plugin. Click this button and browse for your JAR file.

Once this is done, your plugin package and all of its Algorithm classes will be listed in the Registry. They will also be available the next time you start the application.