Package adams.flow.control
Class TryCatch
-
- All Implemented Interfaces:
AdditionalInformationHandler
,ClassCrossReference
,CleanUpHandler
,CrossReference
,Destroyable
,GlobalInfoSupporter
,LoggingLevelHandler
,LoggingSupporter
,OptionHandler
,Pausable
,QuickInfoSupporter
,ShallowCopySupporter<Actor>
,SizeOfHandler
,Stoppable
,StoppableWithFeedback
,VariablesInspectionHandler
,FlowPauseStateListener
,VariableChangeListener
,Actor
,ActorHandler
,ControlActor
,ErrorHandler
,ErrorScopeRestrictor
,FixedNameActorHandler
,Flushable
,InputConsumer
,OutputProducer
,SubFlowWrapUp
,Serializable
,Comparable
public class TryCatch extends AbstractControlActor implements InputConsumer, OutputProducer, FixedNameActorHandler, ClassCrossReference, ErrorScopeRestrictor
Safe-guards the execution of the 'try' sequence of actors. In case of an error, the 'catch' sequence is executed to generate output instead.
This works similar to the Java try-catch-block. Allowing the flow to recover from unexpected errors and, for instance, return default values.
If the 'try' block fails and the 'catch' block accepts input (doesn't have to be a transformer, it can be just a source, eg SequenceSource), then the same input token is presented to the 'catch' block. This allows you to react to errors better. E.g., if the input token is a filename, then you can create an error message made up of the recorded error and the filename and pass this on.
Note for developers: If actors use other actors internally, these need to be accessible. This can be achieved by simply implementing the adams.flow.core.InternalActorHandler interface.
See also:
adams.flow.control.RaiseError
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: TryCatch
-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 gets stopped in case this actor encounters an error; 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
-try <adams.flow.core.Actor> (property: try) The 'try' branch which is attempted to be executed. default: adams.flow.control.SubProcess -name try
-catch <adams.flow.core.Actor> (property: catch) The 'catch' branch which gets executed if the 'try' branch fails. default: adams.flow.control.SubProcess -name catch
-store-error <boolean> (property: storeError) If enabled, then any error gets stored in the specified variable 'errorVariable' ; does not modify the variable if there was no error. default: false
-error-variable <adams.core.VariableName> (property: errorVariable) The name of the variable to store the error messages in. default: trycatch
-error-post-processors <adams.flow.control.errorpostprocessor.ErrorPostProcessor> (property: errorPostProcessor) The error post-processor to use. default: adams.flow.control.errorpostprocessor.Null
- Author:
- fracpete (fracpete at waikato dot ac dot nz)
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static String
BACKUP_INPUT
the key for storing the input token in the backup.protected Actor
m_Catch
the catch branch.protected String
m_ErrorOccurred
error message in try block.protected ErrorPostProcessor
m_ErrorPostProcessor
the error post-processor to use.protected VariableName
m_ErrorVariable
the variable to store the error in.protected Token
m_InputToken
the current input token.protected boolean
m_StoreError
whether to store any error message in a variable.protected Actor
m_Try
the try branch.static String
NAME_CATCH
the name for the catcj branch.static String
NAME_TRY
the name for the try branch.-
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 TryCatch()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Class[]
accepts()
Returns the class that the consumer accepts.protected Hashtable<String,Object>
backupState()
Backs up the current state of the actor before update the variables.String
catchTipText()
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.protected String
doExecute()
Executes the flow item.String
errorPostProcessorTipText()
Returns the tip text for this property.String
errorVariableTipText()
Returns the tip text for this property.void
flushExecution()
Stops the processing of tokens without stopping the flow.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
getCatch()
Returns the try branch.Class[]
getClassCrossReferences()
Returns the cross-referenced classes.protected Actor
getDefaultCatch()
Returns the default catch branch.protected Actor
getDefaultTry()
Returns the default try branch.ErrorPostProcessor
getErrorPostProcessor()
Returns the error post-processor in use.VariableName
getErrorVariable()
Returns the variable to store the error messages in.String
getFixedName(int index)
Returns the name for the sub-actor at this position.String
getQuickInfo()
Returns a quick info about the actor, which will be displayed in the GUI.boolean
getStoreError()
Returns whether to store any error in a variable.Actor
getTry()
Returns the try branch.String
globalInfo()
Returns a string describing the object.String
handleError(Actor source, String type, String msg)
Handles the given error message with the flow that this actor belongs to, if the flow has error logging turned on.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.Token
output()
Returns the generated token.protected void
reset()
Resets the scheme.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
setCatch(Actor value)
Sets the catch branch.void
setErrorPostProcessor(ErrorPostProcessor value)
Sets the error post-processor to use.void
setErrorVariable(VariableName value)
Sets the variable to store the error messages in.void
setStoreError(boolean value)
Sets whether to store any error in a variable.void
setTry(Actor value)
Sets the try branch.String
setUp()
Initializes the item for flow execution.int
size()
Returns the size of the group.void
stopExecution()
Stops the execution.String
storeErrorTipText()
Returns the tip text for this property.String
tryTipText()
Returns the tip text for this property.-
Methods inherited from class adams.flow.control.AbstractControlActor
active, canInspectOptions, check, cleanUp, destroy, firstActive, firstInputConsumer, flowPauseStateChanged, forceVariables, isPaused, isSubFlowWrappedUp, lastActive, pauseExecution, preExecute, resumeExecution, setParent, setUpSubActors, updateParent, wrapUp, 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, handleException, hasErrorHandler, hasStopMessage, index, isBackedUp, isExecuted, isExecuting, isFinished, isHeadless, isStopped, nameTipText, performSetUpChecks, performVariableChecks, postExecute, pruneBackup, pruneBackup, setAnnotations, setErrorHandler, setName, setSilent, setSkip, setStopFlowOnError, setVariables, shallowCopy, shallowCopy, silentTipText, sizeOf, skipTipText, stopExecution, stopFlowOnErrorTipText, updateDetectedVariables, updatePrefix, updateVariables, 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
cleanUp, compareTo, destroy, equals, execute, findVariables, getAnnotations, getDefaultName, getDetectedVariables, getErrorHandler, getFlowExecutionListeningSupporter, getFullName, getName, getNextSibling, getParent, getParentComponent, getPreviousSibling, getRoot, getScopeHandler, getSilent, getSkip, getStopFlowOnError, getStopMessage, getStorageHandler, getVariables, 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, 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
-
m_Try
protected Actor m_Try
the try branch.
-
m_Catch
protected Actor m_Catch
the catch branch.
-
BACKUP_INPUT
public static final String BACKUP_INPUT
the key for storing the input token in the backup.- See Also:
- Constant Field Values
-
NAME_TRY
public static final String NAME_TRY
the name for the try branch.- See Also:
- Constant Field Values
-
NAME_CATCH
public static final String NAME_CATCH
the name for the catcj branch.- See Also:
- Constant Field Values
-
m_InputToken
protected transient Token m_InputToken
the current input token.
-
m_ErrorOccurred
protected String m_ErrorOccurred
error message in try block.
-
m_StoreError
protected boolean m_StoreError
whether to store any error message in a variable.
-
m_ErrorVariable
protected VariableName m_ErrorVariable
the variable to store the error in.
-
m_ErrorPostProcessor
protected ErrorPostProcessor m_ErrorPostProcessor
the error post-processor to use.
-
-
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
-
initialize
protected void initialize()
Initializes the members.- Overrides:
initialize
in classAbstractControlActor
-
reset
protected void reset()
Resets the scheme.- Overrides:
reset
in classAbstractControlActor
-
defineOptions
public void defineOptions()
Adds options to the internal list of options.- Specified by:
defineOptions
in interfaceOptionHandler
- Overrides:
defineOptions
in classAbstractActor
-
getClassCrossReferences
public Class[] getClassCrossReferences()
Returns the cross-referenced classes.- Specified by:
getClassCrossReferences
in interfaceClassCrossReference
- Returns:
- the classes
-
getDefaultTry
protected Actor getDefaultTry()
Returns the default try branch.- Returns:
- the default branch
-
setTry
public void setTry(Actor value)
Sets the try branch.- Parameters:
value
- the try branch
-
getTry
public Actor getTry()
Returns the try branch.- Returns:
- the try branch
-
tryTipText
public String tryTipText()
Returns the tip text for this property.- Returns:
- tip text for this property suitable for displaying in the GUI or for listing the options.
-
getDefaultCatch
protected Actor getDefaultCatch()
Returns the default catch branch.- Returns:
- the default branch
-
setCatch
public void setCatch(Actor value)
Sets the catch branch.- Parameters:
value
- the catch branch
-
getCatch
public Actor getCatch()
Returns the try branch.- Returns:
- the try branch
-
catchTipText
public String catchTipText()
Returns the tip text for this property.- Returns:
- tip text for this property suitable for displaying in the GUI or for listing the options.
-
setStoreError
public void setStoreError(boolean value)
Sets whether to store any error in a variable.- Parameters:
value
- true if to store error in variable
-
getStoreError
public boolean getStoreError()
Returns whether to store any error in a variable.- Returns:
- true if error gets stored in variable
-
storeErrorTipText
public String storeErrorTipText()
Returns the tip text for this property.- Returns:
- tip text for this property suitable for displaying in the GUI or for listing the options.
-
setErrorVariable
public void setErrorVariable(VariableName value)
Sets the variable to store the error messages in.- Parameters:
value
- the name
-
getErrorVariable
public VariableName getErrorVariable()
Returns the variable to store the error messages in.- Returns:
- the name
-
errorVariableTipText
public String errorVariableTipText()
Returns the tip text for this property.- Returns:
- tip text for this property suitable for displaying in the GUI or for listing the options.
-
setErrorPostProcessor
public void setErrorPostProcessor(ErrorPostProcessor value)
Sets the error post-processor to use.- Parameters:
value
- the post-processor
-
getErrorPostProcessor
public ErrorPostProcessor getErrorPostProcessor()
Returns the error post-processor in use.- Returns:
- the post-processor
-
errorPostProcessorTipText
public String errorPostProcessorTipText()
Returns the tip text for this property.- 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 classAbstractActor
- Returns:
- null if no info available, otherwise short string
-
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()
-
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- See Also:
m_InputToken
-
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
-
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
-
size
public int size()
Returns the size of the group.- Specified by:
size
in interfaceActorHandler
- Specified by:
size
in classAbstractControlActor
- Returns:
- the size
-
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 everything is fine, otherwise the error
-
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
-
getFixedName
public String getFixedName(int index)
Returns the name for the sub-actor at this position.- Specified by:
getFixedName
in interfaceFixedNameActorHandler
- Parameters:
index
- the position of the sub-actor- Returns:
- the name to use
-
accepts
public Class[] accepts()
Returns the class that the consumer accepts.- Specified by:
accepts
in interfaceInputConsumer
- Returns:
- the Class of objects that can be processed
-
handleError
public String handleError(Actor source, String type, String msg)
Handles the given error message with the flow that this actor belongs to, if the flow has error logging turned on. Might stop the flow as well.- Specified by:
handleError
in interfaceActor
- Specified by:
handleError
in interfaceErrorHandler
- Overrides:
handleError
in classAbstractActor
- Parameters:
source
- the actor this error originated fromtype
- the type of errormsg
- the error message to log- Returns:
- always null
- See Also:
Flow.getLogErrors()
,Flow.getErrorHandling()
,AbstractActor.getStopFlowOnError()
-
setUp
public String setUp()
Initializes the item for flow execution.- Specified by:
setUp
in interfaceActor
- Overrides:
setUp
in classAbstractControlActor
- Returns:
- null if everything is fine, otherwise error message
- See Also:
AbstractActor.reset()
-
doExecute
protected String doExecute()
Executes the flow item.- Specified by:
doExecute
in classAbstractActor
- Returns:
- null if everything is fine, otherwise error message
-
flushExecution
public void flushExecution()
Stops the processing of tokens without stopping the flow.- Specified by:
flushExecution
in interfaceActorHandler
- Specified by:
flushExecution
in interfaceFlushable
- Overrides:
flushExecution
in classAbstractControlActor
-
generates
public Class[] generates()
Returns the class of objects that it generates.- Specified by:
generates
in interfaceOutputProducer
- Returns:
- the Class of the generated tokens
-
hasPendingOutput
public boolean hasPendingOutput()
Checks whether there is pending output to be collected after executing the flow item.
The method is not allowed allowed to return "true" before the actor has been executed. For actors that return an infinite number of tokens, the m_Executed flag can be returned.- 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
-
stopExecution
public void stopExecution()
Stops the execution. No message set.- Specified by:
stopExecution
in interfaceActor
- Specified by:
stopExecution
in interfaceStoppable
- Overrides:
stopExecution
in classAbstractControlActor
-
-