Class FiniteStateMachineActor
- java.lang.Object
-
- co.paralleluniverse.actors.ActorImpl<Message>
-
- co.paralleluniverse.actors.Actor<java.lang.Object,java.lang.Void>
-
- co.paralleluniverse.actors.behaviors.BehaviorActor
-
- co.paralleluniverse.actors.behaviors.FiniteStateMachineActor
-
- All Implemented Interfaces:
ActorBuilder<java.lang.Object,java.lang.Void>
,Joinable<java.lang.Void>
,Port<java.lang.Object>
,PortAutoCloseable
,ReceivePort<java.lang.Object>
,Stranded
,SuspendableCallable<java.lang.Void>
,java.io.Serializable
,java.lang.AutoCloseable
public class FiniteStateMachineActor extends BehaviorActor
Abehavior
implementing a finite-state-machine. TheFiniteStateMachineActor
's behavior is implemented by overriding theinitialState()
method to return an initial *state* which then runs as the actor's body until it returns a next state, and so on until a state returnsTERMINATE
, in which case the actor terminates. Initialization and termination behavior can be implemented by either 1) subclassing this class and overriding some or all of:init
,terminate
, or 2) providing an instance ofInitializer
which implements these methods to the constructor.- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class co.paralleluniverse.actors.ActorImpl
ActorImpl.ActorLifecycleListener
-
Nested classes/interfaces inherited from interface co.paralleluniverse.strands.channels.ReceivePort
ReceivePort.EOFException
-
-
Field Summary
Fields Modifier and Type Field Description protected static java.lang.Object
NULL_RETURN_VALUE
static SuspendableCallable<SuspendableCallable>
TERMINATE
The termination state for the FSM-
Fields inherited from class co.paralleluniverse.actors.ActorImpl
flightRecorder, ref
-
-
Constructor Summary
Constructors Constructor Description FiniteStateMachineActor()
Creates a new FSM actorFiniteStateMachineActor(Initializer initializer)
Creates a new FSM actorFiniteStateMachineActor(Initializer initializer, MailboxConfig mailboxConfig)
Creates a new FSM actorFiniteStateMachineActor(MailboxConfig mailboxConfig)
Creates a new FSM actorFiniteStateMachineActor(java.lang.String name)
Creates a new FSM actorFiniteStateMachineActor(java.lang.String name, Initializer initializer)
Creates a new FSM actorFiniteStateMachineActor(java.lang.String name, Initializer initializer, MailboxConfig mailboxConfig)
Creates a new FSM actorFiniteStateMachineActor(java.lang.String name, Initializer initializer, Strand strand, MailboxConfig mailboxConfig)
Creates a new FSM actorFiniteStateMachineActor(java.lang.String name, Initializer initializer, Strand strand, MailboxConfig mailboxConfig, SuspendableCallable<SuspendableCallable> initialState)
Creates a new FSM actorFiniteStateMachineActor(java.lang.String name, MailboxConfig mailboxConfig)
Creates a new FSM actor
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
behavior()
Called byBehaviorActor.doRun()
as the body of the logic.static FiniteStateMachineActor
currentFiniteStateMachineActor()
protected SuspendableCallable<SuspendableCallable>
initialState()
Returns this finite-state-machine actor's initial state; the default implementation returnsTERMINATE
.org.slf4j.Logger
log()
TheLogger
object associated with this actor.protected java.lang.Object
readResolve()
SuspendableCallable<SuspendableCallable>
receive(long timeout, java.util.concurrent.TimeUnit unit, MessageProcessor<java.lang.Object,SuspendableCallable<SuspendableCallable>> proc)
Performs a selective receive.<M> M
receive(long timeout, java.util.concurrent.TimeUnit unit, java.lang.Class<M> type)
Performs a selective receive based on type.SuspendableCallable<SuspendableCallable>
receive(MessageProcessor<java.lang.Object,SuspendableCallable<SuspendableCallable>> proc)
Performs a selective receive.SuspendableCallable<SuspendableCallable>
receive(Timeout timeout, MessageProcessor<java.lang.Object,SuspendableCallable<SuspendableCallable>> proc)
Performs a selective receive.<M> M
receive(Timeout timeout, java.lang.Class<M> type)
Performs a selective receive based on type.<M> M
receive(java.lang.Class<M> type)
Performs a selective receive based on type.SuspendableCallable<SuspendableCallable>
tryReceive(MessageProcessor<java.lang.Object,SuspendableCallable<SuspendableCallable>> proc)
Tries to perform a selective receive.<M> M
tryReceive(java.lang.Class<M> type)
Tries to performs a selective receive based on type.-
Methods inherited from class co.paralleluniverse.actors.behaviors.BehaviorActor
checkCodeSwap, doRun, getInitializer, handleLifecycleMessage, handleMessage, init, isRunning, makeRef, onStart, onTerminate, ref, self, shutdown, spawn, spawn, spawn, spawnThread, terminate
-
Methods inherited from class co.paralleluniverse.actors.Actor
addLifecycleListener, build, checkThrownIn, close, currentActor, filterMessage, get, get, getActor, getDeathCause, getMonitor, getName, getQueueLength, getStrand, hire, hire, internalSend, internalSendNonSuspendable, interrupt, isClosed, isDone, isInActor, isRegistered, isStarted, join, join, link, linked, mailbox, migrate, migrateAndRestart, monitor, monitorAddDeath, monitorAddMessage, monitorResetSkippedMessages, monitorSkippedMessage, newActor, newActor, onCodeChange, receive, receive, receive, register, register, reinstantiate, removeLifecycleListener, removeObserverListeners, run, sendSync, setForwardWatch, setMonitor, setName, setStrand, start, stopMonitor, throwIn, toString, tryReceive, trySend, unlink, unlinked, unregister, unwatch, verifyInActor, verifyOnActorStrand, watch, writeReplace
-
Methods inherited from class co.paralleluniverse.actors.ActorImpl
close, getLifecycleListener, getMailbox, isRecordingLevel, record, record, record, record, record, record, record, sendOrInterrupt
-
-
-
-
Field Detail
-
NULL_RETURN_VALUE
protected static java.lang.Object NULL_RETURN_VALUE
-
TERMINATE
public static final SuspendableCallable<SuspendableCallable> TERMINATE
The termination state for the FSM
-
-
Constructor Detail
-
FiniteStateMachineActor
public FiniteStateMachineActor(java.lang.String name, Initializer initializer, Strand strand, MailboxConfig mailboxConfig)
Creates a new FSM actor- Parameters:
name
- the actor name (may benull
).initializer
- an optional delegate object that will be run upon actor initialization and termination. May benull
.strand
- this actor's strand.mailboxConfig
- this actor's mailbox settings.
-
FiniteStateMachineActor
public FiniteStateMachineActor(java.lang.String name, Initializer initializer, Strand strand, MailboxConfig mailboxConfig, SuspendableCallable<SuspendableCallable> initialState)
Creates a new FSM actor- Parameters:
name
- the actor name (may benull
).initializer
- an optional delegate object that will be run upon actor initialization and termination. May benull
.strand
- this actor's strand.mailboxConfig
- this actor's mailbox settings.initialState
- the initial state; will be used instead of callinginitialState()
.
-
FiniteStateMachineActor
public FiniteStateMachineActor(java.lang.String name, Initializer initializer, MailboxConfig mailboxConfig)
Creates a new FSM actor- Parameters:
name
- the actor name (may benull
).initializer
- an optional delegate object that will be run upon actor initialization and termination. May benull
.mailboxConfig
- this actor's mailbox settings.
-
FiniteStateMachineActor
public FiniteStateMachineActor(java.lang.String name, Initializer initializer)
Creates a new FSM actor- Parameters:
name
- the actor name (may benull
).initializer
- an optional delegate object that will be run upon actor initialization and termination. May benull
.
-
FiniteStateMachineActor
public FiniteStateMachineActor(Initializer initializer, MailboxConfig mailboxConfig)
Creates a new FSM actor- Parameters:
initializer
- an optional delegate object that will be run upon actor initialization and termination. May benull
.mailboxConfig
- this actor's mailbox settings.
-
FiniteStateMachineActor
public FiniteStateMachineActor(Initializer initializer)
Creates a new FSM actor- Parameters:
initializer
- an optional delegate object that will be run upon actor initialization and termination. May benull
.
-
FiniteStateMachineActor
public FiniteStateMachineActor(java.lang.String name, MailboxConfig mailboxConfig)
Creates a new FSM actor- Parameters:
name
- the actor name (may benull
).mailboxConfig
- this actor's mailbox settings.
-
FiniteStateMachineActor
public FiniteStateMachineActor(java.lang.String name)
Creates a new FSM actor- Parameters:
name
- the actor name (may benull
).
-
FiniteStateMachineActor
public FiniteStateMachineActor(MailboxConfig mailboxConfig)
Creates a new FSM actor- Parameters:
mailboxConfig
- this actor's mailbox settings.
-
FiniteStateMachineActor
public FiniteStateMachineActor()
Creates a new FSM actor
-
-
Method Detail
-
currentFiniteStateMachineActor
public static FiniteStateMachineActor currentFiniteStateMachineActor()
-
log
public org.slf4j.Logger log()
Description copied from class:BehaviorActor
TheLogger
object associated with this actor.- Specified by:
log
in classBehaviorActor
-
initialState
protected SuspendableCallable<SuspendableCallable> initialState()
Returns this finite-state-machine actor's initial state; the default implementation returnsTERMINATE
.- Returns:
- this finite-state-machine actor's initial state
-
behavior
protected final void behavior() throws java.lang.InterruptedException, SuspendExecution
Description copied from class:BehaviorActor
Called byBehaviorActor.doRun()
as the body of the logic. By default, this implementation runs code similar to:while (isRunning()) handleMessage(receive());
- Overrides:
behavior
in classBehaviorActor
- Throws:
java.lang.InterruptedException
SuspendExecution
-
receive
public final SuspendableCallable<SuspendableCallable> receive(MessageProcessor<java.lang.Object,SuspendableCallable<SuspendableCallable>> proc) throws SuspendExecution, java.lang.InterruptedException
Performs a selective receive. This method blocks until a message that isselected
by the givenMessageProcessor
is available in the mailbox, and returns the value returned byMessageProcessor.process
.Messages that are not selected, are temporarily skipped. They will remain in the mailbox until another call to receive (selective or non-selective) retrieves them.
- Parameters:
proc
- performs the selection.- Returns:
- The non-null value returned by
MessageProcessor.process
- Throws:
java.lang.InterruptedException
SuspendExecution
-
receive
public final SuspendableCallable<SuspendableCallable> receive(long timeout, java.util.concurrent.TimeUnit unit, MessageProcessor<java.lang.Object,SuspendableCallable<SuspendableCallable>> proc) throws java.util.concurrent.TimeoutException, SuspendExecution, java.lang.InterruptedException
Performs a selective receive. This method blocks (but for no longer than the given timeout) until a message that isselected
by the givenMessageProcessor
is available in the mailbox, and returns the value returned byMessageProcessor.process
. If the given timeout expires, this method returnsnull
.Messages that are not selected, are temporarily skipped. They will remain in the mailbox until another call to receive (selective or non-selective) retrieves them.
- Parameters:
timeout
- the duration to wait for a matching message to arrive.unit
- timeout's time unit.proc
- performs the selection.- Returns:
- The non-null value returned by
MessageProcessor.process
, ornull
if the timeout expired. - Throws:
java.lang.InterruptedException
java.util.concurrent.TimeoutException
SuspendExecution
-
receive
public final SuspendableCallable<SuspendableCallable> receive(Timeout timeout, MessageProcessor<java.lang.Object,SuspendableCallable<SuspendableCallable>> proc) throws java.util.concurrent.TimeoutException, SuspendExecution, java.lang.InterruptedException
Performs a selective receive. This method blocks (but for no longer than the given timeout) until a message that isselected
by the givenMessageProcessor
is available in the mailbox, and returns the value returned byMessageProcessor.process
. If the given timeout expires, this method returnsnull
.Messages that are not selected, are temporarily skipped. They will remain in the mailbox until another call to receive (selective or non-selective) retrieves them.
- Parameters:
timeout
- the method will not block for longer than the amount remaining in theTimeout
proc
- performs the selection.- Returns:
- The non-null value returned by
MessageProcessor.process
, ornull
if the timeout expired. - Throws:
java.lang.InterruptedException
java.util.concurrent.TimeoutException
SuspendExecution
-
tryReceive
public final SuspendableCallable<SuspendableCallable> tryReceive(MessageProcessor<java.lang.Object,SuspendableCallable<SuspendableCallable>> proc)
Tries to perform a selective receive. If a messageselected
by the givenMessageProcessor
is immediately available in the mailbox, returns the value returned byMessageProcessor.process
. This method never blocks.Messages that are not selected, are temporarily skipped. They will remain in the mailbox until another call to receive (selective or non-selective) retrieves them.
- Parameters:
proc
- performs the selection.- Returns:
- The non-null value returned by
MessageProcessor.process
, ornull
if no message was slected.
-
receive
public final <M> M receive(java.lang.Class<M> type) throws SuspendExecution, java.lang.InterruptedException
Performs a selective receive based on type. This method blocks until a message of the given type is available in the mailbox, and returns it.Messages that are not selected, are temporarily skipped. They will remain in the mailbox until another call to receive (selective or non-selective) retrieves them.
- Parameters:
type
- the type of the messages to select- Returns:
- The next message of the wanted type.
- Throws:
java.lang.InterruptedException
SuspendExecution
-
receive
public final <M> M receive(long timeout, java.util.concurrent.TimeUnit unit, java.lang.Class<M> type) throws SuspendExecution, java.lang.InterruptedException, java.util.concurrent.TimeoutException
Performs a selective receive based on type. This method blocks (but for no longer than the given timeout) until a message of the given type is available in the mailbox, and returns it. If the given timeout expires, this method returnsnull
.Messages that are not selected, are temporarily skipped. They will remain in the mailbox until another call to receive (selective or non-selective) retrieves them.
- Parameters:
timeout
- the duration to wait for a matching message to arrive.unit
- timeout's time unit.type
- the type of the messages to select- Returns:
- The next message of the wanted type, or
null
if the timeout expires. - Throws:
SuspendExecution
java.lang.InterruptedException
java.util.concurrent.TimeoutException
-
receive
public final <M> M receive(Timeout timeout, java.lang.Class<M> type) throws SuspendExecution, java.lang.InterruptedException, java.util.concurrent.TimeoutException
Performs a selective receive based on type. This method blocks (but for no longer than the given timeout) until a message of the given type is available in the mailbox, and returns it. If the given timeout expires, this method returnsnull
.Messages that are not selected, are temporarily skipped. They will remain in the mailbox until another call to receive (selective or non-selective) retrieves them.
- Parameters:
timeout
- the method will not block for longer than the amount remaining in theTimeout
type
- the type of the messages to select- Returns:
- The next message of the wanted type, or
null
if the timeout expires. - Throws:
SuspendExecution
java.lang.InterruptedException
java.util.concurrent.TimeoutException
-
tryReceive
public final <M> M tryReceive(java.lang.Class<M> type)
Tries to performs a selective receive based on type. If a message of the given type is immediately found in the mailbox, it is returned. Otherwise this method returnsnull
. This method never blocks.Messages that are not selected, are temporarily skipped. They will remain in the mailbox until another call to receive (selective or non-selective) retrieves them.
- Parameters:
type
- the type of the messages to select- Returns:
- The next message of the wanted type if immediately found;
null
otherwise.
-
readResolve
protected java.lang.Object readResolve() throws java.io.ObjectStreamException
- Overrides:
readResolve
in classActor<java.lang.Object,java.lang.Void>
- Throws:
java.io.ObjectStreamException
-
-