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

112
LINES

< > BotCompany Repo | #1035446 // G22NetworkInstance - an instance of a G22Network ready for use in calculation

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

Transpiled version (53257L) is out of date.

1  
transient srecord noeq G22NetworkInstance(G22Network network) {
2  
  new Map<S, Value> valuesByIdentifier;
3  
  new Map<G22NetworkElement, Value> valuesByBlueprint;
4  
  
5  
  event dirtyStatesChanged;
6  
  
7  
  sclass Value {
8  
    settable O object; // the actual value calculated by the node
9  
    settable Timestamp calculatedWhen; // when the value was calculated
10  
    settable volatile bool dirty; // does this node need recalculation?
11  
    settable volatile bool calculating; // are we recalculating right now?
12  
    settable volatile Throwable error; // error during calculation?
13  
    
14  
    O get() { ret object; }
15  
    
16  
    toString { ret classNameWithIdentity(this) + ": " + object; }
17  
    
18  
    bool hasError() { ret error != null; }
19  
  }
20  
  
21  
  void calculateNode(G22Utils g22utils, G22NetworkElement e) {
22  
    if (e == null) ret;
23  
    
24  
    Value value = valueForBlueprint(e);
25  
    
26  
    value.calculating(true);
27  
    try {
28  
      dirtyStatesChanged();
29  
      O instance = e.makeInstance(g22utils, this);
30  
      print("Calculated: " + instance);
31  
      
32  
      value.error(null);
33  
      setObject(e, value, instance);
34  
    } catch print exception {
35  
      value.error(exception);
36  
      value.dirty(false);
37  
    } finally {
38  
      value.calculating(false);
39  
      dirtyStatesChanged();
40  
    }
41  
  }
42  
  
43  
  void setObject(G22NetworkElement e, Value value, O instance) {
44  
    value.object(instance).calculatedWhen(tsNow());
45  
    if (value.dirty()) {
46  
      value.dirty(false);
47  
      dirtyStatesChanged();
48  
    }
49  
    invalidateDependentNodes(e);
50  
  }
51  
  
52  
  void init(G22Utils g22utils) {
53  
    // make empty Value object for each node
54  
    for (e : network.elements) {
55  
      new Value value;
56  
      value.dirty(true);
57  
      dirtyStatesChanged();
58  
      valuesByBlueprint.put(e, value);
59  
      //print("Put value in map: " + e + " => " + value);
60  
      mapPut(valuesByIdentifier, e.identifier(), value);
61  
    }
62  
  }
63  
  
64  
  O getObjectForBlueprint(G22NetworkElement element) {
65  
    var value = valueForBlueprint(element);
66  
    O object = value!;
67  
    printVars("getObjectForBlueprint", +element, +value, +object);
68  
    ret object;
69  
  }
70  
  
71  
  void invalidateDependentNodes(G22NetworkElement element) {
72  
    if (element == null) ret;
73  
    print("invalidateDependentNodes", element);
74  
    for (e : element.downstreamNodes())
75  
      invalidateNode(e);
76  
  }
77  
78  
  // a node is calculable if its upstream nodes (the inputs
79  
  // it needs) are clean
80  
  bool isCalculable(G22NetworkElement element) {
81  
    ret all(element.upstreamNodes(), e -> !isDirty(e));
82  
  }
83  
  
84  
  Value valueForBlueprint(G22NetworkElement element) {
85  
    ret syncGetOrCreate(valuesByBlueprint, element, -> new Value);
86  
  }
87  
  
88  
  bool isDirty(G22NetworkElement element) {
89  
    ret valueForBlueprint(element).dirty();
90  
  }
91  
  
92  
  void invalidateNode(G22NetworkElement element) {
93  
    print("invalidateNode", element);
94  
    
95  
    var value = valueForBlueprint(element);
96  
    if (value.dirty()) ret; // Node already dirty
97  
    
98  
    value.dirty(true);
99  
    dirtyStatesChanged();
100  
    invalidateDependentNodes(element);
101  
  }
102  
  
103  
  // select any calculable node and calculate it
104  
  G22NetworkElement calculateOneNode(G22Utils g22utils) {
105  
    var calculableElements = filter(
106  
      e -> isDirty(e) && isCalculable(e),
107  
      keys(valuesByBlueprint));
108  
    var e = random(calculableElements);
109  
    calculateNode(g22utils, e);
110  
    ret e;
111  
  }
112  
}

Author comment

Began life as a copy of #1035441

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035446
Snippet name: G22NetworkInstance - an instance of a G22Network ready for use in calculation
Eternal ID of this version: #1035446/36
Text MD5: 5e5d19bfd688a42f9e9e805547dbc124
Author: stefan
Category: javax / gazelle 2
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-05-30 16:31:37
Source code size: 3404 bytes / 112 lines
Pitched / IR pitched: No / No
Views / Downloads: 223 / 351
Version history: 35 change(s)
Referenced in: [show references]