Skip to content

Commit

Permalink
初始化为事件驱动库
Browse files Browse the repository at this point in the history
  • Loading branch information
hl845740757 committed Sep 12, 2024
1 parent a1ac786 commit 4b51358
Show file tree
Hide file tree
Showing 75 changed files with 8,330 additions and 0 deletions.
88 changes: 88 additions & 0 deletions Wjybxx.BTree.Core/src/Branch/ActiveSelector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#region LICENSE

// Copyright 2024 wjybxx([email protected])
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#endregion

using System.Collections.Generic;

namespace Wjybxx.BTree.Branch
{
/// <summary>
/// 主动选择节点
/// 每次运行时都会重新测试节点的运行条件,选择一个新的可运行节点。
/// 如果新选择的运行节点与之前的运行节点不同,则取消之前的任务。
/// (ActiveSelector也是比较常用的节点,做内联支持是合适的)
/// </summary>
/// <typeparam name="T"></typeparam>
public class ActiveSelector<T> : SingleRunningChildBranch<T> where T : class
{
public ActiveSelector() {
}

public ActiveSelector(List<Task<T>>? children) : base(children) {
}

protected override void Execute() {
Task<T> childToRun = null;
int childIndex = -1;
for (int idx = 0; idx < children.Count; idx++) {
Task<T> child = children[idx];
if (!Template_CheckGuard(child.Guard)) {
continue; // 不能调用SetGuardFailed,会中断当前运行中的child
}
childToRun = child;
childIndex = idx;
break;
}

if (childToRun == null) {
Stop(this.runningChild); // 不清理index,允许退出后查询最后一次运行的child
SetFailed(TaskStatus.ERROR);
return;
}

Task<T> runningChild = this.runningChild;
if (runningChild == childToRun) {
Task<T> inlinedChild = inlineHelper.GetInlinedChild();
if (inlinedChild != null) {
inlinedChild.Template_ExecuteInlined(ref inlineHelper, runningChild);
} else if (runningChild.IsRunning) {
runningChild.Template_Execute(true);
} else {
Template_StartChild(runningChild, false);
}
} else {
if (runningChild != null) {
runningChild.Stop();
inlineHelper.StopInline();
}
this.runningChild = childToRun;
this.runningIndex = childIndex;
Template_StartChild(childToRun, false);
}
}

protected override void OnChildRunning(Task<T> child) {
inlineHelper.InlineChild(child);
}

protected override void OnChildCompleted(Task<T> child) {
runningChild = null;
inlineHelper.StopInline();
SetCompleted(child.Status, true);
}
}
}
81 changes: 81 additions & 0 deletions Wjybxx.BTree.Core/src/Branch/FixedSwitch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#region LICENSE

// Copyright 2024 wjybxx([email protected])
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#endregion

namespace Wjybxx.BTree.Branch
{
/// <summary>
/// 展开的switch
/// 在编辑器中,children根据坐标排序,容易变动;这里将其展开为字段,从而方便配置。
/// (这个类不是必须的,因为我们可以仅提供编辑器数据结构,在导出时转为Switch)
/// </summary>
/// <typeparam name="T"></typeparam>
[TaskInlinable]
public class FixedSwitch<T> : Switch<T> where T : class
{
private Task<T>? branch1;
private Task<T>? branch2;
private Task<T>? branch3;
private Task<T>? branch4;
private Task<T>? branch5;

public FixedSwitch() {
}

protected override void BeforeEnter() {
base.BeforeEnter();
if (children.Count == 0) {
AddChildIfNotNull(branch1);
AddChildIfNotNull(branch2);
AddChildIfNotNull(branch3);
AddChildIfNotNull(branch4);
AddChildIfNotNull(branch5);
}
}

private void AddChildIfNotNull(Task<T>? branch) {
if (branch != null) {
AddChild(branch);
}
}

public Task<T>? Branch1 {
get => branch1;
set => branch1 = value;
}

public Task<T>? Branch2 {
get => branch2;
set => branch2 = value;
}

public Task<T>? Branch3 {
get => branch3;
set => branch3 = value;
}

public Task<T>? Branch4 {
get => branch4;
set => branch4 = value;
}

public Task<T>? Branch5 {
get => branch5;
set => branch5 = value;
}
}
}
60 changes: 60 additions & 0 deletions Wjybxx.BTree.Core/src/Branch/Foreach.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#region LICENSE

// Copyright 2024 wjybxx([email protected])
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#endregion

using System.Collections.Generic;

namespace Wjybxx.BTree.Branch
{
/// <summary>
/// 迭代所有的子节点最后返回成功
/// </summary>
/// <typeparam name="T"></typeparam>
[TaskInlinable]
public class Foreach<T> : SingleRunningChildBranch<T> where T : class
{
public Foreach() {
}

public Foreach(List<Task<T>>? children) : base(children) {
}

protected override void Enter(int reentryId) {
if (children.Count == 0) {
SetSuccess();
}
}

protected override void OnChildRunning(Task<T> child) {
inlineHelper.InlineChild(child);
}

protected override void OnChildCompleted(Task<T> child) {
runningChild = null;
inlineHelper.StopInline();
if (child.IsCancelled) {
SetCancelled();
return;
}
if (IsAllChildCompleted) {
SetSuccess();
} else {
Template_Execute(false);
}
}
}
}
33 changes: 33 additions & 0 deletions Wjybxx.BTree.Core/src/Branch/ISwitchHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#region LICENSE

// Copyright 2024 wjybxx([email protected])
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#endregion

namespace Wjybxx.BTree.Branch
{
/// <summary>
/// 分支选择接口
/// </summary>
public interface ISwitchHandler<T> where T : class
{
/// <summary>
/// 选择要执行的子节点
/// </summary>
/// <param name="branchTask">要测试的分支</param>
/// <returns>选中的分支索引,-1表示未选中</returns>
int Select(BranchTask<T> branchTask);
}
}
Loading

0 comments on commit 4b51358

Please sign in to comment.