use of com.genericworkflownodes.knime.base.data.port.FileStoreURIPortObject in project GenericKnimeNodes by genericworkflownodes.
the class FileSplitterNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
IURIPortObject input = (IURIPortObject) inData[0];
if (input.getURIContents().size() != 1) {
throw new InvalidSettingsException("This node can only split a single file");
}
// The factory for creating the splitter
String factoryID = m_factoryID.getStringValue();
m_splitter = SplitterFactoryManager.getInstance().getFactory(factoryID).createSplitter();
File f = Paths.get(input.getURIContents().get(0).getURI()).toFile();
// File Store in which we store the files
FileStore fs = exec.createFileStore("FileSplitter");
File[] outputs = new File[m_numParts.getIntValue()];
for (int i = 0; i < m_numParts.getIntValue(); i++) {
int idx = f.getPath().lastIndexOf('.');
String ext;
String name;
if (idx == -1) {
ext = "";
name = f.getName();
} else {
ext = f.getPath().substring(idx);
name = f.getName().substring(0, f.getName().lastIndexOf('.'));
}
outputs[i] = Paths.get(fs.getFile().toString()).resolve(name + i + ext).toFile();
outputs[i].getParentFile().mkdirs();
}
m_splitter.split(f, outputs);
DataContainer dc = exec.createDataContainer(createSpec());
for (int i = 0; i < m_numParts.getIntValue(); i++) {
FileStoreURIPortObject po = new FileStoreURIPortObject(fs);
String relPath = Paths.get(fs.getFile().toString()).relativize(Paths.get(outputs[i].getAbsolutePath())).toString();
po.registerFile(relPath);
PortObjectHandlerCell cell = new PortObjectHandlerCell(po);
dc.addRowToTable(new DefaultRow(new RowKey("Row" + i), cell));
}
dc.close();
return new PortObject[] { (BufferedDataTable) dc.getTable() };
}
use of com.genericworkflownodes.knime.base.data.port.FileStoreURIPortObject in project GenericKnimeNodes by genericworkflownodes.
the class DynamicGenericNodeModel 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);
String name = port.getName();
String ext = "";
boolean isPrefix = port.isPrefix();
if (!isPrefix) {
ext = getOutputType(i);
}
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.");
}
// if MimeType is "Inactive" just create Empty FileStore(Prefix)URIPortObjects
PortObject fsupo;
if (!ext.equals("Inactive")) {
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());
}
} else {
fsupo = InactiveBranchPortObject.INSTANCE;
}
// 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()) {
// if MimeType is "Inactive" just create Empty FileStore(Prefix)URIPortObjects
PortObject po;
if (!ext.equals("Inactive")) {
// 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);
}
} else {
po = InactiveBranchPortObject.INSTANCE;
}
// remember output file
outPorts.add(po);
} else {
throw new Exception("Invalid connection between ports and parameters.");
}
}
return outPorts;
}
use of com.genericworkflownodes.knime.base.data.port.FileStoreURIPortObject 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.base.data.port.FileStoreURIPortObject in project GenericKnimeNodes by genericworkflownodes.
the class PortToFileStoreNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
IURIPortObject input = (IURIPortObject) inData[0];
DataContainer dc = exec.createDataContainer(createSpec());
/**
* Files that are not yet managed by KNIME (e.g. when they come from an InputFiles node) come as URIPortObject
* and must be copied into a FileStore to be handled properly.
* Other files already have a file store and can be put into a <code>PortObjectHandlerCell</code>.
*/
if (input instanceof AbstractFileStoreURIPortObject) {
PortObjectHandlerCell cell = new PortObjectHandlerCell((AbstractFileStoreURIPortObject) input);
dc.addRowToTable(new DefaultRow(new RowKey("files"), cell));
} else {
FileStore fs = exec.createFileStore("files");
fs.getFile().mkdirs();
for (URIContent uc : input.getURIContents()) {
String filename = Paths.get(uc.getURI()).getFileName().toString();
if (!filename.endsWith(uc.getExtension())) {
filename = filename.concat(".").concat(uc.getExtension());
}
// TODO: Report progress
Files.copy(Paths.get(uc.getURI()), Paths.get(fs.getFile().toURI()).resolve(filename));
AbstractFileStoreURIPortObject portObject = new FileStoreURIPortObject(fs);
portObject.registerFile(filename);
PortObjectHandlerCell cell = new PortObjectHandlerCell(portObject);
dc.addRowToTable(new DefaultRow(new RowKey(filename), cell));
}
}
dc.close();
return new PortObject[] { (BufferedDataTable) dc.getTable() };
}
use of com.genericworkflownodes.knime.base.data.port.FileStoreURIPortObject in project GenericKnimeNodes by genericworkflownodes.
the class Image2FilePortNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inObjects, final ExecutionContext exec) throws Exception {
ImagePortObject imageObj = (ImagePortObject) inObjects[0];
DataCell imageCellDC = imageObj.toDataCell();
if (!(imageCellDC instanceof ImageValue)) {
throw new InvalidSettingsException("Image object does not produce" + " valid image object but " + imageCellDC.getClass().getName());
}
ImageValue v = (ImageValue) imageCellDC;
ImageContent content = v.getImageContent();
// check if the extension matches our PNG assumption
checkExtension(v);
// write the file to stash
FileStoreURIPortObject outPort = writeImageFile(content, exec);
return new PortObject[] { outPort };
}
Aggregations