namespace DySerin; /*public class ConditionNode : NodeBase, ICondition { private Func ConditionFunc { get; set; } public ConditionNode(Func conditionFunc) { ConditionFunc = conditionFunc; } public override void Execute() { if (ConditionFunc()) { EnterTrueBranch(); } else { EnterFalseBranch(); } } // 实现条件接口的方法 public bool Evaluate(DynamicContext context) { Context = context; // 更新节点的上下文 return ConditionFunc(); } } public class ActionNode : NodeBase, IAction { private Action ActionMethod { get; set; } public ActionNode(Action actionMethod) { ActionMethod = actionMethod; } public override void Execute() { try { ActionMethod(); EnterTrueBranch(); // 动作成功,进入真分支 } catch (Exception ex) { // 可添加异常处理逻辑 Return(); // 动作失败,终止节点 } } // 实现动作接口的方法 public void Execute(DynamicContext context) { Context = context; // 更新节点的上下文 Execute(); } } */ /* /// /// 根节点 /// public abstract class NodeControl : UserControl { public string Id { get; set; } public string Name { get; set; } public abstract void Execute(NodeContext context); } /// /// 条件节点 /// public class ConditionNodeControl : NodeControl { public Func Condition { get; set; } public NodeControl TrueNode { get; set; } public NodeControl FalseNode { get; set; } public ConditionNodeControl() { this.Content = new TextBlock { Text = "条件节点" }; this.Background = Brushes.LightBlue; } public override void Execute(NodeContext context) { if (Condition(context)) { TrueNode?.Execute(context); } else { FalseNode?.Execute(context); } } } /// /// 动作节点 /// public class ActionNodeControl : NodeControl { public Action Action { get; set; } public ActionNodeControl() { this.Content = new TextBlock { Text = "动作节点" }; this.Background = Brushes.LightGreen; } public override void Execute(NodeContext context) { Action?.Invoke(context); } } /// /// 状态节点 /// public class StateNodeControl : NodeControl { public Func StateFunction { get; set; } public StateNodeControl() { this.Content = new TextBlock { Text = "状态节点" }; this.Background = Brushes.LightYellow; } public override void Execute(NodeContext context) { var result = StateFunction(context); context.Set("StateResult", result); } } /// /// 节点上下文 /// public class NodeContext { private readonly Dictionary _data = new Dictionary(); public void Set(string key, T value) { _data[key] = value; } public T Get(string key) { return _data.ContainsKey(key) ? (T)_data[key] : default; } } */ /* public class Context { public Dictionary Data { get; set; } = new Dictionary(); public void Set(string key, T value) { Data[key] = value; } public T Get(string key) { return Data.ContainsKey(key) ? (T)Data[key] : default; } } public interface INode { Context Enter(Context context); Context Exit(Context context); } public partial class ConditionNode : INode { public Func Condition { get; set; } public INode TrueBranch { get; set; } public INode FalseBranch { get; set; } public Context Enter(Context context) { if (Condition(context)) { return TrueBranch?.Enter(context) ?? context; } else { return FalseBranch?.Enter(context) ?? context; } } public Context Exit(Context context) { return context; } } public partial class ActionNode : INode { public Action Action { get; set; } public bool IsTask { get; set; } public Context Enter(Context context) { if (IsTask) { Task.Run(() => Action(context)); } else { Action(context); } return context; } public Context Exit(Context context) { return context; } } public partial class StateNode : INode { public Context Enter(Context context) { return context; } public Context Exit(Context context) { return context; } } */