Class ExecutionManager

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

public class ExecutionManager extends Object implements GoalListener
  • 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, Collection<String> groups)
    • createInstance

      public static ExecutionManager createInstance(Class<ExecutionManager> instanceType, StateMachine sm, RootContext rootContext, 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
      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(Goal g, int maxIndex)
      Returns a boolean indicating whether any goal up to maxIndex in the pending collection has a resource conflict with the supplied set. A resource conflict occurs between two goals if one goal holds a resource during execution which the other goal either also needs to hold or requires to be available. Two goals with only overlapping required resources can coexist.
    • activateValidPendingGoals

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

      protected int activateNextValidPendingGoal(int startSearchIndex)
      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 during execution of the supplied goal.
    • getHeldResourcesForGoal

      protected Set<Resource> getHeldResourcesForGoal(Goal goal)
      Returns the set of all Resources that will be held by the supplied goal during its execution.
    • 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)
      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
    • submitGoal

      public Goal submitGoal(Goal g)
      Submits the goal the execution manager using addPendingGoal(PendingGoal). This goal will be added to the pool of goals under consideration by the 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 goal to be added
      Returns:
      The duplicate goal if present, otherwise the newly submitted goal
    • getNextScheduledExecutionDate

      public Date getNextScheduledExecutionDate(long goalId)
      Get Date of next scheduled goal execution. Returns null if goalId is for a non-scheduled goal, or if scheduled goal will not execute again.
      Parameters:
      goalId - scheduled goal ID
      Returns:
      Date or null
    • 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)
      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)
      Attempts to create an ActionInterpreter and begin execution of the supplied goal, checking action/state constraints
      Parameters:
      goal - the goal
      Returns:
      true if goal was added
    • interruptWaitForNextGoalStatusUpdate

      public void interruptWaitForNextGoalStatusUpdate(long gid)
    • waitForNextGoalStatusUpdate

      public GoalStatus waitForNextGoalStatusUpdate(long gid, long timeout)
      Wait until a particular goal has its goal status updated to any new value.
      Parameters:
      gid - goal ID
      timeout - time to wait in milliseconds
      Returns:
      The GoalStatus after the soonest update occurred or the timeout duration elapsed without an update
    • waitForNextGoalStatusUpdate

      public GoalStatus waitForNextGoalStatusUpdate(long gid, long timeout, GoalStatus startStatus)
    • waitForNextGoalStatusUpdate

      protected GoalStatus waitForNextGoalStatusUpdate(Goal goal, long timeout, GoalStatus startStatus)
      Wait until a particular goal has its goal status updated to any new value.
      Parameters:
      goal -
      timeout -
      Returns:
      The GoalStatus after the soonest update occurred or the timeout duration elapsed without an update
    • waitForGoalStatus

      protected void waitForGoalStatus(Goal goal, GoalStatus status)
      Convenience method to wait until a particular goal has a particular goal status. This relies on the GoalListener callback.
      Parameters:
      goal -
      status -
    • goalStatusChanged

      public void goalStatusChanged(GoalInfo info)
      Notify EM of goal status changes, so goal can be properly managed (e.g., moved to past goals).
      Specified by:
      goalStatusChanged in interface GoalListener
      Parameters:
      info - GoalInfo of the goal with changed status
    • 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.
    • 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
    • cancelPendingGoalByIndex

      public boolean cancelPendingGoalByIndex(int index)
      Remove a pending goal based on index before it is transferred to active
    • cancelPendingGoalsByIndices

      public boolean cancelPendingGoalsByIndices(Predicate indicesPred)
      Cancels the upcoming pending goals at the supplied indices (Predicate in the form and(index1,index2,...))
    • cancelScheduledGoalsByIndices

      public boolean cancelScheduledGoalsByIndices(Predicate indicesPred)
      Cancels the upcoming scheduled goals at the supplied indices (Predicate in the form and(index1,index2,...))
    • 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
    • suspendSystemGoals

      public List<Long> suspendSystemGoals()
    • suspendSystemGoals

      public List<Long> suspendSystemGoals(Symbol agentTeam)
      Suspends a subset of an agentTeam's active goals. This method ignores goals with PriorityTier SKIPPENDING and scheduled goals. In order to accommodate some practical agent configurations with input methods' addressees, this method can act on the supplied agent or a single descendant which has an active goal. If multiple team members have disjoint active goals, this method will fail and return null.
      Parameters:
      agentTeam -
      Returns:
      the suspended goals' ids or null.
    • 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
    • resumeSystemGoals

      public List<Long> resumeSystemGoals()
    • resumeSystemGoals

      public List<Long> resumeSystemGoals(Symbol agentTeam)
      Resumes a subset of suspended goals for the supplied agentTeam and all of its children. This method ignores goals with PriorityTier SKIPPENDING and scheduled goals. In order to accommodate some practical agent configurations with input methods' addressees, this method can act on the supplied agent or a single descendant which has an active goal. If multiple team members have disjoint active goals, this method will fail.
      Parameters:
      agentTeam -
      Returns:
      the resumed goals' ids
    • getCurrentGoals

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

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

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

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

      public void cancelPendingGoals()
      Cancel all goals currently found in the pendingGoals collection
    • cancelCurrentGoals

      public void cancelCurrentGoals()
      Cancel all goals currently found in either the pending or active Goals collections
    • cancelActiveGoals

      public void cancelActiveGoals()
      Cancel all goals currently found in the activeGoals colection
    • cancelSystemGoals

      public List<Long> cancelSystemGoals()
    • cancelScheduledGoals

      public void cancelScheduledGoals()
    • cancelSystemGoals

      public List<Long> cancelSystemGoals(Symbol agentTeam)
      Cancels a subset of active goals for the supplied agentTeam and all of its children. This method ignores goals with PriorityTier SKIPPENDING and scheduled goals. In order to accommodate some practical agent configurations with input methods' addressees, this method can act on the supplied agent or a single descendant which has an active goal. If multiple team members have disjoint active goals, this method will fail.
      Parameters:
      agentTeam -
      Returns:
      the canceled goals' ids
    • getSystemGoals

      public List<Goal> getSystemGoals()
    • getSystemGoals

      public List<Goal> getSystemGoals(Symbol actor)
    • getNextGoal

      public Goal getNextGoal()
    • getNextGoal

      public Goal getNextGoal(Symbol agent)
    • 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
    • getActiveGoal

      public Goal getActiveGoal(long gid)
      Get an active goal
      Parameters:
      gid - goal id
      Returns:
      the goal
    • getPendingGoal

      public PendingGoal getPendingGoal(long gid)
      Get a pending goal
      Parameters:
      gid - goal id
      Returns:
      the goal
    • getScheduledGoals

      public List<ScheduledGoal> getScheduledGoals()
      Get a copied list of all scheduled goals which have either yet to be submitted, or are currently executing.
      Returns:
      shallow copy of scheduled goals
    • getScheduledGoals

      public List<ScheduledGoal> getScheduledGoals(Symbol agentTeam)
      Get a copied list of all scheduled goals which have either yet to be submitted, or are currently executing for the supplied agentTeam
      Returns:
      shallow copy of scheduled goals
    • getScheduledGoal

      public ScheduledGoal getScheduledGoal(long goalId)
      Get ScheduledGoal instance with goal ID.
      Parameters:
      goalId - scheduled goal ID
      Returns:
      ScheduledGoal or null
    • getPastGoal

      public Goal getPastGoal(long gid)
      Get the goal for a particular past goal ID.
      Parameters:
      gid - goal id.
      Returns:
      the goal corresponding to the ID.
    • 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
    • getGoalPriorityTier

      public PriorityTier getGoalPriorityTier(long gid)
      Query the priority tier of a particular goal.
      Parameters:
      gid - the ID of the goal to check on
      Returns:
      PriorityTier object indicating the priority tier
    • getGoalPriority

      public long getGoalPriority(long gid)
      Query the priority of a particular goal.
      Parameters:
      gid - the ID of the goal to check on
      Returns:
      long indicating the priority value
    • hasGreaterPriority

      public boolean hasGreaterPriority(long goalId, PriorityTier priorityTier, long priority)
      Compare the priority of the supplied goal against the supplied priority/priorityTier values
    • 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
    • updateScheduledGoal

      public boolean updateScheduledGoal(long goalId, Predicate update)
      Modify existing scheduled goal based on update predicate.
      Parameters:
      goalId - ID of scheduled goal
      update - update predicate
      Returns:
      if update was successful
    • waitForActionLearningStart

      public void waitForActionLearningStart(Symbol actor, Predicate newAction)
    • learnAction

      public boolean learnAction(Symbol actor, Predicate newAction)
    • resumeActionLearning

      public boolean resumeActionLearning(Symbol actor, Predicate newAction)
    • endActionLearning

      public boolean endActionLearning(Symbol actor, Predicate newAction)
    • pauseActionLearning

      public boolean pauseActionLearning(Symbol actor, Predicate newAction)
    • cancelActionLearning

      public boolean cancelActionLearning(Symbol actor, Predicate newAction)
    • resumeActionLearning

      public boolean resumeActionLearning(Symbol actor)
    • endActionLearning

      public boolean endActionLearning(Symbol actor)
    • pauseActionLearning

      public boolean pauseActionLearning(Symbol actor)
    • cancelActionLearning

      public boolean cancelActionLearning(Symbol actor)
    • getLearningStatus

      public ActionLearningStatus getLearningStatus(Symbol actor)
    • changeLearningExecution

      public void changeLearningExecution(Symbol actor, Symbol status)
    • modifyAction

      public void modifyAction(Symbol actor, Predicate action, Predicate modification, Predicate location)
    • forgetTaughtAction

      public Justification forgetTaughtAction(Predicate actionSignature)
    • loadLearnedActions

      public List<ActionDBEntry> loadLearnedActions(boolean addToDB)
    • getLearnedActions

      public List<ActionDBEntry> getLearnedActions()
    • isLearnedAction

      public boolean isLearnedAction(Predicate actionSignature)
    • waitForLearningStateUpdate

      public boolean waitForLearningStateUpdate(Symbol agent, Predicate actionSignature)
    • interruptWaitForLearningStateUpdate

      public void interruptWaitForLearningStateUpdate(Symbol agent, Predicate actionSignature)
    • freeze

      public void freeze(Symbol actor)
      '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(Symbol actor)
      Ends the frozen status for the supplied agent/team
    • addAIListener

      public void addAIListener(ActionListener al)
      Add ActionListener to list to be notified of ActionInterpreter and StepExecution events
      Parameters:
      al - listener to be added
    • 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()
    • getStateMachine

      public StateMachine getStateMachine()
      Get main state machine.
      Returns:
    • getRootContext

      public RootContext getRootContext()
    • setActionLearningGuiFlag

      public void setActionLearningGuiFlag(boolean flag)
    • setScheduledGoalsFile

      public void setScheduledGoalsFile(String filename)
    • setGoalManagerNotifier

      public void setGoalManagerNotifier(GoalManagerNotifier notifier)
    • setLearnedActionsFile

      public void setLearnedActionsFile(String filename)
    • getLearnedActionsFile

      public String getLearnedActionsFile()
    • getActionLearningGuiFlag

      public boolean getActionLearningGuiFlag()
    • writeScheduledGoalsToFile

      public void writeScheduledGoalsToFile()
    • 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 TODO: Should this be implemented by each subclass? - probably this is only used in PA right now and explicitly for SIMULATE_PERFORMANCE exec types
      Parameters:
      g - the goal
      context - the context tree which will be executed
      Returns:
      the context which will be executed first