From 436e7a6c0d79daab6f33794c3c2766c1d0c992f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A5l=20Trefall?= Date: Wed, 21 Dec 2022 15:43:16 +0100 Subject: [PATCH] Added Aborted function to primitive tasks and operators. The planner calls this function on the current task when a condition or executing condition fail, or when an operator returns failure. --- Fluid-HTN.UnitTests/MyOperator.cs | 5 +++++ Fluid-HTN/Operators/FuncOperator.cs | 16 +++++++++++++++- Fluid-HTN/Operators/IOperator.cs | 3 ++- Fluid-HTN/Planners/Planner.cs | 10 ++++++++++ Fluid-HTN/Tasks/PrimitiveTasks/IPrimitiveTask.cs | 3 ++- Fluid-HTN/Tasks/PrimitiveTasks/PrimitiveTask.cs | 5 +++++ 6 files changed, 39 insertions(+), 3 deletions(-) diff --git a/Fluid-HTN.UnitTests/MyOperator.cs b/Fluid-HTN.UnitTests/MyOperator.cs index 7b9626a..3942856 100644 --- a/Fluid-HTN.UnitTests/MyOperator.cs +++ b/Fluid-HTN.UnitTests/MyOperator.cs @@ -13,5 +13,10 @@ public TaskStatus Update(IContext ctx) public void Stop(IContext ctx) { } + + public void Aborted(IContext ctx) + { + + } } } diff --git a/Fluid-HTN/Operators/FuncOperator.cs b/Fluid-HTN/Operators/FuncOperator.cs index f9c82f1..dc810f9 100644 --- a/Fluid-HTN/Operators/FuncOperator.cs +++ b/Fluid-HTN/Operators/FuncOperator.cs @@ -8,13 +8,15 @@ public class FuncOperator : IOperator where T : IContext private readonly Func _func; private readonly Action _funcStop; + private readonly Action _funcAborted; // ========================================================= CONSTRUCTION - public FuncOperator(Func func, Action funcStop = null) + public FuncOperator(Func func, Action funcStop = null, Action funcAborted = null) { _func = func; _funcStop = funcStop; + _funcAborted = funcAborted; } // ========================================================= FUNCTIONALITY @@ -40,5 +42,17 @@ public void Stop(IContext ctx) throw new Exception("Unexpected context type!"); } } + + public void Aborted(IContext ctx) + { + if (ctx is T c) + { + _funcAborted?.Invoke(c); + } + else + { + throw new Exception("Unexpected context type!"); + } + } } } diff --git a/Fluid-HTN/Operators/IOperator.cs b/Fluid-HTN/Operators/IOperator.cs index 43b8df1..0a29a23 100644 --- a/Fluid-HTN/Operators/IOperator.cs +++ b/Fluid-HTN/Operators/IOperator.cs @@ -4,5 +4,6 @@ public interface IOperator { TaskStatus Update(IContext ctx); void Stop(IContext ctx); + void Aborted(IContext ctx); } -} \ No newline at end of file +} diff --git a/Fluid-HTN/Planners/Planner.cs b/Fluid-HTN/Planners/Planner.cs index 0a80535..d8dc2ba 100644 --- a/Fluid-HTN/Planners/Planner.cs +++ b/Fluid-HTN/Planners/Planner.cs @@ -240,6 +240,11 @@ public void Tick(Domain domain, T ctx, bool allowImmediateReplan = true) { OnNewTaskConditionFailed?.Invoke(_currentTask, condition); + if (_currentTask is IPrimitiveTask task) + { + task.Aborted(ctx); + } + _currentTask = null; _plan.Clear(); @@ -273,6 +278,8 @@ public void Tick(Domain domain, T ctx, bool allowImmediateReplan = true) { OnCurrentTaskExecutingConditionFailed?.Invoke(task, condition); + task.Aborted(ctx); + _currentTask = null; _plan.Clear(); @@ -337,6 +344,8 @@ public void Tick(Domain domain, T ctx, bool allowImmediateReplan = true) { OnCurrentTaskFailed?.Invoke(task); + task.Aborted(ctx); + _currentTask = null; _plan.Clear(); @@ -361,6 +370,7 @@ public void Tick(Domain domain, T ctx, bool allowImmediateReplan = true) else { // This should not really happen if a domain is set up properly. + task.Aborted(ctx); _currentTask = null; LastStatus = TaskStatus.Failure; } diff --git a/Fluid-HTN/Tasks/PrimitiveTasks/IPrimitiveTask.cs b/Fluid-HTN/Tasks/PrimitiveTasks/IPrimitiveTask.cs index 8372b2d..4f89950 100644 --- a/Fluid-HTN/Tasks/PrimitiveTasks/IPrimitiveTask.cs +++ b/Fluid-HTN/Tasks/PrimitiveTasks/IPrimitiveTask.cs @@ -27,5 +27,6 @@ public interface IPrimitiveTask : ITask void ApplyEffects(IContext ctx); void Stop(IContext ctx); + void Aborted(IContext ctx); } -} \ No newline at end of file +} diff --git a/Fluid-HTN/Tasks/PrimitiveTasks/PrimitiveTask.cs b/Fluid-HTN/Tasks/PrimitiveTasks/PrimitiveTask.cs index fd5cd08..8ecfc0a 100644 --- a/Fluid-HTN/Tasks/PrimitiveTasks/PrimitiveTask.cs +++ b/Fluid-HTN/Tasks/PrimitiveTasks/PrimitiveTask.cs @@ -89,6 +89,11 @@ public void Stop(IContext ctx) Operator?.Stop(ctx); } + public void Aborted(IContext ctx) + { + Operator?.Aborted(ctx); + } + // ========================================================= VALIDITY public bool IsValid(IContext ctx)