You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.1 KiB
C#

using System;
namespace FluidHTN.Conditions
{
public class FuncCondition<T> : ICondition where T : IContext
{
// ========================================================= FIELDS
private readonly Func<T, bool> _func;
// ========================================================= CONSTRUCTION
public FuncCondition(string name, Func<T, bool> func)
{
Name = name;
_func = func;
}
// ========================================================= PROPERTIES
public string Name { get; }
// ========================================================= VALIDITY
public bool IsValid(IContext ctx)
{
if (ctx is T c)
{
var result = _func?.Invoke(c) ?? false;
if (ctx.LogDecomposition) ctx.Log(Name, $"FuncCondition.IsValid:{result}", ctx.CurrentDecompositionDepth+1, this, result ? ConsoleColor.DarkGreen : ConsoleColor.DarkRed);
return result;
}
throw new Exception("Unexpected context type!");
}
}
}