Class Context
java.lang.Object
edu.tufts.hrilab.action.execution.Context
- Direct Known Subclasses:
AndContext
,ArgumentBasedContext
,BlockContext
,FailureContext
,FinallyContext
,IfContext
,NotContext
,OrContext
,PersistentContext
,ReturnContext
,RootContext
,TrueContext
,TryContext
,WhileContext
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final Context
Each Context has a parent context from which the context is generated.protected final ChildContexts
The list of childContexts is used to track the history of the execution.protected final String
Command.protected ActionConstraints
The constraints for selecting an action in this context (and child contexts).protected long
The system time at which this action ended.protected final org.slf4j.Logger
protected long
The system time at which this action must stop.protected long
The system time at which this action started.protected StateMachine
StateMachine for context. -
Constructor Summary
ConstructorsModifierConstructorDescriptionContext
(Context parent, StateMachine sm, String cmd) Context constructor.protected
Context
(Context c, StateMachine sm, String cmd, ExecutionType execType) Context constructor that explicitly sets the execution type. -
Method Summary
Modifier and TypeMethodDescriptionboolean
acquireLocks
(ActionInterpreter actionInt, boolean greedy) Acquire all locks for this contextInstantiate the event specified in the event specificationaddEvent
(Goal g, StateMachine sm) addEvent
(Goal goal, StateMachine sm, ExecutionType executionType) Instantiate the event specified in the goalboolean
Helper method to check if this context is the root of the failure (return true), * or if a child context is the cause of the failure (return false).abstract Context
deep copy of the context, its status, and its childrenprotected void
copyInternal
(Context newContext) createSimulatedEquivalent
(boolean belief) Creates a copy of this context tree to be used for simulation.void
doStep()
If Context has a concrete execution step to perform, it should happen in this method.protected Symbol
Helper method to find actor value in the context tree.protected boolean
protected ActionBinding
getArgument
(String bname) Retrieve binding.Get list of cloned arguments.protected ActionBinding
getArgumentSilent
(String bname) Same as getArgument method but does not generate a log statement in the case the argument is not found.protected Class<?>
getArgumentType
(String bname) Retrieve argument type.getArgumentValue
(String bname) Retrieve argument value.Get all children contexts in a copied ChildContexts instance.Get the ContextDescription for this Context.getContextDescription
(Predicate location) Get the ContextDescription for this context tree based on the location predicate.long
protected long
Get the elapsed timelong
protected static Context
getEquivalentContext
(Context oldContext, Context newRoot) Given a context, find its location in its context tree.protected ActionBinding
getEvaluatedArgument
(String bname) Retrieve argument (ActionBinding).getId()
Get context's unique id.Returns the justification (reason) for the context's execution statusprotected ActionBinding
getLocalArgument
(String bname) Get a local argument without recursing up the context tree.boolean
Gets the logical value for this event.long
Gets the maximum time this action should run untilprotected Context
Goes through the call tree, checking that all ancestors are currently running.final Context
Get next step in the execution of this context.protected Context
Get the next step (i.e., context) to be executed.Get parent context.TODO: remove this method.Get the signature of the Context in Predicate form.long
Get the start timeGet state updates for this context based on the current status (i.e., ActionStatus).protected boolean
boolean
isAction()
boolean
boolean
boolean
boolean
boolean
protected void
A method to perform additional steps during ActionStatus updates (i.e., setStatus).void
releaseLocks
(ActionInterpreter actionInt) Release all locks for this contextprotected void
This resets fields specific to concrete sub-classes and should be overridden by inheriting classes if additional resetting needs to happen.final void
Reset Context instance to enable it to be used by an ActionInterpreter.protected final void
This resets the base Context class's fields and children.protected void
setChildContexts
(ChildContexts childContexts) void
protected void
setEndTime
(long etime) protected void
setExecType
(ExecutionType execType) protected void
setLogicalValue
(boolean value) Set the logical value for this event.protected void
protected void
setStartTime
(long stime) Set the start timeprotected void
final void
setStatus
(ActionStatus status) Convenience method to set the ActionStatus without needing to provide a justification.void
setStatus
(ActionStatus status, Justification justification) Set the ActionStatus and Justification for the status.protected void
protected void
If Context can have next steps (i.e., children), implement this method to add the next child context that should be executed.final Context
setupNextStep
(ScriptParser parser) protected void
toString()
protected void
protected Justification
Verify that the effects (if any) of a context are valid.Verify that the return value (if any) of a context is valid.void
This method blocks until this context is terminated.void
waitForTermination
(long timeout) This method blocks up to maxTime for this context to reach a terminal ActionStatus.
-
Field Details
-
log
protected final org.slf4j.Logger log -
caller
Each Context has a parent context from which the context is generated. The only exception is that there is one root context with no parent. An action being executed as the result of a submitted goal will derive from the root context. An event within an action that invokes will have its own context that is a child of the context for the action script. -
childContexts
The list of childContexts is used to track the history of the execution. -
startTime
protected long startTimeThe system time at which this action started. -
endTime
protected long endTimeThe system time at which this action ended. -
maxTime
protected long maxTimeThe system time at which this action must stop. See: {- See Also:
-
cmd
Command. -
stateMachine
StateMachine for context. -
constraints
The constraints for selecting an action in this context (and child contexts). The constraints include forbidden actions and forbidden contexts. If an action or context is to be temporarily retracted as being forbidden, create a new ActionConstraints without the retracted constraint. For example, if permission has been given to do a forbidden action (e.g. push an obstacle) then the constraint will not be present in the new ActionConstraints and child context will also not have this constraint. However, once context returns to the parent of the context, the constraint is back in place. This allows for constraints to be restricted to a certain context.
-
-
Constructor Details
-
Context
Context constructor.- Parameters:
parent
- parent contextsm
- state machine for this contextcmd
- command of this context
-
Context
Context constructor that explicitly sets the execution type.- Parameters:
c
- parent contextsm
- state machine for this contextcmd
- command of this contextexecType
- execution type
-
-
Method Details
-
getId
Get context's unique id.- Returns:
-
acquireLocks
Acquire all locks for this context- Parameters:
actionInt
- The AI that needs the lockgreedy
- if not greedy, then release the locks if all can't be acquired- Returns:
- true is all locks acquired, false otherwise
-
releaseLocks
Release all locks for this context- Parameters:
actionInt
- The AI that needs the lock
-
getStatus
-
setStatus
Set the ActionStatus and Justification for the status. NOTE: Classes overriding this method must call super.setStatus(status, justification) inside their overriding method to actually set the status and justification. Using the convenience method setStatus(status) will cause an infinite loop, and also abandon the Justification.- Parameters:
status
-justification
-
-
setStatus
Convenience method to set the ActionStatus without needing to provide a justification. This is useful for ActionStatus cases where a justification doesn't make sense (e.g., PROGRESS, SUCCESS, etc). Callers of setStatus should prefer to use the setStatus that takes a Justification whenever possible.- Parameters:
status
-
-
getNearestTerminatedAncestor
Goes through the call tree, checking that all ancestors are currently running. If an ancestor has terminated, that ancestor (i.e., Context) is returned. This is used to determine if asynchronous contexts can/should run (async context or overall condition). This is necessary since parent contexts that are not currently executed might suddenly fail due to overall conditions, or overall conditions might be in the middle of an execution step when a parent finishes (with fail or success).- Returns:
- Context that has terminated status
-
resetContext
public final void resetContext()Reset Context instance to enable it to be used by an ActionInterpreter. This is useful for cases when an ActionInterpreter needs to be used repeatedly without creating new Contexts every run of the ActionInterpreter. -
resetConcreteContext
protected void resetConcreteContext()This resets fields specific to concrete sub-classes and should be overridden by inheriting classes if additional resetting needs to happen. -
resetCoreContext
protected final void resetCoreContext()This resets the base Context class's fields and children. -
setLogicalValue
protected void setLogicalValue(boolean value) Set the logical value for this event.- Parameters:
value
- the logical value of the event
-
getLogicalValue
public boolean getLogicalValue()Gets the logical value for this event. Typically used in controls such as if, while, not...- Returns:
- the logical value of this execution
-
getActualLogicalValue
protected boolean getActualLogicalValue() -
getJustification
Returns the justification (reason) for the context's execution status- Returns:
- jusitifcation
-
doStep
public void doStep()If Context has a concrete execution step to perform, it should happen in this method. This should not include setting up next steps (i.e., children to perform), which should happen in the setupNextStep method. Primitive actions and operators (i.e., leaf nodes in the context tree) are examples where this method should be implemented. -
getNextStep
Get next step in the execution of this context. This context must have ActionStatus of PROGRESS or RECOVERY (i.e., currently being executed) to return a non-null context.- Returns:
-
setupNextStep
protected void setupNextStep()If Context can have next steps (i.e., children), implement this method to add the next child context that should be executed. -
getNextStepForType
Get the next step (i.e., context) to be executed. Returns null if there are no more steps. TODO: remove this method- Returns:
-
verifyEffects
Verify that the effects (if any) of a context are valid.- Returns:
-
verifyReturnValue
Verify that the return value (if any) of a context is valid.- Returns:
-
getSignatureInPredicateForm
Get the signature of the Context in Predicate form. Most Context implementations should override this method.- Returns:
-
getContextDescription
Get the ContextDescription for this context tree based on the location predicate. This searches down the context tree until some context matches the location predicate and returns a ContextDescription.- Returns:
-
getContextDescription
Get the ContextDescription for this Context. TODO: this currently searches down the context tree until some context (e.g., ActionContext) returns a ContextDescription. Eventually, this should probably be abstract, to force every context class to return a ContextDescription.- Returns:
-
getArgumentValue
Retrieve argument value. Generic implementation just recurses up the context tree. Even though some context instances don't have arguments themselves, it's important for calls to pass through non-argument contexts to search for arguments in other parts of the context tree. -
getEvaluatedArgument
Retrieve argument (ActionBinding). Generic implementation just recurses up the context tree. Even though some context instances don't have arguments themselves, it's important for calls to pass through non-argument contexts to search for arguments in other parts of the context tree. -
getArguments
Get list of cloned arguments. Even though some context instances don't have arguments themselves, it's important for calls to pass through non-argument contexts to search for arguments in other parts of the context tree.- Returns:
-
getArgumentType
Retrieve argument type. Generic implementation just recurses up the context tree. Even though some context instances don't have arguments themselves, it's important for calls to pass through non-argument contexts to search for arguments in other parts of the context tree. -
getArgument
Retrieve binding. Generic implementation just recurses up the context tree. Even though some context instances don't have arguments themselves, it's important for calls to pass through non-argument contexts to search for arguments in other parts of the context tree. -
getArgumentSilent
Same as getArgument method but does not generate a log statement in the case the argument is not found. This is useful for method like hasArgumentInScope(bname) so that confusing log error statements aren't made.- Parameters:
bname
-- Returns:
-
getLocalArgument
Get a local argument without recursing up the context tree. Only classes that extend ArgumentBasedContext will have local arguments, so this return null by default.- Parameters:
bname
-- Returns:
-
getActor
Helper method to find actor value in the context tree.- Returns:
-
getStartTime
public long getStartTime()Get the start time- Returns:
- the action's start time
-
setStartTime
protected void setStartTime(long stime) Set the start time- Parameters:
stime
- start time
-
setStartTime
protected void setStartTime() -
setEndTime
protected void setEndTime(long etime) -
getEndTime
public long getEndTime() -
getDuration
public long getDuration() -
getElapsedTime
protected long getElapsedTime()Get the elapsed time- Returns:
- the action's elapsed time
-
getMaxTime
public long getMaxTime()Gets the maximum time this action should run until- Returns:
- The latest time this action should run
-
setTimeout
protected void setTimeout() -
isAction
public boolean isAction() -
isAsynchronous
public boolean isAsynchronous() -
isSimulation
public boolean isSimulation() -
getConstraints
-
setConstraints
-
isApproved
-
startOverAllMonitor
protected void startOverAllMonitor() -
toString
-
getCommand
-
setExecType
-
getExecType
-
isFailure
public boolean isFailure() -
isSuccess
public boolean isSuccess() -
isTerminated
public boolean isTerminated() -
getParentContext
Get parent context.- Returns:
-
getChildContexts
Get all children contexts in a copied ChildContexts instance. This is a shallow copy of the underlying child Contexts.- Returns:
-
performAdditionalStatusUpdates
protected void performAdditionalStatusUpdates()A method to perform additional steps during ActionStatus updates (i.e., setStatus). If a particular Context sub-class needs this to happen, that class should override this method. NOTE: This method is only called when a select set of ActionStatuses are successfully set. -
updatePerformanceModels
protected void updatePerformanceModels() -
causedFailure
public boolean causedFailure()Helper method to check if this context is the root of the failure (return true), * or if a child context is the cause of the failure (return false).- Returns:
-
getStateUpdates
Get state updates for this context based on the current status (i.e., ActionStatus).- Returns:
-
waitForTermination
public void waitForTermination()This method blocks until this context is terminated. -
waitForTermination
public void waitForTermination(long timeout) This method blocks up to maxTime for this context to reach a terminal ActionStatus.- Parameters:
timeout
- max milliseconds to wait
-
getRootContext
TODO: remove this method. only here as a temporary fix to observation contexts being added as child contexts (see Condition's observeCondition method).- Returns:
-
setupNextStep
-
hasNormCheckingAncestor
protected boolean hasNormCheckingAncestor() -
addEvent
Instantiate the event specified in the event specification- Parameters:
eSpec
- the event to be executed
-
addEvent
-
addEvent
Instantiate the event specified in the goal- Parameters:
goal
- the goal to be pursuedsm
- the state machine in charge of tracking states for this goalexecutionType
-- Returns:
- the newly generated action context / child.
-
getStateMachine
-
copy
deep copy of the context, its status, and its children- Parameters:
newParent
- parent to attach the copied context too- Returns:
- the copied context
-
copyInternal
-
setStateMachine
-
setChildContexts
-
createSimulatedEquivalent
Creates a copy of this context tree to be used for simulation. Used when you don't want to modify a context tree, but need to do some safe operations on it.- Parameters:
belief
- If believe should be used in the new tree- Returns:
- The root of the new simulated context tree
-
getEquivalentContext
Given a context, find its location in its context tree. Then use that map topology to find an identical context in a different tree. Assumes that the oldContext's tree and the newRoot's tree have identical topologies! This is typically used for finding a context from a real tree in an equivalent simulated tree //todo: This can maybe be done differently if context IDs are copied as well. That way, map topology doesn't matter.- Parameters:
oldContext
- A reference context, to be found in a different treenewRoot
- New tree to find the copy of oldContext in- Returns:
- A context that is a different instance of, but otherwise identical to, the oldContext
-