Skip to content

Commit

Permalink
Merge pull request #15 from ptrefall/AbortedFunctionInPrimitiveTask
Browse files Browse the repository at this point in the history
Added Aborted function to primitive tasks and operators. The planner …
  • Loading branch information
ptrefall authored Dec 23, 2022
2 parents d102a9b + 436e7a6 commit bf5f6c3
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 3 deletions.
5 changes: 5 additions & 0 deletions Fluid-HTN.UnitTests/MyOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,10 @@ public TaskStatus Update(IContext ctx)
public void Stop(IContext ctx)
{
}

public void Aborted(IContext ctx)
{

}
}
}
16 changes: 15 additions & 1 deletion Fluid-HTN/Operators/FuncOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ public class FuncOperator<T> : IOperator where T : IContext

private readonly Func<T, TaskStatus> _func;
private readonly Action<T> _funcStop;
private readonly Action<T> _funcAborted;

// ========================================================= CONSTRUCTION

public FuncOperator(Func<T, TaskStatus> func, Action<T> funcStop = null)
public FuncOperator(Func<T, TaskStatus> func, Action<T> funcStop = null, Action<T> funcAborted = null)
{
_func = func;
_funcStop = funcStop;
_funcAborted = funcAborted;
}

// ========================================================= FUNCTIONALITY
Expand All @@ -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!");
}
}
}
}
3 changes: 2 additions & 1 deletion Fluid-HTN/Operators/IOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ public interface IOperator
{
TaskStatus Update(IContext ctx);
void Stop(IContext ctx);
void Aborted(IContext ctx);
}
}
}
10 changes: 10 additions & 0 deletions Fluid-HTN/Planners/Planner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ public void Tick(Domain<T> domain, T ctx, bool allowImmediateReplan = true)
{
OnNewTaskConditionFailed?.Invoke(_currentTask, condition);

if (_currentTask is IPrimitiveTask task)
{
task.Aborted(ctx);
}

_currentTask = null;
_plan.Clear();

Expand Down Expand Up @@ -273,6 +278,8 @@ public void Tick(Domain<T> domain, T ctx, bool allowImmediateReplan = true)
{
OnCurrentTaskExecutingConditionFailed?.Invoke(task, condition);

task.Aborted(ctx);

_currentTask = null;
_plan.Clear();

Expand Down Expand Up @@ -337,6 +344,8 @@ public void Tick(Domain<T> domain, T ctx, bool allowImmediateReplan = true)
{
OnCurrentTaskFailed?.Invoke(task);

task.Aborted(ctx);

_currentTask = null;
_plan.Clear();

Expand All @@ -361,6 +370,7 @@ public void Tick(Domain<T> 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;
}
Expand Down
3 changes: 2 additions & 1 deletion Fluid-HTN/Tasks/PrimitiveTasks/IPrimitiveTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ public interface IPrimitiveTask : ITask
void ApplyEffects(IContext ctx);

void Stop(IContext ctx);
void Aborted(IContext ctx);
}
}
}
5 changes: 5 additions & 0 deletions Fluid-HTN/Tasks/PrimitiveTasks/PrimitiveTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit bf5f6c3

Please sign in to comment.