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

235
LINES

< > BotCompany Repo | #1035442 // G22NetworkElement - element in a G22Network (also called "blueprint")

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

Transpiled version (54168L) is out of date.

1  
persistable sclass G22NetworkElement > MetaWithChangeListeners {
2  
  // network element belongs to
3  
  settable G22Network network;
4  
  
5  
  // position in visual representation
6  
  settable Rect bounds;
7  
  
8  
  // identifier in network (optional)
9  
  settableWithVar S identifier;
10  
  
11  
  // script code to adapt element and to create instance
12  
  settableWithVar S code;
13  
  
14  
  // general properties map depending on element type
15  
  Map properties = syncMap();
16  
  
17  
  // Is code panel visible in GUI
18  
  settableWithVar bool codeVisible = true;
19  
  
20  
  {
21  
    onChange(-> { network?.change(); });
22  
  }
23  
  
24  
  class Port extends MetaWithChangeListeners is IUnstructured {
25  
    // position on element's border (0 to 1, 0 to 1)
26  
    settableWithVar DoublePt position;
27  
    
28  
    // name of port within element
29  
    settableWithVar S name;
30  
    
31  
    // cable attached to this port (optional)
32  
    settableWithVar G22NetworkCable cable;
33  
    
34  
    // is it output or input?
35  
    settableWithVar bool isOutput;
36  
    
37  
    // type of object sent through port
38  
    settableWithVar Class dataType;
39  
    
40  
    G22NetworkElement element() { ret G22NetworkElement.this; }
41  
    
42  
    toString {
43  
      ret (isOutput ? "Output" : "Input") + " port"
44  
        + " " + quote(name) + " of type " + shortClassName(dataType);
45  
    }
46  
    
47  
    Rect bounds() {
48  
      int size = 10;
49  
      Rect r = element().bounds;
50  
      if (r == null) null;
51  
      r = growRectTopAndLeft(r, size);
52  
      ret rect(r.x+iround(position.x*r.w),
53  
        r.y+iround(position.y*r.h),
54  
        size, size);
55  
    }
56  
    
57  
    void delete() {
58  
      disconnect();
59  
      ports.remove(this);
60  
      change();
61  
    }
62  
    
63  
    void disconnect {
64  
      cable?.remove();
65  
    }
66  
    
67  
    bool isConnected() { ret cable != null; }
68  
    
69  
    public void _doneLoading {
70  
      onChange(-> element().change());
71  
    }
72  
  }
73  
  
74  
  L<Port> ports = syncL();
75  
  
76  
  LASCompileResult newCompileResult() { ret new LASCompileResult; }
77  
  
78  
  transient LASCompileResult compileResult;
79  
  LASCompileResult compile(G22Utils g22utils) {
80  
    S code = unnull(this.code);
81  
    //if (code == null) null;
82  
    var cr = compileResult;
83  
    if (cr != null && eq(cr.script, code)) ret cr;
84  
    cr = newCompileResult();
85  
    var parser = g22utils.leftArrowParser();
86  
    configureParser(parser);
87  
    cr.script(code).parser(parser).compile();
88  
    ret compileResult = cr;
89  
  }
90  
  
91  
  void configureParser(GazelleV_LeftArrowScriptParser parser) {
92  
    parser.addVar("blueprint");
93  
    parser.addVar("instance");
94  
  }
95  
  
96  
  GazelleV_LeftArrowScript.Script compileScript(G22Utils g22utils) {
97  
    var compiled = compile(g22utils);
98  
    if (compiled == null) null;
99  
    if (!compiled.runnable()) null;
100  
    ret compiled!;
101  
  }
102  
103  
  void configureBlueprint(G22Utils g22utils) {  
104  
    var script = compileScript(g22utils);
105  
    if (script == null) ret;
106  
    
107  
    new FlexibleVarContext ctx;
108  
    var f = script.getFunction("configure");
109  
    if (f != null)
110  
      f.call(ctx, this);
111  
  }
112  
113  
  O makeInstance(G22Utils g22utils, G22NetworkInstance networkInstance) {
114  
    var script = compileScript(g22utils);
115  
    if (script == null) null;
116  
    
117  
    new FlexibleVarContext ctx;
118  
    for (port : ports)
119  
      if (!port.isOutput() && port.cable != null) {
120  
        var outputPort = port.cable.from;
121  
        if (outputPort != null)
122  
          ctx.set(port.name, networkInstance.getObjectForBlueprint(outputPort.element()));
123  
      }
124  
        
125  
    ret script.get(ctx);
126  
  }
127  
128  
  // Nodes that we directly depend on
129  
  Set<G22NetworkElement> upstreamNodes() {
130  
    new Set<G22NetworkElement> set;
131  
    for (port : ports)
132  
      if (!port.isOutput() && port.cable != null) {
133  
        var outputPort = port.cable.from;
134  
        if (outputPort != null)
135  
          set.add(outputPort.element());
136  
      }
137  
    ret set;
138  
  }
139  
  
140  
  // Nodes that directly depend on this one  
141  
  Set<G22NetworkElement> downstreamNodes() {
142  
    new Set<G22NetworkElement> set;
143  
    for (port : ports)
144  
      if (port.isOutput() && port.cable != null) {
145  
        var outputPort = port.cable.to;
146  
        if (outputPort != null)
147  
          set.add(outputPort.element());
148  
      }
149  
    ret set;
150  
  }
151  
  
152  
  toString { ret or2(identifier(), "Unnamed element"); }
153  
  
154  
  Port getPort(S name) {
155  
    ret firstThat(ports, p -> eq(p.name, name));
156  
  }
157  
  
158  
  void addPort(Port port) {
159  
    port.onChange(l0 change);
160  
    ports.add(port);
161  
  }
162  
  
163  
  void addPort aka addSlot(S name, DoublePt etc position) {
164  
    printFunctionCall("addPort", +name, +position);
165  
    Port p = getPort(name);
166  
    if (p != null) ret;
167  
    addPort(new Port().name(name).position(position));
168  
    change();
169  
  }
170  
  
171  
  void addInputPort(S name, DoublePt etc position, Class type) {
172  
    Port p = getPort(name);
173  
    if (p != null) ret;
174  
    addPort(new Port().name(name).position(position)
175  
      .isOutput(false).dataType(type));
176  
    change();
177  
  }
178  
  
179  
  void addOutputPort(S name, DoublePt etc position, Class type) {
180  
    Port p = getPort(name);
181  
    if (p != null) ret;
182  
    p = new Port().name(name).position(position)
183  
      .isOutput(true).dataType(type);
184  
    addPort(p);
185  
    print("Made port " + p);
186  
    change();
187  
  }
188  
  
189  
  void removePorts aka deletePorts aka removeSlots aka deleteSlots() {
190  
    while (nempty(ports))
191  
      last(ports).delete();
192  
  }
193  
  
194  
  void removeOutputPorts {
195  
    for (p : filter(ports, p -> p.isOutput()))
196  
      p.delete();
197  
  }
198  
  
199  
  void removeInputPorts {
200  
    for (p : antiFilter(ports, p -> p.isOutput()))
201  
      p.delete();
202  
  }
203  
  
204  
  L<Port> ports() { ret cloneList(ports); }
205  
  
206  
  // key and value must be persistable!
207  
  void setProperty(O key, O value) { properties.put(key, value); change(); }
208  
  
209  
  O getProperty(O key) { ret properties.get(key); }
210  
  
211  
  void autoCreateOutputPort(G22NetworkInstance.Value myValue) {
212  
    if (myValue == null) ret;
213  
    removeOutputPorts();
214  
    O o = myValue.object();
215  
    Class type = or(_getClass(o), O);
216  
    addOutputPort("output", 1, 0.5, type);
217  
  }
218  
  
219  
  void autoCreateInputPorts(G22Utils g22utils) {
220  
    removeInputPorts();
221  
    var script = compile(g22utils)!;
222  
    int n = l(script.params);
223  
    int i = 0;
224  
    fOr (name, valueDescriptor : script.params) {
225  
      double y = doubleRatio(++i, (n+1));
226  
      addInputPort(name, 0, y, or(valueDescriptor.javaClass(), O));
227  
    }
228  
  }
229  
  
230  
  selfType defaultName(S name) {
231  
    if (empty(identifier()))
232  
      identifier(name);
233  
    this;
234  
  }
235  
}

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: #1035442
Snippet name: G22NetworkElement - element in a G22Network (also called "blueprint")
Eternal ID of this version: #1035442/66
Text MD5: efa8535376e6a0bbacc7259e35358bf5
Author: stefan
Category: javax / gazelle 22
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-06-01 16:12:17
Source code size: 6471 bytes / 235 lines
Pitched / IR pitched: No / No
Views / Downloads: 179 / 357
Version history: 65 change(s)
Referenced in: [show references]