Class PlanExecutor
Planner on a role instance.
PlanExecutor provides the main thinking loop for planning agents:
- Extract current state from
@Statefields - Generate a plan using the planner
- Execute each action in the plan
- Update state after each action
Usage
@RoleSpec(
name = "LifeCell",
goals = {
@Goal(name = "survive", condition = "state == ALIVE")
}
)
public class LifeCellRole extends Role {
@State private CellState state = CellState.DEAD;
@State private int neighborCount = 3;
@ActionSpec(
precondition = "state == DEAD && neighborCount == 3",
postcondition = "state = ALIVE",
fulfills = {"survive"}
)
public void reproduce() {
this.state = CellState.ALIVE;
}
}
// Usage
LifeCellRole role = new LifeCellRole();
PlanExecutor executor = new PlanExecutor(role);
List<String> executed = executor.think();
// Returns: ["reproduce"]
The plan-to-execute contract
This class and PlannerHandleAdapter — the path an agent runs
through core's PlannerHandle SPI — share one invariant: a step that
declares a precondition is measured against the real @State rather
than the planner's simulation, and a step whose precondition has stopped
holding is never executed. Everything else about the two differs, because
their APIs have different shapes, and the
package documentation lists those
differences. This surface extracts beliefs afresh before each step with a
precondition, skips or throws instead of reporting a row, abandons the rest
of the plan when it refuses a step, and checks after the last step that the
plan established the goals it set out to establish.
- Author:
- TnsAI Team
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionPlanExecutor(Object roleInstance) Creates an executor for a role instance.PlanExecutor(Object roleInstance, Planner planner) Creates an executor with a custom planner. -
Method Summary
Modifier and TypeMethodDescriptionvoidexecuteAction(PlanningAction action) Executes a single action on the role instance.Extracts current state from @State annotated fields.getPlan()Gets the current plan without executing it.Gets the underlying planner.Gets the role instance.Gets the list of satisfied goals.Gets the list of unsatisfied goals.booleanisGoalSatisfied(String goalName) Checks if a specific goal is satisfied.static voidrefuseProtectedSteps(Object roleInstance, List<PlanningAction> actions) Refuses planned actions that declare an execution policy or@Security.think()Executes one thinking cycle.think(boolean useChaining) Executes one thinking cycle with optional chaining.
-
Constructor Details
-
PlanExecutor
Creates an executor for a role instance.Uses
BackwardChainingPlannerby default.- Parameters:
roleInstance- role object instance
-
PlanExecutor
Creates an executor with a custom planner.- Parameters:
roleInstance- role object instanceplanner- planner implementation
-
-
Method Details
-
think
Executes one thinking cycle.This is the main entry point for planning agents:
- Extract current state
- Generate plan for unsatisfied goals
- Execute actions in order
- Return executed action names
- Returns:
- list of executed action names; empty when every goal is already satisfied. A planned step whose precondition stopped holding is left out, and logged, when the planner declares goals and none of them is left unsatisfied by then.
- Throws:
PlanningFailureException- if an unsatisfied goal has no plan; if a planned step's precondition no longer holds when execution reaches it while a goal is still unsatisfied, or while the planner declares no goals at all; or if a goal the plan set out to establish — unsatisfied when the plan was produced and satisfied by the plan's declared effects — is unsatisfied once the plan has executed. Steps executed before a refusal keep their effectscom.tnsai.security.SecurityException- withUNAUTHORIZED, before any step runs, if a planned action declares an execution policy or@Security; seerefuseProtectedSteps(java.lang.Object, java.util.List<com.tnsai.planning.PlanningAction>)
-
think
Executes one thinking cycle with optional chaining.- Parameters:
useChaining- true to use backward chaining for multi-step plans- Returns:
- list of executed action names; empty when every goal is already satisfied. A planned step whose precondition stopped holding is left out, and logged, when the planner declares goals and none of them is left unsatisfied by then.
- Throws:
PlanningFailureException- if an unsatisfied goal has no plan; if a planned step's precondition no longer holds when execution reaches it while a goal is still unsatisfied, or while the planner declares no goals at all; or if a goal the plan set out to establish — unsatisfied when the plan was produced and satisfied by the plan's declared effects — is unsatisfied once the plan has executed. Steps executed before a refusal keep their effectscom.tnsai.security.SecurityException- withUNAUTHORIZED, before any step runs, if a planned action declares an execution policy or@Security; seerefuseProtectedSteps(java.lang.Object, java.util.List<com.tnsai.planning.PlanningAction>)
-
extractBeliefs
Extracts current state from @State annotated fields.- Returns:
- current belief map
-
executeAction
Executes a single action on the role instance.- Parameters:
action- action to execute- Throws:
com.tnsai.security.SecurityException- withUNAUTHORIZEDif the action declares an execution policy or@Security
-
refuseProtectedSteps
Refuses planned actions that declare an execution policy or@Security.A plan step invokes the action method directly, not through
ActionExecutor, so neither the access gate nor@Securitychecks nor approval run for it. An action that declares either has to be dispatched throughActionExecutor. Callers check the whole plan before its first step runs, so a refused plan leaves no side effects.- Parameters:
roleInstance- the role instance the actions run onactions- the planned actions- Throws:
com.tnsai.security.SecurityException- withUNAUTHORIZEDfor the first action that declares access restrictions- Since:
- 0.17.0
-
getUnsatisfiedGoals
Gets the list of unsatisfied goals.- Returns:
- unsatisfied goal names
-
getSatisfiedGoals
Gets the list of satisfied goals.- Returns:
- satisfied goal names
-
isGoalSatisfied
Checks if a specific goal is satisfied.- Parameters:
goalName- goal name- Returns:
- true if goal is satisfied
-
getPlan
Gets the current plan without executing it.- Returns:
- planned action names; empty when every goal is already satisfied
- Throws:
PlanningFailureException- if an unsatisfied goal has no plan
-
getPlanner
Gets the underlying planner.- Returns:
- planner instance
-
getRoleInstance
Gets the role instance.- Returns:
- role instance
-