类的使用
cn.wjybxx.btree.Task
使用Task的程序包
程序包
说明
-
cn.wjybxx.btree中Task的使用
修饰符和类型类说明classActionTask<T>行为节点抽象 (并非所有行为节点都需要继承该类)classBranchTask<T>分支任务(可能有多个子节点)class条件节点 1.class可返回详细错误码的条件节点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()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从一棵树转移到另一棵树,可能产生内存泄漏(引用未删除干净) 3.如果需要知道task的索引,可提前调用getChildCount()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 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) static voidTask.resetForRestart(Task<?> task) void替换指定索引位置的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) final voidTask.setControl(Task<T> control) 设置任务的控制节点final voidTask.setGuardFailed(Task<T> control) 设置为前置条件测试失败 1.该方法仅适用于control测试child的guard失败,令child在未运行的情况下直接失败的情况。voidTaskEntry.setRootTask(Task<T> rootTask) static voidfinal booleanTask.template_checkGuard(Task<T> guard) 检查前置条件 1.如果未显式设置guard的上下文,会默认捕获当前Task的上下文 2.guard的上下文在运行结束后会被清理 3.guard只应该依赖共享上下文(黑板和props),不应当对父节点做任何的假设。final voidTask.template_runChild(Task<T> child) 运行子节点,会检查子节点的前置条件final voidTask.template_runChildDirectly(Task<T> child) 运行子节点,不检查子节点的前置条件final voidTask.template_runHook(Task<T> hook) 执行钩子任务,会检查前置条件 1.钩子任务不会触发onChildRunning(Task)和onChildCompleted(Task)2.前置条件其实是特殊的钩子任务final voidTask.template_runHookDirectly(Task<T> hook) 执行钩子任务,不检查前置条件类型变量类型为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非并行分支节点抽象(最多只有一个运行中的子节点) 如果SingleRunningChildBranch.execute()方法是有循环体的,那么一定要注意: 只有循环的尾部运行child才是安全的,如果在运行child后还读写其它数据,可能导致bug(小心递归)。classSwitch<T>Switch-选择一个分支运行,直到其结束声明为Task的cn.wjybxx.btree.branch中的字段返回Task的cn.wjybxx.btree.branch中的方法修饰符和类型方法说明FixedSwitch.getBranch1()FixedSwitch.getBranch2()FixedSwitch.getBranch3()FixedSwitch.getBranch4()FixedSwitch.getBranch5()SingleRunningChildBranch.nextChild()参数类型为Task的cn.wjybxx.btree.branch中的方法修饰符和类型方法说明protected 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 voidParallel.onChildRunning(Task<T> child) 并发节点通常不需要在该事件中将自己更新为运行状态,而是应该在execute()方法的末尾更新protected voidSingleRunningChildBranch.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循环节点抽象 如果LoopDecorator.execute()方法是有循环体的,那么一定要注意: 只有循环的尾部运行child才是安全的,如果在运行child后还读写其它数据,可能导致bug(小心递归)。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) 参数类型为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状态机节点 1.redo和undo是很有用的特性,因此我们在顶层给予支持,但默认的队列不会保存状态。返回Task的cn.wjybxx.btree.fsm中的方法修饰符和类型方法说明StateMachineTask.discardTempNextState()丢弃未切换的临时状态StateMachineTask.getCurState()获取当前状态StateMachineTask.getInitState()ChangeStateTask.getNextState()StateMachineTask.getTempNextState()获取临时的下一个状态StateMachineTask.peekRedoState()查看redo对应的stateStateMachineTask.peekUndoState()查看undo对应的state返回变量类型为Task的类型的cn.wjybxx.btree.fsm中的方法修饰符和类型方法说明StateMachineTask.getRedoQueue()开放以允许填充StateMachineTask.getUndoQueue()开放以允许填充StateMachineTask.setRedoQueueSize(int maxSize) StateMachineTask.setUndoQueueSize(int maxSize) 参数类型为Task的cn.wjybxx.btree.fsm中的方法修饰符和类型方法说明voidStateMachineListener.beforeChangeState(StateMachineTask<T> stateMachineTask, Task<T> curState, Task<T> nextState) 1.两个参数最多一个为null 2.可以设置新状态的黑板和其它数据 3.用户此时可为新状态分配上下文;同时清理前一个状态的上下文 4.用户此时可拿到新状态ChangeStateArgs,后续则不可 5.如果task需要感知redo和undo,则由用户将信息写入黑板final voidStateMachineTask.changeState(Task<T> nextState) 切换状态 -- 如果状态机处于运行中,则立即切换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 final booleanprotected final voidStateMachineTask.notifyChangeState(Task<T> curState, Task<T> nextState) protected voidStateMachineTask.onChildCompleted(Task<T> child) booleanStateMachineHandler.onNextStateAbsent(StateMachineTask<T> stateMachineTask, Task<T> preState) 当状态机没有下一个状态时调用该方法,以避免无可用状态 注意: 1.状态机启动时不会调用该方法 2.如果该方法返回后仍无可用状态,将触发无状态逻辑 3.default voidStateMachineHandler.onNextStateGuardFailed(StateMachineTask<T> stateMachineTask, Task<T> nextState) 下个状态的前置条件检查失败voidStateMachineTask.setInitState(Task<T> initState) voidChangeStateTask.setNextState(Task<T> nextState) 参数类型为Task的cn.wjybxx.btree.fsm中的构造器 -
cn.wjybxx.btree.leaf中Task的使用
修饰符和类型类说明classFailure<T>classRunning<T>classSimpleRandom<T>简单随机任务 在正式的项目中,Random应当从实体上获取。classSuccess<T>classWaitFrame<T>等待N帧