Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

66
LINES

< > BotCompany Repo | #1032157 // Probabilistic - abstract implementation of IProbabilistic, e.g. a probabilistic runnable (=schedulable in a probabilistic scheduler)

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (6795L/39K).

// "Probabilistic" - a probabilistic runnable
// subclass must implement run {} to do first action and schedule
// next steps with at() or atRelative()

// Creates its own scheduler if none is set

// Steppable is implemented only as convenience
//   (it steps the whole scheduler)
//   TODO: This is not very clear

asclass Probabilistic extends Meta implements IProbabilistic, Steppable {
  IProbabilisticScheduler ps;

  void initScheduler {
    if (ps == null) setScheduler(new ProbabilisticScheduler);
  }
  
  public void setScheduler(IProbabilisticScheduler ps) {
    this.ps = ps;
  }
  
  public IProbabilisticScheduler scheduler() {
    initScheduler();
    ret ps;
  }
  
  // Breaking change: Made this relative
  void schedule aka at(Runnable action) {
    scheduleRelative(1.0, action);
  }
  
  void schedule aka at(double probability, Runnable action) {
    initScheduler();
    ps.at(probability, action);
  }
  
  void scheduleRelative aka atRelative(double probability, Runnable action) {
    initScheduler();
    ps.atRelative(probability, action);
  }
  
  void scheduleAll(Iterable<? extends Runnable> actions) {
    forEach schedule(actions);
  }
  
  void scheduleAll(double probability, Iterable<? extends Runnable> actions) {
    forEach(actions, a -> schedule(probability, a));
  }
  
  void scheduleAllRelative(double probability, Iterable<? extends Runnable> actions) {
    forEach(actions, a -> scheduleRelative(probability, a));
  }
  
  ////// the "weird" convenience stuff
  
  void stepAll() {
    main stepAll(scheduler());
  }
  
  // call run() before
  public bool step() {
    ret scheduler().step();
  }

  void runFor(double seconds) { stepForNSeconds(seconds, scheduler()); }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx

No comments. add comment

Snippet ID: #1032157
Snippet name: Probabilistic - abstract implementation of IProbabilistic, e.g. a probabilistic runnable (=schedulable in a probabilistic scheduler)
Eternal ID of this version: #1032157/33
Text MD5: 2eeb85a4fed22e52747d9a3628ea6047
Transpilation MD5: bb22e76ce98c5656f5754219fd6f708c
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-25 03:15:18
Source code size: 1784 bytes / 66 lines
Pitched / IR pitched: No / No
Views / Downloads: 221 / 398
Version history: 32 change(s)
Referenced in: [show references]