use of org.knime.core.data.container.WrappedTable in project knime-core by knime.
the class BufferedDataTable method loadFromFile.
/**
* Factory method to restore a table that has been written using
* the save method.
* @param dirRef The directory to load from.
* @param settings The settings to load from.
* @param exec The exec mon for progress/cancel
* @param tblRep The table repository
* @param bufferRep The buffer repository (needed for blobs).
* @param fileStoreHandlerRepository ...
* @return The table as written by save.
* @throws IOException If reading fails.
* @throws CanceledExecutionException If canceled.
* @throws InvalidSettingsException If settings are invalid.
*/
static BufferedDataTable loadFromFile(final ReferencedFile dirRef, final NodeSettingsRO settings, final ExecutionMonitor exec, final Map<Integer, BufferedDataTable> tblRep, final HashMap<Integer, ContainerTable> bufferRep, final FileStoreHandlerRepository fileStoreHandlerRepository) throws IOException, CanceledExecutionException, InvalidSettingsException {
File dir = dirRef.getFile();
NodeSettingsRO s;
// in version 1.1.x and before, the information was stored in
// an external data.xml (directly in the node dir)
boolean isVersion11x;
File dataXML = new File(dir, TABLE_DESCRIPTION_FILE);
// loading an exported workflow without data
if (!dataXML.exists() && settings == null) {
throw new IOException("No such data file: " + dataXML.getAbsolutePath());
}
DataTableSpec spec;
if (dataXML.exists()) {
// version 1.2.0 and later
s = NodeSettings.loadFromXML(new BufferedInputStream(new FileInputStream(dataXML)));
spec = loadSpec(dirRef);
isVersion11x = false;
} else {
// version 1.1.x
s = settings.getNodeSettings(CFG_TABLE_META);
// needs to be read from zip file!
spec = null;
isVersion11x = true;
}
int id = s.getInt(CFG_TABLE_ID);
LAST_ID.set(Math.max(LAST_ID.get(), id + 1));
String fileName = s.getString(CFG_TABLE_FILE_NAME);
ReferencedFile fileRef;
if (fileName != null) {
fileRef = new ReferencedFile(dirRef, fileName);
File file = fileRef.getFile();
if (!file.exists()) {
throw new IOException("No such data file: " + fileRef);
}
if (!file.isFile() || !file.canRead()) {
throw new IOException("Cannot read file " + fileRef);
}
} else {
// for instance for a column filter node this is null.
fileRef = null;
}
String tableType = s.getString(CFG_TABLE_TYPE);
BufferedDataTable t;
if (tableType.equals(TABLE_TYPE_REFERENCE_IN_SAME_NODE)) {
t = tblRep.get(id);
if (t == null) {
throw new InvalidSettingsException("Table reference with ID " + id + " not found in load map");
}
return t;
} else if (tableType.equals(TABLE_TYPE_CONTAINER)) {
ContainerTable fromContainer;
if (isVersion11x) {
fromContainer = DataContainer.readFromZip(fileRef.getFile());
} else {
fromContainer = BufferedDataContainer.readFromZipDelayed(fileRef, spec, id, bufferRep, fileStoreHandlerRepository);
}
t = new BufferedDataTable(fromContainer, id);
} else {
String[] referenceDirs;
// (no concatenate table in those versions)
if (s.containsKey("table_reference")) {
String refDir = s.getString("table_reference");
referenceDirs = refDir == null ? new String[0] : new String[] { refDir };
} else {
referenceDirs = s.getStringArray(CFG_TABLE_REFERENCE);
}
for (String reference : referenceDirs) {
if (reference == null) {
throw new InvalidSettingsException("Reference dir is \"null\"");
}
ReferencedFile referenceDirRef = new ReferencedFile(dirRef, reference);
loadFromFile(referenceDirRef, s, exec, tblRep, bufferRep, fileStoreHandlerRepository);
}
if (tableType.equals(TABLE_TYPE_REARRANGE_COLUMN)) {
t = new BufferedDataTable(new RearrangeColumnsTable(fileRef, s, tblRep, spec, id, bufferRep, fileStoreHandlerRepository));
} else if (tableType.equals(TABLE_TYPE_JOINED)) {
JoinedTable jt = JoinedTable.load(s, spec, tblRep);
t = new BufferedDataTable(jt);
} else if (tableType.equals(TABLE_TYPE_VOID)) {
VoidTable jt = VoidTable.load(spec);
t = new BufferedDataTable(jt);
} else if (tableType.equals(TABLE_TYPE_CONCATENATE)) {
ConcatenateTable ct = ConcatenateTable.load(s, spec, tblRep);
t = new BufferedDataTable(ct);
} else if (tableType.equals(TABLE_TYPE_WRAPPED)) {
WrappedTable wt = WrappedTable.load(s, tblRep);
t = new BufferedDataTable(wt);
} else if (tableType.equals(TABLE_TYPE_NEW_SPEC)) {
TableSpecReplacerTable replTable;
if (isVersion11x) {
replTable = TableSpecReplacerTable.load11x(fileRef.getFile(), s, tblRep);
} else {
replTable = TableSpecReplacerTable.load(s, spec, tblRep);
}
t = new BufferedDataTable(replTable);
} else if (tableType.equals(TABLE_TYPE_EXTENSION)) {
ExtensionTable et = ExtensionTable.loadExtensionTable(fileRef, spec, s, tblRep, exec);
t = new BufferedDataTable(et);
} else {
throw new InvalidSettingsException("Unknown table identifier: " + tableType);
}
}
t.m_tableID = id;
tblRep.put(id, t);
return t;
}
use of org.knime.core.data.container.WrappedTable in project knime-core by knime.
the class BufferedDataTable method save.
/**
* Saves the table to a directory and writes some settings to the argument
* NodeSettingsWO object. It will also write the reference table in case
* this node is responsible for it (i.e. this node created the reference
* table).
* @param dir The directory to write to.
* @param savedTableIDs Ids of tables that were previously saved, used to identify
* tables that are referenced by the same nodes multiple times.
* @param exec The progress monitor for cancellation.
* @throws IOException If writing fails.
* @throws CanceledExecutionException If canceled.
*/
void save(final File dir, final Set<Integer> savedTableIDs, final ExecutionMonitor exec) throws IOException, CanceledExecutionException {
NodeSettings s = new NodeSettings(CFG_TABLE_META);
Integer bufferedTableID = getBufferedTableId();
s.addInt(CFG_TABLE_ID, bufferedTableID);
File outFile = new File(dir, TABLE_FILE);
if (!savedTableIDs.add(bufferedTableID)) {
s.addString(CFG_TABLE_TYPE, TABLE_TYPE_REFERENCE_IN_SAME_NODE);
} else if (m_delegate instanceof ContainerTable) {
s.addString(CFG_TABLE_TYPE, TABLE_TYPE_CONTAINER);
m_delegate.saveToFile(outFile, s, exec);
} else {
if (m_delegate instanceof RearrangeColumnsTable) {
s.addString(CFG_TABLE_TYPE, TABLE_TYPE_REARRANGE_COLUMN);
} else if (m_delegate instanceof TableSpecReplacerTable) {
s.addString(CFG_TABLE_TYPE, TABLE_TYPE_NEW_SPEC);
} else if (m_delegate instanceof WrappedTable) {
s.addString(CFG_TABLE_TYPE, TABLE_TYPE_WRAPPED);
} else if (m_delegate instanceof JoinedTable) {
s.addString(CFG_TABLE_TYPE, TABLE_TYPE_JOINED);
} else if (m_delegate instanceof VoidTable) {
s.addString(CFG_TABLE_TYPE, TABLE_TYPE_VOID);
} else if (m_delegate instanceof ConcatenateTable) {
s.addString(CFG_TABLE_TYPE, TABLE_TYPE_CONCATENATE);
} else {
assert m_delegate instanceof ExtensionTable;
s.addString(CFG_TABLE_TYPE, TABLE_TYPE_EXTENSION);
}
BufferedDataTable[] references = m_delegate.getReferenceTables();
ArrayList<String> referenceDirs = new ArrayList<String>();
for (BufferedDataTable reference : references) {
if (reference.getOwner() == getOwner() && !savedTableIDs.contains(reference.getBufferedTableId())) {
int index = referenceDirs.size();
String dirName = "r" + index;
File subDir = new File(dir, dirName);
if (!subDir.mkdir() && !subDir.isDirectory()) {
throw new IOException("Could not create directory " + subDir.getAbsolutePath());
}
if (!subDir.canWrite()) {
throw new IOException("Unable to write directory " + subDir.getAbsolutePath());
}
referenceDirs.add(dirName);
reference.save(subDir, savedTableIDs, exec);
}
}
s.addStringArray(CFG_TABLE_REFERENCE, referenceDirs.toArray(new String[referenceDirs.size()]));
m_delegate.saveToFile(outFile, s, exec);
}
// only write the data file to the settings if it has been created
if (outFile.exists()) {
s.addString(CFG_TABLE_FILE_NAME, TABLE_FILE);
} else {
s.addString(CFG_TABLE_FILE_NAME, null);
}
saveSpec(getDataTableSpec(), dir);
File dataXML = new File(dir, TABLE_DESCRIPTION_FILE);
s.saveToXML(new BufferedOutputStream(new FileOutputStream(dataXML)));
}
use of org.knime.core.data.container.WrappedTable in project knime-core by knime.
the class ExecutionContext method createWrappedTable.
/**
* Creates a new <code>BufferedDataTable</code> that simply wraps the
* argument table. This is useful when a node just passes on the input table,
* for example. If the implementation of NodeModel does not use this method
* (but simply returns the input table directy), the framework will perform
* the wrapping operation.
* @param in The input table to wrap.
* @return A new table which can be returned in the execute method.
* @throws NullPointerException If the argument is null.
*/
public BufferedDataTable createWrappedTable(final BufferedDataTable in) {
WrappedTable t = new WrappedTable(in);
BufferedDataTable out = new BufferedDataTable(t);
out.setOwnerRecursively(m_node);
return out;
}
Aggregations