-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExecutionEngine.cs
165 lines (141 loc) · 5.74 KB
/
ExecutionEngine.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System;
using System.Collections.Generic;
using System.Text;
using MetaphysicsIndustries.Collections;
using MetaphysicsIndustries.Epiphany;
namespace MetaphysicsIndustries.Amethyst
{
public class ExecutionEngine : IExecutionEngine
{
public void Execute(AmethystElement[] elements, ValueCache valueCache)
{
AmethystElement[] order = GetExecutionOrder(elements, valueCache);
StringBuilder sb = new StringBuilder();
foreach (AmethystElement elem in order)
{
sb.AppendLine(elem.Text);
try
{
ExecuteElement(elem, valueCache);
}
catch (Exception ex)
{
valueCache.Remove(elem);
throw new ApplicationException(ex.Message, ex);
}
}
}
private AmethystElement[] GetExecutionOrder(AmethystElement[] elements, ValueCache valueCache)
{
Set<AmethystElement> elems = new Set<AmethystElement>();
List<AmethystElement> order = new List<AmethystElement>();
//Dictionary<AmethystElement, int> dependencyCounts = new Dictionary<AmethystElement, int>();
Set<AmethystElement> freeElements = new Set<AmethystElement>();
foreach (AmethystElement aelem in elements)
{
Set<OutputTerminal> terminals = new Set<OutputTerminal>();
foreach (Terminal terminal in aelem.Terminals)
{
if (terminal is OutputTerminal)
{
terminals.Add((OutputTerminal)terminal);
}
}
if (terminals.Count > 0)
{
foreach (OutputTerminal terminal in terminals)
{
if (!valueCache.ContainsKey(terminal))
{
elems.Add(aelem);
break;
}
}
}
else
{
elems.Add(aelem);
}
}
//dependencyCounts.Clear();
//foreach (AmethystElement elem in elements)
//{
// dependencyCounts[elem] = 0;
//}
while (elems.Count > 0)
{
freeElements.Clear();
freeElements.AddRange(elems);
foreach (AmethystElement elem in elems)
{
foreach (Terminal terminal in elem.Terminals)
{
if (terminal is InputTerminal)
{
InputTerminal terminal2 = (InputTerminal)terminal;
if (terminal2.Path != null && terminal2.FromTerminal != null)
{
if (elems.Contains(terminal2.FromTerminal.ParentAmethystElement))
{
//dependency
freeElements.Remove(elem);
break;
}
}
else
{
//unconnected input
throw new Exception("Unconnected Input: " + elem.Text + "." + terminal2.DisplayText);
}
}
}
}
if (freeElements.Count <= 0)
{
//circular dependency
StringBuilder sb2 = new StringBuilder();
foreach (AmethystElement elem in elems)
{
sb2.AppendLine(elem.Text);
}
throw new Exception("Circular dependency: \r\n" + sb2.ToString());
}
order.AddRange(freeElements);
elems.RemoveRange(freeElements);
}
return order.ToArray();
}
private void ExecuteElement(AmethystElement elem, ValueCache valueCache)
{
Dictionary<InputConnectionBase, object> inputs = new Dictionary<InputConnectionBase, object>();
Dictionary<OutputConnectionBase, object> outputs = new Dictionary<OutputConnectionBase, object>();
foreach (Terminal terminal in elem.Terminals)
{
if (terminal is InputTerminal)
{
InputTerminal terminal2 = (InputTerminal)terminal;
inputs[terminal2.Connection] = valueCache[terminal2.FromTerminal];
}
else if (terminal is OutputTerminal)
{
OutputTerminal terminal2 = (OutputTerminal)terminal;
outputs[terminal2.Connection] = null;
}
}
elem.Execute(inputs, outputs);
foreach (OutputConnectionBase con in outputs.Keys)
{
valueCache[(OutputTerminal)elem.TerminalsByConnection[con]] = outputs[con];
}
OnElementExecuted(elem);
}
public event EventHandler<AmethystElementEventArgs> ElementExecuted;
protected void OnElementExecuted(AmethystElement element)
{
if (ElementExecuted != null)
{
ElementExecuted(this, new AmethystElementEventArgs(element));
}
}
}
}