-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDefaultNodeModule.java
More file actions
205 lines (175 loc) · 6.33 KB
/
DefaultNodeModule.java
File metadata and controls
205 lines (175 loc) · 6.33 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
package org.knime.scijava.commands.module;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.knime.core.data.DataCell;
import org.knime.core.data.DataRow;
import org.knime.core.data.DataType;
import org.knime.core.node.ExecutionContext;
import org.knime.scijava.commands.CellOutput;
import org.knime.scijava.commands.MultiOutputListener;
import org.knime.scijava.commands.converter.KNIMEConverterService;
import org.scijava.Context;
import org.scijava.module.Module;
import org.scijava.module.ModuleInfo;
import org.scijava.module.ModuleItem;
import org.scijava.module.ModuleService;
import org.scijava.module.process.ModulePreprocessor;
import org.scijava.module.process.PreprocessorPlugin;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.PluginService;
/**
* Default implementation of {@link NodeModule}.
*
* @author Christian Dietz, University of Konstanz
*/
class DefaultNodeModule implements NodeModule {
@Parameter
private PluginService ps;
@Parameter
private KNIMEConverterService cs;
@Parameter
private ModuleService ms;
// Internally used variables
private final Module module;
private final Map<Integer, ModuleItem<?>> inputMapping;
private final Map<ModuleItem<?>, DataType> outputMapping;
private final NodeModuleOutputChangedListener outputListener;
/**
* Constructor.
*
* @param context
* Scijava context.
* @param info
* info this module is created for
* @param params
* preresolved values for the parameters of the module
* @param inputMapping
* mapping of input column index to input items of the module
* @param outputMapping
* mapping of output items to types for the cells to for them
*/
public DefaultNodeModule(final Context context, final ModuleInfo info,
final Map<String, Object> params,
final Map<Integer, ModuleItem<?>> inputMapping,
final Map<ModuleItem<?>, DataType> outputMapping) {
context.inject(this);
this.inputMapping = inputMapping;
this.outputMapping = outputMapping;
this.module = ms.createModule(info);
// Setting parameters
for (final Entry<String, Object> entry : params.entrySet()) {
module.setInput(entry.getKey(), entry.getValue());
module.resolveInput(entry.getKey());
}
// FIXME: do we need them all?
// FIXME add our own preprocessors, i.e. to call preprocess?
// FIXME or move this to "preprocess"
final List<PreprocessorPlugin> pre = ps
.createInstancesOfType(PreprocessorPlugin.class);
if (pre != null) {
for (final ModulePreprocessor p : pre) {
p.process(module);
}
}
outputListener = new NodeModuleOutputChangedListener();
for (final ModuleItem<?> item : info.inputs()) {
if (MultiOutputListener.class.isAssignableFrom(item.getType())) {
module.setInput(name, outputListener);
outputListener.enableManualPush(true);
}
}
}
/*
* Set module input values from the input row.
*/
private void setModuleInput(final DataRow input) throws Exception {
// input can be null if source node
if (input != null) {
for (final Entry<Integer, ModuleItem<?>> entry : inputMapping
.entrySet()) {
final Object obj = cs.convertToJava(
input.getCell(entry.getKey()),
entry.getValue().getType());
module.setInput(entry.getValue().getName(), obj);
}
}
}
@Override
public void run(final DataRow input, final CellOutput output,
final ExecutionContext ctx) throws Exception {
setModuleInput(input);
outputListener.setCellOutput(output);
outputListener.setExecutionContext(ctx);
module.run();
outputListener.flush();
}
private class NodeModuleOutputChangedListener
implements MultiOutputListener {
private ExecutionContext ctx;
private CellOutput output;
/* true if the modules handles pushes itself, false otherwise */
private boolean manualPush = false;
@Override
public void notifyListener() {
try {
// output can be null if sink node
if (output != null) {
final List<DataCell> cells = new ArrayList<DataCell>();
final ModuleItem<?> result = module.getInfo()
.getOutput("result");
if (result != null) {
// FIXME hack because e.g. python script contains
// result log
cells.add(cs.convertToKnime(
module.getOutput(result.getName()),
result.getType(), outputMapping.get(result),
ctx));
}
output.push(cells.toArray(new DataCell[cells.size()]));
}
} catch (final Exception e) {
// FIXME
e.printStackTrace();
}
}
/**
* Set KNIME Execution Context
*
* @param ctx
* execution context
*/
public void setExecutionContext(final ExecutionContext ctx) {
this.ctx = ctx;
}
/**
* Set CellOutput to push rows to.
*
* @param output
* cell output
*/
public void setCellOutput(final CellOutput output) {
this.output = output;
}
/**
* Final flush. Will push a row in case module does not handle pushing
* output rows.
*/
public void flush() {
if (!manualPush) {
notifyListener();
}
}
/**
* Enable/Disable manual push.
*
* @param b
* if <code>true</code>, signals that the module handles
* pushing rows.
*/
public void enableManualPush(final boolean b) {
manualPush = b;
}
}
}