All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.datatorrent.lib.math.RunningAverage 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.OperatorAnnotation;
import com.datatorrent.common.util.BaseOperator;

/**
 * Calculate the running average of the input numbers and emit it at the end of the window. 
 * 

* This is an end of window operator.
*
* StateFull : Yes, average is computed over application window.
* Partitions : No, will yield wrong results.
*
* Ports:
* input: expects Number
* longAverage: emits Long
* integerAverage: emits Integer
* doubleAverage: emits Double
* floatAverage: emits Float
*
* @displayName Running Average * @category Math * @tags average, numeric * @since 0.3.3 */ @OperatorAnnotation(partitionable = false) public class RunningAverage extends BaseOperator { /** * Computed average. */ double average; /** * Number of values on input port. */ long count; /** * Input number port. */ public final transient DefaultInputPort input = new DefaultInputPort() { @Override public void process(Number tuple) { double sum = (count * average) + tuple.doubleValue(); count++; average = sum / count; } }; /** * Double average output port. */ public final transient DefaultOutputPort doubleAverage = new DefaultOutputPort(); /** * Float average output port. */ public final transient DefaultOutputPort floatAverage = new DefaultOutputPort(); /** * Long average output port. */ public final transient DefaultOutputPort longAverage = new DefaultOutputPort(); /** * Integer average output port. */ public final transient DefaultOutputPort integerAverage = new DefaultOutputPort(); /** * End window operator override. */ @Override public void endWindow() { if (doubleAverage.isConnected()) { doubleAverage.emit(average); } if (floatAverage.isConnected()) { floatAverage.emit((float)average); } if (longAverage.isConnected()) { longAverage.emit((long)average); } if (integerAverage.isConnected()) { integerAverage.emit((int)average); } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy