use of org.knime.core.data.StringValue in project knime-core by knime.
the class DefaultTableTest method createWorkingTables.
// testDefaultTable()
/**
* Creates some new <code>DefaultTable</code> objects that should ...
* theoretically ... do.
*/
private void createWorkingTables() {
// some constructors with (supposedly) no problems
DefaultTable t1 = new DefaultTable(CASE_1_CONTENT, CASE_1_ROWHEADER, CASE_1_COLHEADER);
DataTableSpec t1Spec = t1.getDataTableSpec();
assertEquals(CASE_1_COLHEADER.length, t1Spec.getNumColumns());
// check spec
for (int c = 0; c < CASE_1_COLHEADER.length; c++) {
DataColumnSpec currentColumnSpec = t1Spec.getColumnSpec(c);
String colName = currentColumnSpec.getName().toString();
DataType type = currentColumnSpec.getType();
assertEquals(colName, CASE_1_COLHEADER[c]);
assertTrue(type.isCompatible(StringValue.class));
}
// check content
int r = 0;
for (RowIterator it = t1.iterator(); it.hasNext(); r++) {
DataRow row = it.next();
assertEquals(row.getNumCells(), CASE_1_COLHEADER.length);
for (int i = 0; i < CASE_1_COLHEADER.length; i++) {
StringValue cell = (StringValue) row.getCell(i);
assertEquals(cell.getStringValue(), CASE_1_CONTENT[r][i]);
}
}
// all one-dimensional arrays (meta-info) are optional
new DefaultTable(CASE_1_CONTENT, null, CASE_1_COLHEADER);
new DefaultTable(CASE_1_CONTENT, CASE_1_ROWHEADER, null);
new DefaultTable(CASE_1_CONTENT, CASE_1_ROWHEADER, CASE_1_COLHEADER);
new DefaultTable(CASE_1_CONTENT, null, null);
new DefaultTable(CASE_1_CONTENT, null, CASE_1_COLHEADER);
new DefaultTable(CASE_1_CONTENT, null, null);
}
use of org.knime.core.data.StringValue in project knime-core by knime.
the class StringToDateTimeNodeDialog method updatePreview.
/**
* @param colSelectModel settings model of the column filter
*/
private void updatePreview(final SettingsModelColumnFilter2 colSelectModel) {
final String[] includes = colSelectModel.applyTo(m_spec).getIncludes();
Arrays.stream(colSelectModel.applyTo(m_spec).getIncludes()).mapToInt(s -> m_spec.findColumnIndex(s)).toArray();
m_preview = "";
if (m_dataTable != null) {
if (!(includes.length == 0 || m_dataTable.size() == 0)) {
for (final DataRow row : m_dataTable) {
final DataCell cell = row.getCell(m_spec.findColumnIndex(includes[0]));
if (cell.isMissing()) {
continue;
} else {
m_preview = ((StringValue) cell).getStringValue();
break;
}
}
}
}
m_previewLabel.setText("Content of the first cell: " + m_preview);
}
use of org.knime.core.data.StringValue in project knime-core by knime.
the class StringToDurationPeriodNodeModel method detectTypes.
private void detectTypes(final RowInput rowInput) throws InterruptedException {
final DataTableSpec spec = rowInput.getDataTableSpec();
final String[] includes = m_colSelect.applyTo(spec).getIncludes();
if (m_detectedTypes == null) {
m_detectedTypes = new DataType[includes.length];
}
if (m_type.getStringValue().equals(OutputType.Duration.name())) {
Arrays.fill(m_detectedTypes, DurationCellFactory.TYPE);
}
if (m_type.getStringValue().equals(OutputType.Period.name())) {
Arrays.fill(m_detectedTypes, PeriodCellFactory.TYPE);
}
if (m_type.getStringValue().equals(OutputType.Automatic.name())) {
DataRow row;
while ((row = rowInput.poll()) != null) {
boolean isCellMissing = false;
for (int i = 0; i < includes.length; i++) {
if (m_detectedTypes[i] == null) {
final DataCell cell = row.getCell(spec.findColumnIndex(includes[i]));
if (cell.isMissing()) {
isCellMissing = true;
} else {
final String string = ((StringValue) cell).getStringValue();
try {
DurationPeriodFormatUtils.parseDuration(string);
m_detectedTypes[i] = DurationCellFactory.TYPE;
} catch (DateTimeParseException e1) {
try {
DurationPeriodFormatUtils.parsePeriod(string);
m_detectedTypes[i] = PeriodCellFactory.TYPE;
} catch (DateTimeParseException e2) {
isCellMissing = true;
}
}
}
}
}
if (!isCellMissing) {
// finished - every column type is detected
break;
}
}
}
}
use of org.knime.core.data.StringValue in project knime-core by knime.
the class RuleEngine2PortsNodeModel method computeRearrangerWithoutPMML.
/**
* @param spec
* @param rules
* @param flowVars
* @param ruleIdx
* @param outcomeIdx
* @param outputColumnName
* @return
* @throws InterruptedException
* @throws InvalidSettingsException
*/
private Pair<ColumnRearranger, PortObject> computeRearrangerWithoutPMML(final DataTableSpec spec, final RowInput rules, final Map<String, FlowVariable> flowVars, final int ruleIdx, final int outcomeIdx, final String outputColumnName) throws InterruptedException, InvalidSettingsException {
PortObject po;
ColumnRearranger ret;
RuleFactory factory = RuleFactory.getInstance(RuleNodeSettings.RuleEngine).cloned();
po = InactiveBranchPortObject.INSTANCE;
ret = new ColumnRearranger(spec);
factory.disableMissingComparisons();
factory.disableNaNComparisons();
final List<Rule> ruleList = new ArrayList<>();
int lineNo = 0;
DataRow ruleRow;
while ((ruleRow = rules.poll()) != null) {
lineNo++;
DataCell cell = ruleRow.getCell(ruleIdx);
CheckUtils.checkSetting(!cell.isMissing(), "Missing rule in row: " + ruleRow.getKey());
if (cell instanceof StringValue) {
StringValue sv = (StringValue) cell;
String ruleText = sv.getStringValue();
if (outcomeIdx >= 0) {
try {
ruleText += " => " + m_settings.asStringFailForMissing(ruleRow.getCell(outcomeIdx));
} catch (InvalidSettingsException e) {
if (RuleSupport.isComment(ruleText)) {
ruleText += " => ?";
} else {
throw e;
}
}
}
try {
Rule rule = factory.parse(ruleText, spec, flowVars);
if (rule.getCondition().isEnabled()) {
ruleList.add(rule);
}
} catch (ParseException e) {
ParseException error = Util.addContext(e, ruleText, lineNo);
throw new InvalidSettingsException("Wrong rule in line: " + ruleRow.getKey() + "\n" + error.getMessage(), error);
}
} else {
CheckUtils.checkSetting(false, "Wrong type (" + cell.getType() + ") of rule: " + cell + "\nin row: " + ruleRow.getKey());
}
}
// unfortunately we cannot compute the domain and limits of the output column.
final DataType outType = RuleEngineNodeModel.computeOutputType(ruleList, computeOutcomeType(rules.getDataTableSpec()), RuleNodeSettings.RuleEngine, m_settings.isDisallowLongOutputForCompatibility());
final MutableLong rowIndex = new MutableLong();
final ExecutionMonitor exec = new ExecutionMonitor();
final boolean disallowLongOutputForCompatibility = m_settings.isDisallowLongOutputForCompatibility();
VariableProvider.SingleCellFactoryProto fac = new VariableProvider.SingleCellFactoryProto(new DataColumnSpecCreator(outputColumnName, outType).createSpec()) {
@Override
public DataCell getCell(final DataRow row) {
setProgress(rowIndex.longValue(), m_rowCount, row.getKey(), exec);
rowIndex.increment();
return RuleEngineNodeModel.getRulesOutcome(outType, row, ruleList, disallowLongOutputForCompatibility, this);
}
@Override
public Object readVariable(final String name, final Class<?> type) {
return RuleEngine2PortsNodeModel.this.readVariable(name, type);
}
@Deprecated
@Override
public int getRowCount() {
return RuleEngine2PortsNodeModel.this.getRowCount();
}
@Override
public long getRowCountLong() {
return RuleEngine2PortsNodeModel.this.getRowCountLong();
}
};
if (m_settings.isReplaceColumn()) {
ret.replace(fac, outputColumnName);
} else {
ret.append(fac);
}
return Pair.create(ret, po);
}
use of org.knime.core.data.StringValue in project knime-core by knime.
the class RuleEngine2PortsNodeModel method computeRearrangerWithPMML.
/**
* @param spec
* @param rules
* @param flowVars
* @param ruleIdx
* @param outcomeIdx
* @param confidenceIdx
* @param weightIdx
* @param validationIdx
* @param outputColumnName
* @return
* @throws InterruptedException
* @throws InvalidSettingsException
*/
private Pair<ColumnRearranger, PortObject> computeRearrangerWithPMML(final DataTableSpec spec, final RowInput rules, final Map<String, FlowVariable> flowVars, final int ruleIdx, final int outcomeIdx, final int confidenceIdx, final int weightIdx, final int validationIdx, final String outputColumnName) throws InterruptedException, InvalidSettingsException {
PortObject po;
ColumnRearranger ret;
PMMLDocument doc = PMMLDocument.Factory.newInstance();
final PMML pmmlObj = doc.addNewPMML();
RuleSetModel ruleSetModel = pmmlObj.addNewRuleSetModel();
RuleSet ruleSet = ruleSetModel.addNewRuleSet();
List<DataType> outcomeTypes = new ArrayList<>();
PMMLRuleParser parser = new PMMLRuleParser(spec, flowVars);
int lineNo = 0;
DataRow ruleRow;
while ((ruleRow = rules.poll()) != null) {
++lineNo;
DataCell rule = ruleRow.getCell(ruleIdx);
CheckUtils.checkSetting(!rule.isMissing(), "Missing rule in row: " + ruleRow.getKey());
if (rule instanceof StringValue) {
StringValue ruleText = (StringValue) rule;
String r = ruleText.getStringValue().replaceAll("[\r\n]+", " ");
if (RuleSupport.isComment(r)) {
continue;
}
if (outcomeIdx >= 0) {
r += " => " + m_settings.asStringFailForMissing(ruleRow.getCell(outcomeIdx));
}
ParseState state = new ParseState(r);
try {
PMMLPredicate condition = parser.parseBooleanExpression(state);
SimpleRule simpleRule = ruleSet.addNewSimpleRule();
setCondition(simpleRule, condition);
state.skipWS();
state.consumeText("=>");
state.skipWS();
Expression outcome = parser.parseOutcomeOperand(state, null);
simpleRule.setScore(outcome.toString());
if (confidenceIdx >= 0) {
DataCell confidenceCell = ruleRow.getCell(confidenceIdx);
if (!confidenceCell.isMissing()) {
if (confidenceCell instanceof DoubleValue) {
DoubleValue dv = (DoubleValue) confidenceCell;
double confidence = dv.getDoubleValue();
simpleRule.setConfidence(confidence);
}
}
}
if (weightIdx >= 0) {
DataCell weightCell = ruleRow.getCell(weightIdx);
boolean missing = true;
if (!weightCell.isMissing()) {
if (weightCell instanceof DoubleValue) {
DoubleValue dv = (DoubleValue) weightCell;
double weight = dv.getDoubleValue();
simpleRule.setWeight(weight);
missing = false;
}
}
if (missing && m_settings.isHasDefaultWeight()) {
simpleRule.setWeight(m_settings.getDefaultWeight());
}
}
CheckUtils.checkSetting(outcome.isConstant(), "Outcome is not constant in line " + lineNo + " (" + ruleRow.getKey() + ") for rule: " + rule);
outcomeTypes.add(outcome.getOutputType());
} catch (ParseException e) {
ParseException error = Util.addContext(e, r, lineNo);
throw new InvalidSettingsException("Wrong rule in line: " + ruleRow.getKey() + "\n" + error.getMessage(), error);
}
} else {
CheckUtils.checkSetting(false, "Wrong type (" + rule.getType() + ") of rule: " + rule + "\nin row: " + ruleRow.getKey());
}
}
ColumnRearranger dummy = new ColumnRearranger(spec);
if (!m_settings.isReplaceColumn()) {
dummy.append(new SingleCellFactory(new DataColumnSpecCreator(outputColumnName, RuleEngineNodeModel.computeOutputType(outcomeTypes, computeOutcomeType(rules.getDataTableSpec()), true, m_settings.isDisallowLongOutputForCompatibility())).createSpec()) {
@Override
public DataCell getCell(final DataRow row) {
return null;
}
});
}
PMMLPortObject pmml = createPMMLPortObject(doc, ruleSetModel, ruleSet, parser, dummy.createSpec());
po = pmml;
m_copy = copy(pmml);
String predictionConfidenceColumn = m_settings.getPredictionConfidenceColumn();
if (predictionConfidenceColumn == null || predictionConfidenceColumn.isEmpty()) {
predictionConfidenceColumn = RuleEngine2PortsSettings.DEFAULT_PREDICTION_CONFIDENCE_COLUMN;
}
ret = PMMLRuleSetPredictorNodeModel.createRearranger(pmml, spec, m_settings.isReplaceColumn(), outputColumnName, m_settings.isComputeConfidence(), DataTableSpec.getUniqueColumnName(dummy.createSpec(), predictionConfidenceColumn), validationIdx);
return Pair.create(ret, po);
}
Aggregations