cascading.operation.expression
Class ScriptFunction

java.lang.Object
  extended by cascading.operation.BaseOperation<ScriptOperation.Context>
      extended by cascading.operation.expression.ScriptOperation
          extended by cascading.operation.expression.ScriptFunction
All Implemented Interfaces:
Function<ScriptOperation.Context>, Operation<ScriptOperation.Context>, Serializable

public class ScriptFunction
extends ScriptOperation
implements Function<ScriptOperation.Context>

Class ScriptFunction dynamically resolves a given expression using argument Tuple values. This Function is based on the Janino compiler.

This class is different from ScriptTupleFunction in that it allows any return type instance to be returned by the script. ScriptTupleFunction allows only a single Tuple instance to be returned.

Specifically this function uses the ScriptEvaluator, thus the syntax from that class is inherited here.

A script may use field names directly as parameters in the expression, or field positions with the syntax "$n", where n is an integer.

Given an argument tuple with the fields "a" and "b", the following script returns true:
boolean result = (a + b == $0 + $1);
return boolean;

Unlike an "expression" used by ExpressionFunction, a "script" requires each line to end in an semi-colon (@{code ;}) and the final line to be a return statement.

Further, the types of the tuple elements will be coerced into the given parameterTypes. Regardless of the actual tuple element values, they will be converted to the types expected by the script if possible.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class cascading.operation.expression.ScriptOperation
ScriptOperation.Context
 
Field Summary
 
Fields inherited from class cascading.operation.expression.ScriptOperation
block, parameterNames, parameterTypes, returnType
 
Fields inherited from class cascading.operation.BaseOperation
fieldDeclaration, numArgs, trace
 
Fields inherited from interface cascading.operation.Operation
ANY
 
Constructor Summary
ScriptFunction(Fields fieldDeclaration, String script)
          Constructor ScriptFunction creates a new ScriptFunction instance.
ScriptFunction(Fields fieldDeclaration, String script, Class returnType)
          Constructor ScriptFunction creates a new ScriptFunction instance.
ScriptFunction(Fields fieldDeclaration, String script, Class returnType, Class[] expectedTypes)
          Constructor ScriptFunction creates a new ScriptFunction instance.
ScriptFunction(Fields fieldDeclaration, String script, Class returnType, String[] parameterNames, Class[] parameterTypes)
          Constructor ScriptFunction creates a new ScriptFunction instance.
ScriptFunction(Fields fieldDeclaration, String script, String[] parameterNames, Class[] parameterTypes)
          Constructor ScriptFunction creates a new ScriptFunction instance.
 
Method Summary
 String getScript()
           
 void operate(FlowProcess flowProcess, FunctionCall<ScriptOperation.Context> functionCall)
          Method operate provides the implementation of this Function.
 
Methods inherited from class cascading.operation.expression.ScriptOperation
equals, evaluate, getBlock, getEvaluator, getParameterNames, getParameterTypes, getReturnType, guessParameterNames, hashCode, prepare
 
Methods inherited from class cascading.operation.BaseOperation
cleanup, flush, getFieldDeclaration, getNumArgs, getTrace, isSafe, printOperationInternal, toString, toStringInternal
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface cascading.operation.Operation
cleanup, flush, getFieldDeclaration, getNumArgs, isSafe, prepare
 

Constructor Detail

ScriptFunction

@ConstructorProperties(value={"fieldDeclaration","script"})
public ScriptFunction(Fields fieldDeclaration,
                                                 String script)
Constructor ScriptFunction creates a new ScriptFunction instance.

This constructor will use the runtime OperationCall.getArgumentFields() to source the parameterNames and parameterTypes required by the other constructors.

The returnType will be retrieved from the given fieldDeclaration.getTypeClass(0).

Parameters:
fieldDeclaration - of type Fields
script - of type String

ScriptFunction

@ConstructorProperties(value={"fieldDeclaration","script","returnType"})
public ScriptFunction(Fields fieldDeclaration,
                                                 String script,
                                                 Class returnType)
Constructor ScriptFunction creates a new ScriptFunction instance.

This constructor will use the runtime OperationCall.getArgumentFields() to source the parameterNames and parameterTypes required by the other constructors.

Parameters:
fieldDeclaration - of type Fields
script - of type String
returnType - of type Class

ScriptFunction

@ConstructorProperties(value={"fieldDeclaration","script","returnType","expectedTypes"})
public ScriptFunction(Fields fieldDeclaration,
                                                 String script,
                                                 Class returnType,
                                                 Class[] expectedTypes)
Constructor ScriptFunction creates a new ScriptFunction instance.

This constructor will use the runtime OperationCall.getArgumentFields() to source the parameterNames and parameterTypes required by the other constructors, but use expectedTypes to coerce the incoming types to before passing as parameters to the expression.

Parameters:
fieldDeclaration - of type Fields
script - of type String
returnType - of type Class
expectedTypes - of type Class[]

ScriptFunction

@ConstructorProperties(value={"fieldDeclaration","script","returnType","parameterNames","parameterTypes"})
public ScriptFunction(Fields fieldDeclaration,
                                                 String script,
                                                 Class returnType,
                                                 String[] parameterNames,
                                                 Class[] parameterTypes)
Constructor ScriptFunction creates a new ScriptFunction instance.

This constructor expects all parameter type names to be declared with their types. Positional parameters must be named the same as in the given script with the "$" sign prepended.

Parameters:
fieldDeclaration - of type Fields
script - of type String
returnType - of type Class
parameterNames - of type String[]
parameterTypes - of type Class[]

ScriptFunction

@ConstructorProperties(value={"fieldDeclaration","script","parameterNames","parameterTypes"})
public ScriptFunction(Fields fieldDeclaration,
                                                 String script,
                                                 String[] parameterNames,
                                                 Class[] parameterTypes)
Constructor ScriptFunction creates a new ScriptFunction instance.

This constructor expects all parameter type names to be declared with their types. Positional parameters must be named the same as in the given script with the "$" sign prepended.

Parameters:
fieldDeclaration - of type Fields
script - of type String
parameterNames - of type String[]
parameterTypes - of type Class[]
Method Detail

getScript

public String getScript()

operate

public void operate(FlowProcess flowProcess,
                    FunctionCall<ScriptOperation.Context> functionCall)
Description copied from interface: Function
Method operate provides the implementation of this Function.

Specified by:
operate in interface Function<ScriptOperation.Context>
Parameters:
flowProcess - of type FlowProcess
functionCall - of type FunctionCall


Copyright © 2007-2013 Concurrent, Inc. All Rights Reserved.