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).

1  
// "Probabilistic" - a probabilistic runnable
2  
// subclass must implement run {} to do first action and schedule
3  
// next steps with at() or atRelative()
4  
5  
// Creates its own scheduler if none is set
6  
7  
// Steppable is implemented only as convenience
8  
//   (it steps the whole scheduler)
9  
//   TODO: This is not very clear
10  
11  
asclass Probabilistic extends Meta implements IProbabilistic, Steppable {
12  
  IProbabilisticScheduler ps;
13  
14  
  void initScheduler {
15  
    if (ps == null) setScheduler(new ProbabilisticScheduler);
16  
  }
17  
  
18  
  public void setScheduler(IProbabilisticScheduler ps) {
19  
    this.ps = ps;
20  
  }
21  
  
22  
  public IProbabilisticScheduler scheduler() {
23  
    initScheduler();
24  
    ret ps;
25  
  }
26  
  
27  
  // Breaking change: Made this relative
28  
  void schedule aka at(Runnable action) {
29  
    scheduleRelative(1.0, action);
30  
  }
31  
  
32  
  void schedule aka at(double probability, Runnable action) {
33  
    initScheduler();
34  
    ps.at(probability, action);
35  
  }
36  
  
37  
  void scheduleRelative aka atRelative(double probability, Runnable action) {
38  
    initScheduler();
39  
    ps.atRelative(probability, action);
40  
  }
41  
  
42  
  void scheduleAll(Iterable<? extends Runnable> actions) {
43  
    forEach schedule(actions);
44  
  }
45  
  
46  
  void scheduleAll(double probability, Iterable<? extends Runnable> actions) {
47  
    forEach(actions, a -> schedule(probability, a));
48  
  }
49  
  
50  
  void scheduleAllRelative(double probability, Iterable<? extends Runnable> actions) {
51  
    forEach(actions, a -> scheduleRelative(probability, a));
52  
  }
53  
  
54  
  ////// the "weird" convenience stuff
55  
  
56  
  void stepAll() {
57  
    main stepAll(scheduler());
58  
  }
59  
  
60  
  // call run() before
61  
  public bool step() {
62  
    ret scheduler().step();
63  
  }
64  
65  
  void runFor(double seconds) { stepForNSeconds(seconds, scheduler()); }
66  
}

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: 301 / 502
Version history: 32 change(s)
Referenced in: [show references]