forked from DynamoDS/Dynamo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomNodeDefinition.cs
More file actions
423 lines (361 loc) · 15.3 KB
/
Copy pathCustomNodeDefinition.cs
File metadata and controls
423 lines (361 loc) · 15.3 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Dynamo.Engine;
using Dynamo.Engine.CodeGeneration;
using Dynamo.Graph.Nodes;
using Dynamo.Graph.Nodes.CustomNodes;
using Dynamo.Graph.Workspaces;
using Dynamo.Library;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ProtoCore;
using ProtoCore.AST.AssociativeAST;
namespace Dynamo
{
/// <summary>
/// Compiler definition of a Custom Node.
/// </summary>
public class CustomNodeDefinition : IFunctionDescriptor
{
/// <summary>
/// This function creates CustomNodeDefinition.
/// </summary>
/// <param name="functionId">Custom node unique ID</param>
/// <param name="displayName">Custom node name</param>
/// <param name="nodeModels">Nodes inside custom node</param>
public CustomNodeDefinition(
Guid functionId,
string displayName="",
IEnumerable<NodeModel> nodeModels=null)
{
if (functionId == Guid.Empty)
throw new ArgumentException(@"FunctionId invalid.", "functionId");
nodeModels = nodeModels ?? new List<NodeModel>();
#region Find outputs
// Find output elements for the node
var outputs = nodeModels.OfType<Output>().ToList();
var topMost = new List<Tuple<int, NodeModel>>();
returns = new List<Tuple<string, string>>();
// if we found output nodes, add select their inputs
// these will serve as the function output
if (outputs.Any())
{
topMost.AddRange(
outputs.Where(x => x.InPorts[0].IsConnected).Select(x => Tuple.Create(0, x as NodeModel)));
returns = outputs.Select(x => x.Return).ToList();
}
else
{
// if there are no explicitly defined output nodes
// get the top most nodes and set THEM as the output
IEnumerable<NodeModel> topMostNodes = nodeModels.Where(node => node.IsTopMostNode);
var rtnPorts =
//Grab multiple returns from each node
topMostNodes.SelectMany(
topNode =>
//If the node is a recursive instance...
topNode is Function && (topNode as Function).Definition.FunctionId == functionId
// infinity output
? new[] {new {portIndex = 0, node = topNode, name = "∞"}}
// otherwise, grab all ports with connected outputs and package necessary info
: topNode.OutPorts
.Select(
(port, i) =>
new {portIndex = i, node = topNode, name = port.Name})
.Where(x => !topNode.OutPorts[x.portIndex].IsConnected));
foreach (var rtnAndIndex in rtnPorts.Select((rtn, i) => new {rtn, idx = i}))
{
topMost.Add(Tuple.Create(rtnAndIndex.rtn.portIndex, rtnAndIndex.rtn.node));
var outName = rtnAndIndex.rtn.name ?? rtnAndIndex.idx.ToString();
returns.Add(new Tuple<string, string>(outName, string.Empty));
}
}
var nameDict = new Dictionary<string, int>();
foreach (var name in returns.Select(p => p.Item1))
{
if (nameDict.ContainsKey(name))
nameDict[name]++;
else
nameDict[name] = 0;
}
nameDict = nameDict.Where(x => x.Value != 0).ToDictionary(x => x.Key, x => x.Value);
returns.Reverse();
var returnKeys = new List<string>();
for (int i = 0; i < returns.Count(); ++i)
{
var info = returns[i];
int amt;
var name = info.Item1;
if (nameDict.TryGetValue(name, out amt))
{
nameDict[name] = amt - 1;
var newName = string.IsNullOrEmpty(name) ? amt + ">" : name + amt;
returnKeys.Add(newName);
returns[i] = new Tuple<string, string>(newName, info.Item2);
}
else
returnKeys.Add(name);
}
returnKeys.Reverse();
returns.Reverse();
#endregion
#region Find inputs
//Find function entry point, and then compile
var inputNodes = nodeModels.OfType<Symbol>().ToList();
var parameters = inputNodes.Select(x => new TypedParameter(
x.GetAstIdentifierForOutputIndex(0).Value,
x.Parameter.Type,
x.Parameter.DefaultValue,
null,
x.Parameter.Summary,
x.Parameter.NameIsValid));
var displayParameters = inputNodes.Select(x => x.Parameter.Name);
#endregion
FunctionBody = nodeModels.Where(node => !(node is Symbol));
DisplayName = displayName;
FunctionId = functionId;
Parameters = parameters;
ReturnKeys = returnKeys;
DisplayParameters = displayParameters;
OutputNodes = topMost.Select(x => x.Item2.GetAstIdentifierForOutputIndex(x.Item1));
DirectDependencies = nodeModels
.OfType<Function>()
.Select(node => node.Definition)
.Where(def => def.FunctionId != functionId)
.Distinct();
ReturnType = ProtoCore.TypeSystem.BuildPrimitiveTypeObject(PrimitiveType.Var);
}
internal static CustomNodeDefinition MakeProxy(Guid functionId, string displayName)
{
var def = new CustomNodeDefinition(functionId, displayName);
def.IsProxy = true;
return def;
}
/// <summary>
/// Is this CustomNodeDefinition properly loaded?
/// </summary>
public bool IsProxy { get; private set; }
/// <summary>
/// Indicates whether any of this definition's input parameters are invalid.
/// An input is invalid when its input expression fails to parse. For example,
/// this would happen if the input name contained spaces or illegal characters.
/// </summary>
public bool ContainsInvalidInput
{
get { return Parameters.Any(p => !p.NameIsValid); }
}
/// <summary>
/// Function name.
/// </summary>
public string FunctionName
{
get { return AstBuilder.StringConstants.FunctionPrefix +
FunctionId.ToString().Replace("-", string.Empty); }
}
/// <summary>
/// Function unique ID.
/// </summary>
public Guid FunctionId { get; private set; }
/// <summary>
/// User-friendly parameters
/// </summary>
public IEnumerable<string> DisplayParameters { get; private set; }
/// <summary>
/// Function parameters.
/// </summary>
public IEnumerable<TypedParameter> Parameters { get; private set; }
/// <summary>
/// If the function returns a dictionary, this specifies all keys in
/// that dictionary.
/// </summary>
public IEnumerable<string> ReturnKeys { get; private set; }
/// <summary>
/// NodeModels making up the body of the custom node.
/// </summary>
public IEnumerable<NodeModel> FunctionBody { get; private set; }
/// <summary>
/// Identifiers associated with the outputs of the custom node.
/// </summary>
public IEnumerable<AssociativeNode> OutputNodes { get; private set; }
/// <summary>
/// User friendly name on UI.
/// </summary>
public string DisplayName { get; private set; }
/// <summary>
/// Return type.
/// </summary>
public ProtoCore.Type ReturnType { get; private set; }
private List<Tuple<string, string>> returns;
/// <summary>
/// The collection of output name and its description.
/// </summary>
public IEnumerable<Tuple<string, string>> Returns
{
get
{
return returns;
}
}
#region Dependencies
/// <summary>
/// Returns all custom node definitions.
/// </summary>
public IEnumerable<CustomNodeDefinition> Dependencies
{
get { return FindAllDependencies(new HashSet<CustomNodeDefinition>()); }
}
/// <summary>
/// Returns custom node definitions for direct dependencies.
/// </summary>
public IEnumerable<CustomNodeDefinition> DirectDependencies { get; private set; }
private IEnumerable<CustomNodeDefinition> FindAllDependencies(ISet<CustomNodeDefinition> dependencySet)
{
var query = DirectDependencies.Where(def => !dependencySet.Contains(def));
foreach (var definition in query)
{
yield return definition;
dependencySet.Add(definition);
foreach (var def in definition.FindAllDependencies(dependencySet))
yield return def;
}
}
#endregion
#region IFunctionDescriptor Members
/// <summary>
/// Name to create custom node
/// </summary>
public string MangledName
{
get { return FunctionId.ToString(); }
}
#endregion
}
/// <summary>
/// Basic information about a custom node.
/// </summary>
public class CustomNodeInfo
{
/// <summary>
/// This function creates CustomNodeInfo.
/// </summary>
/// <param name="functionId">Custom node unique ID</param>
/// <param name="name">Custom node name</param>
/// <param name="category">Custom node category</param>
/// <param name="description">Custom node description</param>
/// <param name="path">Path to custom node</param>
/// <param name="isVisibleInDynamoLibrary">Bool value controls the visibility in library search</param>
public CustomNodeInfo(Guid functionId, string name, string category, string description, string path, bool isVisibleInDynamoLibrary = true)
{
if (functionId == Guid.Empty)
throw new ArgumentException(@"FunctionId invalid.", "functionId");
FunctionId = functionId;
Name = name;
Description = description;
Path = path;
IsVisibleInDynamoLibrary = isVisibleInDynamoLibrary;
Category = category;
if (String.IsNullOrWhiteSpace(Category))
Category = Dynamo.Properties.Resources.DefaultCustomNodeCategory;
}
[JsonConstructor] public CustomNodeInfo() { }
/// <summary>
/// Returns custom node unique ID
/// </summary>
public Guid FunctionId { get; set; }
/// <summary>
/// Returns custom node name
/// </summary>
public string Name { get; set; }
/// <summary>
/// Returns custom node category
/// </summary>
public string Category { get; set; } = string.Empty;
/// <summary>
/// Returns custom node description
/// </summary>
public string Description { get; set; }
/// <summary>
/// Returns path to custom node
/// </summary>
public string Path { get; set; }
/// <summary>
/// Indicates if custom node is part of the package.
/// If true, then custom node is part of package manager.
/// </summary>
public bool IsPackageMember { get; set; }
/// <summary>
/// Indicates if custom node is part of the library search.
/// If true, then custom node is part of library search.
/// </summary>
public bool IsVisibleInDynamoLibrary { get; set; }
/// <summary>
/// Only valid if IsPackageMember is true.
/// Can be used to identify which package
/// requested this CustomNode to load.
/// </summary>
public PackageInfo PackageInfo { get; internal set; }
private static readonly string[] topLevelJsonKeys = { "Uuid", "Category", "Description", "Name" };
private static readonly Dictionary<string, object> propertyLookup = new Dictionary<string, object>();
private static object isVisibleInDynamoLibraryProp = null;
private static DefaultJsonNameTable propertyTable = null;
internal static bool GetFromJsonDocument(string path, out CustomNodeInfo info, out Exception ex)
{
if (propertyTable == null)
{
propertyTable = new DefaultJsonNameTable();
foreach (var pn in topLevelJsonKeys)
{
propertyLookup[pn] = propertyTable.Add(pn);
}
isVisibleInDynamoLibraryProp = propertyTable.Add(nameof(IsVisibleInDynamoLibrary));
}
try
{
var data = new JObject();
// JsonTextRead will automatically dispose of the stream reader
using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read))
using (var jr = new JsonTextReader(new StreamReader(fs)) { PropertyNameTable = propertyTable })
{
while (jr.Read())
{
if (data.Count == topLevelJsonKeys.Length + 1)
{
break; // we have all of the
}
if (jr.TokenType == JsonToken.PropertyName)
{
if (jr.Depth == 1)
{
foreach (var prop in propertyLookup)
{
if (jr.Value == prop.Value)
{
data[prop.Key] = jr.ReadAsString() ?? "";
break;
}
}
}
else if (jr.Value == isVisibleInDynamoLibraryProp)
{
data[nameof(IsVisibleInDynamoLibrary)] = jr.ReadAsBoolean();
}
}
}
}
ex = null;
data["FunctionId"] = data.GetValue("Uuid");
data["Path"] = path;
info = data.ToObject<CustomNodeInfo>();
return true;
}
catch (Exception e)
{
ex = e;
info = null;
return false;
}
}
}
}