001/*
002 * Copyright (c) 2016-2017 Chris K Wensel <chris@wensel.net>. All Rights Reserved.
003 * Copyright (c) 2007-2017 Xplenty, Inc. All Rights Reserved.
004 *
005 * Project and contact information: http://www.cascading.org/
006 *
007 * This file is part of the Cascading project.
008 *
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *     http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 */
021
022package cascading.operation.expression;
023
024import java.beans.ConstructorProperties;
025
026import cascading.flow.FlowProcess;
027import cascading.operation.Filter;
028import cascading.operation.FilterCall;
029import cascading.tuple.Tuple;
030import org.codehaus.janino.ExpressionEvaluator;
031
032/**
033 * Class ExpressionFilter dynamically resolves a given expression using argument {@link Tuple} values. Any Tuple that
034 * returns true for the given expression will be removed from the stream. This {@link Filter}
035 * is based on the <a href="http://www.janino.net/">Janino</a> compiler.
036 * <p>
037 * Specifically this filter uses the {@link ExpressionEvaluator}, thus the syntax from that class is inherited here.
038 * <p>
039 * An expression may use field names directly as parameters in the expression, or field positions with the syntax
040 * "$n", where n is an integer.
041 * <p>
042 * Given an argument tuple with the fields "a" and "b", the following expression returns true: <br>
043 * {@code a + b == $0 + $1}<br>
044 * <p>
045 * Further, the types of the tuple elements will be coerced into the given parameterTypes. Regardless of the actual
046 * tuple element values, they will be converted to the types expected by the expression.
047 * <p>
048 * Field names used in the expression should be valid Java variable names; for example, '+' or '-' are not allowed.
049 * Also the use of a field name that begins with an upper-case character is likely to fail and should be avoided.
050 */
051public class ExpressionFilter extends ExpressionOperation implements Filter<ScriptOperation.Context>
052  {
053  /**
054   * Constructor ExpressionFilter creates a new ExpressionFilter instance.
055   * <p>
056   * This constructor, when used with incoming arguments that have type information, the argument field
057   * names can be used directly in the the expression, for example {@code a + b }. The type of {@code a} and {@code b}
058   * will be inherited from the incoming argument fields.
059   * <p>
060   * Or, if the incoming argument selector is {@link cascading.tuple.Fields#NONE}, an expression using only static method calls
061   * or constants can be used, for example {@code Math.random() < SomeClass.someValue() }.
062   *
063   * @param expression of type String
064   */
065  @ConstructorProperties({"expression"})
066  public ExpressionFilter( String expression )
067    {
068    super( expression );
069    }
070
071  /**
072   * Constructor ExpressionFilter creates a new ExpressionFilter instance.
073   *
074   * @param expression    of type String
075   * @param parameterType of type Class
076   */
077  @ConstructorProperties({"expression", "parameterType"})
078  public ExpressionFilter( String expression, Class parameterType )
079    {
080    super( expression, parameterType );
081    }
082
083  /**
084   * Constructor ExpressionFilter creates a new ExpressionFilter instance.
085   *
086   * @param expression     of type String
087   * @param parameterNames of type String[]
088   * @param parameterTypes of type Class[]
089   */
090  @ConstructorProperties({"expression", "parameterNames", "parameterTypes"})
091  public ExpressionFilter( String expression, String[] parameterNames, Class[] parameterTypes )
092    {
093    super( expression, parameterNames, parameterTypes );
094    }
095
096  @Override
097  public boolean isRemove( FlowProcess flowProcess, FilterCall<Context> filterCall )
098    {
099    return (Boolean) evaluate( filterCall.getContext(), filterCall.getArguments() );
100    }
101  }