com.datatorrent.lib.math.Change Maven / Gradle / Ivy
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.datatorrent.lib.math;
import com.datatorrent.api.DefaultInputPort;
import com.datatorrent.api.DefaultOutputPort;
import com.datatorrent.api.annotation.OutputPortFieldAnnotation;
import com.datatorrent.lib.util.BaseNumberValueOperator;
/**
* Operator compares data values arriving on input port with base value input operator.
*
*
* Arriving base value is stored in operator for comparison, old base value is overwritten.
* This emits <change in value,percentage change>.
* Operator expects values arriving on data input port and base value input operator.
* Change in value and percentage change in values are emitted on separate ports.
* This operator can not be partitioned, since copies won't get consecutive operators.
* This is StateFull operator, tuples that arrive on base port are kept in
* cache forever.
*
* Input Ports:
* data: expects V extends Number, Data values
* base: expects V extends Number, Base Value stored for comparison
*
* Output Ports:
* change: emits V extends Number, Diff from base value
* percent: emits Doubl, percent change in value compared to base value.
*
*
* Properties:
* inverse: if set to true the key in the filter will block tuple
* filterBy: List of keys to filter on
*
* Specific compile time checks: None
* Specific run time checks: None
*
*
*
* @displayName Change
* @category Math
* @tags change, key value, numeric, percentage
* @since 0.3.3
*/
public class Change extends BaseNumberValueOperator
{
/**
* Input data port that takes a number.
*/
public final transient DefaultInputPort data = new DefaultInputPort()
{
/**
* Process each key, compute change or percent, and emit it.
*/
@Override
public void process(V tuple)
{
if (baseValue != 0) { // Avoid divide by zero, Emit an error tuple?
double cval = tuple.doubleValue() - baseValue;
change.emit(getValue(cval));
percent.emit((cval / baseValue) * 100);
}
}
};
/**
* Input port that takes a number It stores the value for base comparison.
*/
public final transient DefaultInputPort base = new DefaultInputPort()
{
/**
* Process each key to store the value. If same key appears again update
* with latest value.
*/
@Override
public void process(V tuple)
{
if (tuple.doubleValue() != 0.0) { // Avoid divide by zero, Emit an error
// tuple?
baseValue = tuple.doubleValue();
}
}
};
/**
* Output port that emits change in value compared to base value.
*/
@OutputPortFieldAnnotation(optional = true)
public final transient DefaultOutputPort change = new DefaultOutputPort();
/**
* Output port that emits percent change in data value compared to base value.
*/
@OutputPortFieldAnnotation(optional = true)
public final transient DefaultOutputPort percent = new DefaultOutputPort();
/**
* baseValue is a state full field. It is retained across windows.
*/
private double baseValue = 0;
}