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

68
LINES

< > BotCompany Repo | #1035441 // G22Network - a network of objects that together create a recognition

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

Transpiled version (53008L) is out of date.

concept G22Network > ConceptWithChangeListeners {
  delegate Port to G22NetworkElement.
  
  settableWithVar S description;
  Cl<G22NetworkElement> elements = syncL();
  settableWithVar int magneticDistance = 100;
  
  // Is the network safe to auto-calculate?
  settableWithVar bool autoCalculate = true;
  
  toString { ret or2(description, super.toString()); }
  
  // return new connections made
  Cl<G22NetworkCable> doMagneticConnections() {
    //deleteMagneticConnections();

    new L<G22NetworkCable> newCables;
    new L<Port> inputPorts;
    new L<Port> outputPorts;
    
    for (element : elements)
      for (port : element.ports())
        (port.isOutput() ? outputPorts : inputPorts).add(port);
        
    for (port1 : inputPorts) {
      if (port1.isConnected()) continue;
      Rect bounds1 = port1.bounds();
      new Lowest<G22NetworkElement.Port> best;
      
      for (port2 : outputPorts) {
        double distance = rectDistance(bounds1, port2.bounds());
        
        // too far away? skip
        if (distance >= magneticDistance) continue;
        
        // wrong type? skip
        if (!isSubclassOf(port2.dataType(), port1.dataType())) continue;
        
        // candidate found - store with distance
        best.put(port2, distance);
      }
      
      var port2 = best!;
      if (port2 != null) {
        var cable = new G22NetworkCable().from(port2).to(port1);
        newCables.add(cable);
        cable.connect();
        print("Made cable: " + cable);
      }
    }
    
    ret newCables;
  }
  
  void deleteMagneticConnections {
    for (cable : allCables())
      if (cable.isAutomatic())
        cable.remove();
  }
  
  Set<G22NetworkCable> allCables() {
    new Set<G22NetworkCable> set;
    for (element : elements)
      for (port : element.ports())
        addIfNotNull(set, port.cable);
    ret set;
  }
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1035441
Snippet name: G22Network - a network of objects that together create a recognition
Eternal ID of this version: #1035441/21
Text MD5: 1711970151c99b425a160ec90947fc64
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 17:37:27
Source code size: 1934 bytes / 68 lines
Pitched / IR pitched: No / No
Views / Downloads: 44 / 92
Version history: 20 change(s)
Referenced in: [show references]