Package adams.flow.control
Class Switch
-
- All Implemented Interfaces:
AdditionalInformationHandler
,CleanUpHandler
,Destroyable
,GlobalInfoSupporter
,LenientModeSupporter
,LoggingLevelHandler
,LoggingSupporter
,OptionHandler
,Pausable
,QuickInfoSupporter
,ShallowCopySupporter<Actor>
,SizeOfHandler
,Stoppable
,StoppableWithFeedback
,VariablesInspectionHandler
,FlowPauseStateListener
,VariableChangeListener
,IndexedBooleanConditionSupporter
,AtomicExecution
,Actor
,ActorHandler
,ControlActor
,ErrorHandler
,Flushable
,InputConsumer
,MutableActorHandler
,OutputProducer
,SubFlowWrapUp
,Serializable
,Comparable
public class Switch extends AbstractDirectedControlActor implements InputConsumer, OutputProducer, MutableActorHandler, IndexedBooleanConditionSupporter, LenientModeSupporter
Emulates a Switch control statement. The first 'condition' that evaluates to 'true' executes the corresponding 'case' actor and stops evaluation of conditions.
A catch-all or default can be set up as well by having one more 'case' than 'conditions' (the last case acts as default).
If any output is generated then this gets recorded and forwarded in the flow.
Normally, the number of conditions and number of switch branches have to be the same. A special case are adams.flow.condition.bool.IndexedBooleanCondition conditions. They can be used alone, as they determine which switch branch to use.
Input/output:
- accepts:
adams.flow.core.Unknown
- generates:
adams.flow.core.Unknown
-logging-level <OFF|SEVERE|WARNING|INFO|CONFIG|FINE|FINER|FINEST> (property: loggingLevel) The logging level for outputting errors and debugging output. default: WARNING
-name <java.lang.String> (property: name) The name of the actor. default: Switch
-annotation <adams.core.base.BaseAnnotation> (property: annotations) The annotations to attach to this actor. default:
-skip <boolean> (property: skip) If set to true, transformation is skipped and the input token is just forwarded as it is. default: false
-stop-flow-on-error <boolean> (property: stopFlowOnError) If set to true, the flow execution at this level gets stopped in case this actor encounters an error; the error gets propagated; useful for critical actors. default: false
-silent <boolean> (property: silent) If enabled, then no errors are output in the console; Note: the enclosing actor handler must have this enabled as well. default: false
-finish-before-stopping <boolean> (property: finishBeforeStopping) If enabled, actor first finishes processing all data before stopping. default: false
-stopping-timeout <int> (property: stoppingTimeout) The timeout in milliseconds when waiting for actors to finish (<= 0 for infinity; see 'finishBeforeStopping'). default: -1 minimum: -1
-condition <adams.flow.condition.bool.BooleanCondition> [-condition ...] (property: conditions) The switch conditions to evaluate - the first condition that evaluates to 'true' triggers the execution of the corresponding 'case' actor. default: adams.flow.condition.bool.True
-case <adams.flow.core.Actor> [-case ...] (property: cases) The 'cases' - one of them gets executed if the corresponding 'condition' evaluates to 'true'. default: adams.flow.sink.Null
-lenient <boolean> (property: lenient) If enabled, the token passing through is not required to have a payload (eg generated by the Start source). default: false
- Author:
- fracpete (fracpete at waikato dot ac dot nz)
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Switch.SwitchDirector
A specialized director for the Switch control actor.
-
Field Summary
Fields Modifier and Type Field Description static String
BACKUP_INPUT
the key for storing the current input token in the backup.protected List<Actor>
m_Cases
the "cases" to execute if the corresponding expression matches.protected BooleanCondition[]
m_Conditions
the "conditions" for the various switch cases.protected Token
m_InputToken
the input token.protected boolean
m_Lenient
whether we operate in lenient mode, ie not requiring a payload.protected List
m_Queue
the generated tokens.-
Fields inherited from class adams.flow.control.AbstractDirectedControlActor
m_Director, m_FinishBeforeStopping, m_StoppingTimeout, m_StoppingWarningInterval
-
Fields inherited from class adams.flow.control.AbstractControlActor
m_Compatibility, m_PauseStateManager, m_SetUpSubActors
-
Fields inherited from class adams.flow.core.AbstractActor
m_Annotations, m_BackupState, m_DetectedObjectVariables, m_DetectedVariables, m_ErrorHandler, m_Executed, m_Executing, m_ExecutionListeningSupporter, m_FullName, m_LoggingPrefix, m_Name, m_Parent, m_ScopeHandler, m_Self, m_Silent, m_Skip, m_StopFlowOnError, m_StopMessage, m_Stopped, m_StorageHandler, m_VariablesUpdated
-
Fields inherited from class adams.core.option.AbstractOptionHandler
m_OptionManager
-
Fields inherited from class adams.core.logging.LoggingObject
m_Logger, m_LoggingIsEnabled, m_LoggingLevel
-
Fields inherited from interface adams.flow.core.Actor
FILE_EXTENSION, FILE_EXTENSION_GZ
-
-
Constructor Summary
Constructors Constructor Description Switch()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Class[]
accepts()
Returns the class that the consumer accepts.String
add(int index, Actor actor)
Inserts the actor at the given position.String
add(Actor actor)
Inserts the actor at the end.protected Hashtable<String,Object>
backupState()
Backs up the current state of the actor before update the variables.String
casesTipText()
Returns the tip text for this property.void
cleanUp()
Cleans up after the execution has finished.String
conditionsTipText()
Returns the tip text for this property.Token
currentInput()
Returns the current input token, if any.void
defineOptions()
Adds options to the internal list of options.Class[]
generates()
Returns the class of objects that it generates.Actor
get(int index)
Returns the actor at the given position.ActorHandlerInfo
getActorHandlerInfo()
Returns some information about the actor handler, e.g., whether it can contain standalones and the actor execution.Actor[]
getCases()
Returns the cases.BooleanCondition[]
getConditions()
Returns the conditions to evaluate.boolean
getLenient()
Returns whether in lenient mode, ie not requiring a payload in the tokens.String
getQuickInfo()
Returns a quick info about the actor, which will be displayed in the GUI.String
globalInfo()
Returns a string describing the object.boolean
hasInput()
Returns whether an input token is currently present.boolean
hasPendingOutput()
Checks whether there is pending output to be collected after executing the flow item.int
indexOf(String actor)
Returns the index of the actor.protected void
initialize()
Initializes the members.void
input(Token token)
The method that accepts the input token and then processes it.String
lenientTipText()
Returns the tip text for this property.protected AbstractDirector
newDirector()
Returns an instance of a director.Token
output()
Returns the generated token.protected String
postExecute()
Post-execute hook.protected void
recordOutput(Token token)
Records the output generated by a case actor.Actor
remove(int index)
Removes the actor at the given position and returns the removed object.void
removeAll()
Removes all actors.protected void
reset()
Resets the actor.protected void
restoreState(Hashtable<String,Object> state)
Restores the state of the actor before the variables got updated.String
set(int index, Actor actor)
Sets the actor at the given position.void
setCases(Actor[] value)
Sets the cases.void
setConditions(BooleanCondition[] value)
Sets the conditions to evaluate.void
setLenient(boolean value)
Sets whether in lenient mode, ie not requiring a payload in the tokens.String
setUp()
Initializes the sub-actors for flow execution.int
size()
Returns the size of the group.void
stopExecution()
Stops the execution.boolean
supports(BooleanCondition condition)
Returns whether the condition is supported.-
Methods inherited from class adams.flow.control.AbstractDirectedControlActor
doExecute, finishBeforeStoppingTipText, flushExecution, getDirector, getFinishBeforeStopping, getStoppingTimeout, getStoppingWarningInterval, setFinishBeforeStopping, setStoppingTimeout, setStoppingWarningInterval, stoppingTimeoutTipText, stoppingWarningIntervalTipText, updateVariables, wrapUp
-
Methods inherited from class adams.flow.control.AbstractControlActor
active, canInspectOptions, check, destroy, firstActive, firstInputConsumer, flowPauseStateChanged, forceVariables, isPaused, isSubFlowWrappedUp, lastActive, pauseExecution, preExecute, resumeExecution, setParent, setUpSubActors, updateParent, wrapUpSubFlow
-
Methods inherited from class adams.flow.core.AbstractActor
annotationsTipText, canPerformSetUpCheck, compareTo, configureLogger, equals, execute, finalUpdateVariables, findVariables, findVariables, forCommandLine, forName, forName, getAdditionalInformation, getAnnotations, getDefaultName, getDetectedVariables, getErrorHandler, getFlowActors, getFlowExecutionListeningSupporter, getFullName, getName, getNextSibling, getParent, getParentComponent, getPreviousSibling, getRoot, getScopeHandler, getSilent, getSkip, getStopFlowOnError, getStopMessage, getStorageHandler, getVariables, handleError, handleException, hasErrorHandler, hasStopMessage, index, isBackedUp, isExecuted, isExecuting, isFinished, isHeadless, isStopped, nameTipText, performSetUpChecks, performVariableChecks, pruneBackup, pruneBackup, setAnnotations, setErrorHandler, setName, setSilent, setSkip, setStopFlowOnError, setVariables, shallowCopy, shallowCopy, silentTipText, sizeOf, skipTipText, stopExecution, stopFlowOnErrorTipText, updateDetectedVariables, updatePrefix, variableChanged
-
Methods inherited from class adams.core.option.AbstractOptionHandler
cleanUpOptions, finishInit, getDefaultLoggingLevel, getOptionManager, loggingLevelTipText, newOptionManager, setLoggingLevel, toCommandLine, toString
-
Methods inherited from class adams.core.logging.LoggingObject
getLogger, getLoggingLevel, initializeLogging, isLoggingEnabled
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface adams.flow.core.Actor
compareTo, destroy, equals, execute, findVariables, getAnnotations, getDefaultName, getDetectedVariables, getErrorHandler, getFlowExecutionListeningSupporter, getFullName, getName, getNextSibling, getParent, getParentComponent, getPreviousSibling, getRoot, getScopeHandler, getSilent, getSkip, getStopFlowOnError, getStopMessage, getStorageHandler, getVariables, handleError, hasErrorHandler, hasStopMessage, index, isExecuted, isFinished, isHeadless, isStopped, setAnnotations, setErrorHandler, setName, setParent, setSilent, setSkip, setStopFlowOnError, setVariables, shallowCopy, shallowCopy, sizeOf, stopExecution, toCommandLine, variableChanged, wrapUp
-
Methods inherited from interface adams.flow.core.ActorHandler
check, firstActive, flushExecution, lastActive
-
Methods inherited from interface adams.core.AdditionalInformationHandler
getAdditionalInformation
-
Methods inherited from interface adams.core.logging.LoggingLevelHandler
getLoggingLevel, setLoggingLevel
-
Methods inherited from interface adams.core.logging.LoggingSupporter
getLogger, isLoggingEnabled
-
Methods inherited from interface adams.core.option.OptionHandler
cleanUpOptions, getOptionManager
-
Methods inherited from interface adams.core.VariablesInspectionHandler
canInspectOptions
-
-
-
-
Field Detail
-
BACKUP_INPUT
public static final String BACKUP_INPUT
the key for storing the current input token in the backup.- See Also:
- Constant Field Values
-
m_Conditions
protected BooleanCondition[] m_Conditions
the "conditions" for the various switch cases.
-
m_Cases
protected List<Actor> m_Cases
the "cases" to execute if the corresponding expression matches.
-
m_Lenient
protected boolean m_Lenient
whether we operate in lenient mode, ie not requiring a payload.
-
m_InputToken
protected transient Token m_InputToken
the input token.
-
m_Queue
protected List m_Queue
the generated tokens.
-
-
Method Detail
-
globalInfo
public String globalInfo()
Returns a string describing the object.- Specified by:
globalInfo
in interfaceGlobalInfoSupporter
- Specified by:
globalInfo
in classAbstractOptionHandler
- Returns:
- a description suitable for displaying in the gui
-
defineOptions
public void defineOptions()
Adds options to the internal list of options.- Specified by:
defineOptions
in interfaceOptionHandler
- Overrides:
defineOptions
in classAbstractDirectedControlActor
-
initialize
protected void initialize()
Initializes the members.- Overrides:
initialize
in classAbstractControlActor
-
reset
protected void reset()
Resets the actor.- Overrides:
reset
in classAbstractControlActor
-
newDirector
protected AbstractDirector newDirector()
Returns an instance of a director.- Overrides:
newDirector
in classAbstractDirectedControlActor
- Returns:
- the director
-
setConditions
public void setConditions(BooleanCondition[] value)
Sets the conditions to evaluate.- Parameters:
value
- the conditions
-
getConditions
public BooleanCondition[] getConditions()
Returns the conditions to evaluate.- Returns:
- the conditions
-
conditionsTipText
public String conditionsTipText()
Returns the tip text for this property.- Returns:
- tip text for this property suitable for displaying in the GUI or for listing the options.
-
setCases
public void setCases(Actor[] value)
Sets the cases.- Parameters:
value
- the cases
-
getCases
public Actor[] getCases()
Returns the cases.- Returns:
- the cases
-
casesTipText
public String casesTipText()
Returns the tip text for this property.- Returns:
- tip text for this property suitable for displaying in the GUI or for listing the options.
-
setLenient
public void setLenient(boolean value)
Sets whether in lenient mode, ie not requiring a payload in the tokens.- Specified by:
setLenient
in interfaceLenientModeSupporter
- Parameters:
value
- true if lenient
-
getLenient
public boolean getLenient()
Returns whether in lenient mode, ie not requiring a payload in the tokens.- Specified by:
getLenient
in interfaceLenientModeSupporter
- Returns:
- true if lenient
-
lenientTipText
public String lenientTipText()
Returns the tip text for this property.- Specified by:
lenientTipText
in interfaceLenientModeSupporter
- Returns:
- tip text for this property suitable for displaying in the GUI or for listing the options.
-
getQuickInfo
public String getQuickInfo()
Returns a quick info about the actor, which will be displayed in the GUI.- Specified by:
getQuickInfo
in interfaceActor
- Specified by:
getQuickInfo
in interfaceQuickInfoSupporter
- Overrides:
getQuickInfo
in classAbstractDirectedControlActor
- Returns:
- null if no info available, otherwise short string
-
size
public int size()
Returns the size of the group.- Specified by:
size
in interfaceActorHandler
- Specified by:
size
in classAbstractControlActor
- Returns:
- the number of cases
-
get
public Actor get(int index)
Returns the actor at the given position.- Specified by:
get
in interfaceActorHandler
- Specified by:
get
in classAbstractControlActor
- Parameters:
index
- the position- Returns:
- the actor
-
set
public String set(int index, Actor actor)
Sets the actor at the given position.- Specified by:
set
in interfaceActorHandler
- Specified by:
set
in classAbstractControlActor
- Parameters:
index
- the positionactor
- the actor to set at this position- Returns:
- null if successful, otherwise error message
-
add
public String add(Actor actor)
Inserts the actor at the end.- Specified by:
add
in interfaceMutableActorHandler
- Parameters:
actor
- the actor to insert- Returns:
- null if successful, otherwise error message
-
add
public String add(int index, Actor actor)
Inserts the actor at the given position.- Specified by:
add
in interfaceMutableActorHandler
- Parameters:
index
- the positionactor
- the actor to insert- Returns:
- null if successful, otherwise error message
-
remove
public Actor remove(int index)
Removes the actor at the given position and returns the removed object.- Specified by:
remove
in interfaceMutableActorHandler
- Parameters:
index
- the position- Returns:
- the removed actor
-
removeAll
public void removeAll()
Removes all actors.- Specified by:
removeAll
in interfaceMutableActorHandler
-
indexOf
public int indexOf(String actor)
Returns the index of the actor.- Specified by:
indexOf
in interfaceActorHandler
- Specified by:
indexOf
in classAbstractControlActor
- Parameters:
actor
- the name of the actor to look for- Returns:
- the index of -1 if not found
-
supports
public boolean supports(BooleanCondition condition)
Returns whether the condition is supported.- Specified by:
supports
in interfaceIndexedBooleanConditionSupporter
- Returns:
- true if supported, false otherwise
-
getActorHandlerInfo
public ActorHandlerInfo getActorHandlerInfo()
Returns some information about the actor handler, e.g., whether it can contain standalones and the actor execution.- Specified by:
getActorHandlerInfo
in interfaceActorHandler
- Specified by:
getActorHandlerInfo
in classAbstractControlActor
- Returns:
- the info
-
accepts
public Class[] accepts()
Returns the class that the consumer accepts.- Specified by:
accepts
in interfaceInputConsumer
- Returns:
- adams.flow.core.Unknown.class
-
backupState
protected Hashtable<String,Object> backupState()
Backs up the current state of the actor before update the variables.- Overrides:
backupState
in classAbstractActor
- Returns:
- the backup
- See Also:
AbstractActor.updateVariables()
,AbstractActor.restoreState(Hashtable)
-
restoreState
protected void restoreState(Hashtable<String,Object> state)
Restores the state of the actor before the variables got updated.- Overrides:
restoreState
in classAbstractActor
- Parameters:
state
- the backup of the state to restore from- See Also:
AbstractActor.updateVariables()
,AbstractActor.backupState()
-
setUp
public String setUp()
Initializes the sub-actors for flow execution.- Specified by:
setUp
in interfaceActor
- Overrides:
setUp
in classAbstractDirectedControlActor
- Returns:
- null if everything is fine, otherwise error message
- See Also:
AbstractActor.reset()
-
input
public void input(Token token)
The method that accepts the input token and then processes it.- Specified by:
input
in interfaceInputConsumer
- Parameters:
token
- the token to accept and process
-
hasInput
public boolean hasInput()
Returns whether an input token is currently present.- Specified by:
hasInput
in interfaceInputConsumer
- Returns:
- true if input token present
-
currentInput
public Token currentInput()
Returns the current input token, if any.- Specified by:
currentInput
in interfaceInputConsumer
- Returns:
- the input token, null if none present
-
postExecute
protected String postExecute()
Post-execute hook.- Overrides:
postExecute
in classAbstractActor
- Returns:
- null if everything is fine, otherwise error message
- See Also:
AbstractActor.m_Executed
-
generates
public Class[] generates()
Returns the class of objects that it generates.- Specified by:
generates
in interfaceOutputProducer
- Returns:
- the Class of the generated tokens
-
recordOutput
protected void recordOutput(Token token)
Records the output generated by a case actor.- Parameters:
token
- the token to record
-
hasPendingOutput
public boolean hasPendingOutput()
Checks whether there is pending output to be collected after executing the flow item.- Specified by:
hasPendingOutput
in interfaceOutputProducer
- Returns:
- true if there is pending output
-
output
public Token output()
Returns the generated token.- Specified by:
output
in interfaceOutputProducer
- Returns:
- the generated token
-
cleanUp
public void cleanUp()
Cleans up after the execution has finished. Also removes graphical components.- Specified by:
cleanUp
in interfaceActor
- Specified by:
cleanUp
in interfaceCleanUpHandler
- Overrides:
cleanUp
in classAbstractDirectedControlActor
-
stopExecution
public void stopExecution()
Stops the execution.- Specified by:
stopExecution
in interfaceActor
- Specified by:
stopExecution
in interfaceStoppable
- Overrides:
stopExecution
in classAbstractDirectedControlActor
-
-