forked from JLuebben/Floppy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.py
More file actions
1146 lines (962 loc) · 36.3 KB
/
node.py
File metadata and controls
1146 lines (962 loc) · 36.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from collections import OrderedDict
from copy import copy
from floppy.FloppyTypes import Type, MetaType
NODECLASSES = {}
# STOREDVALUES = {}
class InputNotAvailable(Exception):
pass
class InputAlreadySet(Exception):
pass
def abstractNode(cls: type):
"""
Removes a Node class from the NODECLASSES dictionary and then returns the class object.
Use this as a decorator to stop a not fully implemented Node class from being available in the editor.
:param cls: Node class object.
:return: Unmodified node class object.
:rtype: Node
"""
del NODECLASSES[cls.__name__]
return cls
def Input(*args, **kwargs):
pass
def Output(*args, **kwargs):
pass
def Tag(*args, **kwargs):
pass
class Info(object):
"""
Class for handling all information related to both inputs and outputs.
"""
def __init__(self, name, varType, hints=None, default='', select=None, owner=False, list=False, optional=False):
self.name = name
self.connected = False
self.varType = varType
self.optional = optional
if not hints:
self.hints = [varType.__name__]
else:
self.hints = [varType.__name__] + hints
self.default = default
self.valueSet = False
self.value = None
self.select = select
self.owner = owner
self.list = list
self.loopLevel = 0
self.usedDefault = False
self.pure = 0
def setOwner(self, owner):
self.owner = owner
def setDefault(self, value):
if not self.varType == object and not issubclass(self.varType, Type):
try:
self.default = self.varType(value)
except ValueError:
self.default = ''
if self.varType == bool:
try:
if value.upper() == 'TRUE':
self.default = True
else:
self.default = False
except:
self.default = value
else:
self.default = value
def __str__(self):
return 'INFO'
def reset(self, nodeLoopLevel=0, force=False):
if nodeLoopLevel > self.loopLevel and not force:
# print('Not resetting Input {} because owing node has higher node\n'
# 'level than the node setting the Input: {}vs.{}'.format(self.name, nodeLoopLevel, self.loopLevel))
return
self.default = None
self.valueSet = False
self.value = None
class InputInfo(Info):
def __call__(self, noException=False):
if self.valueSet:
if not self.varType == object:
if isinstance(self.varType, MetaType):
if self.list:
return [self.varType.checkType(i) for i in self.value]
return self.varType.checkType(self.value)
else:
if self.list:
return [self.varType(i) for i in self.value]
return self.varType(self.value)
else:
return self.value
elif self.default != None and not self.connected:
self.usedDefault = True if self.loopLevel > 0 else False
if not self.varType == object and self.default:
return self.varType(self.default)
else:
return self.default
else:
if noException:
return None
else:
raise InputNotAvailable('Input not set for node.')
def set(self, value, override=False, loopLevel=0):
if self.valueSet and not override:
raise InputAlreadySet('Input \'{}\' of node \'{}\' is already set.'.format(self.name, str(self.owner)))
self.value = value
self.valueSet = True
if not self.name == 'Control':
self.loopLevel = loopLevel
def setPure(self):
self.pure = 1
def setConnected(self, value: bool):
self.connected = value
def isAvailable(self):
if self.valueSet:
# print('^^^^^^^^^^^^^^^^^^', self.name, self.value, self.valueSet)
return True
elif self.default != None and not self.connected and not self.usedDefault and self.pure < 2:
if self.pure == 1:
self.pure = 2
# self.usedDefault = True
# print('+++++++++++++++++', self.name, self.value, self.valueSet, self.owner, self.usedDefault, self.pure)
return True
return False
class OutputInfo(Info):
def __call__(self, value):
try:
value.__FloppyType__ = self.varType
except AttributeError:
pass
self.value = value
self.valueSet = True
class MetaNode(type):
"""
Meta class for the Node class. Makes node declaration objects available in the class's scope and registers each
Node object to have a convenient way of accessing all subclasses of Node.
"""
inputs = []
outputs = []
@classmethod
def __prepare__(metacls, name, bases):
MetaNode.inputs = []
MetaNode.outputs = []
MetaNode.tags = []
return {'Input': MetaNode.addInput,
'input': MetaNode.addInput,
'Output': MetaNode.addOutput,
'output': MetaNode.addOutput,
'Tag': MetaNode.addTag,
'tag': MetaNode.addTag}
def addTag(*args):
for arg in args:
MetaNode.tags.append(arg)
def addInput(name: str,
varType: object,
hints=None,
default='',
select=None,
list=False,
optional=False):
MetaNode.inputs.append({'name': name,
'varType': varType,
'hints': hints,
'default': default,
'select': select,
'list': list,
'optional': optional})
def addOutput(name: str,
varType: object,
hints=None,
default='',
select=None,
list=False):
MetaNode.outputs.append({'name': name,
'varType': varType,
'hints': hints,
'default': default,
'select': select,
'list': list})
def __new__(cls, name, bases, classdict):
result = type.__new__(cls, name, bases, classdict)
# result.__dict__['Input'] = result._addInput
NODECLASSES[name] = result
try:
result.__inputs__ = result.__bases__[0].__inputs__.copy()
except AttributeError:
result.__inputs__ = OrderedDict()
try:
result.__outputs__ = result.__bases__[0].__outputs__.copy()
except AttributeError:
result.__outputs__ = OrderedDict()
try:
result.__tags__ = result.__bases__[0].__tags__.copy()
except AttributeError:
result.__tags__ = []
for inp in MetaNode.inputs:
result._addInput(data=inp, cls=result)
for out in MetaNode.outputs:
result._addOutput(data=out, cls=result)
for tag in MetaNode.tags:
result._addTag(tag)
return result
@abstractNode
class Node(object, metaclass=MetaNode):
"""
Base class for Nodes.
To add Inputs to a custom Node class call 'Input(name, varType, hints, list)' in the class's
body e.g.:
class MyNode(Node):
Input('myStringInput', str, list=True)
To access the value of an input during the Node's 'run' method or 'check' method use
'myNodeInstance._myStringInput'. An 'InputNotAvailable' Exception is raised is the input is not set yet.
"""
Input('TRIGGER', object, optional=True)
Tag('Node')
def __init__(self, nodeID, graph):
self.loopLevel = 0
self.__pos__ = (0, 0)
self.graph = graph
self.locked = False
self.subgraph = 'main'
self.ID = nodeID
self.buffered = False
self.inputs = OrderedDict()
self.outputs = OrderedDict()
self.outputBuffer = {}
self.inputPins = OrderedDict()
self.outputPins = OrderedDict()
for i, inp in enumerate(self.__inputs__.values()):
inp = copy(inp)
inp.setOwner(self)
inpID = '{}:I{}'.format(self.ID, inp.name)
newPin = Pin(inpID, inp, self)
self.inputPins[inp.name] = newPin
self.inputs[inp.name] = inp
for i, out in enumerate(self.__outputs__.values()):
out = copy(out)
out.setOwner(self)
outID = '{}:O{}'.format(self.ID, out.name)
newPin = Pin(outID, out, self)
self.outputPins[out.name] = newPin
self.outputs[out.name] = out
self.outputBuffer[out.name] = None
if not self.inputs.keys():
raise AttributeError('Nodes without any input are not valid.')
if len(self.inputs.keys()) == 2:
self.inputs[list(self.inputs.keys())[1]].setPure()
self.setup()
def setup(self):
"""
This method will be called after a node instance is initialized.
Override this to initialize attributes required for custom node behavior.
This way the annoying calls of super(Node, self).__init__(*args, **kwargs) calls can be avoided.
:return:
"""
pass
def __str__(self):
return '{}-{}'.format(self.__class__.__name__, self.ID)
def __hash__(self):
return hash(str(self))
def lock(self):
self.locked = True
def unlock(self):
self.locked = False
self.graph.runningNodes.remove(self.ID)
def run(self) -> None:
"""
Execute the node. Override this to implement logic.
:rtype: None
"""
print('Executing node {}'.format(self))
# print('===============\nExecuting node {}'.format(self))
# print('{} is loopLevel ='.format(str(self)), self.loopLevel,'\n================')
def notify(self):
"""
Manage the node's state after execution and set input values of subsequent nodes.
:return: None
:rtype: None
"""
for con in self.graph.getConnectionsFrom(self):
self.buffered = False
outputName = con['outputName']
nextNode = con['inputNode']
nextInput = con['inputName']
# nextNode.prepare()
if self.outputs[outputName].valueSet:
nextNode.setInput(nextInput, self.outputs[outputName].value, override=True, loopLevel=self.loopLevel)
else:
nextNode.setInput(nextInput, self.outputs[outputName].default, override=True, loopLevel=self.loopLevel)
if not self.graph.getConnectionsFrom(self):
self.buffered = True
for out in self.outputs.values():
self.outputBuffer[out.name] = out.value
[Info.reset(inp, self.loopLevel) for inp in self.inputs.values()]
def setInput(self, inputName, value, override=False, loopLevel=False):
"""
Sets the value of an input.
:param inputName: str representing the name of the input.
:param value: object of the appropriate type for that input.
:param override: boolean specifying whether the input should be overridden if it was set already.
:param looped: boolean. Set to True if the input is set by a looped node. If True, the node becomes a looped
node itself. Defaults to False.
:return: None
"""
self.loopLevel = max([self.loopLevel, loopLevel])
self.inputs[inputName].set(value, override=override, loopLevel=loopLevel)
# print('%%%%%%%%%%%%%%%%', str(self), inputName, value)
def check(self) -> bool:
"""
Checks whether all prerequisites for executing the node instance are met.
Override this to implement custom behavior.
By default the methods returns True if all inputs have been set. False otherwise.
:return: Boolean; True if ready, False if not ready.
:rtype: bool
"""
if self.locked:
return False
if self.buffered and self.outputs.keys():
print('Node {} has buffered output. Trying to notify outgoing connections.'.format(self))
return self.notify()
for inp in self.inputs.values():
if not inp.isAvailable():
if inp.optional and not inp.connected:
continue
# print(' {}: Prerequisites not met.'.format(str(self)))
return False
# print(' {}: ready.'.format(str(self)))
return True
def report(self):
"""
Creates and returns a dictionary encoding the current state of the Node instance.
Override this method to implement custom reporting behavior. Check the ReportWidget documentation for details
on how to implement custom templates.
The 'keep' key can be used to cache data by the editor. The value assigned to 'keep' must be another key of
the report dictionary or 'CLEAR'. If it is a key, the value assigned to that key will be cached. If it is
'CLEAR' the editors cache will be purged. This can be useful for plotting an ongoing stream of data points
in the editor.
"""
return {'template': 'DefaultTemplate',
'class': self.__class__.__name__,
'ID': self.ID,
'inputs': [(i, v.varType.__name__, str(v.value) if len(str(v.value)) < 10 else str(v.value)[:10]+'...') for i, v in self.inputs.items()],
'outputs': [(i, v.varType.__name__, str(v.value) if len(str(v.value)) < 10 else str(v.value)[:10]+'...') for i, v in self.outputs.items()],
'keep': None}
# def prepare(self):
# """
# Method for preparing a node for execution.
# This method is called on each node before the main execution loop of the owning graph instance is started.
# The methods makes sure that artifacts from previous execution are reset to their original states and default
# values of inputs that are connected to other nodes' outputs are removed.
# :return:
# """
# return
# [InputInfo.reset(inp) for inp in self.inputs.values()]
def _addInput(*args, data='', cls=None):
"""
This should be a classmethod.
:param args:
:param data:
:param cls:
:return:
"""
inputInfo = InputInfo(**data)
cls.__inputs__[data['name']] = inputInfo
def _addOutput(*args, data='', cls=None):
"""
This should be a classmethod.
:param args:
:param data:
:param cls:
:return:
"""
outputInfo = OutputInfo(**data)
cls.__outputs__[data['name']] = outputInfo
@classmethod
def _addTag(cls, tag='Node'):
"""
Adds a Tag to a Node class object.
:param tag:
:return:
"""
cls.__tags__.append(tag)
def __getattr__(self, item):
"""
Catches self._<Input/Ouptput> accesses and calls the appropriate methods.
:param item: str; Attribute name.
:return: object; Attribute
:rtype: object
"""
if item.startswith('_') and not item.startswith('__'):
try:
return self.inputs[item.lstrip('_')]()
except KeyError:
try:
return self.outputs[item.lstrip('_')]
except KeyError:
raise AttributeError('No I/O with name {} defined.'.format(item.lstrip('_')))
# raise AttributeError('No Input with name {} defined.'.format(item.lstrip('_')))
else:
return super(Node, self).__getattr__(item)
def getInputPin(self, inputName):
"""
Returns a reference to the Pin instance associated with the input with the given name.
:param inputName: str; Name of the input.
:return: Pin instance
:rtype: Pin
"""
return self.inputPins[inputName]
def getOutputPin(self, outputName):
return self.outputPins[outputName]
def getInputInfo(self, inputName):
return self.inputs[inputName]
def getOutputInfo(self, outputName):
return self.outputs[outputName]
def getInputID(self, inputName):
return '{}:I{}'.format(self.ID, inputName)
def getOutputID(self, outputName):
return '{}:O{}'.format(self.ID, outputName)
def getInputofType(self, varType):
for inp in self.inputs.values():
if issubclass(varType, inp.varType) or issubclass(inp.varType, varType):
return inp
def getOutputofType(self, varType):
for out in self.outputs.values():
if issubclass(varType, out.varType) or issubclass(out.varType, varType):
return out
def save(self):
"""
Returns a dictionary containing all data necessary to reinstanciate the Node instance with the same properties
it currently has. A list of the dictionaries of each node instance in a graph is all the data necessary to
reinstanciate the whole graph.
:return:
"""
inputConns = [self.graph.getConnectionOfInput(inp) for inp in self.inputs.values()]
# print(inputConns)
inputConns = {inputConn['inputName']: inputConn['outputNode'].getOutputID(inputConn['outputName']) for inputConn in inputConns if inputConn}
outputConns = {out.name: self.graph.getConnectionsOfOutput(out) for out in self.outputs.values()}
for key, conns in outputConns.items():
conns = [outputConn['inputNode'].getInputID(outputConn['inputName']) for outputConn in conns]
outputConns[key] = conns
return {'class': self.__class__.__name__,
'position': self.__pos__,
'inputs': [(inputName, inp.varType.__name__, inp(True), inp.default)
for inputName, inp in self.inputs.items()],
'inputConnections': inputConns,
'outputs': [(outputName, out.varType.__name__, out.value, out.default)
for outputName, out in self.outputs.items()],
'outputConnections': outputConns,
'subgraph': self.subgraph}
@classmethod
def matchHint(cls, text: str):
return cls.matchInputHint(text) or cls.matchOutputHint(text) or cls.matchClassTag(text)
@classmethod
def matchClassTag(cls, text: str):
return any([tag.lower().startswith(text) for tag in cls.__tags__])
@classmethod
def matchInputHint(cls, text: str):
if text == 'object':
return True
if any([any([hint.startswith(text) for hint in inp.hints]) for inp in cls.__inputs__.values()]):
return True
@classmethod
def matchOutputHint(cls, text: str):
if text == 'object':
return True
if any([any([hint.startswith(text) for hint in out.hints]) for out in cls.__outputs__.values()]):
return True
class Pin(object):
"""
Class for storing all information required to represent a input/output pin.
"""
def __init__(self, pinID, info, node):
self.ID = pinID
self.ID = pinID
self.name = info.name
self.info = info
info.ID = pinID
self.node = node
@abstractNode
class ProxyNode(Node):
"""
A dummy node without any functionality used as a place holder for subgraphs.
"""
def addProxyInput(self, name, output, input, varType):
pass
def addProxyOutput(self, name, output, input, varType):
pass
@abstractNode
class ControlNode(Node):
"""
Base class for nodes controlling the program flow e.g. If/Else constructs and loops.
Control nodes have an additional control input and a finalize output.
The control input is a special input that supports multiple input connections. For example a loop node gets
notified of a finished iteration over its body by setting the input of the control input. If all iterations are
completed, the last set input is passed to the finalize output.
An If/Else construct uses the control input to notify the node that the selected branch terminated. When that
happens, the value of the control input is set to the finalize output.
Restricting the option to have multiple connections to ControlNodes only makes sure that the node responsible for
branches in the execution tree is also the node responsible for putting the pieces back together.
"""
Input('Start', object)
Input('Control', object)
Output('Final', object)
def __init__(self, *args, **kwargs):
super(ControlNode, self).__init__(*args, **kwargs)
self.waiting = False
class Switch(ControlNode):
"""
Node for creating a basic if/else construction.
The input 'Switch' accepts a bool. Depending of the value of the input, the 'True' or 'False' outputs are set to
the value of the 'Start' input.
As soon as the 'Control' input is set by one of the code branches originating from the 'True' and 'False' outputs,
the value of the 'Final' output is set to the value of the 'Control' input.
"""
Input('Switch', bool)
Output('True', object)
Output('False', object)
Tag('If')
Tag('Else')
def __init__(self, *args, **kwargs):
super(Switch, self).__init__(*args, **kwargs)
self.fresh = True
def check(self):
if self.fresh:
for inp in self.inputs.values():
if inp.name == 'Control':
continue
if not inp.isAvailable():
# print(' {}: Prerequisites not met.'.format(str(self)))
return False
return True
else:
if self.inputs['Control'].isAvailable():
return True
def run(self):
print('Executing node {}'.format(self))
if self.fresh:
if self._Switch:
self._True(self._Start)
else:
self._False(self._Start)
else:
self._Final(self._Control)
def notify(self):
if self.fresh:
output = self.outputs['True'] if self._Switch else self.outputs['False']
for con in self.graph.getConnectionsOfOutput(output):
outputName = con['outputName']
nextNode = con['inputNode']
nextInput = con['inputName']
nextNode.setInput(nextInput, self.outputs[outputName].value)
self.fresh = False
self.inputs['Start'].reset()
self.inputs['Switch'].reset()
else:
output = self.outputs['Final']
for con in self.graph.getConnectionsOfOutput(output):
outputName = con['outputName']
nextNode = con['inputNode']
nextInput = con['inputName']
nextNode.setInput(nextInput, self.outputs[outputName].value)
self.fresh = True
self.inputs['Control'].reset()
#
# class Loop(ControlNode):
# """
# Generic loop node that iterates over a range(x: int)-like expression.
# """
# Input('Iterations', int)
# Output('LoopBody', object)
#
# def __init__(self, *args, **kwargs):
# super(ControlNode, self).__init__(*args, **kwargs)
# self.fresh = True
# self.counter = 0
# self.loopLevel = 0
#
# # def prepare(self):
# # pass
#
# def check(self):
# if self.fresh:
# for inp in self.inputs.values():
# if inp.name == 'Control':
# continue
# if not inp.isAvailable():
# # print(' {}: Prerequisites not met.'.format(str(self)))
# return False
# return True
# if self.counter > 0:
# if self.inputs['Control'].isAvailable():
# return True
#
# def run(self):
# print('Executing node {}'.format(self))
# if self.fresh:
# self.counter = self._Iterations
# self._LoopBody(self._Start)
# self.fresh = False
# elif self.counter == 0:
# self._Final(self._Control)
# else:
# self.counter -= 1
# self._LoopBody(self._Control)
#
#
# def notify(self):
# if self.counter > 0:
# output = self.outputs['LoopBody']
# for con in self.graph.getConnectionsOfOutput(output):
# outputName = con['outputName']
# nextNode = con['inputNode']
# nextInput = con['inputName']
# # nextNode.prepare()
# nextNode.setInput(nextInput, self.outputs[outputName].value, override=True, loopLevel=self.loopLevel+1)
# self.inputs['Control'].reset()
#
# else:
# output = self.outputs['Final']
# for con in self.graph.getConnectionsOfOutput(output):
# outputName = con['outputName']
# nextNode = con['inputNode']
# nextInput = con['inputName']
# nextNode.setInput(nextInput, self.outputs[outputName].value, loopLevel=self.loopLevel)
# # self.prepare()
# self.fresh = True
# for inp in self.inputs.values():
# if not inp.name == 'Iterations':
# inp.reset()
# # print(self.inProgress)
# # exit()
class WaitAll(Node):
"""
Watis for all inputs to be set before executing further nodes.
"""
Input('Pass', object)
Input('Wait', object)
Output('Out', object)
def run(self):
self._Out(self._Pass)
def notify(self):
super(WaitAll, self).notify()
[inp.reset(self.loopLevel) for inp in self.inputs.values()]
class WaitAny(WaitAll):
"""
Waits for any inputs to be set. This doesn't make much sense, does it?
"""
def check(self):
for inp in self.inputs.values():
if inp.valueSet:
# print(' {}: Prerequisites not met.'.format(str(self)))
return True
def run(self):
super(WaitAny, self).run()
for inp in self.inputs.values():
if inp.valueSet:
self._out(inp.value)
class Test(Node):
Input('Test', bool)
Output('T', bool)
def run(self):
super(Test, self).run()
print(self._Test)
self._T(self._Test)
class TestNode(Node):
Input('strInput', str)
Output('strOutput', str)
def run(self):
super(TestNode, self).run()
import time
time.sleep(self.ID/2000.)
self._strOutput('')
# def report(self):
# r = super(TestNode, self).report()
# r['template'] = 'plotTemplate'
# return r
class FinalTestNode(TestNode):
def run(self):
super(FinalTestNode, self).run()
class TestNode2(Node):
Input('strInput', str)
Input('floatInput', float, default=10.)
Input('Input', str, default='TestNode')
Output('strOutput', str)
class CreateBool(Node):
"""
Creates a Boolean.
"""
Input('Value', bool, select=(True, False))
Output('Boolean', bool)
def run(self):
super(CreateBool, self).run()
self._Boolean(self._Value)
class CreateInt(Node):
"""
Creates an Integer.
"""
Input('Value', int, )
Output('Integer', int)
def run(self):
super(CreateInt, self).run()
self._Integer(self._Value)
class ReadFile(Node):
"""
Node for reading a string from a file.
"""
Input('Name', str)
Output('Content', str)
def run(self):
super(ReadFile, self).run()
fileName = self._Name
try:
with open(fileName, 'r') as fp:
c = fp.read()
except IOError:
self.raiseError('IOError', 'No file named {}.'.format(fileName))
return 1
self._Content(c)
class WriteFile(Node):
Input('Name', str)
Input('Content', str)
Output('Trigger', object)
def run(self):
super(WriteFile, self).run()
with open(self._Name, 'w') as fp:
fp.write(self._Content)
@abstractNode
class ForLoop(ControlNode):
"""
Generic loop node that iterates over all elements in a list.
"""
# Input('Start', object, list=True)
# Output('ListElement', object)
def __init__(self, *args, **kwargs):
super(ForLoop, self).__init__(*args, **kwargs)
self.fresh = True
self.counter = 0
self.done = False
self.loopLevel = 0
def setInput(self, inputName, value, override=False, loopLevel=0):
if inputName == 'Control':
loopLevel = self.loopLevel
super(ForLoop, self).setInput(inputName, value, override, loopLevel)
# print(' XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
def check(self):
if self.fresh:
for inp in self.inputs.values():
if inp.name == 'Control':
continue
if not inp.isAvailable():
# print(' {}: Prerequisites not met.'.format(str(self)))
return False
return True
else:
if self.inputs['Control'].isAvailable():
return True
def run(self):
super(ForLoop, self).run()
self.fresh = False
try:
self._ListElement(self._Start[self.counter])
except IndexError:
self._Final(self._Start)
self.done = True
self.counter += 1
def notify(self):
if not self.done:
for oName in self.outputs.keys():
if oName == 'Final':
continue
output = self.outputs[oName]
for con in self.graph.getConnectionsOfOutput(output):
outputName = con['outputName']
nextNode = con['inputNode']
nextInput = con['inputName']
# nextNode.prepare()
nextNode.setInput(nextInput, self.outputs[outputName].value, override=True, loopLevel=self.loopLevel+1)
self.inputs['Control'].reset(force=True)
else:
output = self.outputs['Final']
for con in self.graph.getConnectionsOfOutput(output):
outputName = con['outputName']
nextNode = con['inputNode']
nextInput = con['inputName']
nextNode.setInput(nextInput, self.outputs[outputName].value, loopLevel=self.loopLevel)
# self.prepare()
self.fresh = True
for inp in self.inputs.values():
if not inp.name == 'Iterations':
inp.reset()
self.counter = 0
self.fresh = True
self.done = False
class ForEach(ForLoop):
Input('Start', object, list=True)
Output('ListElement', object)
class IsEqual(Node):
"""
Sets output to object1 == object2.
"""
Input('object1', object)
Input('object2', object)
Output('Equal', bool)
def run(self):
super(IsEqual, self).run()
self._Equal(self._object1 == self._object2)
class CreateString(Node):
"""
Creates a string object.
"""
Input('Str', str)
Output('String', str)
def run(self):
super(CreateString, self).run()
self._String(self._Str)
@abstractNode
class DebugNode(Node):
"""
Subclass this node for creating custom nodes related to debugging a graph.
"""
Tag('Debug')
def print(self, *args):
string = ' '.join(args)
print('[DEBUG]', string)
class DebugPrint(DebugNode):
"""
Prints node instance specific debugging information to the cmd line. The input is than passed on straight to
the output without manipulating the object.
Custom debug information can be specified in an objects corresponding floppy.types.Type subclass.
"""
Input('Object', object)
Output('Out', object)
def run(self):
super(DebugPrint, self).run()
obj = self._Object
try:
self.print(str(obj.__FloppyType__.debugInfoGetter(obj)()))
except AttributeError:
self.print(str(obj))
self._Out(obj)
class Join(Node):
Input('Str1', str)
Input('Str2', str)
Output('Joined', str)
def run(self):
super(Join, self).run()
self._Joined(''.join([self._Str1, self._Str2]))
class Break(Node):
Input('Input', object)
Output('Output', object)
Tag('Loop')
def run(self):