use of org.knime.core.data.DoubleValue in project knime-core by knime.
the class RuleEngine2PortsSimpleSettings method asString.
/**
* Converts a {@link DataCell} to {@link String} for rules.
*
* @param cell A {@link DataCell}.
* @return The value of {@code cell} as a {@link String}, properly escaped.
*/
protected String asString(final DataCell cell) {
if (cell.isMissing()) {
return "\"?\"";
}
if (cell instanceof StringValue) {
StringValue sv = (StringValue) cell;
String s = sv.getStringValue();
if (isTreatOutcomesAsReferences() && s.startsWith("$")) {
return s;
}
return escapedText(s);
}
if (cell instanceof BooleanValue) {
return Boolean.toString(((BooleanValue) cell).getBooleanValue()).toUpperCase();
}
String string = cell.toString();
if (cell instanceof DoubleValue) {
return string;
}
return escapedText(string);
}
use of org.knime.core.data.DoubleValue in project knime-core by knime.
the class RuleEngineVariable2PortsNodeModel method performExecute.
/**
* Creates the flow variable according to the computed value.
*
* @param rules The rules to check for match.
* @throws InvalidSettingsException When there is an error in the settings.
*/
private void performExecute(final List<Rule> rules, final DataType outcomeColumnType) throws InvalidSettingsException {
String newFlowVar = m_newVariableName;
if (newFlowVar == null || newFlowVar.isEmpty()) {
newFlowVar = DEFAULT_VARIABLE_NAME;
}
final DataType outType = computeOutputType(rules, outcomeColumnType);
final VariableProvider provider = new VariableProvider() {
@Override
public Object readVariable(final String name, final Class<?> type) {
return RuleEngineVariable2PortsNodeModel.this.readVariable(name, type);
}
@Override
@Deprecated
public int getRowCount() {
throw new IllegalStateException("Row count is not available.");
}
@Override
public long getRowCountLong() {
throw new IllegalStateException("Row count is not available.");
}
@Override
@Deprecated
public int getRowIndex() {
throw new IllegalStateException("Row index is not available.");
}
@Override
public long getRowIndexLong() {
throw new IllegalStateException("Row index is not available.");
}
};
boolean wasMatch = false;
for (Rule r : rules) {
if (r.getCondition().matches(null, provider).getOutcome() == MatchState.matchedAndStop) {
Outcome outcome2 = r.getOutcome();
// r.getSideEffect().perform(row, this);
final DataCell cell = (DataCell) outcome2.getComputedResult(null, provider);
wasMatch = true;
if (outType.equals(StringCell.TYPE) && !cell.isMissing() && !cell.getType().equals(StringCell.TYPE)) {
pushFlowVariableString(newFlowVar, cell.toString());
break;
} else {
if (cell.isMissing()) {
throw new UnsupportedOperationException("Missing result, TODO");
}
if (outType.equals(IntCell.TYPE)) {
pushFlowVariableInt(newFlowVar, ((IntValue) cell).getIntValue());
break;
} else if (outType.equals(DoubleCell.TYPE)) {
pushFlowVariableDouble(newFlowVar, ((DoubleValue) cell).getDoubleValue());
break;
} else if (outType.equals(StringCell.TYPE)) {
pushFlowVariableString(newFlowVar, ((StringValue) cell).getStringValue());
break;
} else {
// TODO
throw new UnsupportedOperationException("Wrong type: " + cell.getClass());
}
}
}
}
if (!wasMatch) {
if (outType.equals(StringCell.TYPE)) {
pushFlowVariableString(newFlowVar, "");
} else if (outType.equals(IntCell.TYPE)) {
pushFlowVariableInt(newFlowVar, 0);
} else {
pushFlowVariableDouble(newFlowVar, 0.0);
}
}
}
use of org.knime.core.data.DoubleValue in project knime-core by knime.
the class TSAverageHandler method getMean.
private DataCell getMean() {
if (m_previous instanceof IntValue) {
// get an int, create an int
double mean = (((DoubleValue) m_previous).getDoubleValue() + ((DoubleValue) m_next).getDoubleValue()) * 0.5;
return new IntCell((int) Math.round(mean));
}
if (m_previous instanceof LongValue) {
// get an int, create an int
double mean = (((DoubleValue) m_previous).getDoubleValue() + ((DoubleValue) m_next).getDoubleValue()) * 0.5;
return new LongCell(Math.round(mean));
}
if (m_previous instanceof DoubleValue) {
// get an double, create an double
double mean = (((DoubleValue) m_previous).getDoubleValue() + ((DoubleValue) m_next).getDoubleValue()) * 0.5;
return new DoubleCell(mean);
}
if (m_previous instanceof DateAndTimeValue) {
// get an int, create an int
DateAndTimeValue dataCell1 = (DateAndTimeValue) m_previous;
DateAndTimeValue dataCell2 = ((DateAndTimeValue) m_next);
boolean hasDate = dataCell1.hasDate() | dataCell2.hasDate();
boolean hasTime = dataCell1.hasTime() | dataCell2.hasTime();
boolean hasMilis = dataCell1.hasMillis() | dataCell2.hasMillis();
double d = dataCell1.getUTCTimeInMillis() + dataCell2.getUTCTimeInMillis();
d *= 0.5;
return new DateAndTimeCell((long) d, hasDate, hasTime, hasMilis);
}
return DataType.getMissingCell();
}
use of org.knime.core.data.DoubleValue in project knime-core by knime.
the class TSLinearHandler method getLinearInterpolation.
private DataCell getLinearInterpolation(final double stepnumber, final double stepcount) {
if (m_previous instanceof DoubleValue || m_previous instanceof IntValue || m_previous instanceof LongValue) {
double prev = ((DoubleValue) m_previous).getDoubleValue();
double next = ((DoubleValue) m_next).getDoubleValue();
double lin = prev + 1.0 * stepnumber / (1.0 * stepcount) * (next - prev);
if (m_previous instanceof IntValue) {
// get an int, create an int
return new IntCell((int) Math.round(lin));
}
if (m_previous instanceof LongValue) {
// get an long, create an long
return new LongCell(Math.round(lin));
}
return new DoubleCell(lin);
}
if (m_previous instanceof DateAndTimeValue) {
// get an int, create an int
DateAndTimeValue dataCell1 = (DateAndTimeValue) m_previous;
DateAndTimeValue dataCell2 = ((DateAndTimeValue) m_next);
boolean hasDate = dataCell1.hasDate() | dataCell2.hasDate();
boolean hasTime = dataCell1.hasTime() | dataCell2.hasTime();
boolean hasMilis = dataCell1.hasMillis() | dataCell2.hasMillis();
double prev = dataCell1.getUTCTimeInMillis();
double next = dataCell2.getUTCTimeInMillis();
double d = prev + stepnumber / stepcount * (next - prev);
return new DateAndTimeCell((long) d, hasDate, hasTime, hasMilis);
}
return DataType.getMissingCell();
}
use of org.knime.core.data.DoubleValue in project knime-core by knime.
the class RuleEngineVariableNodeModel method performExecute.
/**
* Creates the flow variable according to the computed value.
*
* @param rules The rules to check for match.
* @throws InvalidSettingsException When there is an error in the settings.
*/
private void performExecute(final List<Rule> rules) throws InvalidSettingsException {
String newFlowVar = m_settings.getNewColName();
final DataType outType = computeOutputType(rules);
final VariableProvider provider = new VariableProvider() {
@Override
public Object readVariable(final String name, final Class<?> type) {
return RuleEngineVariableNodeModel.this.readVariable(name, type);
}
@Override
@Deprecated
public int getRowCount() {
throw new IllegalStateException("Row count is not available.");
}
@Override
@Deprecated
public int getRowIndex() {
throw new IllegalStateException("Row index is not available.");
}
@Override
public long getRowCountLong() {
throw new IllegalStateException("Row count is not available.");
}
@Override
public long getRowIndexLong() {
throw new IllegalStateException("Row index is not available.");
}
};
boolean wasMatch = false;
for (Rule r : rules) {
if (r.getCondition().matches(null, provider).getOutcome() == MatchState.matchedAndStop) {
Outcome outcome2 = r.getOutcome();
// r.getSideEffect().perform(row, this);
final DataCell cell = (DataCell) outcome2.getComputedResult(null, provider);
wasMatch = true;
if (outType.equals(StringCell.TYPE) && !cell.isMissing() && !cell.getType().equals(StringCell.TYPE)) {
pushFlowVariableString(newFlowVar, cell.toString());
break;
} else {
if (cell.isMissing()) {
throw new UnsupportedOperationException("Missing result, TODO");
}
if (outType.equals(IntCell.TYPE)) {
pushFlowVariableInt(newFlowVar, ((IntValue) cell).getIntValue());
break;
} else if (outType.equals(DoubleCell.TYPE)) {
pushFlowVariableDouble(newFlowVar, ((DoubleValue) cell).getDoubleValue());
break;
} else if (outType.equals(StringCell.TYPE)) {
pushFlowVariableString(newFlowVar, ((StringValue) cell).getStringValue());
break;
} else {
// TODO
throw new UnsupportedOperationException("Wrong type: " + cell.getClass());
}
}
}
}
if (!wasMatch) {
if (outType.equals(StringCell.TYPE)) {
pushFlowVariableString(newFlowVar, "");
} else if (outType.equals(IntCell.TYPE)) {
pushFlowVariableInt(newFlowVar, 0);
} else {
pushFlowVariableDouble(newFlowVar, 0.0);
}
}
}
Aggregations