groovyx.gpars.dataflow
Class DataflowExpression<T>

java.lang.Object
  extended by groovyx.gpars.serial.WithSerialId
      extended by groovyx.gpars.dataflow.DataflowExpression<T>
All Implemented Interfaces:
groovy.lang.GroovyObject, DataflowReadChannel<T>, java.io.Serializable
Direct Known Subclasses:
DataflowComplexExpression, DataflowExpression.TransformOne, DataflowGetPropertyExpression, DataflowVariable, RemoteDataflowExpression

public abstract class DataflowExpression<T>
extends WithSerialId
implements groovy.lang.GroovyObject, DataflowReadChannel<T>

The base class for all dataflow elements.

Author:
Alex Tkachman, Vaclav Pech
See Also:
Serialized Form

Nested Class Summary
static class DataflowExpression.BindDataflow<T>
          Represents a remote message binding a value to a remoted DataflowExpression
(package private)  class DataflowExpression.DataflowExpressionsCollector
          Listener for availability of data flow expressions we depend from
private static class DataflowExpression.TransformMany<V>
           
private static class DataflowExpression.TransformOne<V>
           
private static class DataflowExpression.WaitingThread
          A logical representation of a synchronous or asynchronous request to read the value once it is bound.
 
Field Summary
private static java.lang.String ATTACHMENT
           
private static DataflowExpression.WaitingThread dummyWaitingThread
          A request chain terminator
private  groovy.lang.MetaClass metaClass
          The current metaclass
private static java.lang.String RESULT
           
protected static int S_INITIALIZED
           
protected static int S_INITIALIZING
           
protected static int S_NOT_INITIALIZED
          Possible states
private static long serialVersionUID
           
protected  int state
          Holds the current state of the variable
protected static java.util.concurrent.atomic.AtomicIntegerFieldUpdater<DataflowExpression> stateUpdater
          Updater for the state field
protected  T value
          Holds the actual value.
private  DataflowExpression.WaitingThread waiting
          Points to the head of the chain of requests waiting for a value to be bound
protected static java.util.concurrent.atomic.AtomicReferenceFieldUpdater<DataflowExpression,DataflowExpression.WaitingThread> waitingUpdater
          Updater for the waiting field
 
Fields inherited from class groovyx.gpars.serial.WithSerialId
serialHandle
 
Constructor Summary
protected DataflowExpression()
          Creates a new unbound Dataflow Expression
 
Method Summary
 void bind(T value)
          Assigns a value to the variable.
 void bindSafely(T value)
          Assigns a value to the variable.
 void bindUnique(T value)
          Assigns a value to the variable.
private  void doBind(T value)
          Performs the actual bind operation, unblocks all blocked threads and informs all asynchronously waiting actors.
private  void doBindImpl(T value)
           
 void doBindRemote(java.util.UUID hostId, T message)
          Binds the value after receiving a bing message over the wire
protected  T evaluate()
          Evaluate expression after the ones we depend on are ready
 groovy.lang.MetaClass getMetaClass()
           
 java.lang.Object getProperty(java.lang.String propertyName)
          Returns either standard property of expression or creates expression, which will request given property when receiver became available
 T getVal()
          Reads the value of the variable.
 T getVal(long timeout, java.util.concurrent.TimeUnit units)
          Reads the value of the variable.
 void getValAsync(MessageStream callback)
          Asynchronously retrieves the value of the variable.
 void getValAsync(java.lang.Object attachment, MessageStream callback)
          Used by Dataflow operators.
private static void handleInterruption(java.util.concurrent.atomic.AtomicBoolean newWaiting)
           
 java.lang.Object invokeMethod(java.lang.String name, java.lang.Object args)
           
 boolean isBound()
          Check if value has been set already for this expression
 void join()
          Blocks, if the value has not been assigned yet to the DataflowVariable
 void join(long timeout, java.util.concurrent.TimeUnit units)
          Blocks, if the value has not been assigned yet to the DataflowVariable
private  void notifyRemote(java.util.UUID hostId)
          Sends notifications to all subscribers
 DataflowExpression<T> poll()
          Retrieves the bound value.
 void rightShift(groovy.lang.Closure closure)
          Schedule closure to be executed by pooled actor after data became available It is important to notice that even if data already available the execution of closure will not happen immediately but will be scheduled
 void setMetaClass(groovy.lang.MetaClass metaClass)
           
 void setProperty(java.lang.String propertyName, java.lang.Object newValue)
           
private  void scheduleCallback(java.lang.Object attachment, MessageStream callback)
          Sends the result back to the actor, which is waiting asynchronously for the value to be bound.
protected  void subscribe()
          Utility method to call at the very end of constructor of derived expressions.
protected  void subscribe(DataflowExpression.DataflowExpressionsCollector listener)
           
 java.lang.String toString()
           
static
<V> DataflowExpression<V>
transform(java.lang.Object another, groovy.lang.Closure closure)
           
 void whenBound(groovy.lang.Closure closure)
          Schedule closure to be executed by pooled actor after data becomes available It is important to notice that even if data already available the execution of closure will not happen immediately but will be scheduled.
 void whenBound(MessageStream stream)
          Send the bound data to provided stream when it becomes available
 void whenBound(groovyx.gpars.group.PGroup group, groovy.lang.Closure closure)
          Schedule closure to be executed by pooled actor after data becomes available It is important to notice that even if data already available the execution of closure will not happen immediately but will be scheduled.
 void whenBound(Pool pool, groovy.lang.Closure closure)
          Schedule closure to be executed by pooled actor after data becomes available It is important to notice that even if data already available the execution of closure will not happen immediately but will be scheduled.
 void wheneverBound(groovy.lang.Closure closure)
          Send all pieces of data bound in the future to the provided stream when it becomes available *
 void wheneverBound(MessageStream stream)
          Send all pieces of data bound in the future to the provided stream when it becomes available
 
Methods inherited from class groovyx.gpars.serial.WithSerialId
createRemoteHandle, getOrCreateSerialHandle, getRemoteClass, writeReplace
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ATTACHMENT

private static final java.lang.String ATTACHMENT
See Also:
Constant Field Values

RESULT

private static final java.lang.String RESULT
See Also:
Constant Field Values

stateUpdater

protected static final java.util.concurrent.atomic.AtomicIntegerFieldUpdater<DataflowExpression> stateUpdater
Updater for the state field


waitingUpdater

protected static final java.util.concurrent.atomic.AtomicReferenceFieldUpdater<DataflowExpression,DataflowExpression.WaitingThread> waitingUpdater
Updater for the waiting field


serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

metaClass

private groovy.lang.MetaClass metaClass
The current metaclass


value

protected volatile T value
Holds the actual value. Is null before a concrete value is bound to it.


state

protected volatile int state
Holds the current state of the variable


waiting

private volatile DataflowExpression.WaitingThread waiting
Points to the head of the chain of requests waiting for a value to be bound


S_NOT_INITIALIZED

protected static final int S_NOT_INITIALIZED
Possible states

See Also:
Constant Field Values

S_INITIALIZING

protected static final int S_INITIALIZING
See Also:
Constant Field Values

S_INITIALIZED

protected static final int S_INITIALIZED
See Also:
Constant Field Values

dummyWaitingThread

private static final DataflowExpression.WaitingThread dummyWaitingThread
A request chain terminator

Constructor Detail

DataflowExpression

protected DataflowExpression()
Creates a new unbound Dataflow Expression

Method Detail

isBound

public final boolean isBound()
Check if value has been set already for this expression

Specified by:
isBound in interface DataflowReadChannel<T>
Returns:
true if bound already

getValAsync

public final void getValAsync(MessageStream callback)
Asynchronously retrieves the value of the variable. Sends the actual value of the variable as a message back the the supplied actor once the value has been bound. The actor can perform other activities or release a thread back to the pool by calling react() waiting for the message with the value of the Dataflow Variable.

Specified by:
getValAsync in interface DataflowReadChannel<T>
Parameters:
callback - An actor to send the bound value to.

getValAsync

public final void getValAsync(java.lang.Object attachment,
                              MessageStream callback)
Used by Dataflow operators. Asynchronously retrieves the value of the variable. Sends a message back the the supplied MessageStream with a map holding the supplied attachment under the 'attachment' key and the actual value of the variable under the 'result' key once the value has been bound. Attachment is an arbitrary value helping the actor.operator match its request with the reply. The actor/operator can perform other activities or release a thread back to the pool by calling react() waiting for the message with the value of the Dataflow Variable.

Specified by:
getValAsync in interface DataflowReadChannel<T>
Parameters:
attachment - arbitrary non-null attachment if reader needs better identification of result
callback - An actor to send the bound value plus the supplied index to.

join

public final void join()
                throws java.lang.InterruptedException
Blocks, if the value has not been assigned yet to the DataflowVariable

Throws:
java.lang.InterruptedException - If the current thread gets interrupted while waiting for the variable to be bound

join

public final void join(long timeout,
                       java.util.concurrent.TimeUnit units)
                throws java.lang.InterruptedException
Blocks, if the value has not been assigned yet to the DataflowVariable

Parameters:
timeout - The timeout value
units - Units for the timeout
Throws:
java.lang.InterruptedException - If the current thread gets interrupted while waiting for the variable to be bound

getVal

public final T getVal()
               throws java.lang.InterruptedException
Reads the value of the variable. Blocks, if the value has not been assigned yet.

Specified by:
getVal in interface DataflowReadChannel<T>
Returns:
The actual value
Throws:
java.lang.InterruptedException - If the current thread gets interrupted while waiting for the variable to be bound

getVal

public final T getVal(long timeout,
                      java.util.concurrent.TimeUnit units)
               throws java.lang.InterruptedException
Reads the value of the variable. Blocks up to given timeout, if the value has not been assigned yet.

Specified by:
getVal in interface DataflowReadChannel<T>
Parameters:
timeout - The timeout value
units - Units for the timeout
Returns:
The actual value
Throws:
java.lang.InterruptedException - If the current thread gets interrupted while waiting for the variable to be bound

poll

public final DataflowExpression<T> poll()
Retrieves the bound value. Returns null, if no value is available.

Specified by:
poll in interface DataflowReadChannel<T>
Returns:
The value bound to the DFV or null
Throws:
java.lang.InterruptedException - If the current thread is interrupted

handleInterruption

private static void handleInterruption(java.util.concurrent.atomic.AtomicBoolean newWaiting)
                                throws java.lang.InterruptedException
Throws:
java.lang.InterruptedException

bindSafely

public final void bindSafely(T value)
Assigns a value to the variable. Returns silently if invoked on an already bound variable.

Parameters:
value - The value to assign

bind

public final void bind(T value)
Assigns a value to the variable. Can only be invoked once on each instance of DataflowVariable. Allows attempts to bind to equal values. Throws exception if invoked on an already bound variable.

Parameters:
value - The value to assign

bindUnique

public final void bindUnique(T value)
Assigns a value to the variable. Can only be invoked once on each instance of DataflowVariable Doesn't allow attempts to bind to equal values. Throws exception if invoked on an already bound variable.

Parameters:
value - The value to assign

doBind

private void doBind(T value)
Performs the actual bind operation, unblocks all blocked threads and informs all asynchronously waiting actors.

Parameters:
value - The value to assign

doBindImpl

private void doBindImpl(T value)

doBindRemote

public final void doBindRemote(java.util.UUID hostId,
                               T message)
Binds the value after receiving a bing message over the wire

Parameters:
hostId - Id of the bind originator host
message - The value to bind

notifyRemote

private void notifyRemote(java.util.UUID hostId)
Sends notifications to all subscribers

Parameters:
hostId - The local host id

scheduleCallback

private void scheduleCallback(java.lang.Object attachment,
                              MessageStream callback)
Sends the result back to the actor, which is waiting asynchronously for the value to be bound. The message will either be a map holding the attachment under the 'attachment' key and the actual bound value under the 'result' key, or it will be the result itself if the callback doesn't care about the index.

Parameters:
attachment - An arbitrary object identifying the request
callback - The actor to send the message to

rightShift

public final void rightShift(groovy.lang.Closure closure)
Schedule closure to be executed by pooled actor after data became available It is important to notice that even if data already available the execution of closure will not happen immediately but will be scheduled

Specified by:
rightShift in interface DataflowReadChannel<T>
Parameters:
closure - closure to execute when data available

whenBound

public final void whenBound(groovy.lang.Closure closure)
Schedule closure to be executed by pooled actor after data becomes available It is important to notice that even if data already available the execution of closure will not happen immediately but will be scheduled.

Specified by:
whenBound in interface DataflowReadChannel<T>
Parameters:
closure - closure to execute when data available

whenBound

public void whenBound(Pool pool,
                      groovy.lang.Closure closure)
Schedule closure to be executed by pooled actor after data becomes available It is important to notice that even if data already available the execution of closure will not happen immediately but will be scheduled.

Specified by:
whenBound in interface DataflowReadChannel<T>
Parameters:
pool - The thread pool to use for task scheduling for asynchronous message delivery
closure - closure to execute when data available

whenBound

public final void whenBound(groovyx.gpars.group.PGroup group,
                            groovy.lang.Closure closure)
Schedule closure to be executed by pooled actor after data becomes available It is important to notice that even if data already available the execution of closure will not happen immediately but will be scheduled.

Specified by:
whenBound in interface DataflowReadChannel<T>
Parameters:
group - The PGroup to use for task scheduling for asynchronous message delivery
closure - closure to execute when data available

whenBound

public final void whenBound(MessageStream stream)
Send the bound data to provided stream when it becomes available

Specified by:
whenBound in interface DataflowReadChannel<T>
Parameters:
stream - stream where to send result

wheneverBound

public final void wheneverBound(groovy.lang.Closure closure)
Send all pieces of data bound in the future to the provided stream when it becomes available *

Specified by:
wheneverBound in interface DataflowReadChannel<T>
Parameters:
closure - closure to execute when data available

wheneverBound

public final void wheneverBound(MessageStream stream)
Send all pieces of data bound in the future to the provided stream when it becomes available

Specified by:
wheneverBound in interface DataflowReadChannel<T>
Parameters:
stream - stream where to send result

transform

public static <V> DataflowExpression<V> transform(java.lang.Object another,
                                                  groovy.lang.Closure closure)

subscribe

protected final void subscribe()
Utility method to call at the very end of constructor of derived expressions. Create and subscribe listener


evaluate

protected T evaluate()
Evaluate expression after the ones we depend on are ready

Returns:
value to bind

subscribe

protected void subscribe(DataflowExpression.DataflowExpressionsCollector listener)

invokeMethod

public final java.lang.Object invokeMethod(java.lang.String name,
                                           java.lang.Object args)
Specified by:
invokeMethod in interface groovy.lang.GroovyObject

getProperty

public final java.lang.Object getProperty(java.lang.String propertyName)
Returns either standard property of expression or creates expression, which will request given property when receiver became available

Specified by:
getProperty in interface groovy.lang.GroovyObject
Parameters:
propertyName - The name of the property to retrieve
Returns:
The property value, instance of DataflowGetPropertyExpression

setMetaClass

public final void setMetaClass(groovy.lang.MetaClass metaClass)
Specified by:
setMetaClass in interface groovy.lang.GroovyObject

setProperty

public final void setProperty(java.lang.String propertyName,
                              java.lang.Object newValue)
Specified by:
setProperty in interface groovy.lang.GroovyObject

getMetaClass

public final groovy.lang.MetaClass getMetaClass()
Specified by:
getMetaClass in interface groovy.lang.GroovyObject

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

Copyright © 2008–2010 Václav Pech. All Rights Reserved.