Difference between revisions of "Water Tap"
From BP Wiki
(→Alternating Taps Example) |
(→Alternating Taps Example) |
||
| Line 1: | Line 1: | ||
= Alternating Taps Example = | = Alternating Taps Example = | ||
| + | Below is the full source code of the alternating water taps example. | ||
| + | |||
| + | == Events == | ||
| + | * addHot | ||
| + | * addCold | ||
| + | |||
| + | == b-Threads == | ||
| + | * addHotThreeTimes: requests addHot 3 times | ||
| + | * addColdThreeTimes: requests addCold 3 times | ||
| + | * Interleave: Causes alternation between addHot and addCold events by blocking one while waiting for the other and vice versa. | ||
| + | |||
| + | |||
| + | == Source Code == | ||
public class AlternatingTaps implements BApplication { | public class AlternatingTaps implements BApplication { | ||
Revision as of 10:06, 15 April 2014
Alternating Taps Example
Below is the full source code of the alternating water taps example.
Events
- addHot
- addCold
b-Threads
- addHotThreeTimes: requests addHot 3 times
- addColdThreeTimes: requests addCold 3 times
- Interleave: Causes alternation between addHot and addCold events by blocking one while waiting for the other and vice versa.
Source Code
public class AlternatingTaps implements BApplication {
@SuppressWarnings("serial")
static class TapEvent extends Event {
public TapEvent(String name) {
this.setName(name);
}
}
static TapEvent addHot = new TapEvent("AddHot");
static TapEvent addCold = new TapEvent("AddCold");
@SuppressWarnings("serial")
public class AddHotThreeTimes extends BThread {
public void runBThread() throws BPJException {
for (int i = 1; i <= 3; i++) {
bp.bSync(addHot, none, none);
}
}
}
@SuppressWarnings("serial")
public class AddColdThreeTimes extends BThread {
public void runBThread() throws BPJException {
for (int i = 1; i <= 3; i++) {
bp.bSync(addCold, none, none);
}
}
}
@SuppressWarnings("serial")
public class Interleave extends BThread {
public void runBThread() throws BPJException {
while (true) {
bp.bSync(none, addHot, addCold);
bp.bSync(none, addCold, addHot);
}
}
}
@SuppressWarnings("serial")
public class DisplayEvents extends BThread {
public void runBThread() throws BPJException {
while (true) {
bp.bSync(none, all, none);
System.out.println("Physically turned water tap per event: " + bp.lastEvent);
}
}
}
public void runBApplication() {
System.out.println("runBApplication () at " + this);
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(); }
static public void main(String arg[]) {
try {
BProgram.startBApplication(AlternatingTaps.class, "bp.unittest");
} catch (Exception e) {
e.printStackTrace();
}
}
}