Class ExecutionManager

java.lang.Object
edu.tufts.hrilab.action.manager.ExecutionManager
All Implemented Interfaces:
ActionListener
Direct Known Subclasses:
QueueExecutionManager

public class ExecutionManager extends Object implements ActionListener
  • Field Details

    • goalComparator

      protected edu.tufts.hrilab.action.manager.ExecutionManager.GoalComparator goalComparator
      Comparison method determining the order goals get placed into the pendingGoals queue.
    • agentHierarchy

      protected Map<Symbol,Set<Symbol>> agentHierarchy
      Team/Agent hierarchy information (transitive)
    • rootAgent

      protected Symbol rootAgent
      Root AgentTeam in the hierarchy
    • agentTeams

      protected final Map<Symbol,AgentTeam> agentTeams
      Tree of AgentTeam Objects - contains all active goals spread throughout the hierarchy
    • resourceLock

      protected final Object resourceLock
    • goalsLock

      protected final Object goalsLock
    • pendingGoals

      protected final TreeSet<PendingGoal> pendingGoals
      A collection of all the goals currently waiting to be executed
    • pendingGoalsLock

      protected final Lock pendingGoalsLock
  • Constructor Details

    • ExecutionManager

      public ExecutionManager()
  • Method Details

    • init

      protected void init(StateMachine sm, RootContext rootContext, String priorityFile, Collection<String> groups)
    • createInstance

      public static ExecutionManager createInstance(Class<ExecutionManager> instanceType, StateMachine sm, RootContext rootContext, String priorityFile, Collection<String> groups)
      Parameters:
      instanceType - ExecutionManager class or subclass to be used
      sm - state machine for root context
      rootContext - base context at the root of all execution
      priorityFile - the path to the file starting from config.action.manager.priority containing goal priority information
      groups - DIARC group constraints used to register this EM and its class instances that are registered with TRADE (e.g., ActionLearning)
      Returns:
      ExecutionManager instance
    • getRelevantAgents

      protected Set<Symbol> getRelevantAgents(Symbol agentTeam)
      Returns the set of AgentTeam(names) whose execution may be affected by a change in task for the supplied AgentTeam. Practically, this returns the supplied agentTeam, its children, and all direct ancestors in the hierarchy
    • getDescendants

      protected Set<Symbol> getDescendants(Symbol agentTeam)
      Returns the names of the supplied agentTeam and all its children in the hierarchy
    • resourceConflictInPending

      protected boolean resourceConflictInPending(Set<Resource> requiredResources, int maxIndex)
      Returns a boolean indicating whether any goal up to maxIndex in the pending collection has a resource conflict with the supplied set.
    • activateValidPendingGoals

      protected void activateValidPendingGoals()
      Activate as many pending goals as possible in order of priority while avoiding resource conflicts
    • activateNextValidPendingGoal

      protected Goal activateNextValidPendingGoal()
      Transfers the highest priority goal from the pending collection which has all required resources available to active
      Returns:
      the goal which was transferred to active for execution. If no such valid goal existed, returns null.
    • consumedResources

      protected boolean consumedResources(Goal g)
      Indicates whether the supplied goal consumed any resources during execution. Used on goal completion to determine whether a check should be made for new goals to be submitted as a result of completion
      Parameters:
      g -
      Returns:
      true if the completion of the supplied goal freed up any resources, false otherwise
    • getResourceConflictingActiveGoals

      protected Set<Goal> getResourceConflictingActiveGoals(Set<Resource> requiredResources)
      Return the set of all currently active goals which are occupying some subset of the supplied resource set.
    • getLockedResourceNames

      protected List<Symbol> getLockedResourceNames(Set<Resource> requiredResources)
      Returns the list of resource names which are locked from the supplied list
    • getRequiredResourcesForGoal

      protected Set<Resource> getRequiredResourcesForGoal(Goal goal)
      Returns the set of all Resources required to be available in order to execute the supplied goal.
    • onActiveGoalUpdated

      protected void onActiveGoalUpdated(Goal g, GoalStatus status, ExecutionManager.UpdateType updateType)
      Callback triggered when an active goal's status has been updated.
      Parameters:
      g - the goal whose status has been updated
      status - the new status
      updateType - indicates whether the active goal was newly added, already existed, or removed
    • onPendingGoalUpdated

      protected void onPendingGoalUpdated(Goal g, int index, ExecutionManager.UpdateType updateType)
      Callback triggered when a pending goal's status has been updated
      Parameters:
      g - the goal whose status has been updated
      index - the index of the goal in the pendingGoals collection updateType indicates whether the active goal was newly added, already existed, or removed
    • handleConflictingLowerPriorityGoal

      protected void handleConflictingLowerPriorityGoal(Goal g, Set<Resource> necessaryResources)
      Determines what is done to an added pending goal when resources are not available to execute the action.
      Parameters:
      g - the goal that was added
      necessaryResources - the required resources which are unavailable
    • getPriorityTierForGoal

      public PriorityTier getPriorityTierForGoal(Predicate g)
    • getPriorityForGoal

      public long getPriorityForGoal(Predicate g)
    • submitGoal

      public long submitGoal(Predicate g, Symbol priorityTierSymbol)
    • submitGoal

      @Deprecated public Goal submitGoal(Goal g)
      Deprecated.
    • submitGoal

      public Goal submitGoal(Predicate g)
    • submitGoal

      public Goal submitGoal(Predicate g, ExecutionType executionType)
    • submitGoal

      public Goal submitGoal(Predicate g, Predicate metric)
    • submitGoal

      public Goal submitGoal(Goal g, ExecutionType execType)
    • submitGoal

      public Goal submitGoal(Predicate g, ExecutionType execType, long priority)
    • submitGoal

      public Goal submitGoal(Goal g, ExecutionType execType, long priority)
    • submitGoal

      public Goal submitGoal(Predicate g, ExecutionType execType, long priority, PriorityTier priorityTier)
    • submitGoal

      public Goal submitGoal(Goal g, ExecutionType execType, long priority, PriorityTier priorityTier)
      Submits the goal the execution manager using addPendingGoal(Goal, ExecutionType, long, PriorityTier). This goal will be added to the pool of goals under consideration by the execution manager
      Parameters:
      g - the goal to be added
      Returns:
      The duplicate goal if present, otherwise the newly submitted goal
    • joinOnGoal

      public GoalStatus joinOnGoal(long gid)
      Wait indefinitely for the goal to have terminal goal status.
      Parameters:
      gid - goal ID
      Returns:
      GoalStatus of the goal
    • joinOnGoal

      public GoalStatus joinOnGoal(long gid, long millis)
      Wait (up to specified milliseconds) for the goal to have terminal goal status.
      Parameters:
      gid - goal ID
      millis - maximum time to wait for terminal status
      Returns:
      GoalStatus of the goal (may not be a terminal status)
    • shouldSupersede

      protected boolean shouldSupersede(Goal newGoal, Set<Goal> activeGoals)
      Parameters:
      newGoal - new goal in question
      activeGoals - list of currently active goals to compare against
      Returns:
      a boolean indicating whether the new goal's execution should take priority over all supplied active goals
    • supersedeGoals

      protected void supersedeGoals(Goal newGoal, Set<Goal> activeGoals, Set<Resource> relevantResources)
      Interrupt execution of the provided active goals and push them back to the pending collection, replacing them with execution of a new goal.
      Parameters:
      newGoal - the new goal to be executed after interruption
      activeGoals - a list of currently active goals to be pushed back to pending
    • transferGoalToActive

      protected void transferGoalToActive(Goal goal)
      Transfer a goal from pending goals to active goals, and start execution of the goal. Note that the goal is not guaranteed to be in the active goals when this method returns. If the goal fails, for instance, the goal will automatically be moved to the past goals.
      Parameters:
      goal - the goal to transfer and execute
    • executeGoal

      protected boolean executeGoal(Goal goal, ExecutionType executionType)
      Attempts to create an ActionInterpreter and begin execution of the supplied goal, checking action/state constraints
      Parameters:
      goal - the goal
      executionType - goal type (act, simulation, ...)
      Returns:
      true if goal was added
    • actionComplete

      public void actionComplete(ActionInterpreter ai)
      Cleans up references to completed actions/goals. Also resets the slice time now that there is one less action. Remove a script interpreter from the goal manager.
      Specified by:
      actionComplete in interface ActionListener
      Parameters:
      ai - the AI to remove
    • actionStarted

      public void actionStarted(ActionInterpreter ai)
      Specified by:
      actionStarted in interface ActionListener
    • stepComplete

      public void stepComplete(Context step)
      Specified by:
      stepComplete in interface ActionListener
    • stepStarted

      public void stepStarted(Context step)
      Specified by:
      stepStarted in interface ActionListener
    • transferGoalToPastGoals

      protected void transferGoalToPastGoals(Goal goal)
      Searches for the supplied goal in the pending and active goal collections, moving it to pastGoals and performing the relevant bookkeeping if found
    • joinOnPendingGoal

      protected void joinOnPendingGoal(long gid)
      Wait indefinitely for the matching pending goal to have its condition notified. Returns immediately if the goal is not found or pending.
    • addPendingGoal

      protected void addPendingGoal(Goal g, ExecutionType execType)
      Adds a pending goal with default priority. See addPendingGoal(Goal, ExecutionType, long, PriorityTier)
    • addPendingGoal

      protected void addPendingGoal(Goal g, ExecutionType execType, long priority)
      Adds a pending goal with default priority tier, but overridden value. See addPendingGoal(Goal, ExecutionType, long, PriorityTier)
    • addPendingGoal

      protected void addPendingGoal(Goal g, ExecutionType execType, long priority, PriorityTier priorityTier)
      Add the goal to the pool of goals being considered by this execution manager. When and if execution of the provided goal occurs is subject to the ExecutionManager implementation. Generally, higher priority goals will be executed first.
      Parameters:
      g - the submitted goal
      execType - the execution type of the submitted goal
      priority - priority value of the provided goal
      priorityTier - priority tier of the provided goal
    • cancelGoal

      public boolean cancelGoal(long gid)
      Search both pending and active goal collections and cancel the goal with the supplied goal id if found
      Parameters:
      gid -
      Returns:
      boolean indicating whether the goal was found in one of the collections or not
    • suspendGoal

      public boolean suspendGoal(long gid)
      Search all goal collections and suspend the goal with the supplied goal id if found
      Parameters:
      gid -
      Returns:
      true if the goal was found and had an active status, otherwise false
    • resumeGoal

      public boolean resumeGoal(long gid)
      Search all goal collections and resume the goal with the supplied goal id if found
      Parameters:
      gid -
      Returns:
      true if the goal was found and had an active status, otherwise false
    • getCurrentGoals

      public List<Goal> getCurrentGoals()
      Returns:
      A copied list of all current pending and active goals
    • getActiveGoals

      public List<Goal> getActiveGoals()
      Get a copied list of the goals currently undergoing execution.
      Returns:
      list of Goals
    • getPendingGoals

      public List<PendingGoal> getPendingGoals()
      Get a copied list of the currently managed pending goals waiting to begin execution.
      Returns:
      list of PendingGoals
    • getPastGoals

      public List<Goal> getPastGoals()
      Get a copied list of the previously executed goals
      Returns:
      list of Goals
    • getGoal

      public Goal getGoal(long gid)
      Get the goal for a particular goal ID. This checks pending, current, and past goals.
      Parameters:
      gid - goal id.
      Returns:
      the goal corresponding to the ID.
    • getCurrentGoal

      public Goal getCurrentGoal(Symbol actor, int index)
      Get the n-th most recently submitted goal. 0 = most recently started goal, 1 = 2nd most recently started goal, etc.
      Parameters:
      actor - actor to consider currently running goals for (can be null, meaning all actors)
      index - into the currently running goals
      Returns:
      n-th most recently start goal, or null if no n-th goal is being executed
    • getActiveGoal

      public Goal getActiveGoal(Symbol actor, int index)
      Get the n-th most recently started, currently running, goal. 0 = most recently started goal, 1 = 2nd most recently started goal, etc.
      Parameters:
      actor - actor to consider currently running goals for (can be null, meaning all actors)
      index - into the currently running goals
      Returns:
      n-th most recently start goal, or null if no n-th goal is being executed
    • getActiveGoals

      protected List<Goal> getActiveGoals(Goal queryGoal)
      Get a list of the currently running goals matching the goalPredicate query.
      Returns:
      goals with matching goalPredicate, or null if no matching goal found
    • getPastGoals

      public List<Goal> getPastGoals(Goal queryGoal)
      Get a list of all past goals matching the goalPredicate query.
      Returns:
      goals with matching goalPredicate, or null if no matching goal found
    • getCurrentGoals

      public List<Goal> getCurrentGoals(Goal queryGoal)
      Get a list of all current pending and/or active goals matching the goalPredicate query.
      Returns:
      goals with matching goalPredicate, or null if no matching goal found
    • getAllGoals

      public List<Goal> getAllGoals(Goal queryGoal)
      Get a list of all pending, active, and past goals matching the goalPredicate query.
      Returns:
      goals with matching goalPredicate, or null if no matching goal found
    • getGoalStatus

      public GoalStatus getGoalStatus(long gid)
      Query the GoalStatus of a particular goal.
      Parameters:
      gid - the ID of the goal to check on
      Returns:
      GoalStatus object indicating the status
    • getActiveGoalStatus

      public GoalStatus getActiveGoalStatus(long gid)
      Query the GoalStatus of a particular goal.
      Parameters:
      gid - the ID of the goal to check on
      Returns:
      GoalStatus object indicating the status
    • getActionStatus

      public ActionStatus getActionStatus(long gid)
      Query the ActionStatus of the action (i.e., root context for this goal) being executed in pursuit of the specified goal.
      Parameters:
      gid - the ID of the goal to check on
      Returns:
      ActionStatus enum indicating the status
    • getGoalFailConditions

      public Justification getGoalFailConditions(long gid)
      Get failure condition for a particular goal.
      Parameters:
      gid - the ID of the goal to check on
      Returns:
      a list of Predicates describing the failure
    • freeze

      public void freeze()
      Calls freeze(Symbol) for the root AgentTeam in the hierarchy
    • freeze

      public void freeze(Symbol agent)
      'Freezes' a team or agent and blocks until unfrozen. A frozen agent will have all current goals suspended and will not be able to begin execution of any new ones. Similarly, all members of a frozen team will be frozen.
    • endFreeze

      public void endFreeze()
      Calls endFreeze(Symbol) for self
    • endFreeze

      public void endFreeze(Symbol agent)
      Ends the frozen status for the supplied agent/team
    • checkIgnoreTentativeAccept

      public boolean checkIgnoreTentativeAccept(Predicate goalPred)
      Call ignoreTentativeAccept if service is present in the system
    • notifyUIActiveGoalUpdated

      protected void notifyUIActiveGoalUpdated(Goal g, GoalStatus status, ExecutionManager.UpdateType updateType)
    • notifyUIPendingGoalUpdated

      protected void notifyUIPendingGoalUpdated(Goal g, int index, ExecutionManager.UpdateType updateType)
    • addAIListener

      public void addAIListener(ActionListener al)
      Add ActionListener to list to be notified of ActionInterpreter and StepExecution events
      Parameters:
      al - listener to be added
    • getUntypedSymbol

      protected Symbol getUntypedSymbol(Symbol s)
    • getAgentTeam

      protected AgentTeam getAgentTeam(Symbol name)
    • configPruningMechanism

      public void configPruningMechanism(boolean pruneData, long pastHistoryLength)
      configure the pruning mechanism
      Parameters:
      pruneData - denotes if the goal manager should prune past goals
      pastHistoryLength - denotes length of time to keep past goals
    • pruneOldData

      public void pruneOldData()
    • showEditor

      public void showEditor(String path)
      Display a GUI for this goal manager
    • getStateMachine

      public StateMachine getStateMachine()
    • getRootContext

      public RootContext getRootContext()
    • shutdown

      public void shutdown()
      Shutdown this ExecutionManager. All currently managed goals are canceled, ending the ActionInterpreter. Note: the goals are automagically moved to the pastGoals list by the ActionListener mechanism.
    • submitGoalContext

      public Context submitGoalContext(Goal g, Context context)
      Entry point to submit a context tree to be executed
      Parameters:
      g - the goal
      context - the context tree which will be executed
      Returns:
      the context which will be executed first