use of com.genericworkflownodes.knime.parameter.FileListParameter in project GenericKnimeNodes by genericworkflownodes.
the class DynamicGenericNodeModel method getOutputBaseNames.
/**
* Tries to guess the optimal output file names given all the input edges.
* The file names will be extracted from the configuration, hence the file
* names need to be transferred into config prior to using this method. See
* {@link GenericKnimeNodeModel#transferIncomingPorts2Config(PortObject[])}.
*
* @return A list of base names for the output files.
* @throws Exception
*/
private List<String> getOutputBaseNames() throws Exception {
// 1. we select always the list with the highest number of files.
// 2. we prefer lists over files (independent of the number of
// elements).
// 3. we prefer files over prefixes since we assume that prefixes are
// often indices or reference data
// 4. ignore optional parameters
List<String> basenames = new ArrayList<String>();
// find the port
int naming_port = 0;
int max_size = -1;
boolean seen_prefix = false;
boolean isFileParameter = false;
for (int i = 0; i < m_nodeConfig.getInputPorts().size(); ++i) {
Port port = m_nodeConfig.getInputPorts().get(i);
String name = port.getName();
Parameter<?> p = m_nodeConfig.getParameter(name);
// we don't assume that optional ports are naming relevant
if (p.isOptional()) {
continue;
}
if (p instanceof FileListParameter) {
FileListParameter flp = (FileListParameter) p;
if (max_size == -1 || (isFileParameter && (max_size <= flp.getValue().size()))) {
max_size = flp.getValue().size();
naming_port = i;
} else if (flp.getValue().size() != max_size) {
throw new Exception("The number of output files cannot be determined since multiple input file lists with disagreeing numbers exist.");
}
} else if (max_size == -1 || seen_prefix) {
// is a regular incoming port but we have no better option
max_size = 1;
naming_port = i;
// indicating that we have (for now) selected a file parameter
// which will be overruled by any FileListParameter
isFileParameter = true;
seen_prefix = port.isPrefix();
}
}
if (m_nodeConfig.getInputPorts().size() > 0) {
// generate the filenames if there are input ports
// without ports, the names are set in transferOutgoingPorts2Config
Port port = m_nodeConfig.getInputPorts().get(naming_port);
String name = port.getName();
Parameter<?> p = m_nodeConfig.getParameter(name);
if (p instanceof FileListParameter) {
// we have multiple base names
FileListParameter flp = (FileListParameter) p;
for (String fName : flp.getValue()) {
basenames.add(FilenameUtils.getBaseName(fName));
}
} else {
// we only have a single basename
// FilenameUtils.getBaseName()
basenames.add(FilenameUtils.getBaseName(((FileParameter) p).getValue()));
}
}
return basenames;
}
use of com.genericworkflownodes.knime.parameter.FileListParameter in project GenericKnimeNodes by genericworkflownodes.
the class GenericKnimeNodeModel method transferOutgoingPorts2Config.
/**
* Creates a list of lists of output files (as {@link URI}s) pointing to the
* files that will be generated by the executed tool.
*
* @param jobdir
* The working directory of the executable.
* @param inData
* The input data as {@link PortObject} array
* @return A list of lists of output files
* @throws Exception
* If the input has an invalid configuration.
*/
private List<PortObject> transferOutgoingPorts2Config(final File jobdir, PortObject[] inData, ExecutionContext exec) throws Exception {
final int nOut = m_nodeConfig.getOutputPorts().size();
List<PortObject> outPorts = new ArrayList<PortObject>(nOut);
for (int i = 0; i < nOut; i++) {
Port port = m_nodeConfig.getOutputPorts().get(i);
// Before we know about its extension, a port is reset to active
port.setActive(true);
String name = port.getName();
String ext = "";
boolean isPrefix = port.isPrefix();
if (!isPrefix) {
ext = getOutputType(i);
}
if (ext.toLowerCase().equals("inactive")) {
port.setActive(false);
outPorts.add(InactiveBranchPortObject.INSTANCE);
continue;
}
Parameter<?> p = m_nodeConfig.getParameter(name);
// basenames and number of output files guessed from input
List<String> basenames = getOutputBaseNames();
if (p instanceof FileListParameter && port.isMultiFile()) {
// we currently do not support lists of prefixes
if (isPrefix) {
throw new InvalidSettingsException("GKN currently does not support lists of prefixes as output.");
}
FileListParameter flp = (FileListParameter) p;
List<String> fileNames = new ArrayList<String>();
if (basenames.size() == 0) {
throw new Exception("Cannot determine number of output files if no input file is given.");
}
PortObject fsupo = new FileStoreURIPortObject(exec.createFileStore(m_nodeConfig.getName() + "_" + i));
for (int f = 0; f < basenames.size(); ++f) {
// create basename: <base_name>_<port_nr>_<outfile_nr>
String file_basename = String.format("%s_%d", basenames.get(f), f);
File file = ((FileStoreURIPortObject) fsupo).registerFile(file_basename + "." + ext);
fileNames.add(file.getAbsolutePath());
}
// add filled portobject
outPorts.add(fsupo);
// overwrite existing settings with new values generated by the
// stash
flp.setValue(fileNames);
} else if (p instanceof FileParameter && !port.isMultiFile()) {
PortObject po;
// if we have no basename to use (e.g., Node without input-file)
// we use the nodename
String basename;
if (basenames.isEmpty()) {
basename = m_nodeConfig.getName();
} else {
basename = basenames.get(0);
}
// create basename: <base_name>_<outfile_nr>
String fileName = basename;
if (!isPrefix) {
fileName += '.' + ext;
}
if (isPrefix) {
po = new FileStorePrefixURIPortObject(exec.createFileStore(m_nodeConfig.getName() + "_" + i), fileName);
((FileParameter) p).setValue(((FileStorePrefixURIPortObject) po).getPrefix());
LOGGER.debug("> setting param " + name + "->" + ((FileStorePrefixURIPortObject) po).getPrefix());
} else {
po = new FileStoreURIPortObject(exec.createFileStore(m_nodeConfig.getName() + "_" + i));
// we do not append the file extension if we have a prefix
File file = ((FileStoreURIPortObject) po).registerFile(fileName);
((FileParameter) p).setValue(file.getAbsolutePath());
LOGGER.debug("> setting param " + name + "->" + file);
}
// remember output file
outPorts.add(po);
} else {
throw new Exception("Invalid connection between ports and parameters.");
}
}
return outPorts;
}
use of com.genericworkflownodes.knime.parameter.FileListParameter in project GenericKnimeNodes by genericworkflownodes.
the class DockerCommandGenerator method extractParamterValues.
private List<List<? extends CommandLineElement>> extractParamterValues(CLIElement cliElement, List<CommandLineElement> dockerCommands, Map<String, String> hostDockerMap) throws IOException {
List<List<? extends CommandLineElement>> extractedParameterValues = new ArrayList<List<? extends CommandLineElement>>();
for (CLIMapping cliMapping : cliElement.getMapping()) {
if (nodeConfig.getParameterKeys().contains(cliMapping.getReferenceName())) {
Parameter<?> p = nodeConfig.getParameter(cliMapping.getReferenceName());
if (!p.isNull()) {
if (p instanceof ListParameter) {
ListParameter lp = (ListParameter) p;
if (lp.getStrings().size() > 0) {
final List<CommandLineElement> tmp = new ArrayList<CommandLineElement>();
for (final String s : lp.getStrings()) {
tmp.add(new CommandLineFixedString(s));
}
extractedParameterValues.add(tmp);
}
} else if (p instanceof FileParameter) {
extractedParameterValues.add(handleFileParameter(((FileParameter) p).getValue(), dockerCommands, hostDockerMap));
} else if (p instanceof FileListParameter) {
List<String> fl = ((FileListParameter) p).getValue();
if (fl.size() > 0) {
for (String hostFile : fl) {
extractedParameterValues.add(handleFileParameter(hostFile, dockerCommands, hostDockerMap));
}
}
} else {
List<CommandLineElement> l = new ArrayList<CommandLineElement>();
l.add(new CommandLineParameter(p));
extractedParameterValues.add(l);
}
}
}
}
return extractedParameterValues;
}
Aggregations