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 StringBACKUP_INPUTthe key for storing the input token in the backup.protected Actorm_Catchthe catch branch.protected Stringm_ErrorOccurrederror message in try block.protected ErrorPostProcessorm_ErrorPostProcessorthe error post-processor to use.protected VariableNamem_ErrorVariablethe variable to store the error in.protected Tokenm_InputTokenthe current input token.protected booleanm_PrefixErrorwhether to prefix the error message with source actor's full name and type.protected booleanm_StoreErrorwhether to store any error message in a variable.protected Actorm_Trythe try branch.static StringNAME_CATCHthe name for the catcj branch.static StringNAME_TRYthe 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.StringcatchTipText()Returns the tip text for this property.TokencurrentInput()Returns the current input token, if any.voiddefineOptions()Adds options to the internal list of options.protected StringdoExecute()Executes the flow item.StringerrorPostProcessorTipText()Returns the tip text for this property.StringerrorVariableTipText()Returns the tip text for this property.voidflushExecution()Stops the processing of tokens without stopping the flow.Class[]generates()Returns the class of objects that it generates.Actorget(int index)Returns the actor at the given position.ActorHandlerInfogetActorHandlerInfo()Returns some information about the actor handler, e.g., whether it can contain standalones and the actor execution.ActorgetCatch()Returns the try branch.Class[]getClassCrossReferences()Returns the cross-referenced classes.protected ActorgetDefaultCatch()Returns the default catch branch.protected ActorgetDefaultTry()Returns the default try branch.ErrorPostProcessorgetErrorPostProcessor()Returns the error post-processor in use.VariableNamegetErrorVariable()Returns the variable to store the error messages in.StringgetFixedName(int index)Returns the name for the sub-actor at this position.booleangetPrefixError()Returns whether to add the source actor's full name and type (e.g., 'execute').StringgetQuickInfo()Returns a quick info about the actor, which will be displayed in the GUI.booleangetStoreError()Returns whether to store any error in a variable.ActorgetTry()Returns the try branch.StringglobalInfo()Returns a string describing the object.StringhandleError(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.booleanhasInput()Returns whether an input token is currently present.booleanhasPendingOutput()Checks whether there is pending output to be collected after executing the flow item.intindexOf(String actor)Returns the index of the actor.protected voidinitialize()Initializes the members.voidinput(Token token)The method that accepts the input token and then processes it.Tokenoutput()Returns the generated token.StringprefixErrorTipText()Returns the tip text for this property.protected voidreset()Resets the scheme.protected voidrestoreState(Hashtable<String,Object> state)Restores the state of the actor before the variables got updated.Stringset(int index, Actor actor)Sets the actor at the given position.voidsetCatch(Actor value)Sets the catch branch.voidsetErrorPostProcessor(ErrorPostProcessor value)Sets the error post-processor to use.voidsetErrorVariable(VariableName value)Sets the variable to store the error messages in.voidsetPrefixError(boolean value)Sets whether to add the source actor's full name and type (e.g., 'execute').voidsetStoreError(boolean value)Sets whether to store any error in a variable.voidsetTry(Actor value)Sets the try branch.StringsetUp()Initializes the item for flow execution.intsize()Returns the size of the group.voidstopExecution()Stops the execution.StringstoreErrorTipText()Returns the tip text for this property.StringtryTipText()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_PrefixError
protected boolean m_PrefixError
whether to prefix the error message with source actor's full name and type.
-
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:
globalInfoin interfaceGlobalInfoSupporter- Specified by:
globalInfoin classAbstractOptionHandler- Returns:
- a description suitable for displaying in the gui
-
initialize
protected void initialize()
Initializes the members.- Overrides:
initializein classAbstractControlActor
-
reset
protected void reset()
Resets the scheme.- Overrides:
resetin classAbstractControlActor
-
defineOptions
public void defineOptions()
Adds options to the internal list of options.- Specified by:
defineOptionsin interfaceOptionHandler- Overrides:
defineOptionsin classAbstractActor
-
getClassCrossReferences
public Class[] getClassCrossReferences()
Returns the cross-referenced classes.- Specified by:
getClassCrossReferencesin 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.
-
setPrefixError
public void setPrefixError(boolean value)
Sets whether to add the source actor's full name and type (e.g., 'execute').- Parameters:
value- true if to add prefix to error message
-
getPrefixError
public boolean getPrefixError()
Returns whether to add the source actor's full name and type (e.g., 'execute').- Returns:
- true if to add prefix to error message
-
prefixErrorTipText
public String prefixErrorTipText()
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:
getQuickInfoin interfaceActor- Specified by:
getQuickInfoin interfaceQuickInfoSupporter- Overrides:
getQuickInfoin 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:
backupStatein 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:
restoreStatein 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:
inputin 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:
hasInputin interfaceInputConsumer- Returns:
- true if input token present
-
currentInput
public Token currentInput()
Returns the current input token, if any.- Specified by:
currentInputin 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:
getActorHandlerInfoin interfaceActorHandler- Specified by:
getActorHandlerInfoin classAbstractControlActor- Returns:
- the info
-
size
public int size()
Returns the size of the group.- Specified by:
sizein interfaceActorHandler- Specified by:
sizein classAbstractControlActor- Returns:
- the size
-
get
public Actor get(int index)
Returns the actor at the given position.- Specified by:
getin interfaceActorHandler- Specified by:
getin 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:
setin interfaceActorHandler- Specified by:
setin 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:
indexOfin interfaceActorHandler- Specified by:
indexOfin 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:
getFixedNamein 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:
acceptsin 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:
handleErrorin interfaceActor- Specified by:
handleErrorin interfaceErrorHandler- Overrides:
handleErrorin 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:
setUpin interfaceActor- Overrides:
setUpin classAbstractControlActor- Returns:
- null if everything is fine, otherwise error message
- See Also:
AbstractActor.reset()
-
doExecute
protected String doExecute()
Executes the flow item.- Specified by:
doExecutein 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:
flushExecutionin interfaceActorHandler- Specified by:
flushExecutionin interfaceFlushable- Overrides:
flushExecutionin classAbstractControlActor
-
generates
public Class[] generates()
Returns the class of objects that it generates.- Specified by:
generatesin 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:
hasPendingOutputin interfaceOutputProducer- Returns:
- true if there is pending output
-
output
public Token output()
Returns the generated token.- Specified by:
outputin interfaceOutputProducer- Returns:
- the generated token
-
stopExecution
public void stopExecution()
Stops the execution. No message set.- Specified by:
stopExecutionin interfaceActor- Specified by:
stopExecutionin interfaceStoppable- Overrides:
stopExecutionin classAbstractControlActor
-
-