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

com.datatorrent.lib.algo.MatchAllMap 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.algo;

import java.util.Map;

import com.datatorrent.api.DefaultInputPort;
import com.datatorrent.api.DefaultOutputPort;
import com.datatorrent.api.annotation.OperatorAnnotation;

import com.datatorrent.lib.util.BaseMatchOperator;
import com.datatorrent.lib.util.UnifierBooleanAnd;

/**
 * This operator filters the incoming stream of key value pairs by obtaining the values corresponding to a specified key,
 * and comparing those values to a specified number. 
 * If the comparison returns true for all key value pairs within a window, then a true is emitted at the end of the window. 
 * Otherwise a false is emitted at the end of the window.
 * 

* Each tuple is tested for the compare function. The function is given by * "key", "value", and "cmp". If all tuples passes a Boolean(true) is emitted, else a Boolean(false) is emitted on end of window on the output port "all". * The comparison is done by getting double value from the Number. *

*

* This module is an end of window module
*
* StateFull : Yes, tuple are compared across application window(s).
* Partitions : Yes, match status is unified on output port.
*
* Ports:
* data: expects Map<K,V extends Number>
* all: emits Boolean
*
* Properties:
* key: The key on which compare is done
* value: The value to compare with
* cmp: The compare function. Supported values are "lte", "lt", "eq", "neq", "gt", "gte". Default is "eq"
*
* Specific compile time checks:
* Key must be non empty
* Value must be able to convert to a "double"
* Compare string, if specified, must be one of "lte", "lt", "eq", "neq", "gt", "gte"
*
*

* * @displayName Emit All Matching Values (Number) * @category Rules and Alerts * @tags filter, key value * * @since 0.3.2 */ @OperatorAnnotation(partitionable = true) public class MatchAllMap extends BaseMatchOperator { /** * The input port on which key value pairs are received. */ public final transient DefaultInputPort> data = new DefaultInputPort>() { /** * Sets match flag to false for on first non matching tuple */ @Override public void process(Map tuple) { if (!result) { return; } V val = tuple.get(getKey()); if (val == null) { // skip if key does not exist return; } result = compareValue(val.doubleValue()); } }; /** * The output port on which true is emitted if all key value pairs satisfy the specified comparison function. */ public final transient DefaultOutputPort all = new DefaultOutputPort() { @Override public Unifier getUnifier() { return new UnifierBooleanAnd(); } }; protected boolean result = true; /** * Emits the match flag */ @Override public void endWindow() { all.emit(result); result = true; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy