Difference between revisions of "Water Tap"

From BP Wiki
Jump to: navigation, search
(Created page with "= Alternating Taps Example = public class AlternatingTaps implements BApplication { @SuppressWarnings("serial") static class TapEvent extends Event { public TapEvent(...")
 
(Alternating Taps Example)
Line 1: Line 1:
= Alternating Taps Example =  
+
= Alternating Taps Example =
  
  
 
  public class AlternatingTaps implements BApplication {
 
  public class AlternatingTaps implements BApplication {
@SuppressWarnings("serial")
+
  @SuppressWarnings("serial")
static class TapEvent extends Event {
+
  static class TapEvent extends Event {
  
public TapEvent(String name) {
+
      public TapEvent(String name) {
this.setName(name);
+
        this.setName(name);
}
+
      }
  
}
+
  }
  
static TapEvent addHot = new TapEvent("AddHot");
+
  static TapEvent addHot = new TapEvent("AddHot");
static TapEvent addCold = new TapEvent("AddCold");
+
  static TapEvent addCold = new TapEvent("AddCold");
  
@SuppressWarnings("serial")
+
  @SuppressWarnings("serial")
public class AddHotThreeTimes extends BThread {
+
  public class AddHotThreeTimes extends BThread {
public void runBThread() throws BPJException {
+
      public void runBThread() throws BPJException {
for (int i = 1; i <= 3; i++) {
+
        for (int i = 1; i <= 3; i++) {
bp.bSync(addHot, none, none);
+
            bp.bSync(addHot, none, none);
}
+
        }
}
+
      }
}
+
  }
  
@SuppressWarnings("serial")
+
  @SuppressWarnings("serial")
public class AddColdThreeTimes extends BThread {
+
  public class AddColdThreeTimes extends BThread {
public void runBThread() throws BPJException {
+
      public void runBThread() throws BPJException {
for (int i = 1; i <= 3; i++) {
+
        for (int i = 1; i <= 3; i++) {
bp.bSync(addCold, none, none);
+
            bp.bSync(addCold, none, none);
}
+
        }
}
+
      }
}
+
  }
  
@SuppressWarnings("serial")
+
  @SuppressWarnings("serial")
public class Interleave extends BThread {
+
  public class Interleave extends BThread {
public void runBThread() throws BPJException {
+
      public void runBThread() throws BPJException {
while (true) {
+
        while (true) {
bp.bSync(none, addHot, addCold);
+
            bp.bSync(none, addHot, addCold);
bp.bSync(none, addCold, addHot);
+
            bp.bSync(none, addCold, addHot);
}
+
        }
}
+
      }
}
+
  }
  
@SuppressWarnings("serial")
+
  @SuppressWarnings("serial")
public class DisplayEvents extends BThread {
+
  public class DisplayEvents extends BThread {
public void runBThread() throws BPJException {
+
      public void runBThread() throws BPJException {
while (true) {
+
        while (true) {
bp.bSync(none, all, none);
+
            bp.bSync(none, all, none);
System.out.println("Physically turned water tap per event: " + bp.lastEvent);
+
            System.out.println("Physically turned water tap per event: " + bp.lastEvent);
}
+
        }
}
+
      }
}
+
  }
  
public void runBApplication() {
+
  public void runBApplication() {
System.out.println("runBApplication () at " + this);
+
      System.out.println("runBApplication () at " + this);
bp.add(new AddHotThreeTimes(), 1.0);
+
      bp.add(new AddHotThreeTimes(), 1.0);
// bp.add(new DisplayEvents(), 2.0);
+
  //   bp.add(new DisplayEvents(), 2.0);
bp.add(new AddColdThreeTimes(), 3.0);
+
      bp.add(new AddColdThreeTimes(), 3.0);
bp.add(new Interleave(), 4.0);
+
      bp.add(new Interleave(), 4.0);
  
bp.startAll();
+
      bp.startAll();
}
+
  }
  
static public void main(String arg[]) {
+
  static public void main(String arg[]) {
try {
+
      try {
  
BProgram.startBApplication(AlternatingTaps.class, "bp.unittest");
+
        BProgram.startBApplication(AlternatingTaps.class, "bp.unittest");
  
} catch (Exception e) {
+
      } catch (Exception e) {
e.printStackTrace();
+
        e.printStackTrace();
}
+
      }
}
+
  }
  
 
}
 
}

Revision as of 10:03, 15 April 2014

Alternating Taps Example

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();
     }
  }

}