-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.py
159 lines (109 loc) · 4.06 KB
/
base.py
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
from dispel4py.core import GenericPE, NAME
class BasePE(GenericPE):
'''
'''
INPUT_NAME = 'input'
OUTPUT_NAME = 'output'
def __init__(self, inputs=[], outputs=[], num_inputs=0, num_outputs=0):
GenericPE.__init__(self)
for i in range(num_inputs):
name = '%s%s' % (BasePE.INPUT_NAME, i)
self.inputconnections[name] = {NAME: name}
for i in range(num_outputs):
name = '%s%s' % (BasePE.OUTPUT_NAME, i)
self.outputconnections[name] = {NAME: name}
for name in inputs:
self.inputconnections[name] = {NAME: name}
for name in outputs:
self.outputconnections[name] = {NAME: name}
class IterativePE(GenericPE):
INPUT_NAME = 'input'
OUTPUT_NAME = 'output'
def __init__(self):
GenericPE.__init__(self)
self._add_input(IterativePE.INPUT_NAME)
self._add_output(IterativePE.OUTPUT_NAME)
def process(self, inputs):
data = inputs[IterativePE.INPUT_NAME]
result = self._process(data)
if result is not None:
return {self.OUTPUT_NAME: result}
def _process(self, data):
return None
class ProducerPE(GenericPE):
OUTPUT_NAME = 'output'
def __init__(self):
GenericPE.__init__(self)
self._add_output(ProducerPE.OUTPUT_NAME)
def process(self, inputs):
result = self._process(inputs)
if result is not None:
return {self.OUTPUT_NAME: result}
class ConsumerPE(GenericPE):
INPUT_NAME = 'input'
def __init__(self):
GenericPE.__init__(self)
self._add_input(ConsumerPE.INPUT_NAME)
def process(self, inputs):
data = inputs[ConsumerPE.INPUT_NAME]
self._process(data)
class SimpleFunctionPE(IterativePE):
INPUT_NAME = IterativePE.INPUT_NAME
OUTPUT_NAME = IterativePE.OUTPUT_NAME
def __init__(self, compute_fn=None, params={}):
IterativePE.__init__(self)
if compute_fn:
self.name = 'PE_%s' % compute_fn.__name__
self.compute_fn = compute_fn
self.params = params
def _process(self, data):
return self.compute_fn(data, **self.params)
from dispel4py.workflow_graph import WorkflowGraph
def create_iterative_chain(functions,
FunctionPE_class=SimpleFunctionPE,
name_prefix='PE_',
name_suffix=''):
prev = None
first = None
graph = WorkflowGraph()
for fn_desc in functions:
try:
fn = fn_desc[0]
params = fn_desc[1]
except TypeError:
fn = fn_desc
params = {}
# print 'adding %s to chain' % fn.__name__
pe = FunctionPE_class()
pe.compute_fn = fn
pe.params = params
pe.name = name_prefix + fn.__name__ + name_suffix
if prev:
graph.connect(prev, IterativePE.OUTPUT_NAME,
pe, IterativePE.INPUT_NAME)
else:
first = pe
prev = pe
# Map inputs and outputs of the wrapper to the nodes in the subgraph
graph.inputmappings = {'input': (first, IterativePE.INPUT_NAME)}
graph.outputmappings = {'output': (prev, IterativePE.OUTPUT_NAME)}
return graph
class CompositePE(WorkflowGraph):
def __init__(self, create_graph=None, params={}):
WorkflowGraph.__init__(self)
self.inputmappings = {}
self.outputmappings = {}
if create_graph:
create_graph(self, **params)
def _map_input(self, input_name, internal_pe, internal_input):
'''
Map the composite PE input to the named input of a PE that is contained
in the graph.
'''
self.inputmappings[input_name] = (internal_pe, internal_input)
def _map_output(self, output_name, internal_pe, internal_output):
'''
Map the composite PE output to the named output of a PE that is
contained in the graph.
'''
self.outputmappings[output_name] = (internal_pe, internal_output)