BPJ

BPJ

BPJ is a behavioral programming library for Java. A BPJ program consists of behavioral bthreads, a.k.a BThreads. Each such thread is an instance of a class that extends bp.BThread and overrides the runBThread() method. BThreads collaborate by calling the bSync() method...

In each iteration of the behavioral execution cycle :

  1. All b-threads synchronize and place their "bids":
    • Requesting an event: proposing that the event be considered for triggering, and asking to be notified when it is triggered.
    • Waiting for an event: without proposing its triggering, asking to be notified when the event is triggered.
    • Blocking an event: forbidding the triggering of the event, vetoing requests of other b-threads.
  2. An event that is requested and not blocked is selected.
  3. b-threads that requested or wait for the selected event are notified.
  4. The notified b-threads progress to their next states, where they place new bids.

BPJ by examples

Incremental development of a system for controlling water level in a tank with hot and cold water sources. The b-thread 'Add hot' repeatedly requests five times the event AddHot. The b-thread 'Add cold' performs a similar action with the event AddCold, reflecting a separate requirement, which was introduced when adding five water quantities proved to be insufficient. When the two b-threads run simultaneously, with the first at a higher priority, the runs will include five consecutive AddHot events followed by five AddCold events. A new requirement is then introduced, to the effect that water temperature should be kept stable. We add the b-thread Interleave, to interleave AddHot and AddCold events.

Though the below is very close to the working code, the examples should be considered as pseudo-code. Refer to the user guide and the working examples in the downloadable packages for full details.

Add hot

public class RequestFiveAddHotEvents extends BThread {
  public void runBThread() {
    for (int i = 1; i <= 5; i++) {
      bp.bSync(addHot, none, none);
    }
  }
}
  

Add cold

public class RequestFiveAddColdEvents extends BThread {
  public void runBThread() {
    for (int i = 1; i <= 5; i++) {
      bp.bSync(addCold, none, none);
    }
  }
}
  

Interleave

public class Interleave extends BThread {
  public void runBThread() {
    while (true) {
      bp.bSync(none, addHot, addCold);
      bp.bSync(none, addCold, addHot);
    }
  }
}
  

Display events

public class DisplayEvents extends BThread {
  public void runBThread() throws BPJException {
    while (true) {
      bp.bSync(none, all, none);
      System.out.println("turned water tap: " + bp.lastEvent);
    }
  }
}
  

Main method

public class Main {
  public static void main(String[] args) {
    BProgram bp = new BProgram();
    bp.add(new AddHotThreeTimes(), 1.0);
    bp.add(new DisplayEvents(), 2.0);
    bp.add(new AddColdThreeTimes(), 3.0);
    bp.add(new Interleave(), 4.0);
    bp.startAll();
  }
}