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
Abehaviorimplementing 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 ofInitializerwhich 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.ObjectNULL_RETURN_VALUEstatic SuspendableCallable<SuspendableCallable>TERMINATEThe 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 voidbehavior()Called byBehaviorActor.doRun()as the body of the logic.static FiniteStateMachineActorcurrentFiniteStateMachineActor()protected SuspendableCallable<SuspendableCallable>initialState()Returns this finite-state-machine actor's initial state; the default implementation returnsTERMINATE.org.slf4j.Loggerlog()TheLoggerobject associated with this actor.protected java.lang.ObjectreadResolve()SuspendableCallable<SuspendableCallable>receive(long timeout, java.util.concurrent.TimeUnit unit, MessageProcessor<java.lang.Object,SuspendableCallable<SuspendableCallable>> proc)Performs a selective receive.<M> Mreceive(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> Mreceive(Timeout timeout, java.lang.Class<M> type)Performs a selective receive based on type.<M> Mreceive(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> MtryReceive(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:BehaviorActorTheLoggerobject associated with this actor.- Specified by:
login 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, SuspendExecutionDescription copied from class:BehaviorActorCalled byBehaviorActor.doRun()as the body of the logic. By default, this implementation runs code similar to:while (isRunning()) handleMessage(receive());- Overrides:
behaviorin classBehaviorActor- Throws:
java.lang.InterruptedExceptionSuspendExecution
-
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 isselectedby the givenMessageProcessoris 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.InterruptedExceptionSuspendExecution
-
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 isselectedby the givenMessageProcessoris 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, ornullif the timeout expired. - Throws:
java.lang.InterruptedExceptionjava.util.concurrent.TimeoutExceptionSuspendExecution
-
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 isselectedby the givenMessageProcessoris 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 theTimeoutproc- performs the selection.- Returns:
- The non-null value returned by
MessageProcessor.process, ornullif the timeout expired. - Throws:
java.lang.InterruptedExceptionjava.util.concurrent.TimeoutExceptionSuspendExecution
-
tryReceive
public final SuspendableCallable<SuspendableCallable> tryReceive(MessageProcessor<java.lang.Object,SuspendableCallable<SuspendableCallable>> proc)
Tries to perform a selective receive. If a messageselectedby the givenMessageProcessoris 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, ornullif no message was slected.
-
receive
public final <M> M receive(java.lang.Class<M> type) throws SuspendExecution, java.lang.InterruptedExceptionPerforms 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.InterruptedExceptionSuspendExecution
-
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.TimeoutExceptionPerforms 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
nullif the timeout expires. - Throws:
SuspendExecutionjava.lang.InterruptedExceptionjava.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 theTimeouttype- the type of the messages to select- Returns:
- The next message of the wanted type, or
nullif the timeout expires. - Throws:
SuspendExecutionjava.lang.InterruptedExceptionjava.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;
nullotherwise.
-
readResolve
protected java.lang.Object readResolve() throws java.io.ObjectStreamException- Overrides:
readResolvein classActor<java.lang.Object,java.lang.Void>- Throws:
java.io.ObjectStreamException
-
-