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.

transient srecord noeq G22NetworkInstance(G22Network network) {
  new Map<S, Value> valuesByIdentifier;
  new Map<G22NetworkElement, Value> valuesByBlueprint;
  
  event dirtyStatesChanged;
  
  sclass Value {
    settable O object; // the actual value calculated by the node
    settable Timestamp calculatedWhen; // when the value was calculated
    settable volatile bool dirty; // does this node need recalculation?
    settable volatile bool calculating; // are we recalculating right now?
    settable volatile Throwable error; // error during calculation?
    
    O get() { ret object; }
    
    toString { ret classNameWithIdentity(this) + ": " + object; }
    
    bool hasError() { ret error != null; }
  }
  
  void calculateNode(G22Utils g22utils, G22NetworkElement e) {
    if (e == null) ret;
    
    Value value = valueForBlueprint(e);
    
    value.calculating(true);
    try {
      dirtyStatesChanged();
      O instance = e.makeInstance(g22utils, this);
      print("Calculated: " + instance);
      
      value.error(null);
      setObject(e, value, instance);
    } catch print exception {
      value.error(exception);
      value.dirty(false);
    } finally {
      value.calculating(false);
      dirtyStatesChanged();
    }
  }
  
  void setObject(G22NetworkElement e, Value value, O instance) {
    value.object(instance).calculatedWhen(tsNow());
    if (value.dirty()) {
      value.dirty(false);
      dirtyStatesChanged();
    }
    invalidateDependentNodes(e);
  }
  
  void init(G22Utils g22utils) {
    // make empty Value object for each node
    for (e : network.elements) {
      new Value value;
      value.dirty(true);
      dirtyStatesChanged();
      valuesByBlueprint.put(e, value);
      //print("Put value in map: " + e + " => " + value);
      mapPut(valuesByIdentifier, e.identifier(), value);
    }
  }
  
  O getObjectForBlueprint(G22NetworkElement element) {
    var value = valueForBlueprint(element);
    O object = value!;
    printVars("getObjectForBlueprint", +element, +value, +object);
    ret object;
  }
  
  void invalidateDependentNodes(G22NetworkElement element) {
    if (element == null) ret;
    print("invalidateDependentNodes", element);
    for (e : element.downstreamNodes())
      invalidateNode(e);
  }

  // a node is calculable if its upstream nodes (the inputs
  // it needs) are clean
  bool isCalculable(G22NetworkElement element) {
    ret all(element.upstreamNodes(), e -> !isDirty(e));
  }
  
  Value valueForBlueprint(G22NetworkElement element) {
    ret syncGetOrCreate(valuesByBlueprint, element, -> new Value);
  }
  
  bool isDirty(G22NetworkElement element) {
    ret valueForBlueprint(element).dirty();
  }
  
  void invalidateNode(G22NetworkElement element) {
    print("invalidateNode", element);
    
    var value = valueForBlueprint(element);
    if (value.dirty()) ret; // Node already dirty
    
    value.dirty(true);
    dirtyStatesChanged();
    invalidateDependentNodes(element);
  }
  
  // select any calculable node and calculate it
  G22NetworkElement calculateOneNode(G22Utils g22utils) {
    var calculableElements = filter(
      e -> isDirty(e) && isCalculable(e),
      keys(valuesByBlueprint));
    var e = random(calculableElements);
    calculateNode(g22utils, e);
    ret e;
  }
}

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: 66 / 140
Version history: 35 change(s)
Referenced in: [show references]