use of com.genericworkflownodes.knime.base.data.port.PortObjectHandlerCell in project GenericKnimeNodes by genericworkflownodes.
the class ListZipLoopEndNodeModel method execute.
@Override
protected PortObject[] execute(PortObject[] inObjects, ExecutionContext exec) {
if (!(getLoopStartNode() instanceof LoopStartNodeTerminator)) {
throw new IllegalStateException("Loop End is not connected" + " to matching/corresponding Loop Start node. You" + " are trying to create an infinite loop!");
}
if (!m_loopStarted) {
// first time we are getting to this: create container
m_incomingPortObjects = new ArrayList<List<IURIPortObject>>(PORT_COUNT);
m_bufferedContainers = new BufferedDataContainer[PORT_COUNT];
for (int i = 0; i < PORT_COUNT; ++i) {
// create data container
m_bufferedContainers[i] = exec.createDataContainer(createPseudoSpec());
// create container to collect the incoming port objects
m_incomingPortObjects.add(new ArrayList<IURIPortObject>());
}
m_loopStarted = true;
}
for (int i = 0; i < PORT_COUNT; i++) {
if (inObjects[i] == null) {
// skip unconnected ports
continue;
}
IURIPortObject po = (IURIPortObject) inObjects[i];
// some data we need
int currentIteration = peekFlowVariableInt("currentIteration");
if (po.getURIContents().size() > 1) {
LOGGER.warn(String.format("More then one incoming object at port %d. The outgoing port will only hold the first one.", i));
}
// collect port object for later use
m_incomingPortObjects.get(i).add(po);
// AbstractFileStoreURIPortObject
if (po instanceof AbstractFileStoreURIPortObject) {
PortObjectHandlerCell pfsc = new PortObjectHandlerCell((AbstractFileStoreURIPortObject) po);
String rowKey = String.format("Row_%d_%d", i, currentIteration);
m_bufferedContainers[i].addRowToTable(new DefaultRow(rowKey, pfsc));
}
}
// check if this is the last iteration
if (((LoopStartNodeTerminator) getLoopStartNode()).terminateLoop()) {
FileStoreReferenceURIPortObject[] portObjects = new FileStoreReferenceURIPortObject[PORT_COUNT];
for (int i = 0; i < PORT_COUNT; i++) {
// assign collected uris to new portobject
portObjects[i] = FileStoreReferenceURIPortObject.create(m_incomingPortObjects.get(i));
// close the container
m_bufferedContainers[i].close();
}
m_loopStarted = false;
return portObjects;
} else {
continueLoop();
return new PortObject[PORT_COUNT];
}
}
use of com.genericworkflownodes.knime.base.data.port.PortObjectHandlerCell 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.PortObjectHandlerCell in project GenericKnimeNodes by genericworkflownodes.
the class SplitTableToPortNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
BufferedDataTable input = (BufferedDataTable) inData[0];
List<URIContent> contents = new ArrayList<>();
String mimetype = null;
int index = input.getDataTableSpec().findColumnIndex(m_fileCol.getColumnName());
for (DataRow row : input) {
PortObjectHandlerCell cell = (PortObjectHandlerCell) row.getCell(index);
URIContent uc = cell.getURIContent();
String mt = MIMETypeHelper.getMIMEtypeByExtension(uc.getExtension());
if (mimetype == null) {
mimetype = mt;
} else if (!mt.equals(mimetype)) {
throw new InvalidSettingsException("File ports do not support mixed mimetypes.");
}
contents.add(uc);
}
URIPortObject output = new URIPortObject(contents);
return new PortObject[] { output };
}
use of com.genericworkflownodes.knime.base.data.port.PortObjectHandlerCell 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() };
}
Aggregations