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

public abstract class Context extends Object
  • Field Details

    • log

      protected final org.slf4j.Logger log
    • caller

      protected final Context 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

      protected final ChildContexts childContexts
      The list of childContexts is used to track the history of the execution.
    • startTime

      protected long startTime
      The system time at which this action started.
    • endTime

      protected long endTime
      The system time at which this action ended.
    • maxTime

      protected long maxTime
      The system time at which this action must stop. See: {
      See Also:
    • cmd

      protected final String cmd
      Command.
    • stateMachine

      protected StateMachine stateMachine
      StateMachine for context.
    • constraints

      protected ActionConstraints 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

      public Context(Context parent, StateMachine sm, String cmd)
      Context constructor.
      Parameters:
      parent - parent context
      sm - state machine for this context
      cmd - command of this context
    • Context

      protected Context(Context c, StateMachine sm, String cmd, ExecutionType execType)
      Context constructor that explicitly sets the execution type.
      Parameters:
      c - parent context
      sm - state machine for this context
      cmd - command of this context
      execType - execution type
  • Method Details

    • getId

      public Long getId()
      Get context's unique id.
      Returns:
    • acquireLocks

      public boolean acquireLocks(ActionInterpreter actionInt, boolean greedy)
      Acquire all locks for this context
      Parameters:
      actionInt - The AI that needs the lock
      greedy - if not greedy, then release the locks if all can't be acquired
      Returns:
      true is all locks acquired, false otherwise
    • releaseLocks

      public void releaseLocks(ActionInterpreter actionInt)
      Release all locks for this context
      Parameters:
      actionInt - The AI that needs the lock
    • getStatus

      public ActionStatus getStatus()
    • setStatus

      public void setStatus(ActionStatus status, Justification justification)
      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

      public final void setStatus(ActionStatus status)
      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

      protected Context 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

      public Justification 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

      public final Context 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

      protected Context 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

      protected Justification verifyEffects()
      Verify that the effects (if any) of a context are valid.
      Returns:
    • verifyReturnValue

      public Justification verifyReturnValue()
      Verify that the return value (if any) of a context is valid.
      Returns:
    • getSignatureInPredicateForm

      public Predicate getSignatureInPredicateForm()
      Get the signature of the Context in Predicate form. Most Context implementations should override this method.
      Returns:
    • getContextDescription

      public ContextDescription getContextDescription(Predicate location)
      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

      public ContextDescription 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

      public Object getArgumentValue(String bname)
      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

      protected ActionBinding getEvaluatedArgument(String bname)
      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

      public List<ActionBinding> 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

      protected Class<?> getArgumentType(String bname)
      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

      protected ActionBinding getArgument(String bname)
      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

      protected ActionBinding getArgumentSilent(String bname)
      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

      protected ActionBinding getLocalArgument(String bname)
      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

      protected Symbol getActor(String actorVarName)
      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

      public ActionConstraints getConstraints()
    • setConstraints

      public void setConstraints(ActionConstraints ac)
    • isApproved

      public Justification isApproved()
    • startOverAllMonitor

      protected void startOverAllMonitor()
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getCommand

      public String getCommand()
    • setExecType

      protected void setExecType(ExecutionType execType)
    • getExecType

      public ExecutionType getExecType()
    • isFailure

      public boolean isFailure()
    • isSuccess

      public boolean isSuccess()
    • isTerminated

      public boolean isTerminated()
    • getParentContext

      public Context getParentContext()
      Get parent context.
      Returns:
    • getChildContexts

      public ChildContexts 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

      public List<Predicate> 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

      public Context 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

      public final Context setupNextStep(ScriptParser parser)
    • hasNormCheckingAncestor

      protected boolean hasNormCheckingAncestor()
    • addEvent

      public Context addEvent(EventSpec eSpec)
      Instantiate the event specified in the event specification
      Parameters:
      eSpec - the event to be executed
    • addEvent

      public Context addEvent(Goal g, StateMachine sm)
    • addEvent

      public Context addEvent(Goal goal, StateMachine sm, ExecutionType executionType)
      Instantiate the event specified in the goal
      Parameters:
      goal - the goal to be pursued
      sm - the state machine in charge of tracking states for this goal
      executionType -
      Returns:
      the newly generated action context / child.
    • getStateMachine

      public StateMachine getStateMachine()
    • copy

      public abstract Context copy(Context newParent)
      deep copy of the context, its status, and its children
      Parameters:
      newParent - parent to attach the copied context too
      Returns:
      the copied context
    • copyInternal

      protected void copyInternal(Context newContext)
    • setStateMachine

      protected void setStateMachine(StateMachine sm)
    • setChildContexts

      protected void setChildContexts(ChildContexts childContexts)
    • createSimulatedEquivalent

      public Context createSimulatedEquivalent(boolean belief)
      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

      protected static Context getEquivalentContext(Context oldContext, Context newRoot)
      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 tree
      newRoot - New tree to find the copy of oldContext in
      Returns:
      A context that is a different instance of, but otherwise identical to, the oldContext