Skip to content

Commit a8974dd

Browse files
committed
Split DefaultNodeModule & DefaultNodeModuleService into Command- & Script-
Signed-off-by: Squareys <[email protected]>
1 parent 911fe46 commit a8974dd

File tree

7 files changed

+320
-52
lines changed

7 files changed

+320
-52
lines changed

org.knime.scijava.commands/src/org/knime/scijava/commands/module/DefaultNodeModule.java renamed to org.knime.scijava.commands/src/org/knime/scijava/commands/module/AbstractNodeModule.java

Lines changed: 66 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.knime.core.data.DataCell;
99
import org.knime.core.data.DataRow;
1010
import org.knime.core.data.DataType;
11+
import org.knime.core.data.filestore.FileStore;
1112
import org.knime.core.node.ExecutionContext;
1213
import org.knime.core.node.NodeLogger;
1314
import org.knime.scijava.commands.CellOutput;
@@ -30,24 +31,23 @@
3031
*
3132
* @author Christian Dietz, University of Konstanz
3233
*/
33-
class DefaultNodeModule implements NodeModule {
34+
abstract class AbstractNodeModule implements NodeModule {
3435

3536
@Parameter
3637
private PluginService ps;
3738

3839
@Parameter
39-
private KNIMEConverterService cs;
40+
private KNIMEConverterService convertService;
4041

4142
@Parameter
4243
private ModuleService ms;
4344

44-
// Internally used variables
45-
private final Module module;
46-
4745
private final Map<Integer, ModuleItem<?>> inputMapping;
48-
4946
private final Map<ModuleItem<?>, DataType> outputMapping;
5047

48+
private final ModuleInfo moduleInfo;
49+
private final Module module;
50+
5151
private NodeModuleOutputChangedListener outputListener;
5252

5353
private final LogService logService;
@@ -68,7 +68,7 @@ class DefaultNodeModule implements NodeModule {
6868
* @param logger
6969
* NodeLogger to delegate output to.
7070
*/
71-
public DefaultNodeModule(final Context context, final ModuleInfo info,
71+
public AbstractNodeModule(final Context context, final ModuleInfo info,
7272
final Map<String, Object> params,
7373
final Map<Integer, ModuleItem<?>> inputMapping,
7474
final Map<ModuleItem<?>, DataType> outputMapping,
@@ -77,8 +77,9 @@ public DefaultNodeModule(final Context context, final ModuleInfo info,
7777

7878
this.inputMapping = inputMapping;
7979
this.outputMapping = outputMapping;
80+
this.moduleInfo = info;
8081
this.module = ms.createModule(info);
81-
this.outputListener = new NodeModuleOutputChangedListener(false);
82+
this.outputListener = new NodeModuleOutputChangedListener(true);
8283
this.logService = new KNIMELogService(logger);
8384

8485
preProcess(params);
@@ -96,7 +97,6 @@ private void preProcess(final Map<String, Object> params) {
9697
module.resolveInput(entry.getKey());
9798
}
9899

99-
// FIXME: do we need them all?
100100
final List<PreprocessorPlugin> pre = ps
101101
.createInstancesOfType(PreprocessorPlugin.class);
102102

@@ -109,7 +109,7 @@ private void preProcess(final Map<String, Object> params) {
109109
/* MultiOutputListener */
110110
final String name = item.getName();
111111

112-
outputListener = new NodeModuleOutputChangedListener(true);
112+
outputListener = new NodeModuleOutputChangedListener(false);
113113
module.setInput(name, outputListener);
114114
module.resolveInput(name);
115115
} else if (LogService.class.equals(item.getType())) {
@@ -125,12 +125,12 @@ private void preProcess(final Map<String, Object> params) {
125125
/*
126126
* Set module input values from the input row.
127127
*/
128-
private void setModuleInput(final DataRow input) throws Exception {
128+
protected void setModuleInput(final DataRow input) throws Exception {
129129
// input can be null if source node
130130
if (input != null) {
131131
for (final Entry<Integer, ModuleItem<?>> entry : inputMapping
132132
.entrySet()) {
133-
final Object obj = cs.convertToJava(
133+
final Object obj = convertService.convertToJava(
134134
input.getCell(entry.getKey()),
135135
entry.getValue().getType());
136136
module.setInput(entry.getValue().getName(), obj);
@@ -151,42 +151,76 @@ public void run(final DataRow input, final CellOutput output,
151151
outputListener.flush();
152152
}
153153

154+
/**
155+
* Create cells for the currently set output {@link ModuleItem}s of the
156+
* module.
157+
*
158+
* @param context
159+
* Execution Context to use when creating {@link FileStore}s
160+
* @return array of cells with the values of the module output
161+
* @throws Exception
162+
*/
163+
protected DataCell[] getModuleOutput(final ExecutionContext context)
164+
throws Exception {
165+
final List<DataCell> cells = new ArrayList<DataCell>();
166+
for (final ModuleItem<?> entry : getInfo().outputs()) {
167+
cells.add(convertService.convertToKnime(
168+
getModule().getOutput(entry.getName()), entry.getType(),
169+
getOutputMapping().get(entry), context));
170+
}
171+
172+
return cells.toArray(new DataCell[cells.size()]);
173+
}
174+
175+
@Override
176+
public Map<Integer, ModuleItem<?>> getIntputMapping() {
177+
return inputMapping;
178+
}
179+
180+
@Override
181+
public Map<ModuleItem<?>, DataType> getOutputMapping() {
182+
return outputMapping;
183+
}
184+
185+
@Override
186+
public ModuleInfo getInfo() {
187+
return moduleInfo;
188+
}
189+
190+
@Override
191+
public Module getModule() {
192+
return module;
193+
}
194+
195+
protected final KNIMEConverterService getConvertService() {
196+
return convertService;
197+
}
198+
154199
private class NodeModuleOutputChangedListener
155200
implements MultiOutputListener {
156201
private ExecutionContext ctx;
157202

158203
private CellOutput output;
159204

160205
/* true if the modules handles pushes itself, false otherwise */
161-
private final boolean manualPush;
206+
private final boolean pushOnFlush;
162207

163208
/**
164209
* Constructor.
165210
*
166-
* @param manualPush
167-
* if <code>true</code>, signals that the module handles
168-
* pushing rows.
211+
* @param pushOnFlush
212+
* if <code>true</code>, will push a final row when flush is
213+
* called.
169214
*/
170-
public NodeModuleOutputChangedListener(final boolean manualPush) {
171-
this.manualPush = manualPush;
215+
public NodeModuleOutputChangedListener(final boolean pushOnFlush) {
216+
this.pushOnFlush = pushOnFlush;
172217
}
173218

174219
@Override
175220
public void notifyListener() throws Exception {
176221
// output can be null if sink node
177222
if (output != null) {
178-
final List<DataCell> cells = new ArrayList<DataCell>();
179-
for (final ModuleItem<?> entry : module.getInfo().outputs()) {
180-
// FIXME hack because e.g. python script contains
181-
// result log
182-
if (!entry.getName().equals("result")) {
183-
cells.add(cs.convertToKnime(
184-
module.getOutput(entry.getName()),
185-
entry.getType(), outputMapping.get(entry),
186-
ctx));
187-
}
188-
}
189-
output.push(cells.toArray(new DataCell[cells.size()]));
223+
output.push(getModuleOutput(ctx));
190224
}
191225
}
192226

@@ -213,10 +247,11 @@ public void setCellOutput(final CellOutput output) {
213247
/**
214248
* Final flush. Will push a row in case module does not handle pushing
215249
* output rows.
250+
*
216251
* @throws Exception
217252
*/
218253
public void flush() throws Exception {
219-
if (!manualPush) {
254+
if (!pushOnFlush) {
220255
notifyListener();
221256
}
222257
}
Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package org.knime.scijava.commands.module;
22

3-
import java.util.ArrayList;
43
import java.util.HashMap;
54
import java.util.HashSet;
6-
import java.util.List;
75
import java.util.Map;
86
import java.util.Optional;
97

@@ -19,7 +17,9 @@
1917
import org.knime.scijava.commands.settings.SettingsModelTypeService;
2018
import org.knime.scijava.commands.settings.models.SettingsModelColumnSelection;
2119
import org.knime.scijava.commands.settings.types.SettingsModelColumnSelectionType;
20+
import org.scijava.Context;
2221
import org.scijava.Identifiable;
22+
import org.scijava.command.CommandInfo;
2323
import org.scijava.module.ModuleInfo;
2424
import org.scijava.module.ModuleItem;
2525
import org.scijava.module.ModuleService;
@@ -33,7 +33,7 @@
3333
* @author Christian Dietz, University of Konstanz
3434
*/
3535
@Plugin(type = NodeModuleService.class)
36-
public class DefaultNodeModuleService extends AbstractService
36+
public abstract class AbstractNodeModuleService extends AbstractService
3737
implements NodeModuleService {
3838

3939
@Parameter
@@ -47,7 +47,7 @@ public class DefaultNodeModuleService extends AbstractService
4747

4848
@Override
4949
public NodeModule createNodeModule(final ModuleInfo info,
50-
final Map<String, SettingsModel> models, final DataTableSpec spec,
50+
final Map<String, SettingsModel> models, final DataTableSpec inSpec,
5151
final NodeLogger knimeLogger) {
5252

5353
final Map<Integer, ModuleItem<?>> inputMapping = new HashMap<>();
@@ -71,7 +71,7 @@ public NodeModule createNodeModule(final ModuleInfo info,
7171
final SettingsModelColumnSelection settingsModel = (SettingsModelColumnSelection) models
7272
.get(item.getName());
7373
inputMapping.put(
74-
spec.findColumnIndex(settingsModel.getStringValue()),
74+
inSpec.findColumnIndex(settingsModel.getStringValue()),
7575
item);
7676
} else {
7777
@SuppressWarnings("unchecked")
@@ -80,11 +80,16 @@ public NodeModule createNodeModule(final ModuleInfo info,
8080
}
8181
}
8282

83-
return new DefaultNodeModule(getContext(), info, params, inputMapping,
84-
outputMapping, knimeLogger);
85-
83+
return createNodeModule(getContext(), (CommandInfo) info, params,
84+
inputMapping, outputMapping, knimeLogger);
8685
}
8786

87+
protected abstract NodeModule createNodeModule(final Context context,
88+
final CommandInfo info, final Map<String, Object> params,
89+
final Map<Integer, ModuleItem<?>> inputMapping,
90+
final Map<ModuleItem<?>, DataType> outputMapping,
91+
final NodeLogger knimeLogger);
92+
8893
@Override
8994
public DataTableSpec createOutSpec(final ModuleInfo info,
9095
final Map<String, SettingsModel> models,
@@ -100,22 +105,14 @@ public DataTableSpec createOutSpec(final ModuleInfo info,
100105
nameGen = new UniqueNameGenerator(new HashSet<>());
101106
}
102107

103-
// TODO (What?)... What?
104-
final List<DataColumnSpec> tableSpecs = new ArrayList<>();
105-
106-
// FIXME (result)
107-
for (final ModuleItem<?> item : info.outputs()) {
108-
if (item.getName().equals("result")) {
109-
continue;
110-
}
111-
tableSpecs.add(
112-
nameGen.newColumn(item.getName(), outputMapping.get(item)));
113-
}
114-
115108
return new DataTableSpec(
116-
tableSpecs.toArray(new DataColumnSpec[tableSpecs.size()]));
109+
createOutColumnSpecs(info, outputMapping, nameGen));
117110
}
118111

112+
protected abstract DataColumnSpec[] createOutColumnSpecs(final ModuleInfo info,
113+
final Map<ModuleItem<?>, DataType> outputMapping,
114+
final UniqueNameGenerator nameGen);
115+
119116
@Override
120117
public Map<ModuleItem<?>, DataType> getOutputMapping(final ModuleInfo info,
121118
final Map<String, SettingsModel> models) {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.knime.scijava.commands.module;
2+
3+
import java.util.Map;
4+
5+
import org.knime.core.data.DataType;
6+
import org.knime.core.node.NodeLogger;
7+
import org.scijava.Context;
8+
import org.scijava.command.CommandInfo;
9+
import org.scijava.module.ModuleItem;
10+
11+
/**
12+
* Default implementation of {@link NodeModule}.
13+
*
14+
* @author Christian Dietz, University of Konstanz
15+
*/
16+
class CommandNodeModule extends AbstractNodeModule {
17+
18+
/**
19+
* Constructor.
20+
*
21+
* @param context
22+
* Scijava context.
23+
* @param info
24+
* info of the command module this module is created for
25+
* @param params
26+
* preresolved values for the parameters of the module
27+
* @param inputMapping
28+
* mapping of input column index to input items of the module
29+
* @param outputMapping
30+
* mapping of output items to types for the cells to for them
31+
* @param logger
32+
* NodeLogger to delegate output to.
33+
*/
34+
public CommandNodeModule(final Context context, final CommandInfo info,
35+
final Map<String, Object> params,
36+
final Map<Integer, ModuleItem<?>> inputMapping,
37+
final Map<ModuleItem<?>, DataType> outputMapping,
38+
final NodeLogger logger) {
39+
super(context, info, params, inputMapping, outputMapping, logger);
40+
}
41+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.knime.scijava.commands.module;
2+
3+
import java.util.ArrayList;
4+
import java.util.Map;
5+
6+
import org.knime.core.data.DataColumnSpec;
7+
import org.knime.core.data.DataType;
8+
import org.knime.core.node.NodeLogger;
9+
import org.knime.core.util.UniqueNameGenerator;
10+
import org.scijava.Context;
11+
import org.scijava.command.CommandInfo;
12+
import org.scijava.module.ModuleInfo;
13+
import org.scijava.module.ModuleItem;
14+
import org.scijava.plugin.Plugin;
15+
16+
/**
17+
* Default implementation of {@link NodeModuleService}.
18+
*
19+
* @author Christian Dietz, University of Konstanz
20+
*/
21+
@Plugin(type = NodeModuleService.class)
22+
public class CommandNodeModuleService extends AbstractNodeModuleService {
23+
24+
@Override
25+
protected NodeModule createNodeModule(final Context context,
26+
final CommandInfo info, final Map<String, Object> params,
27+
final Map<Integer, ModuleItem<?>> inputMapping,
28+
final Map<ModuleItem<?>, DataType> outputMapping,
29+
final NodeLogger knimeLogger) {
30+
return new CommandNodeModule(context, info, params, inputMapping,
31+
outputMapping, knimeLogger);
32+
}
33+
34+
@Override
35+
protected DataColumnSpec[] createOutColumnSpecs(final ModuleInfo info,
36+
final Map<ModuleItem<?>, DataType> outputMapping,
37+
final UniqueNameGenerator nameGen) {
38+
final ArrayList<DataColumnSpec> outSpecs = new ArrayList<>();
39+
40+
for (final ModuleItem<?> item : info.outputs()) {
41+
outSpecs.add(
42+
nameGen.newColumn(item.getName(), outputMapping.get(item)));
43+
}
44+
45+
return outSpecs.toArray(new DataColumnSpec[outSpecs.size()]);
46+
}
47+
48+
}

0 commit comments

Comments
 (0)