Package com.tnsai.planning
Class BackwardChainingPlanner
java.lang.Object
com.tnsai.planning.BackwardChainingPlanner
- All Implemented Interfaces:
Planner
Default planner implementation using backward chaining algorithm.
Backward chaining starts from the goal and works backwards to find a sequence of actions that can achieve it. This is effective when:
- The goal is well-defined
- Actions have clear preconditions and effects
- Multi-step plans are needed
Algorithm
function plan(goal, state):
if goal is satisfied in state:
return empty plan
for each action that fulfills goal:
if action's precondition is satisfied:
return [action]
else:
subplan = plan(precondition, state)
if subplan exists:
return subplan + [action]
return no plan found
Active recursion is guarded by the target precondition and a snapshot of simulated state, so revisiting an action after the state has moved on is still valid while a genuine loop is not.
An empty plan means one thing: every goal was already satisfied. Anything
else raises PlanningFailureException, whose
reason tells an unreachable goal
apart from a cyclic precondition, an exhausted depth budget, and an
exhausted search budget.
Usage
Planner planner = new BackwardChainingPlanner(MyRole.class);
Map<String, Object> state = Map.of("health", 30, "ammo", 5);
List<PlanningAction> plan = planner.plan(state);
- Author:
- TnsAI Team
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionBackwardChainingPlanner(Class<?> roleClass) Creates a planner from a role class.BackwardChainingPlanner(Class<?> roleClass, int maxDepth) Creates a planner from a role class with custom max depth.BackwardChainingPlanner(List<PlanningGoal> goals, List<PlanningAction> actions) Creates a planner with explicit goals and actions.BackwardChainingPlanner(List<PlanningGoal> goals, List<PlanningAction> actions, int maxDepth) Creates a planner with explicit goals, actions, and max depth. -
Method Summary
Modifier and TypeMethodDescriptionapplyEffects(PlanningAction action, Map<String, Object> state) Simulates applying an action's effects to a state.extractState(Object roleInstance) Extracts current state from@Stateannotated fields, including fields inherited from superclasses, which core'sRoleSpecExtractoralso collects.findActionsForGoal(PlanningGoal goal, Map<String, Object> state) Finds actions that can help achieve a goal.findApplicableActions(Map<String, Object> state) Finds actions whose preconditions are satisfied.findSatisfiedGoals(Map<String, Object> state) Finds goals that are currently satisfied.findUnsatisfiedGoals(Map<String, Object> state) Finds goals that are not currently satisfied.Returns all registered actions.getGoals()Returns all registered goals.booleanisGoalSatisfied(String goalName, Map<String, Object> state) Checks if a specific goal is satisfied.intmaxDepth()Configured recursion budget.Creates a plan to achieve unsatisfied goals.Creates a plan with optional chaining.planForGoalWithChaining(PlanningGoal goal, Map<String, Object> state, int maxDepth) Plans for a single goal using backward chaining.Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.tnsai.planning.Planner
getAction, getGoal, isGoalSatisfied, isPreconditionSatisfied
-
Constructor Details
-
BackwardChainingPlanner
Creates a planner from a role class.Extracts goals from
@RoleSpecand actions from@Actionmethods.- Parameters:
roleClass- class annotated with @RoleSpec
-
BackwardChainingPlanner
Creates a planner from a role class with custom max depth.- Parameters:
roleClass- class annotated with @RoleSpecmaxDepth- requested maximum planning depth; values above the internal recursion safety ceiling are capped
-
BackwardChainingPlanner
Creates a planner with explicit goals and actions.- Parameters:
goals- planning goalsactions- available actions
-
BackwardChainingPlanner
public BackwardChainingPlanner(List<PlanningGoal> goals, List<PlanningAction> actions, int maxDepth) Creates a planner with explicit goals, actions, and max depth.- Parameters:
goals- planning goalsactions- available actionsmaxDepth- requested maximum planning depth; values above the internal recursion safety ceiling are capped
-
-
Method Details
-
plan
Description copied from interface:PlannerCreates a plan to achieve unsatisfied goals. -
plan
Description copied from interface:PlannerCreates a plan with optional chaining. -
maxDepth
public int maxDepth()Configured recursion budget. Goal and precondition search both stop when remaining depth reaches zero. -
planForGoalWithChaining
public List<PlanningAction> planForGoalWithChaining(PlanningGoal goal, Map<String, Object> state, int maxDepth) Plans for a single goal using backward chaining.- Parameters:
goal- target goalstate- current statemaxDepth- requested recursion depth; values above the internal recursion safety ceiling are capped- Returns:
- action sequence to achieve goal
- Throws:
PlanningFailureException- when a cyclic precondition is detected, recursion depth is exhausted, or the bounded search budget is exhausted
-
getGoals
Description copied from interface:PlannerReturns all registered goals. -
getActions
Description copied from interface:PlannerReturns all registered actions.- Specified by:
getActionsin interfacePlanner- Returns:
- list of actions
-
findUnsatisfiedGoals
Description copied from interface:PlannerFinds goals that are not currently satisfied.- Specified by:
findUnsatisfiedGoalsin interfacePlanner- Parameters:
state- current state- Returns:
- unsatisfied goals, ordered by priority (highest first)
-
findSatisfiedGoals
Description copied from interface:PlannerFinds goals that are currently satisfied.- Specified by:
findSatisfiedGoalsin interfacePlanner- Parameters:
state- current state- Returns:
- satisfied goals
-
isGoalSatisfied
Description copied from interface:PlannerChecks if a specific goal is satisfied.- Specified by:
isGoalSatisfiedin interfacePlanner- Parameters:
goalName- goal namestate- current state- Returns:
- true if goal condition is satisfied
-
findActionsForGoal
Description copied from interface:PlannerFinds actions that can help achieve a goal.- Specified by:
findActionsForGoalin interfacePlanner- Parameters:
goal- target goalstate- current state- Returns:
- applicable actions for the goal
-
findApplicableActions
Description copied from interface:PlannerFinds actions whose preconditions are satisfied.- Specified by:
findApplicableActionsin interfacePlanner- Parameters:
state- current state- Returns:
- applicable actions
-
applyEffects
Description copied from interface:PlannerSimulates applying an action's effects to a state.- Specified by:
applyEffectsin interfacePlanner- Parameters:
action- action to applystate- state to modify (returns new state)- Returns:
- new state after applying effects
-
extractState
Extracts current state from@Stateannotated fields, including fields inherited from superclasses, which core'sRoleSpecExtractoralso collects. A subclass@Statewith the same name as an inherited one wins here.- Parameters:
roleInstance- role object instance- Returns:
- state map
-