forked from DynamoDS/Dynamo
/
Function.cs
737 lines (618 loc) · 26 KB
/
Function.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
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
#region
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using Dynamo.Models;
using Dynamo.Utilities;
using ProtoCore.AST.AssociativeAST;
#endregion
namespace Dynamo.Nodes
{
/// <summary>
/// DesignScript Custom Node instance.
/// </summary>
[NodeName("Custom Node")]
[NodeDescription("Instance of a Custom Node")]
[IsInteractive(false)]
[NodeSearchable(false)]
[IsMetaNode]
public partial class Function : NodeModel
{
protected internal Function(WorkspaceModel ws, CustomNodeDefinition def) : this(ws)
{
Definition = def;
ResyncWithDefinition();
ArgumentLacing = LacingStrategy.Disabled;
}
public Function(WorkspaceModel ws)
: base(ws)
{}
public new string Name
{
get { return Definition.WorkspaceModel.Name; }
set
{
Definition.WorkspaceModel.Name = value;
RaisePropertyChanged("Name");
}
}
public override string Description
{
get { return Definition == null ? string.Empty : Definition.WorkspaceModel.Description; }
set
{
Definition.WorkspaceModel.Description = value;
RaisePropertyChanged("Description");
}
}
[Obsolete("Use Definition.FunctionId.ToString()")]
public string Symbol
{
get { return Definition.FunctionId.ToString(); }
}
public new string Category
{
get
{
return Workspace.DynamoModel.CustomNodeManager.NodeInfos.ContainsKey(Definition.FunctionId)
? Workspace.DynamoModel.CustomNodeManager.NodeInfos[Definition.FunctionId].Category
: "Custom Nodes";
}
}
/// <summary>
/// </summary>
public CustomNodeDefinition Definition { get; set; }
/// <summary>
/// Updates this Custom Node's data to match its Definition.
/// </summary>
public void ResyncWithDefinition()
{
DisableReporting();
if (Definition.Parameters != null)
{
InPortData.Clear();
foreach (string arg in Definition.Parameters)
InPortData.Add(new PortData(arg, "parameter"));
}
OutPortData.Clear();
if (Definition.ReturnKeys != null && Definition.ReturnKeys.Any())
{
foreach (string key in Definition.ReturnKeys)
OutPortData.Add(new PortData(key, "return value"));
}
else
OutPortData.Add(new PortData("", "return value"));
RegisterAllPorts();
NickName = Definition.DisplayName;
EnableReporting();
}
/// <summary>
/// Return if the custom node instance is in sync with its definition.
/// It may be out of sync if .dyf file is opened and updated and then
/// .dyn file is opened.
/// </summary>
/// <returns></returns>
public bool IsInSyncWithDefinition()
{
if (Definition.Parameters != null)
{
if (Definition.Parameters.Count() != InPortData.Count() ||
!Definition.Parameters.SequenceEqual(InPortData.Select(p => p.NickName)))
return false;
}
if (Definition.ReturnKeys != null)
{
if (Definition.ReturnKeys.Count() != OutPortData.Count() ||
!Definition.ReturnKeys.SequenceEqual(OutPortData.Select(p => p.NickName)))
return false;
}
return true;
}
//protected override InputNode Compile(IEnumerable<string> portNames)
//{
// return SaveResult ? base.Compile(portNames) : new FunctionNode(Symbol, portNames);
//}
/// <summary>
/// Sets the inputs of this function.
/// </summary>
/// <param name="inputs"></param>
[Obsolete]
public void SetInputs(IEnumerable<string> inputs)
{
int i = 0;
foreach (string input in inputs)
{
if (InPortData.Count > i)
InPortData[i].NickName = input;
else
InPortData.Add(new PortData(input, "Input #" + (i + 1)));
i++;
}
if (i < InPortData.Count)
{
//for (var k = i; k < InPortData.Count; k++)
// InPorts[k].KillAllConnectors();
//MVVM: confirm that extension methods on observable collection do what we expect
InPortData.RemoveRange(i, InPortData.Count - i);
}
}
[Obsolete]
public void SetOutputs(IEnumerable<string> outputs)
{
int i = 0;
foreach (string output in outputs)
{
if (OutPortData.Count > i)
OutPortData[i].NickName = output;
else
OutPortData.Add(new PortData(output, "Output #" + (i + 1)));
i++;
}
if (i < OutPortData.Count)
{
//for (var k = i; k < OutPortData.Count; k++)
// OutPorts[k].KillAllConnectors();
OutPortData.RemoveRange(i, OutPortData.Count - i);
}
}
protected override void SaveNode(XmlDocument xmlDoc, XmlElement nodeElement, SaveContext context)
{
//Debug.WriteLine(pd.Object.GetType().ToString());
XmlElement outEl = xmlDoc.CreateElement("ID");
outEl.SetAttribute("value", Definition.FunctionId.ToString());
nodeElement.AppendChild(outEl);
outEl = xmlDoc.CreateElement("Name");
outEl.SetAttribute("value", NickName);
nodeElement.AppendChild(outEl);
outEl = xmlDoc.CreateElement("Description");
outEl.SetAttribute("value", Description);
nodeElement.AppendChild(outEl);
outEl = xmlDoc.CreateElement("Inputs");
foreach (string input in InPortData.Select(x => x.NickName))
{
XmlElement inputEl = xmlDoc.CreateElement("Input");
inputEl.SetAttribute("value", input);
outEl.AppendChild(inputEl);
}
nodeElement.AppendChild(outEl);
outEl = xmlDoc.CreateElement("Outputs");
foreach (string output in OutPortData.Select(x => x.NickName))
{
XmlElement outputEl = xmlDoc.CreateElement("Output");
outputEl.SetAttribute("value", output);
outEl.AppendChild(outputEl);
}
nodeElement.AppendChild(outEl);
}
protected override void LoadNode(XmlNode nodeElement)
{
List<XmlNode> childNodes = nodeElement.ChildNodes.Cast<XmlNode>().ToList();
XmlNode nameNode = childNodes.LastOrDefault(subNode => subNode.Name.Equals("Name"));
if (nameNode != null && nameNode.Attributes != null)
NickName = nameNode.Attributes[0].Value;
XmlNode idNode = childNodes.LastOrDefault(subNode => subNode.Name.Equals("ID"));
if (idNode != null && idNode.Attributes != null)
{
string id = idNode.Attributes[0].Value;
Guid funcId;
if (!Guid.TryParse(id, out funcId) && nodeElement.Attributes != null)
funcId = GuidUtility.Create(GuidUtility.UrlNamespace, nodeElement.Attributes["nickname"].Value);
if (!VerifyFuncId(ref funcId))
{
LoadProxyCustomNode(funcId);
}
Definition = Workspace.DynamoModel.CustomNodeManager.GetFunctionDefinition(funcId);
if (Definition.IsProxy)
{
Error("Cannot load custom node");
}
}
foreach (XmlNode subNode in childNodes)
{
if (subNode.Name.Equals("Outputs"))
{
var data =
subNode.ChildNodes.Cast<XmlNode>()
.Select(
(outputNode, i) =>
new
{
data = new PortData(outputNode.Attributes[0].Value, "Output #" + (i + 1)),
idx = i
});
foreach (var dataAndIdx in data)
{
if (OutPortData.Count > dataAndIdx.idx)
OutPortData[dataAndIdx.idx] = dataAndIdx.data;
else
OutPortData.Add(dataAndIdx.data);
}
}
else if (subNode.Name.Equals("Inputs"))
{
var data =
subNode.ChildNodes.Cast<XmlNode>()
.Select(
(inputNode, i) =>
new
{
data = new PortData(inputNode.Attributes[0].Value, "Input #" + (i + 1)),
idx = i
});
foreach (var dataAndIdx in data)
{
if (InPortData.Count > dataAndIdx.idx)
InPortData[dataAndIdx.idx] = dataAndIdx.data;
else
InPortData.Add(dataAndIdx.data);
}
}
#region Legacy output support
else if (subNode.Name.Equals("Output"))
{
var data = new PortData(subNode.Attributes[0].Value, "function output");
if (OutPortData.Any())
OutPortData[0] = data;
else
OutPortData.Add(data);
}
#endregion
}
if (!IsInSyncWithDefinition())
{
ResyncWithDefinition();
}
else
{
RegisterAllPorts();
}
//argument lacing on functions should be set to disabled
//by default in the constructor, but for any workflow saved
//before this was the case, we need to ensure it here.
ArgumentLacing = LacingStrategy.Disabled;
//if (Definition != null)
// ResyncWithDefinition();
}
private bool VerifyFuncId(ref Guid funcId)
{
if (funcId == null)
return false;
// if the dyf does not exist on the search path...
if (Workspace.DynamoModel.CustomNodeManager.Contains(funcId))
return true;
CustomNodeManager manager = Workspace.DynamoModel.CustomNodeManager;
// if there is a node with this name, use it instead
if (manager.Contains(NickName))
{
funcId = manager.GetGuidFromName(NickName);
return true;
}
return false;
}
private void LoadProxyCustomNode(Guid funcId)
{
var proxyDef = new CustomNodeDefinition(funcId)
{
WorkspaceModel = new CustomNodeWorkspaceModel(this.Workspace.DynamoModel, NickName, "Custom Nodes") { FileName = null }
};
proxyDef.IsProxy = true;
string userMsg = "Failed to load custom node: " + NickName + ". Replacing with proxy custom node.";
Workspace.DynamoModel.Logger.Log(userMsg);
// tell custom node loader, but don't provide path, forcing user to resave explicitly
Workspace.DynamoModel.CustomNodeManager.SetFunctionDefinition(funcId, proxyDef);
}
//public override void Evaluate(FSharpList<FScheme.Value> args, Dictionary<PortData, FScheme.Value> outPuts)
//{
// if (OutPortData.Count > 1)
// {
// var query = (Evaluate(args) as FScheme.Value.List).Item.Zip(
// OutPortData,
// (value, data) => new { value, data });
// foreach (var result in query)
// outPuts[result.data] = result.value;
// }
// else
// base.Evaluate(args, outPuts);
//}
//public override FScheme.Value Evaluate(FSharpList<FScheme.Value> args)
//{
// //return ((FScheme.Value.Function)Controller.FSchemeEnvironment.LookupSymbol(Symbol))
// // .Item.Invoke(args);
// throw new NotImplementedException("FSchemeEnvironment has been removed.");
//}
internal override IEnumerable<AssociativeNode> BuildAst(List<AssociativeNode> inputAstNodes)
{
var resultAst = new List<AssociativeNode>();
if (OutPortData.Count == 1)
{
if (IsPartiallyApplied)
{
var count = Definition.Parameters.Count();
AssociativeNode functionCall = AstFactory.BuildFunctionObject(
Definition.FunctionName,
count,
Enumerable.Range(0, count).Where(HasInput),
inputAstNodes);
resultAst.Add(AstFactory.BuildAssignment(AstIdentifierForPreview, functionCall));
resultAst.Add(
AstFactory.BuildAssignment(GetAstIdentifierForOutputIndex(0), AstIdentifierForPreview));
}
else
{
AssociativeNode functionCall = AstFactory.BuildFunctionCall(
Definition.FunctionName,
inputAstNodes);
resultAst.Add(AstFactory.BuildAssignment(AstIdentifierForPreview, functionCall));
// assign the entire result to the only output port
var outId = GetAstIdentifierForOutputIndex(0);
if (AstIdentifierForPreview.Value != outId.Value)
resultAst.Add(AstFactory.BuildAssignment(outId, AstIdentifierForPreview));
}
}
else
{
AssociativeNode functionCall = AstFactory.BuildFunctionCall(
Definition.FunctionName,
inputAstNodes);
resultAst.Add(AstFactory.BuildAssignment(AstIdentifierForPreview, functionCall));
/* previewId = customNodeFunc(arg0, arg1 ...);
* outId0 = previewId[key0];
* outId1 = previewId[key1];
* ...
*/
// indexers for each output
IEnumerable<AssociativeNode> indexers = Definition.ReturnKeys != null
? Definition.ReturnKeys.Select(AstFactory.BuildStringNode) as IEnumerable<AssociativeNode>
: Enumerable.Range(0, OutPortData.Count).Select(AstFactory.BuildIntNode);
// for each output, pull the output from the result
// based on the associated return key and assign to
// corresponding output identifier
resultAst.AddRange(
indexers.Select(
(rtnKey, index) =>
AstFactory.BuildAssignment(
GetAstIdentifierForOutputIndex(index),
AstFactory.BuildIdentifier(AstIdentifierForPreview.Name, rtnKey))));
}
return resultAst;
}
#region Serialization/Deserialization methods
protected override void SerializeCore(XmlElement element, SaveContext context)
{
base.SerializeCore(element, context); //Base implementation must be called
if (context == SaveContext.Undo)
{
var helper = new XmlElementHelper(element);
helper.SetAttribute("functionId", Definition.FunctionId.ToString());
helper.SetAttribute("functionName", NickName);
helper.SetAttribute("functionDesc", Description);
XmlDocument xmlDoc = element.OwnerDocument;
foreach (string input in InPortData.Select(x => x.NickName))
{
XmlElement inputEl = xmlDoc.CreateElement("functionInput");
inputEl.SetAttribute("inputValue", input);
element.AppendChild(inputEl);
}
foreach (string input in OutPortData.Select(x => x.NickName))
{
XmlElement outputEl = xmlDoc.CreateElement("functionOutput");
outputEl.SetAttribute("outputValue", input);
element.AppendChild(outputEl);
}
}
}
protected override void DeserializeCore(XmlElement element, SaveContext context)
{
base.DeserializeCore(element, context); //Base implementation must be called
if (context == SaveContext.Undo)
{
var helper = new XmlElementHelper(element);
NickName = helper.ReadString("functionName");
Guid funcId;
if (!Guid.TryParse(helper.ReadString("functionId"), out funcId))
funcId = GuidUtility.Create(GuidUtility.UrlNamespace, NickName);
if (!VerifyFuncId(ref funcId))
{
LoadProxyCustomNode(funcId);
return;
}
Definition = Workspace.DynamoModel.CustomNodeManager.GetFunctionDefinition(funcId);
XmlNodeList inNodes = element.SelectNodes("functionInput");
XmlNodeList outNodes = element.SelectNodes("functionOutput");
var inData =
inNodes.Cast<XmlNode>()
.Select(
(inputNode, i) =>
new
{
data = new PortData(inputNode.Attributes[0].Value, "Input #" + (i + 1)),
idx = i
});
foreach (var dataAndIdx in inData)
{
if (InPortData.Count > dataAndIdx.idx)
InPortData[dataAndIdx.idx] = dataAndIdx.data;
else
InPortData.Add(dataAndIdx.data);
}
var outData =
outNodes.Cast<XmlNode>()
.Select(
(outputNode, i) =>
new
{
data = new PortData(outputNode.Attributes[0].Value, "Output #" + (i + 1)),
idx = i
});
foreach (var dataAndIdx in outData)
{
if (OutPortData.Count > dataAndIdx.idx)
OutPortData[dataAndIdx.idx] = dataAndIdx.data;
else
OutPortData.Add(dataAndIdx.data);
}
//Added it the same way as LoadNode. But unsure of when 'Output' ChildNodes will
//be added to element. As of now I dont think it is added during serialize
#region Legacy output support
foreach (var portData in
from XmlNode subNode in element.ChildNodes
where subNode.Name.Equals("Output")
select new PortData(subNode.Attributes[0].Value, "function output"))
{
if (OutPortData.Any())
OutPortData[0] = portData;
else
OutPortData.Add(portData);
}
#endregion
RegisterAllPorts();
Description = helper.ReadString("functionDesc");
}
}
#endregion
}
[NodeName("Input")]
[NodeCategory(BuiltinNodeCategories.CORE_INPUT)]
[NodeDescription("A function parameter, use with custom nodes")]
[NodeSearchTags("variable", "argument", "parameter")]
[IsInteractive(false)]
[NotSearchableInHomeWorkspace]
[IsDesignScriptCompatible]
public partial class Symbol : NodeModel
{
private string inputSymbol = "";
public Symbol(WorkspaceModel ws)
: base(ws)
{
OutPortData.Add(new PortData("", "Symbol"));
RegisterAllPorts();
ArgumentLacing = LacingStrategy.Disabled;
}
public string InputSymbol
{
get { return inputSymbol; }
set
{
inputSymbol = value;
ReportModification();
RaisePropertyChanged("InputSymbol");
}
}
public override IdentifierNode GetAstIdentifierForOutputIndex(int outputIndex)
{
return string.IsNullOrEmpty(InputSymbol)
? AstIdentifierForPreview
: AstFactory.BuildIdentifier(InputSymbol);
}
//protected internal override INode Build(Dictionary<NodeModel, Dictionary<int, INode>> preBuilt, int outPort)
//{
// Dictionary<int, INode> result;
// if (!preBuilt.TryGetValue(this, out result))
// {
// result = new Dictionary<int, INode>();
// result[outPort] = new SymbolNode(GUID.ToString());
// preBuilt[this] = result;
// }
// return result[outPort];
//}
protected override void SaveNode(XmlDocument xmlDoc, XmlElement nodeElement, SaveContext context)
{
//Debug.WriteLine(pd.Object.GetType().ToString());
XmlElement outEl = xmlDoc.CreateElement("Symbol");
outEl.SetAttribute("value", InputSymbol);
nodeElement.AppendChild(outEl);
}
protected override void LoadNode(XmlNode nodeElement)
{
foreach (var subNode in
nodeElement.ChildNodes.Cast<XmlNode>()
.Where(subNode => subNode.Name == "Symbol"))
{
InputSymbol = subNode.Attributes[0].Value;
}
ArgumentLacing = LacingStrategy.Disabled;
}
/*
[NodeMigration(from: "0.6.3.0", to: "0.7.0.0")]
public static NodeMigrationData Migrate_0630_to_0700(NodeMigrationData data)
{
NodeMigrationData migrationData = new NodeMigrationData(data.Document);
XmlElement oldNode = data.MigratedNodes.ElementAt(0);
XmlElement dummyNode = MigrationManager.CreateDummyNode(oldNode, 0, 1);
migrationData.AppendNode(dummyNode);
return migrationData;
}
*/
}
[NodeName("Output")]
[NodeCategory(BuiltinNodeCategories.CORE_INPUT)]
[NodeDescription("A function output, use with custom nodes")]
[IsInteractive(false)]
[NotSearchableInHomeWorkspace]
[IsDesignScriptCompatible]
public partial class Output : NodeModel
{
private string symbol = "";
public Output(WorkspaceModel ws)
: base(ws)
{
InPortData.Add(new PortData("", ""));
RegisterAllPorts();
ArgumentLacing = LacingStrategy.Disabled;
}
public string Symbol
{
get { return symbol; }
set
{
symbol = value;
ReportModification();
RaisePropertyChanged("Symbol");
}
}
public override IdentifierNode GetAstIdentifierForOutputIndex(int outputIndex)
{
if (outputIndex < 0 || outputIndex > OutPortData.Count)
throw new ArgumentOutOfRangeException("outputIndex", @"Index must correspond to an OutPortData index.");
return AstIdentifierForPreview;
}
internal override IEnumerable<AssociativeNode> BuildAst(List<AssociativeNode> inputAstNodes)
{
AssociativeNode assignment;
if (null == inputAstNodes || inputAstNodes.Count == 0)
assignment = AstFactory.BuildAssignment(AstIdentifierForPreview, AstFactory.BuildNullNode());
else
assignment = AstFactory.BuildAssignment(AstIdentifierForPreview, inputAstNodes[0]);
return new[] { assignment };
}
protected override void SaveNode(XmlDocument xmlDoc, XmlElement nodeElement, SaveContext context)
{
//Debug.WriteLine(pd.Object.GetType().ToString());
XmlElement outEl = xmlDoc.CreateElement("Symbol");
outEl.SetAttribute("value", Symbol);
nodeElement.AppendChild(outEl);
}
protected override void LoadNode(XmlNode nodeElement)
{
foreach (var subNode in
nodeElement.ChildNodes.Cast<XmlNode>()
.Where(subNode => subNode.Name == "Symbol"))
{
Symbol = subNode.Attributes[0].Value;
}
ArgumentLacing = LacingStrategy.Disabled;
}
/*
[NodeMigration(from: "0.6.3.0", to: "0.7.0.0")]
public static NodeMigrationData Migrate_0630_to_0700(NodeMigrationData data)
{
NodeMigrationData migrationData = new NodeMigrationData(data.Document);
XmlElement oldNode = data.MigratedNodes.ElementAt(0);
XmlElement dummyNode = MigrationManager.CreateDummyNode(oldNode, 1, 0);
migrationData.AppendNode(dummyNode);
return migrationData;
}
*/
}
}