c-o
This commit is contained in:
@@ -0,0 +1,254 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// file="Blueprint.cs"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the “Software”), to deal in the Software without restriction,
|
||||
// including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
// and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and
|
||||
// this permission notice shall be included in all copies
|
||||
// or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED “AS IS”,
|
||||
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH
|
||||
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
using StgSharp;
|
||||
using StgSharp.Logic;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace StgSharp.Logic
|
||||
{
|
||||
public partial class Blueprint : IConvertableToBlueprintNode
|
||||
{
|
||||
private BeginningNode beginNode;
|
||||
private Dictionary<string, BlueprintNode> allNode;
|
||||
private EndingNode endNode;
|
||||
private Func<bool> terminate;
|
||||
|
||||
private int currentNodeIndex;
|
||||
private List<Task> allThread;
|
||||
private List<BlueprintNode> nativeNodeList;
|
||||
private List<BlueprintNode> nodeList;
|
||||
|
||||
public Blueprint()
|
||||
{
|
||||
allNode = new Dictionary<string, BlueprintNode>();
|
||||
allThread = new List<Task>();
|
||||
nodeList = new List<BlueprintNode>();
|
||||
nativeNodeList = new List<BlueprintNode>();
|
||||
terminate = () => true;
|
||||
beginNode = new BeginningNode();
|
||||
endNode = new EndingNode();
|
||||
}
|
||||
|
||||
public BlueprintNode BeginLayer => beginNode;
|
||||
public BlueprintNode EndLayer => endNode;
|
||||
|
||||
public bool IsRunning => allThread.Any(t => t.Status == TaskStatus.Running);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public (string name, BlueprintPipelineArgs arg)[] GetOutput()
|
||||
{
|
||||
return endNode.OutputData;
|
||||
}
|
||||
|
||||
public void Init(int threadCount)
|
||||
{
|
||||
if (threadCount > nodeList.Count)
|
||||
{
|
||||
InternalIO.InternalWriteLog(
|
||||
"Thread setting error: amount of threads of this blueprint is larger than amount of nodes.", LogType.Warning);
|
||||
threadCount = nodeList.Count;
|
||||
}
|
||||
if (threadCount!=0)
|
||||
{
|
||||
foreach (var item in allNode)
|
||||
{
|
||||
if (item.Value.IsNative)
|
||||
{
|
||||
nativeNodeList.Add(item.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
nodeList.Add(item.Value);
|
||||
}
|
||||
}
|
||||
nativeNodeList.Sort();
|
||||
PrepareTasks(threadCount);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var item in allNode)
|
||||
{
|
||||
nodeList.Add(item.Value);
|
||||
}
|
||||
}
|
||||
nodeList.Sort();
|
||||
}
|
||||
|
||||
public void PrepareTasks(int count)
|
||||
{
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
allThread.Add(new Task(IndividualTask));
|
||||
}
|
||||
}
|
||||
|
||||
private void IndividualTask()
|
||||
{
|
||||
do
|
||||
{
|
||||
BlueprintNode node;
|
||||
while (RequestNextNode(out node))
|
||||
{
|
||||
node.Run();
|
||||
}
|
||||
} while (!terminate());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Run all nodes repeatedly until it is instructed to terminate.
|
||||
/// </summary>
|
||||
/// <param name="terminateInstructor">Instructor to terminate all tasks. Returns <see langword="true"/> if tasks should terminate</param>
|
||||
public void RunRepeatedly(Func<bool> terminateInstructor)
|
||||
{
|
||||
if (allThread.Count != 0)
|
||||
{
|
||||
MultiThreadRun(terminateInstructor);
|
||||
return;
|
||||
}
|
||||
SingleThreadRun(terminateInstructor);
|
||||
}
|
||||
|
||||
private void MultiThreadRun(Func<bool> terminateInstructor)
|
||||
{
|
||||
if (terminate != terminateInstructor)
|
||||
{
|
||||
terminate = terminateInstructor;
|
||||
if (allThread.Count != 0)
|
||||
{
|
||||
foreach (var t in allThread)
|
||||
{
|
||||
t.Start();
|
||||
}
|
||||
}
|
||||
}
|
||||
beginNode.Run();
|
||||
foreach (var node in nativeNodeList)
|
||||
{
|
||||
node.Run();
|
||||
}
|
||||
EndLayer.Run();
|
||||
}
|
||||
|
||||
private void SingleThreadRun(Func<bool> terminateInstructor)
|
||||
{
|
||||
beginNode.Run();
|
||||
foreach (var item in nodeList)
|
||||
{
|
||||
item.Run();
|
||||
}
|
||||
EndLayer.Run();
|
||||
}
|
||||
|
||||
public void AddNativeNode(BlueprintNode node)
|
||||
{
|
||||
node.IsNative = true;
|
||||
allNode.Add(node.Name, node);
|
||||
}
|
||||
|
||||
public void AddAsyncNode(BlueprintNode node)
|
||||
{
|
||||
node.IsNative = false;
|
||||
allNode.Add(node.Name, node);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetInput(params (string name, BlueprintPipelineArgs arg)[] parameters)
|
||||
{
|
||||
beginNode.InputData = parameters;
|
||||
}
|
||||
|
||||
private bool RequestNextNode(out BlueprintNode node)
|
||||
{
|
||||
if (currentNodeIndex < nodeList.Count)
|
||||
{
|
||||
BlueprintNode ret = nodeList[currentNodeIndex];
|
||||
Interlocked.Increment(ref currentNodeIndex);
|
||||
node = ret;
|
||||
return true;
|
||||
}
|
||||
Interlocked.Exchange(ref currentNodeIndex, 0);
|
||||
node = default!;
|
||||
return false;
|
||||
}
|
||||
|
||||
string[] IConvertableToBlueprintNode.InputInterfacesName
|
||||
{
|
||||
get
|
||||
{
|
||||
return beginNode.OutputInterfaces.Select(
|
||||
p => p.Key).ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
BlueprintNodeAction IConvertableToBlueprintNode.MainExecution
|
||||
{
|
||||
get
|
||||
{
|
||||
return (in Dictionary<string, BlueprintPipeline> input,
|
||||
in Dictionary<string, BlueprintPipeline> output)
|
||||
=>
|
||||
{
|
||||
beginNode.Run();
|
||||
foreach (var node in nativeNodeList)
|
||||
{
|
||||
node.Run();
|
||||
}
|
||||
endNode.Run();
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
string[] IConvertableToBlueprintNode.OutputInterfacesName
|
||||
{
|
||||
get
|
||||
{
|
||||
return beginNode.OutputInterfaces.Select(
|
||||
p => p.Key).ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal class NodeClearException : Exception
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,316 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// file="BlueprintNode.cs"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the “Software”), to deal in the Software without restriction,
|
||||
// including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
// and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and
|
||||
// this permission notice shall be included in all copies
|
||||
// or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED “AS IS”,
|
||||
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH
|
||||
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
|
||||
using StgSharp.Entities;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Logic
|
||||
{
|
||||
public delegate void BlueprintNodeAction(
|
||||
in Dictionary<string, BlueprintPipeline> inpput,
|
||||
in Dictionary<string, BlueprintPipeline> output);
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <risk>
|
||||
/// Reset pipeline has risk, this operation is a direct operation to port
|
||||
/// in current node, the port on the other node cannot get the change.
|
||||
/// </risk>
|
||||
#pragma warning disable CA1036
|
||||
public class BlueprintNode : IComparable<BlueprintNode>
|
||||
#pragma warning restore CA1036
|
||||
{
|
||||
private BlueprintNodeAction mainOperation;
|
||||
|
||||
private protected bool levelAvailable, nativeRequested;
|
||||
private protected int _level;
|
||||
private string name;
|
||||
|
||||
private protected Dictionary<string, BlueprintPipeline> _inputInterfaces;
|
||||
private protected Dictionary<string, BlueprintPipeline> _outputInterfaces;
|
||||
|
||||
|
||||
internal BlueprintNode(string name,
|
||||
BlueprintNodeAction operation, bool isNative,
|
||||
string[] inputPorts,
|
||||
string[] outputPorts)
|
||||
{
|
||||
this.levelAvailable = false;
|
||||
this.name = name;
|
||||
nativeRequested = isNative;
|
||||
mainOperation = operation;
|
||||
_inputInterfaces = new Dictionary<string, BlueprintPipeline>();
|
||||
_outputInterfaces = new Dictionary<string, BlueprintPipeline>();
|
||||
if (inputPorts.Length == 0 )
|
||||
{
|
||||
throw new ArgumentNullException(nameof(inputPorts));
|
||||
}
|
||||
if (outputPorts.Length == 0)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(outputPorts));
|
||||
}
|
||||
foreach (var portName in inputPorts)
|
||||
{
|
||||
_inputInterfaces.Add(portName, null);
|
||||
}
|
||||
foreach (var portName in outputPorts)
|
||||
{
|
||||
_outputInterfaces.Add(portName, null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public BlueprintNode(string name, IConvertableToBlueprintNode convertable)
|
||||
{
|
||||
this.levelAvailable = false;
|
||||
this.name = name;
|
||||
_inputInterfaces = new Dictionary<string, BlueprintPipeline>();
|
||||
_outputInterfaces = new Dictionary<string, BlueprintPipeline>();
|
||||
mainOperation = convertable.MainExecution;
|
||||
foreach (var soket in convertable.InputInterfacesName)
|
||||
{
|
||||
_inputInterfaces.Add(soket, null);
|
||||
}
|
||||
foreach (var soket in convertable.OutputInterfacesName)
|
||||
{
|
||||
_outputInterfaces.Add(soket, null);
|
||||
}
|
||||
}
|
||||
|
||||
public BlueprintNode(string name,
|
||||
string[] inputPorts,
|
||||
string[] outputPorts
|
||||
)
|
||||
{
|
||||
this.levelAvailable = false;
|
||||
this.name = name;
|
||||
_inputInterfaces = new Dictionary<string, BlueprintPipeline>();
|
||||
_outputInterfaces = new Dictionary<string, BlueprintPipeline>();
|
||||
if (inputPorts.Length * outputPorts.Length == 0)
|
||||
{
|
||||
throw new Exception();
|
||||
}
|
||||
foreach (var portName in inputPorts)
|
||||
{
|
||||
_inputInterfaces.Add(portName, null);
|
||||
}
|
||||
foreach (var portName in outputPorts)
|
||||
{
|
||||
_outputInterfaces.Add(portName, null);
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsNative
|
||||
{
|
||||
get => nativeRequested;
|
||||
internal set => nativeRequested = value;
|
||||
}
|
||||
|
||||
public int Level
|
||||
{
|
||||
get
|
||||
{
|
||||
if (levelAvailable)
|
||||
{
|
||||
return _level;
|
||||
}
|
||||
_level = 0;
|
||||
foreach (var item in _inputInterfaces)
|
||||
{
|
||||
if (item.Value == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (_level <= item.Value.Level)
|
||||
{
|
||||
_level = item.Value.Level;
|
||||
}
|
||||
}
|
||||
_level += 1;
|
||||
return _level;
|
||||
}
|
||||
}
|
||||
|
||||
public string Name => name;
|
||||
|
||||
internal Dictionary<string, BlueprintPipeline> InputInterfaces => _inputInterfaces;
|
||||
internal Dictionary<string, BlueprintPipeline> OutputInterfaces => _outputInterfaces;
|
||||
|
||||
|
||||
public void AddAfter(
|
||||
BlueprintNode former,
|
||||
string formerOutputPortName,
|
||||
string thisInputPortName)
|
||||
{
|
||||
this.levelAvailable = false;
|
||||
BlueprintPipeline pipeline = new BlueprintPipeline(former, this);
|
||||
if (former is BeginningNode bnode)
|
||||
{
|
||||
bnode.SetCertainOutputPort(formerOutputPortName, pipeline);
|
||||
}
|
||||
else
|
||||
{
|
||||
former.SetCertainOutputPort(formerOutputPortName, pipeline);
|
||||
}
|
||||
SetCertainInputPort(thisInputPortName, pipeline);
|
||||
}
|
||||
|
||||
|
||||
public void AddBefore(
|
||||
BlueprintNode after,
|
||||
string thisOutputPortName,
|
||||
string afterInputPortName)
|
||||
{
|
||||
this.levelAvailable = false;
|
||||
BlueprintPipeline pipeline = new BlueprintPipeline(this, after);
|
||||
if (after is EndingNode enode)
|
||||
{
|
||||
enode.SetCertainInputPort(afterInputPortName, pipeline);
|
||||
}
|
||||
else
|
||||
{
|
||||
after.SetCertainInputPort(afterInputPortName, pipeline);
|
||||
}
|
||||
SetCertainOutputPort(thisOutputPortName, pipeline);
|
||||
}
|
||||
|
||||
|
||||
public void Run()
|
||||
{
|
||||
try
|
||||
{
|
||||
//Console.WriteLine(name);
|
||||
BlueprintPipeline.WaitAll(_inputInterfaces);
|
||||
mainOperation(in _inputInterfaces, in _outputInterfaces);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public int CompareTo(BlueprintNode other)
|
||||
{
|
||||
#pragma warning disable CA1062
|
||||
return this.Level.CompareTo(other.Level);
|
||||
#pragma warning restore CA1062
|
||||
}
|
||||
|
||||
#region port operation
|
||||
|
||||
public BlueprintPipeline GetCertainInputPort(string name)
|
||||
{
|
||||
BlueprintPipeline ret;
|
||||
if (!_inputInterfaces.TryGetValue(name,out ret))
|
||||
{
|
||||
throw new Exception($"Cannot find the interface named {name} in node {Name}.");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
public BlueprintPipeline GetCertainOutputPort(string name)
|
||||
{
|
||||
BlueprintPipeline ret;
|
||||
if (!_outputInterfaces.TryGetValue(name, out ret))
|
||||
{
|
||||
throw new Exception($"Cannot find the interface named {name} in node {Name}.");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public void SetCertainInputPort(string name, BlueprintPipeline pipeline)
|
||||
{
|
||||
if (!_inputInterfaces.ContainsKey(name))
|
||||
{
|
||||
throw new Exception($"Cannot find the interface named {name} in node {Name}.");
|
||||
}
|
||||
_inputInterfaces[name] = pipeline;
|
||||
}
|
||||
|
||||
public void SetCertainOutputPort(string name, BlueprintPipeline pipeline)
|
||||
{
|
||||
if (!_outputInterfaces.ContainsKey(name))
|
||||
{
|
||||
throw new Exception($"Cannot find the interface named {name} in node {Name}.");
|
||||
}
|
||||
_outputInterfaces[name] = pipeline;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
}//--------------------------------- end of class -----------------------------------
|
||||
|
||||
public interface IConvertableToBlueprintNode
|
||||
{
|
||||
|
||||
public string[] InputInterfacesName { get; }
|
||||
public BlueprintNodeAction MainExecution { get; }
|
||||
public string[] OutputInterfacesName { get; }
|
||||
|
||||
}
|
||||
|
||||
public class BlueprintNodeRunException : Exception
|
||||
{
|
||||
public BlueprintNodeRunException()
|
||||
{
|
||||
}
|
||||
|
||||
public BlueprintNodeRunException(Exception ex) : base(string.Empty, ex)
|
||||
{
|
||||
}
|
||||
|
||||
public BlueprintNodeRunException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public BlueprintNodeRunException(string message, Exception innerException) : base(message, innerException)
|
||||
{
|
||||
}
|
||||
|
||||
protected BlueprintNodeRunException(SerializationInfo info, StreamingContext context) : base(info, context)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Logic
|
||||
{
|
||||
/*
|
||||
public class CrossCycleProcess:BlueprintNode
|
||||
{
|
||||
|
||||
public CrossCycleProcess(string name, Action operation)
|
||||
:base()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
/**/
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// file="PipeLine.cs"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the “Software”), to deal in the Software without restriction,
|
||||
// including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
// and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and
|
||||
// this permission notice shall be included in all copies
|
||||
// or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED “AS IS”,
|
||||
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH
|
||||
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Security.Authentication.ExtendedProtection;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Logic
|
||||
{
|
||||
|
||||
public class BlueprintPipeline
|
||||
{
|
||||
|
||||
private BlueprintNode after;
|
||||
private BlueprintNode former;
|
||||
private BlueprintPipelineArgs args;
|
||||
private SemaphoreSlim formerCompleteSemaphore;
|
||||
|
||||
public BlueprintPipeline(BlueprintNode former, BlueprintNode after)
|
||||
{
|
||||
this.former = former;
|
||||
this.after = after;
|
||||
formerCompleteSemaphore = new SemaphoreSlim(initialCount: 0, maxCount: 1);
|
||||
}
|
||||
|
||||
public BlueprintNode After => after;
|
||||
|
||||
public BlueprintNode Former => former;
|
||||
|
||||
public int Level => former.Level;
|
||||
|
||||
public BlueprintPipelineArgs Args
|
||||
{
|
||||
get => args;
|
||||
set => args = value;
|
||||
}
|
||||
|
||||
public void CompleteAndSkip()
|
||||
{
|
||||
//Console.WriteLine($"Complete\t{former.ContextName}\t->\t{after.ContextName}");
|
||||
formerCompleteSemaphore.Release();
|
||||
}
|
||||
|
||||
public void CompleteAndWriteData(BlueprintPipelineArgs args)
|
||||
{
|
||||
this.args = args;
|
||||
//Console.WriteLine($"Complete\t{former.ContextName}\t->\t{after.ContextName}");
|
||||
formerCompleteSemaphore.Release();
|
||||
}
|
||||
|
||||
public static void SkipAll(Dictionary<string, BlueprintPipeline> ports)
|
||||
{
|
||||
if (ports == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(ports));
|
||||
}
|
||||
foreach (var item in ports)
|
||||
{
|
||||
if (item.Value!=null)
|
||||
{
|
||||
item.Value.CompleteAndSkip();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void WaitAll(Dictionary<string, BlueprintPipeline> ports)
|
||||
{
|
||||
if (ports == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(ports));
|
||||
}
|
||||
foreach (var item in ports)
|
||||
{
|
||||
if (item.Value!=null)
|
||||
{
|
||||
item.Value.WaitForStart();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void WaitForStart()
|
||||
{
|
||||
//Console.WriteLine($"Waiting \t{former.ContextName}\t->\t{after.ContextName}");
|
||||
formerCompleteSemaphore.Wait();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public abstract class BlueprintPipelineArgs
|
||||
{
|
||||
public object? Value { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// file="RouteSwitch.cs"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the “Software”), to deal in the Software without restriction,
|
||||
// including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
// and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and
|
||||
// this permission notice shall be included in all copies
|
||||
// or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED “AS IS”,
|
||||
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH
|
||||
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection.Emit;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp.Logic
|
||||
{
|
||||
public class RouteSwitch : BlueprintNode
|
||||
{
|
||||
private protected Dictionary<string, (BlueprintPipeline pipeline, Func<bool> openingRule)> outputDictionary;
|
||||
|
||||
public RouteSwitch(string name, string inputPorts) :
|
||||
base(name, [inputPorts], [string.Empty])
|
||||
{
|
||||
outputDictionary = new Dictionary<string, (BlueprintPipeline pipeline, Func<bool> openingRule)>();
|
||||
}
|
||||
|
||||
public new int Level
|
||||
{
|
||||
get
|
||||
{
|
||||
if (levelAvailable)
|
||||
{
|
||||
return _level;
|
||||
}
|
||||
_level = 0;
|
||||
KeyValuePair<string, BlueprintPipeline> item = _inputInterfaces.First();
|
||||
if (item.Value == null)
|
||||
{
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
if (_level <= item.Value.Level)
|
||||
{
|
||||
_level = item.Value.Level;
|
||||
}
|
||||
return _level;
|
||||
}
|
||||
}
|
||||
|
||||
public new void AddBefore(
|
||||
BlueprintNode after,
|
||||
string thisOutputPortName,
|
||||
string afterInputPortName)
|
||||
{
|
||||
this.AppendNode(after, afterInputPortName, () => true);
|
||||
}
|
||||
|
||||
public void AppendNode(BlueprintNode node, string portName, Func<bool> openningRule)
|
||||
{
|
||||
if (node == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(node));
|
||||
}
|
||||
string indexName = $"{node.Name}.{portName}";
|
||||
BlueprintPipeline p = new BlueprintPipeline(this, node);
|
||||
node.InputInterfaces[portName] = p;
|
||||
outputDictionary.Add(indexName, (p, openningRule));
|
||||
}
|
||||
|
||||
public new void Run()
|
||||
{
|
||||
try
|
||||
{
|
||||
BlueprintPipeline input = InputInterfaces.First().Value;
|
||||
input.WaitForStart();
|
||||
foreach (KeyValuePair<string, (BlueprintPipeline pipeline, Func<bool> openingRule)> item in outputDictionary)
|
||||
{
|
||||
if (item.Value.openingRule())
|
||||
{
|
||||
item.Value.pipeline.CompleteAndWriteData(input.Args);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public sealed class RouteHub : BlueprintNode
|
||||
{
|
||||
|
||||
private Dictionary<string, (BlueprintPipeline pipeline, Func<bool> controllingFunc)> inputDictionary;
|
||||
|
||||
public RouteHub(string name, string[] inputPorts, string[] outputPorts) : base(name, inputPorts, outputPorts)
|
||||
{
|
||||
inputDictionary = new Dictionary<string, (BlueprintPipeline pipeline, Func<bool> controllingFunc)>();
|
||||
}
|
||||
|
||||
|
||||
public new int Level
|
||||
{
|
||||
get
|
||||
{
|
||||
if (levelAvailable)
|
||||
{
|
||||
return _level;
|
||||
}
|
||||
_level = 0;
|
||||
foreach (KeyValuePair<string, BlueprintPipeline> item in _inputInterfaces)
|
||||
{
|
||||
if (item.Value == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (_level <= item.Value.Level)
|
||||
{
|
||||
_level = item.Value.Level;
|
||||
}
|
||||
}
|
||||
return _level;
|
||||
}
|
||||
}
|
||||
|
||||
public void PrependNode(BlueprintNode node, string portName, Func<bool> usingRule)
|
||||
{
|
||||
if (node == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(node));
|
||||
}
|
||||
string indexName = $"{node.Name}.{portName}";
|
||||
BlueprintPipeline p = new BlueprintPipeline(this, node);
|
||||
node.InputInterfaces[portName] = p;
|
||||
inputDictionary.Add(indexName, (p, usingRule));
|
||||
}
|
||||
|
||||
public new void Run()
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (KeyValuePair<string, (BlueprintPipeline pipeline, Func<bool> controllingFunc)> item in inputDictionary)
|
||||
{
|
||||
item.Value.pipeline.WaitForStart();
|
||||
if (item.Value.controllingFunc())
|
||||
{
|
||||
_outputInterfaces.First().Value.CompleteAndWriteData(item.Value.pipeline.Args);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
using StgSharp.Logic;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Logic
|
||||
{
|
||||
internal sealed class BeginningNode : BlueprintNode
|
||||
{
|
||||
public BeginningNode() : base(name: "BeginningNode", null,
|
||||
true, ["default"], ["default"]
|
||||
)
|
||||
{ }
|
||||
|
||||
internal (string name, BlueprintPipelineArgs arg)[] InputData
|
||||
{
|
||||
set
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
foreach (var parameter in value)
|
||||
{
|
||||
OutputInterfaces[parameter.name].Args = parameter.arg;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public new void SetCertainOutputPort(string name, BlueprintPipeline pipeline)
|
||||
{
|
||||
if (_outputInterfaces.ContainsKey(name))
|
||||
{
|
||||
_outputInterfaces[name] = pipeline;
|
||||
}
|
||||
_outputInterfaces.Add(name, pipeline);
|
||||
}
|
||||
|
||||
public new void Run()
|
||||
{
|
||||
BlueprintPipeline.SkipAll(_outputInterfaces);
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class EndingNode : BlueprintNode
|
||||
{
|
||||
public EndingNode() : base(name: "EndingNode",
|
||||
operation: (
|
||||
in Dictionary<string, BlueprintPipeline> input,
|
||||
in Dictionary<string, BlueprintPipeline> output
|
||||
) =>
|
||||
{
|
||||
},
|
||||
true, ["default"], ["default"]
|
||||
)
|
||||
{ }
|
||||
|
||||
internal (string name, BlueprintPipelineArgs arg)[] OutputData
|
||||
{
|
||||
get
|
||||
{
|
||||
return InputInterfaces.Select(
|
||||
key => (key.Key, key.Value.Args)).
|
||||
ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
public new void SetCertainInputPort(string name, BlueprintPipeline pipeline)
|
||||
{
|
||||
if (_inputInterfaces.ContainsKey(name))
|
||||
{
|
||||
_inputInterfaces[name] = pipeline;
|
||||
}
|
||||
_inputInterfaces.Add(name, pipeline);
|
||||
}
|
||||
|
||||
public new void Run()
|
||||
{
|
||||
BlueprintPipeline.WaitAll(_inputInterfaces);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user