Skip to content

Commit 039fe09

Browse files
committed
Added self to executor and nodes
1 parent 6ce79bd commit 039fe09

28 files changed

Lines changed: 66 additions & 41 deletions

src/NodeDev.Core/Graph.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using NodeDev.Core.Nodes;
1+
using NodeDev.Core.Connections;
2+
using NodeDev.Core.Nodes;
23
using System.Collections.Concurrent;
34
using System.Reflection;
45
using System.Text.Json;
@@ -32,6 +33,24 @@ public async Task Invoke(Func<Task> action)
3233

3334
#endregion
3435

36+
#region Connect/Disconnect
37+
38+
public void Connect(Connection connection1, Connection connection2)
39+
{
40+
if(!connection1.Connections.Contains(connection2))
41+
connection1.Connections.Add(connection2);
42+
if(!connection2.Connections.Contains(connection1))
43+
connection2.Connections.Add(connection1);
44+
}
45+
46+
public void Disconnect(Connection connection1, Connection connection2)
47+
{
48+
connection1.Connections.Remove(connection2);
49+
connection2.Connections.Remove(connection1);
50+
}
51+
52+
#endregion
53+
3554
#region AddNode
3655

3756
public void AddNode(Node node)

src/NodeDev.Core/GraphExecutor.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,19 @@ private Node FindEntryNode()
2626
return Graph.Nodes.First(n => n.Value is EntryNode).Value;
2727
}
2828

29-
public void Execute(object?[] inputs, object?[] outputs)
29+
public void Execute(object? self, object?[] inputs, object?[] outputs)
3030
{
3131
var node = FindEntryNode();
3232

33-
var execConnection = node.Execute(null, inputs, inputs);
33+
var execConnection = node.Execute(self, null, inputs, inputs);
3434
if (execConnection == null)
3535
throw new Exception("Entry node should have an output connection");
36+
if (inputs.Length != node.Outputs.Count)
37+
throw new Exception("EntryNode doesn't have the same amount of inputs as the provided inputs array");
38+
39+
for (int i = 0; i < inputs.Length; ++i)
40+
Connections[node.Outputs[i]] = inputs[i];
41+
3642

3743
var stack = new Stack<Connection>(); // stack of input connections on nodes that can alter execution path
3844

@@ -55,15 +61,15 @@ public void Execute(object?[] inputs, object?[] outputs)
5561
for(int i = 0; i < outputs.Length; i++)
5662
{
5763
var output = nodeToExecute.Inputs[i];
58-
outputs[i] = CrawlBackInputs(output);
64+
outputs[i] = CrawlBackInputs(self, output);
5965
}
6066
return;
6167
}
6268

63-
var nodeInputs = GetNodeInputs(nodeToExecute);
69+
var nodeInputs = GetNodeInputs(self, nodeToExecute);
6470
var nodeOutputs = new object?[nodeToExecute.Outputs.Count];
6571

66-
execConnection = nodeToExecute.Execute(connectionToExecute, nodeInputs, nodeOutputs);
72+
execConnection = nodeToExecute.Execute(self, connectionToExecute, nodeInputs, nodeOutputs);
6773
for(int i = 0; i < nodeOutputs.Length; i++)
6874
{
6975
var output = nodeToExecute.Outputs[i];
@@ -75,21 +81,21 @@ public void Execute(object?[] inputs, object?[] outputs)
7581
}
7682
}
7783

78-
private object?[] GetNodeInputs(Node node)
84+
private object?[] GetNodeInputs(object? self, Node node)
7985
{
8086
var inputs = new object?[node.Inputs.Count];
8187

8288
for (int i = 0; i < node.Inputs.Count; i++)
8389
{
8490
var input = node.Inputs[i];
8591

86-
inputs[i] = CrawlBackInputs(input);
92+
inputs[i] = CrawlBackInputs(self, input);
8793
}
8894

8995
return inputs;
9096
}
9197

92-
private object? CrawlBackInputs(Connection inputConnection)
98+
private object? CrawlBackInputs(object? self, Connection inputConnection)
9399
{
94100
var other = inputConnection.Connections.FirstOrDefault();
95101
if (other == null)
@@ -101,11 +107,11 @@ public void Execute(object?[] inputs, object?[] outputs)
101107

102108
// if this is not a flow node, we are allowed to execute the node on demande to calculate the outputs
103109
// this will also automatically crawl back the inputs of the node we are executing
104-
var inputs = GetNodeInputs(other.Parent);
110+
var inputs = GetNodeInputs(self, other.Parent);
105111
var outputs = new object?[other.Parent.Outputs.Count];
106112

107113
object? myOutput = null;
108-
other.Parent.Execute(null, inputs, outputs);
114+
other.Parent.Execute(self, null, inputs, outputs);
109115
for (int i = 0; i < outputs.Length; i++)
110116
{
111117
var output = other.Parent.Outputs[i];

src/NodeDev.Core/Nodes/Debug/WriteLine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public WriteLine(Graph graph, string? id = null) : base(graph, id)
1616
Inputs.Add(new("Line", this, TypeFactory.CreateUndefinedGenericType("T")));
1717
}
1818

19-
protected override void ExecuteInternal(object?[] inputs, object?[] outputs)
19+
protected override void ExecuteInternal(object? self, object?[] inputs, object?[] outputs)
2020
{
2121
Console.WriteLine(inputs[1]);
2222
}

src/NodeDev.Core/Nodes/Flow/Branch.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public Branch(Graph graph, string? id = null) : base(graph, id)
2626
Outputs.Add(new("IfFalse", this, TypeFactory.ExecType));
2727
}
2828

29-
public override Connection? Execute(Connection? connectionBeingExecuted, object?[] inputs, object?[] nodeOutputs)
29+
public override Connection? Execute(object? self, Connection? connectionBeingExecuted, object?[] inputs, object?[] nodeOutputs)
3030
{
3131
if (inputs[1] is bool b && b == true)
3232
return Outputs[0];

src/NodeDev.Core/Nodes/Flow/EntryNode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public EntryNode(Graph graph, string? id = null) : base(graph, id)
2323

2424
public override bool IsFlowNode => true;
2525

26-
public override Connection? Execute(Connection? inputExec, object?[] inputs, object?[] outputs)
26+
public override Connection? Execute(object? self, Connection? inputExec, object?[] inputs, object?[] outputs)
2727
{
2828
return Outputs[0];
2929
}

src/NodeDev.Core/Nodes/Flow/ReturnNode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public ReturnNode(Graph graph, string? id = null) : base(graph, id)
2323

2424
public override bool IsFlowNode => throw new NotImplementedException();
2525

26-
public override Connection? Execute(Connection? execInput, object?[] inputs, object?[] outputs)
26+
public override Connection? Execute(object? self, Connection? execInput, object?[] inputs, object?[] outputs)
2727
{
2828
return null;
2929
}

src/NodeDev.Core/Nodes/GetPropertyOrField.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ internal void SetMemberTarget(MemberInfo memberInfo)
8686
}
8787

8888

89-
protected override void ExecuteInternal(object?[] inputs, object?[] outputs)
89+
protected override void ExecuteInternal(object? self, object?[] inputs, object?[] outputs)
9090
{
9191
if (TargetMember == null)
9292
throw new Exception("Target method is not set");

src/NodeDev.Core/Nodes/Math/Add.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public Add(Graph graph, string? id = null) : base(graph, id)
1717
Name = "Add";
1818
}
1919

20-
protected override void ExecuteInternal(object?[] inputs, object?[] outputs)
20+
protected override void ExecuteInternal(object? self, object?[] inputs, object?[] outputs)
2121
{
2222
dynamic? a = inputs[0];
2323
dynamic? b = inputs[1];

src/NodeDev.Core/Nodes/Math/And.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public And(Graph graph, string? id = null) : base(graph, id)
1919
Outputs.Add(new("c", this, TypeFactory.Get(typeof(bool))));
2020
}
2121

22-
protected override void ExecuteInternal(object?[] inputs, object?[] outputs)
22+
protected override void ExecuteInternal(object? self, object?[] inputs, object?[] outputs)
2323
{
2424
if (inputs[0] == null || inputs[1] == null)
2525
{

src/NodeDev.Core/Nodes/Math/BiggerThan.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public BiggerThan(Graph graph, string? id = null) : base(graph, id)
2121
Outputs.Add(new("c", this, TypeFactory.Get<bool>()));
2222
}
2323

24-
protected override void ExecuteInternal(object?[] inputs, object?[] outputs)
24+
protected override void ExecuteInternal(object? self, object?[] inputs, object?[] outputs)
2525
{
2626
dynamic? a = inputs[0];
2727
dynamic? b = inputs[1];

0 commit comments

Comments
 (0)