类的使用
cn.wjybxx.btree.Task
使用Task的程序包
程序包
说明
-
cn.wjybxx.btree中Task的使用
修饰符和类型类说明classBranchTask<T>分支任务(可能有多个子节点)classDecorator<T>装饰任务(最多只有一个子节点)classLeafTask<T>叶子任务(不能有子节点)classTaskEntry<T>任务入口(可联想程序的Main)声明为Task的cn.wjybxx.btree中的字段类型参数类型为Task的cn.wjybxx.btree中的字段返回Task的cn.wjybxx.btree中的方法修饰符和类型方法说明BranchTask.getChild(int index) Decorator.getChild()Decorator.getChild(int index) LeafTask.getChild(int index) Task.getChild(int index) 获取指定索引的childTaskEntry.getChild(int index) Task.getControl()BranchTask.getFirstChild()获取第一个子节点 -- 主要为MainPolicy提供帮助Task.getGuard()TaskInlineHelper.getInlinedRunningChild()获取被内联运行的子节点BranchTask.getLastChild()获取最后一个子节点TaskEntry.getRootTask()default <T> Task<T> TreeLoader.loadRootTask(String treeName) Task.removeChild(int index) 删除指定索引的childBranchTask.removeChildImpl(int index) Decorator.removeChildImpl(int index) LeafTask.removeChildImpl(int index) Task.removeChildImpl(int index) TaskEntry.removeChildImpl(int index) 替换指定索引位置的child (该方法可避免Task的结构发生变化,也可以减少事件数)BranchTask.setChildImpl(int index, Task<T> task) Decorator.setChildImpl(int index, Task<T> task) LeafTask.setChildImpl(int index, Task<T> task) Task.setChildImpl(int index, Task<T> task) TaskEntry.setChildImpl(int index, Task<T> task) Task.setFlags(int flags) default <T> Task<T> TreeLoader.tryLoadRootTask(String treeName) 尝试加载行为树的根节点返回变量类型为Task的类型的cn.wjybxx.btree中的方法修饰符和类型方法说明BranchTask.childStream()Decorator.childStream()LeafTask.childStream()Task.childStream()该接口主要用于测试,该接口有一定的开销TaskEntry.childStream()BranchTask.getChildren()参数类型为Task的cn.wjybxx.btree中的方法修饰符和类型方法说明final int1.尽量不要在运行时增删子节点(危险操作) 2.不建议将Task从一棵树转移到另一棵树,可能产生内存泄漏(引用未删除干净)protected final intBranchTask.addChildImpl(Task<T> task) protected final intDecorator.addChildImpl(Task<T> task) protected final intLeafTask.addChildImpl(Task<T> task) protected abstract intTask.addChildImpl(Task<T> task) protected final intTaskEntry.addChildImpl(Task<T> task) final intBranchTask.indexChild(Task<?> task) final intDecorator.indexChild(Task<?> task) final intLeafTask.indexChild(Task<?> task) intTask.indexChild(Task<?> task) final intTaskEntry.indexChild(Task<?> task) final voidTaskInlineHelper.inlineChild(Task<T> runningChild) 尝试内联运行中的子节点final booleanBranchTask.isFirstChild(Task<?> child) 是否是第一个子节点final booleanBranchTask.isLastChild(Task<?> child) 是否是第最后一个子节点protected final voidLeafTask.onChildCompleted(Task<T> child) protected abstract voidTask.onChildCompleted(Task<T> child) 子节点进入完成状态 1.避免方法数太多,实现类测试task的status即可 2.getNormalizedStatus()有助于switch测试 3.task可能是取消状态,甚至可能没运行过直接失败(前置条件失败) 4.钩子任务和guard不会调用该方法 5.isExecuting()有助于检测冲突,减少调用栈深度 6.同一子节点连续通知的情况下,completed的逻辑应当覆盖onChildRunning(Task)的影响。protected voidTaskEntry.onChildCompleted(Task<T> child) protected voidDecorator.onChildRunning(Task<T> child) 子类如果支持内联,则重写该方法protected final voidLeafTask.onChildRunning(Task<T> child) protected abstract voidTask.onChildRunning(Task<T> child) 子节点还需要继续运行 1.child在运行期间只会通知一次 2.该方法不应该触发状态迁移,即不应该使自己进入完成状态protected voidTaskEntry.onChildRunning(Task<T> child) final booleanTask.removeChild(Task<?> task) 删除指定childstatic voidTask.resetForRestart(Task<?> task) 重置目标任务void替换指定索引位置的child (该方法可避免Task的结构发生变化,也可以减少事件数)final voidTask.setChildCancelToken(Task<T> child, ICancelToken childCancelToken) 设置子节点的取消令牌BranchTask.setChildImpl(int index, Task<T> task) Decorator.setChildImpl(int index, Task<T> task) LeafTask.setChildImpl(int index, Task<T> task) Task.setChildImpl(int index, Task<T> task) TaskEntry.setChildImpl(int index, Task<T> task) final voidTask.setControl(Task<T> control) 设置任务的控制节点final voidTask.setGuardFailed(Task<T> control) 设置为前置条件测试失败 1.该方法仅适用于control测试child的guard失败,令child在未运行的情况下直接失败的情况。voidTaskEntry.setRootTask(Task<T> rootTask) static void停止目标任务final booleanTask.template_checkGuard(Task<T> guard) 检查前置条件 1.如果未显式设置guard的上下文,会默认捕获当前Task的上下文 2.guard的上下文在运行结束后会被清理 3.guard只应该依赖共享上下文(黑板和props),不应当对父节点做任何的假设。final voidTask.template_runChild(Task<T> child) 运行子节点,会检查子节点的前置条件 注:如果想减少方法调用,对于运行中的子节点,可直接调用子节点的template_execute(boolean)方法。final voidTask.template_runChildDirectly(Task<T> child) 运行子节点,不检查子节点的前置条件final voidTask.template_runHook(Task<T> hook) 执行钩子任务,会检查前置条件 1.钩子任务不会触发onChildRunning(Task)和onChildCompleted(Task)2.前置条件其实是特殊的钩子任务 3.条件分支不应该有钩子任务final voidTask.template_runHookDirectly(Task<T> hook) 执行钩子任务,不检查前置条件final voidTask.template_runInlinedChild(Task<T> inlinedChild, TaskInlineHelper<T> helper, Task<T> runningChild) 执行被内联的子任务final voidTask.unsetChildCancelToken(Task<T> child) 删除子节点的取消令牌类型变量类型为Task的cn.wjybxx.btree中的方法参数参数类型为Task的cn.wjybxx.btree中的构造器类型变量类型为Task的cn.wjybxx.btree中的构造器参数 -
cn.wjybxx.btree.branch中Task的使用
修饰符和类型类说明class主动选择节点 每次运行时都会重新测试节点的运行条件,选择一个新的可运行节点。classFixedSwitch<T>展开的switch 在编辑器中,children根据坐标排序,容易变动;这里将其展开为字段,从而方便配置。classForeach<T>迭代所有的子节点最后返回成功classJoin<T>Join 1.在得出结果之前不会重复执行已完成的任务。classParallel<T>并行节点基类 定义该类主要说明一些注意事项,包括: 1.不建议在子节点完成事件中再次驱动子节点,避免运行execute()方法,否则可能导致其它task单帧内运行多次。classSelector<T>classSelectorN<T>多选Selector。classSequence<T>class服务并发节点 1.其中第一个任务为主要任务,其余任务为后台服务。class简单并发节点。class非并行分支节点抽象(最多只有一个运行中的子节点)classSwitch<T>Switch-选择一个分支运行,直到其结束声明为Task的cn.wjybxx.btree.branch中的字段返回Task的cn.wjybxx.btree.branch中的方法修饰符和类型方法说明FixedSwitch.getBranch1()FixedSwitch.getBranch2()FixedSwitch.getBranch3()FixedSwitch.getBranch4()FixedSwitch.getBranch5()SingleRunningChildBranch.getRunningChild()获取运行中的子节点SingleRunningChildBranch.nextChild()参数类型为Task的cn.wjybxx.btree.branch中的方法修饰符和类型方法说明static <T> ParallelChildHelper<T> Parallel.getChildHelper(Task<T> child) 获取child的helperprotected voidActiveSelector.onChildCompleted(Task<T> child) protected voidForeach.onChildCompleted(Task<T> child) protected voidJoin.onChildCompleted(Task<T> child) voidJoinPolicy.onChildCompleted(Join<T> join, Task<T> child) Join在调用该方法前更新了完成计数和成功计数protected voidSelector.onChildCompleted(Task<T> child) protected voidSelectorN.onChildCompleted(Task<T> child) protected voidSequence.onChildCompleted(Task<T> child) protected voidServiceParallel.onChildCompleted(Task<T> child) protected voidSimpleParallel.onChildCompleted(Task<T> child) protected voidSingleRunningChildBranch.onChildCompleted(Task<T> child) 子类的实现模板:protected voidSwitch.onChildCompleted(Task<T> child) protected voidActiveSelector.onChildRunning(Task<T> child) protected voidForeach.onChildRunning(Task<T> child) protected voidJoin.onChildRunning(Task<T> child) protected voidParallel.onChildRunning(Task<T> child) 1.并发节点通常不需要在该事件中将自己更新为运行状态,而是应该在execute()方法的末尾更新 2.实现类可以在该方法中内联子节点protected voidSelector.onChildRunning(Task<T> child) protected voidSelectorN.onChildRunning(Task<T> child) protected voidSequence.onChildRunning(Task<T> child) protected voidServiceParallel.onChildRunning(Task<T> child) protected voidSimpleParallel.onChildRunning(Task<T> child) protected voidSingleRunningChildBranch.onChildRunning(Task<T> child) 子类如果支持内联,则重写该方法protected voidSwitch.onChildRunning(Task<T> child) voidFixedSwitch.setBranch1(Task<T> branch1) voidFixedSwitch.setBranch2(Task<T> branch2) voidFixedSwitch.setBranch3(Task<T> branch3) voidFixedSwitch.setBranch4(Task<T> branch4) voidFixedSwitch.setBranch5(Task<T> branch5) 参数类型为Task的cn.wjybxx.btree.branch中的构造器类型变量类型为Task的cn.wjybxx.btree.branch中的构造器参数限定符构造器说明ActiveSelector(List<Task<T>> children) ServiceParallel(List<Task<T>> children) SimpleParallel(List<Task<T>> children) SingleRunningChildBranch(List<Task<T>> children) -
cn.wjybxx.btree.branch.join中Task的使用
参数类型为Task的cn.wjybxx.btree.branch.join中的方法修饰符和类型方法说明voidJoinAnyOf.onChildCompleted(Join<T> join, Task<T> child) voidJoinMain.onChildCompleted(Join<T> join, Task<T> child) voidJoinSelector.onChildCompleted(Join<T> join, Task<T> child) voidJoinSelectorN.onChildCompleted(Join<T> join, Task<T> child) voidJoinSequence.onChildCompleted(Join<T> join, Task<T> child) voidJoinWaitAll.onChildCompleted(Join<T> join, Task<T> child) -
cn.wjybxx.btree.decorator中Task的使用
修饰符和类型类说明class每一帧都检查子节点的前置条件,如果前置条件失败,则取消child执行并返回失败。classAlwaysFail<T>在子节点完成之后固定返回失败class在子节点完成之后仍返回运行。class在子节点完成之后固定返回成功classInverter<T>反转装饰器,它用于反转子节点的执行结果。class循环节点抽象classOnlyOnce<T>只执行一次。classRepeat<T>重复N次classSubtreeRef<T>子树引用classUntilCond<T>循环子节点直到给定的条件达成classUntilFail<T>重复运行子节点,直到该任务失败classUntilSuccess<T>重复运行子节点,直到该任务成功参数类型为Task的cn.wjybxx.btree.decorator中的方法修饰符和类型方法说明protected voidAlwaysCheckGuard.onChildCompleted(Task<T> child) protected voidAlwaysFail.onChildCompleted(Task<T> child) protected voidAlwaysRunning.onChildCompleted(Task<T> child) protected voidAlwaysSuccess.onChildCompleted(Task<T> child) protected voidInverter.onChildCompleted(Task<T> child) protected voidOnlyOnce.onChildCompleted(Task<T> child) protected voidRepeat.onChildCompleted(Task<T> child) protected voidSubtreeRef.onChildCompleted(Task<T> child) protected voidUntilCond.onChildCompleted(Task<T> child) protected voidUntilFail.onChildCompleted(Task<T> child) protected voidUntilSuccess.onChildCompleted(Task<T> child) protected voidAlwaysCheckGuard.onChildRunning(Task<T> child) protected voidAlwaysFail.onChildRunning(Task<T> child) protected voidAlwaysRunning.onChildRunning(Task<T> child) protected voidAlwaysSuccess.onChildRunning(Task<T> child) protected voidInverter.onChildRunning(Task<T> child) protected voidOnlyOnce.onChildRunning(Task<T> child) protected voidRepeat.onChildRunning(Task<T> child) protected voidSubtreeRef.onChildRunning(Task<T> child) protected voidUntilCond.onChildRunning(Task<T> child) protected voidUntilFail.onChildRunning(Task<T> child) protected voidUntilSuccess.onChildRunning(Task<T> child) void参数类型为Task的cn.wjybxx.btree.decorator中的构造器限定符构造器说明AlwaysCheckGuard(Task<T> child) AlwaysFail(Task<T> child) AlwaysSuccess(Task<T> child) LoopDecorator(Task<T> child) UntilSuccess(Task<T> child) -
cn.wjybxx.btree.fsm中Task的使用
修饰符和类型类说明classclass栈式状态机,在普通状态机的基础上支持了redo和undoclass状态机节点 ps:以我的经验来看,状态机是最重要的节点,Join则是仅次于状态机的节点 -- 不能以使用数量而定。声明为Task的cn.wjybxx.btree.fsm中的字段修饰符和类型字段说明StateMachineTask.initState初始状态StateMachineTask.tempNextState待切换的状态,主要用于支持当前状态退出后再切换 -- 即支持当前状态设置结果返回Task的cn.wjybxx.btree.fsm中的方法修饰符和类型方法说明StateMachineTask.discardTempNextState()丢弃未切换的临时状态StateMachineTask.getCurState()获取当前状态StateMachineTask.getInitState()ChangeStateTask.getNextState()StateMachineTask.getTempNextState()获取临时的下一个状态StackStateMachineTask.peekRedoState()查看redo对应的stateStackStateMachineTask.peekUndoState()查看undo对应的state参数类型为Task的cn.wjybxx.btree.fsm中的方法修饰符和类型方法说明final booleanStackStateMachineTask.addRedoState(Task<T> curState) 向redo队列中添加一个状态final booleanStackStateMachineTask.addUndoState(Task<T> curState) 向undo队列中添加一个状态protected voidStackStateMachineTask.beforeChangeState(Task<T> curState, Task<T> nextState) voidStateMachineHandler.beforeChangeState(StateMachineTask<T> stateMachineTask, Task<T> curState, Task<T> nextState) 该方法在进入新状态前调用 1.两个参数最多一个为null 2.可以设置新状态的黑板和其它数据 3.用户此时可为新状态分配上下文(黑板、取消令牌、共享属性);同时清理前一个状态的上下文 4.用户此时可拿到新状态ChangeStateArgs,后续则不可 5.如果task需要感知redo和undo,则由用户将信息写入黑板final voidStateMachineHandlers.RedoHandler.beforeChangeState(StateMachineTask<T> stateMachineTask, Task<T> curState, Task<T> nextState) protected voidStateMachineTask.beforeChangeState(Task<T> curState, Task<T> nextState) final voidStateMachineTask.changeState(Task<T> nextState) 切换状态 -- 如果状态机处于运行中,则立即切换final voidStateMachineTask.changeState(Task<T> nextState, ChangeStateArgs changeStateArgs) 切换状态 1.如果当前有一个待切换的状态,则会被悄悄丢弃(todo 可以增加一个通知) 2.无论何种模式,在当前状态进入完成状态时一定会触发 3.如果状态机未运行,则仅仅保存在那里,等待下次运行的时候执行 4.当前状态可先正常完成,然后再切换状态,就可以避免进入被取消状态;可参考ChangeStateTaskstatic <T> StateMachineTask<T> StateMachineTask.findStateMachine(Task<T> task) 查找task最近的状态机节点 1.仅递归查询父节点和长兄节点 2.优先查找附近的,然后测试长兄节点 - 状态机作为第一个节点的情况比较常见static <T> StateMachineTask<T> StateMachineTask.findStateMachine(Task<T> task, String name) 查找task最近的状态机节点 1.名字不为空的情况下,支持从兄弟节点中查询 2.优先测试父节点,然后测试兄弟节点protected booleandefault intStateMachineHandler.onChildCompleted(StateMachineTask<T> stateMachineTask, Task<T> curState) 该方法在当前状态正常结束(非stop结束)时调用final intStateMachineHandlers.RedoHandler.onChildCompleted(StateMachineTask<T> stateMachineTask, Task<T> curState) protected voidStateMachineTask.onChildCompleted(Task<T> child) protected voidStateMachineTask.onChildRunning(Task<T> child) default booleanStateMachineHandler.onNextStateAbsent(StateMachineTask<T> stateMachineTask, Task<T> preState) 当状态机没有下一个状态时调用该方法,以避免无可用状态 注意: 1.状态机启动时不会调用该方法 2.如果该方法返回后仍无可用状态,将触发无状态逻辑booleanStateMachineHandlers.RedoHandler.onNextStateAbsent(StateMachineTask<T> stateMachineTask, Task<T> preState) voidStateMachineTask.setInitState(Task<T> initState) voidChangeStateTask.setNextState(Task<T> nextState) 参数类型为Task的cn.wjybxx.btree.fsm中的构造器 -
cn.wjybxx.btree.leaf中Task的使用
修饰符和类型类说明classActionTask<T>行为节点抽象 (并非所有行为节点都需要继承该类)class条件节点 注意:并非条件节点必须继承该类。classFailure<T>classRunning<T>classSimpleRandom<T>简单随机任务 在正式的项目中,Random应当从实体上获取。classSuccess<T>classWaitFrame<T>等待N帧