-
Notifications
You must be signed in to change notification settings - Fork 675
Expand file tree
/
Copy pathLiveRunner.cs
More file actions
1924 lines (1688 loc) · 75.3 KB
/
Copy pathLiveRunner.cs
File metadata and controls
1924 lines (1688 loc) · 75.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
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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using ProtoCore;
using ProtoCore.AssociativeGraph;
using ProtoCore.AST.AssociativeAST;
using ProtoCore.DSASM;
using ProtoCore.Mirror;
using ProtoCore.Utils;
using ProtoFFI;
using Dynamo.Utilities;
using System.IO;
namespace ProtoScript.Runners
{
/// <summary>
/// A subtree represents a node in graph. It contains a list of AST node.
/// </summary>
public struct Subtree
{
/// <summary>
/// The GUID of corresponding UI node.
/// </summary>
public Guid GUID;
/// <summary>
/// Specify if all ast nodes should be executed.
/// </summary>
public bool ForceExecution;
/// <summary>
/// Sepcify if the VM should do delta computation for these ASTs
/// By default it is true.
/// </summary>
public bool DeltaComputation;
public List<AssociativeNode> AstNodes;
public List<AssociativeNode> ModifiedAstNodes;
internal bool IsInput;
public Subtree(List<AssociativeNode> astNodes, System.Guid guid)
{
GUID = guid;
AstNodes = astNodes;
ForceExecution = false;
DeltaComputation = true;
ModifiedAstNodes = new List<AssociativeNode>();
IsInput = false;
}
public Subtree(Subtree other)
{
GUID = other.GUID;
AstNodes = other.AstNodes;
ForceExecution = other.ForceExecution;
DeltaComputation = other.DeltaComputation;
ModifiedAstNodes = other.ModifiedAstNodes;
IsInput = other.IsInput;
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append(GUID.ToString() + " " + ForceExecution + " ");
if (AstNodes != null)
AstNodes.ForEach((a) => sb.AppendLine(a.ToString()));
else
sb.AppendLine("AstNodes: null");
return sb.ToString();
}
}
/// <summary>
/// GraphSyncData contains three lists: Subtrees that are added, modified
/// and deleted in a session.
/// </summary>
public class GraphSyncData
{
/// <summary>
/// Session ID
/// </summary>
public Guid SessionID
{
get;
private set;
}
/// <summary>
/// Deleted sub trees.
/// </summary>
public List<Subtree> DeletedSubtrees
{
get;
private set;
}
/// <summary>
/// Added sub trees.
/// </summary>
public List<Subtree> AddedSubtrees
{
get;
private set;
}
/// <summary>
/// Modified sub trees.
/// </summary>
public List<Subtree> ModifiedSubtrees
{
get;
private set;
}
/// <summary>
/// Newly added nodes' IDs.
/// </summary>
public IEnumerable<Guid> AddedNodeIDs
{
get
{
return AddedSubtrees.Select(ts => ts.GUID);
}
}
/// <summary>
/// Modified nodes' IDs.
/// </summary>
public IEnumerable<Guid> ModifiedNodeIDs
{
get
{
return ModifiedSubtrees.Select(ts => ts.GUID);
}
}
/// <summary>
/// Deleted nodes' IDs.
/// </summary>
public IEnumerable<Guid> DeletedNodeIDs
{
get
{
return DeletedSubtrees.Select(ts => ts.GUID);
}
}
/// <summary>
/// All node IDs in this graph sync data.
/// </summary>
public IEnumerable<Guid> NodeIDs
{
get
{
return AddedNodeIDs.Concat(ModifiedNodeIDs).Concat(DeletedNodeIDs);
}
}
public GraphSyncData(List<Subtree> deleted, List<Subtree> added, List<Subtree> modified):
this(Guid.Empty, deleted, added, modified)
{
}
public GraphSyncData(Guid sessionID, List<Subtree> deleted, List<Subtree> added, List<Subtree> modified)
{
SessionID = sessionID;
DeletedSubtrees = deleted ?? new List<Subtree>();
AddedSubtrees = added ?? new List<Subtree>();
ModifiedSubtrees = modified ?? new List<Subtree>();
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("SyncData");
sb.AppendLine("Deleted Subtrees: " + DeletedSubtrees.Count);
DeletedSubtrees.ForEach((t) => sb.AppendLine("\t" + t.ToString()));
sb.AppendLine("Added Subtrees: " + AddedSubtrees.Count);
AddedSubtrees.ForEach((t) => sb.AppendLine("\t" + t.ToString()));
sb.AppendLine("Modified Subtrees: " + ModifiedSubtrees.Count);
ModifiedSubtrees.ForEach((t) => sb.AppendLine("\t" + t.ToString()));
return sb.ToString();
}
}
/// <summary>
/// This is the data returned by ChangeSetComputer and consumed by ChangeSetApplier
/// </summary>
public class ChangeSetData
{
public ChangeSetData() { }
public bool ContainsDeltaAST = false;
public List<AssociativeNode> DeletedBinaryExprASTNodes;
public List<AssociativeNode> DeletedFunctionDefASTNodes;
public List<AssociativeNode> RemovedBinaryNodesFromModification;
public List<AssociativeNode> ModifiedNodesForRuntimeSetValue;
public List<AssociativeNode> RemovedFunctionDefNodesFromModification;
public List<AssociativeNode> ForceExecuteASTList;
public List<AssociativeNode> ModifiedFunctions;
public List<AssociativeNode> ModifiedNestedLangBlock;
}
/// <summary>
/// ChangeSetApplier modifes the VM state given the changes computed from a ChangeSetComputer instance
/// </summary>
public class ChangeSetApplier
{
private ProtoCore.Core core = null;
private RuntimeCore runtimeCore = null;
public void Apply(ProtoCore.Core core, RuntimeCore runtimeCore, ChangeSetData changeSet)
{
Validity.Assert(null != changeSet);
this.core = core;
this.runtimeCore = runtimeCore;
ApplyChangeSetDeleted(changeSet);
ApplyChangeSetModified(changeSet);
ApplyChangeSetForceExecute(changeSet);
}
private void ApplyChangeSetDeleted(ChangeSetData changeSet)
{
DeactivateGraphnodes(changeSet.DeletedBinaryExprASTNodes);
ReActivateGraphNodesInCycle(changeSet.DeletedBinaryExprASTNodes);
RemoveValuesForDeletedNodes(changeSet.DeletedBinaryExprASTNodes);
UndefineFunctions(changeSet.DeletedFunctionDefASTNodes);
ProtoCore.AssociativeEngine.Utils.MarkGraphNodesDirtyFromFunctionRedef(runtimeCore, changeSet.DeletedFunctionDefASTNodes);
}
private void ApplyChangeSetModified(ChangeSetData changeSet)
{
ClearModifiedNestedBlocks(changeSet.ModifiedNestedLangBlock);
DeactivateGraphnodes(changeSet.RemovedBinaryNodesFromModification);
ReActivateGraphNodesInCycle(changeSet.RemovedBinaryNodesFromModification);
// Set new value for modified ASTs
SetValueForModifiedNodes(changeSet.ModifiedNodesForRuntimeSetValue);
// Undefine a function that was removed
UndefineFunctions(changeSet.RemovedFunctionDefNodesFromModification);
// Mark all graphnodes dependent on the removed function as dirty
ProtoCore.AssociativeEngine.Utils.MarkGraphNodesDirtyFromFunctionRedef(runtimeCore, changeSet.RemovedFunctionDefNodesFromModification);
// Mark all graphnodes dependent on the modified functions as dirty
ProtoCore.AssociativeEngine.Utils.MarkGraphNodesDirtyFromFunctionRedef(runtimeCore, changeSet.ModifiedFunctions);
}
private void ApplyChangeSetForceExecute(ChangeSetData changeSet)
{
// Check if there are nodes to force execute
if (changeSet.ForceExecuteASTList.Count > 0)
{
// Mark all graphnodes dirty which are associated with the force exec ASTs
var firstDirtyNode = ProtoCore.AssociativeEngine.Utils.MarkGraphNodesDirtyAtGlobalScope(
runtimeCore, changeSet.ForceExecuteASTList);
Validity.Assert(firstDirtyNode != null);
// If the only ASTs to execute are force exec, then set the entrypoint here.
// Otherwise the entrypoint is set by the code generator when the new ASTs are compiled
if (!changeSet.ContainsDeltaAST)
{
runtimeCore.SetStartPC(firstDirtyNode.updateBlock.startpc);
}
}
}
private void ReActivateGraphNodesInCycle(List<AssociativeNode> nodeList)
{
if (nodeList == null || !nodeList.Any()) return;
var assocGraph = core.DSExecutable.instrStreamList[0].dependencyGraph;
var graphNodes = assocGraph.GetGraphNodesAtScope(Constants.kInvalidIndex, Constants.kInvalidIndex);
foreach (var node in nodeList)
{
var bNode = node as BinaryExpressionNode;
var identifier = bNode?.LeftNode as IdentifierNode;
if (identifier == null) continue;
var rootNodes = new List<GraphNode>();
foreach(var gNode in graphNodes)
{
if(identifier.Value == gNode.updateNodeRefList[0].nodeList[0].symbol.name)
{
rootNodes.Add(gNode);
}
}
foreach (var rootNode in rootNodes)
{
// Walk the dependency graph for the rootNode and clear cycles from dependent graph nodes.
var guids = rootNode.ClearCycles(graphNodes);
// Clear warnings for all graphnodes participating in cycle.
foreach (var id in guids)
{
core.BuildStatus.ClearWarningsForGraph(id);
}
}
}
}
/// <summary>
/// Get the StackValue to be set at runtime
/// The StackValue can be a primitive or an object
/// Currently, only primitives are supported
/// </summary>
/// <param name="bnode"></param>
/// <returns></returns>
private StackValue GetStackValueForRuntime(BinaryExpressionNode bnode)
{
StackValue svSet = StackValue.BuildNull();
if (CoreUtils.IsPrimitiveASTNode(bnode.RightNode))
{
svSet = CoreUtils.BuildStackValueForPrimitive(bnode.RightNode, this.runtimeCore);
}
else
{
// Build or retrieve a DS Pointer (object) and set it here
// DS Pointer must be created and set on the DS heap)
svSet = StackValue.BuildNull();
}
return svSet;
}
/// <summary>
/// Sets a new rhs for binary asts that were modified
/// Sets the VM entry point
/// </summary>
/// <param name="modifiedNodes"></param>
private void SetValueForModifiedNodes(List<AssociativeNode> modifiedNodes)
{
foreach(var node in modifiedNodes)
{
var bnode = node as BinaryExpressionNode;
if (bnode == null) continue;
StackValue sv = GetStackValueForRuntime(bnode);
runtimeCore.ExecutionInstance.CurrentDSASMExec.SetAssociativeUpdateRegister(bnode.OriginalAstID, sv);
}
GraphNode gnode = ProtoCore.AssociativeEngine.Utils.MarkGraphNodesDirtyAtGlobalScope(runtimeCore, modifiedNodes);
if (gnode == null) return;
var startPC = gnode.updateBlock.startpc;
Validity.Assert(startPC != Constants.kInvalidIndex);
runtimeCore.SetStartPC(startPC);
}
private void RemoveValuesForDeletedNodes(List<AssociativeNode> deletedNodes)
{
var bNodes = deletedNodes.OfType<BinaryExpressionNode>();
foreach (var node in bNodes)
{
runtimeCore.ExecutionInstance.CurrentDSASMExec.DeleteUpdateRegister(node.OriginalAstID);
}
}
/// <summary>
/// Deactivate a single graphnode regardless of its associated dependencies
/// </summary>
/// <param name="nodeList"></param>
private void DeactivateGraphnodes(List<AssociativeNode> nodeList)
{
if (null == nodeList)
{
return;
}
var workingStack = new Stack<AssociativeNode>(nodeList);
var astIDs = new HashSet<int>();
while (workingStack.Any())
{
var node = workingStack.Pop() as BinaryExpressionNode;
if (node != null)
{
astIDs.Add(node.OriginalAstID);
workingStack.Push(node.RightNode);
}
}
if (!astIDs.Any())
{
return;
}
foreach (var gnode in core.DSExecutable.instrStreamList[0].dependencyGraph.GraphList)
{
if (astIDs.Contains(gnode.OriginalAstID))
{
gnode.isActive = false;
}
}
}
/// <summary>
/// This method updates a redefined function
/// </summary>
/// <param name="subtree"></param>
/// <returns></returns>
private void UndefineFunctions(IEnumerable<AssociativeNode> functionDefintions)
{
foreach (var funcDef in functionDefintions)
{
core.SetFunctionInactive(funcDef as FunctionDefinitionNode);
}
}
/// <summary>
/// Removes the modified nested block from the VM codegens in preparation for the next run
/// </summary>
/// <param name="modifuedGuids"></param>
private void ClearModifiedNestedBlocks(List<AssociativeNode> astNodes)
{
BinaryExpressionNode bnode = null;
foreach (AssociativeNode node in astNodes)
{
bnode = node as BinaryExpressionNode;
if (bnode.RightNode is LanguageBlockNode)
{
if (core.CodeBlockList[0].children != null)
{
core.CodeBlockList[0].children.RemoveAll(x => x.guid == bnode.guid);
}
// Remove from the global codeblocks
core.CodeBlockList.RemoveAll(x => x.guid == bnode.guid);// && x.AstID == bnode.OriginalAstID);
// Remove from the runtime codeblocks
var keysToRemove = core.CompleteCodeBlockDict.Where(x => x.Value.guid == bnode.guid).Select(x => x.Key).ToList();
keysToRemove.ForEach(key => core.CompleteCodeBlockDict.Remove(key));
}
}
}
}
/// <summary>
/// ChangeSetComputer handles delta computation of AST's
/// </summary>
public class ChangeSetComputer
{
private Dictionary<System.Guid, Subtree> currentSubTreeList = null;
private ProtoCore.Core core = null;
private ProtoCore.RuntimeCore runtimeCore = null;
public ChangeSetData csData { get; private set; }
public ChangeSetComputer(ProtoCore.Core core, ProtoCore.RuntimeCore runtimeCore)
{
this.core = core;
this.runtimeCore = runtimeCore;
currentSubTreeList = new Dictionary<Guid, Subtree>();
}
/// <summary>
/// Deep clone the change set computer
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public ChangeSetComputer Clone()
{
ChangeSetComputer comp = new ChangeSetComputer(this.core, this.runtimeCore);
comp.currentSubTreeList = new Dictionary<Guid, Subtree>();
foreach (var subTreePairs in currentSubTreeList)
{
comp.currentSubTreeList.Add(subTreePairs.Key, subTreePairs.Value);
}
if (csData != null)
{
comp.csData = new ChangeSetData();
comp.csData.ContainsDeltaAST = csData.ContainsDeltaAST;
comp.csData.DeletedBinaryExprASTNodes = new List<AssociativeNode>(csData.DeletedBinaryExprASTNodes);
comp.csData.DeletedFunctionDefASTNodes = new List<AssociativeNode>(csData.DeletedFunctionDefASTNodes);
comp.csData.RemovedBinaryNodesFromModification = new List<AssociativeNode>(csData.RemovedBinaryNodesFromModification);
comp.csData.ModifiedNodesForRuntimeSetValue = new List<AssociativeNode>(csData.ModifiedNodesForRuntimeSetValue);
comp.csData.RemovedFunctionDefNodesFromModification = new List<AssociativeNode>(csData.RemovedFunctionDefNodesFromModification);
comp.csData.ForceExecuteASTList = new List<AssociativeNode>(csData.ForceExecuteASTList);
comp.csData.ModifiedFunctions = new List<AssociativeNode>(csData.ModifiedFunctions);
comp.csData.ModifiedNestedLangBlock = new List<AssociativeNode>(csData.ModifiedNestedLangBlock);
}
return comp;
}
/// <summary>
/// Given deltaGraphNodes, estimate the reachable graphnodes from the live core
/// </summary>
/// <param name="liveCore"></param>
/// <param name="deltaGraphNodes"></param>
/// <returns></returns>
private List<GraphNode> EstimateReachableGraphNodes(RuntimeCore rt, List<GraphNode> deltaGraphNodes)
{
List<GraphNode> reachableNodes = new List<GraphNode>();
foreach (GraphNode executingNode in deltaGraphNodes)
{
reachableNodes.AddRange(ProtoCore.AssociativeEngine.Utils.UpdateDependencyGraph(
executingNode,
rt.CurrentExecutive.CurrentDSASMExec,
executingNode.exprUID,
executingNode.IsSSANode(),
true,
0,
true));
}
return reachableNodes;
}
/// <summary>
/// Estimate the nodes that are affected by the changes in astList
/// Returns a list of guids that map to the affected nodes
/// </summary>
/// <param name="astList"></param>
/// <returns></returns>
public List<Guid> EstimateNodesAffectedByASTList(List<AssociativeNode> astList)
{
List<Guid> cbnGuidList = new List<Guid>();
// Get the VM graphnodes associated with the astList
List<GraphNode> deltaGraphNodeList = ProtoCore.AssociativeEngine.Utils.GetGraphNodesFromAST(core.DSExecutable, astList);
// Get the reachable VM graphnodes given the modified graphnode list
List<GraphNode> reachableNodes = EstimateReachableGraphNodes(runtimeCore, deltaGraphNodeList);
// Append the modified nodes(deltaGraphNodeList) into the reachable list as they are also going to be executed when run
reachableNodes.AddRange(deltaGraphNodeList);
// Get the list of guid's of the ASTs
foreach (GraphNode graphnode in reachableNodes)
{
if (!cbnGuidList.Contains(graphnode.guid))
{
cbnGuidList.Add(graphnode.guid);
}
}
return cbnGuidList;
}
private IEnumerable<AssociativeNode> GetDeltaAstListDeleted(IEnumerable<Subtree> deletedSubTrees)
{
var deltaAstList = new List<AssociativeNode>();
csData.DeletedBinaryExprASTNodes = new List<AssociativeNode>();
csData.DeletedFunctionDefASTNodes = new List<AssociativeNode>();
if (deletedSubTrees == null || !deletedSubTrees.Any())
{
return deltaAstList;
}
foreach (var st in deletedSubTrees)
{
var deletedBinaryExpressions = new List<AssociativeNode>();
if (st.AstNodes != null && st.AstNodes.Any())
{
deletedBinaryExpressions.AddRange(st.AstNodes);
}
else
{
// Handle the case where only the GUID of the deleted subtree was provided
// Get the cached subtree that is now being deleted
Subtree removeSubTree;
if (currentSubTreeList.TryGetValue(st.GUID, out removeSubTree))
{
if (removeSubTree.AstNodes != null)
{
deletedBinaryExpressions.AddRange(removeSubTree.AstNodes);
}
}
}
// Cache removed function definitions
Subtree oldSubTree;
if (currentSubTreeList.TryGetValue(st.GUID, out oldSubTree))
{
if (oldSubTree.AstNodes != null)
{
csData.DeletedFunctionDefASTNodes.AddRange(oldSubTree.AstNodes.Where(n => n is FunctionDefinitionNode));
}
currentSubTreeList.Remove(st.GUID);
}
// Build the nullify ASTs
var nullNodes = BuildNullAssignments(deletedBinaryExpressions, st.GUID);
deltaAstList.AddRange(nullNodes);
core.BuildStatus.ClearWarningsForGraph(st.GUID);
runtimeCore.RuntimeStatus.ClearWarningsForGraph(st.GUID);
csData.DeletedBinaryExprASTNodes.AddRange(deletedBinaryExpressions);
}
return deltaAstList;
}
internal IEnumerable<AssociativeNode> GetDeltaAstListAdded(IEnumerable<Subtree> addedSubTrees)
{
var deltaAstList = new List<AssociativeNode>();
if (addedSubTrees != null)
{
foreach (var st in addedSubTrees)
{
currentSubTreeList.Add(st.GUID, st);
if (st.AstNodes != null)
{
deltaAstList.AddRange(st.AstNodes);
foreach (AssociativeNode node in st.AstNodes)
{
var bnode = node as BinaryExpressionNode;
if (bnode != null)
{
bnode.guid = st.GUID;
bnode.IsInputExpression = st.IsInput;
}
SetNestedLanguageBlockASTGuids(st.GUID, new List<ProtoCore.AST.Node>() { bnode });
}
}
}
}
return deltaAstList;
}
/// <summary>
/// Traverse the list of ASTs and set the guid of the nested binary expressions
/// </summary>
/// <param name="guid"></param>
/// <param name="astList"></param>
private void SetNestedLanguageBlockASTGuids(Guid guid, List<ProtoCore.AST.Node> astList)
{
foreach (ProtoCore.AST.Node node in astList)
{
ProtoCore.AST.Node rightNode = null;
if (node is ProtoCore.AST.AssociativeAST.BinaryExpressionNode)
{
(node as ProtoCore.AST.AssociativeAST.BinaryExpressionNode).guid = guid;
rightNode = (node as ProtoCore.AST.AssociativeAST.BinaryExpressionNode).RightNode;
}
else if (node is ProtoCore.AST.ImperativeAST.BinaryExpressionNode)
{
(node as ProtoCore.AST.ImperativeAST.BinaryExpressionNode).guid = guid;
rightNode = (node as ProtoCore.AST.ImperativeAST.BinaryExpressionNode).RightNode;
}
ProtoCore.AST.Node langblock = null;
List<ProtoCore.AST.Node> nextAstList = new List<ProtoCore.AST.Node>();
if (rightNode is ProtoCore.AST.AssociativeAST.LanguageBlockNode)
{
langblock = (rightNode as ProtoCore.AST.AssociativeAST.LanguageBlockNode).CodeBlockNode;
}
else if (rightNode is ProtoCore.AST.ImperativeAST.LanguageBlockNode)
{
langblock = (rightNode as ProtoCore.AST.ImperativeAST.LanguageBlockNode).CodeBlockNode;
}
if (langblock != null)
{
if (langblock is ProtoCore.AST.AssociativeAST.CodeBlockNode)
{
ProtoCore.AST.AssociativeAST.CodeBlockNode codeBlock = langblock as ProtoCore.AST.AssociativeAST.CodeBlockNode;
foreach (ProtoCore.AST.AssociativeAST.AssociativeNode assocNode in codeBlock.Body)
{
nextAstList.Add(assocNode as ProtoCore.AST.Node);
}
}
else if (langblock is ProtoCore.AST.ImperativeAST.CodeBlockNode)
{
ProtoCore.AST.ImperativeAST.CodeBlockNode codeBlock = langblock as ProtoCore.AST.ImperativeAST.CodeBlockNode;
foreach (ProtoCore.AST.ImperativeAST.ImperativeNode imperativeNode in codeBlock.Body)
{
nextAstList.Add(imperativeNode as ProtoCore.AST.Node);
}
}
}
SetNestedLanguageBlockASTGuids(guid, nextAstList);
}
}
/// <summary>
/// Update the cached ASTs in the subtree given the modified ASTs
/// </summary>
/// <param name="st"></param>
/// <param name="modifiedASTList"></param>
private void UpdateCachedASTList(Subtree st, List<AssociativeNode> modifiedASTList)
{
List<AssociativeNode> removedModifiedNodes = new List<AssociativeNode>();
// Disable removed nodes from the cache
Subtree oldSubTree;
bool cachedTreeExists = currentSubTreeList.TryGetValue(st.GUID, out oldSubTree);
if (cachedTreeExists && oldSubTree.AstNodes != null)
{
List<AssociativeNode> removedNodes = GetInactiveASTList(oldSubTree.AstNodes, st.AstNodes);
// TODO: test this if-logic if necessary for optimized execution
if (st.IsInput && removedNodes.Any())
{
if (removedNodes.Count == modifiedASTList.Count)
{
for (int i = 0; i < removedNodes.Count; i++)
{
if (modifiedASTList[i] is BinaryExpressionNode modifiedNode && removedNodes[i] is BinaryExpressionNode removedNode)
{
modifiedNode.OriginalAstID = removedNode.OriginalAstID;
}
}
}
}
else if (!st.ForceExecution)
{
// We only need the removed binary ASTs
// Function definitions are handled in ChangeSetData.RemovedFunctionDefNodesFromModification
csData.RemovedBinaryNodesFromModification.AddRange(removedNodes.Where(n => n is BinaryExpressionNode));
}
foreach (var removedAST in csData.RemovedBinaryNodesFromModification)
{
core.BuildStatus.ClearWarningsForAst(removedAST.ID);
runtimeCore.RuntimeStatus.ClearWarningsForAst(removedAST.ID);
}
}
// Cache the modifed functions
//var modifiedFunctions = st.AstNodes.Where(n => n is FunctionDefinitionNode);
var modifiedFunctions = modifiedASTList.Where(n => n is FunctionDefinitionNode);
csData.ModifiedFunctions.AddRange(modifiedFunctions);
// Handle cached subtree
if (!cachedTreeExists)
{
// Cache the subtree if it does not exist yet
// This scenario is possible if a subtree was deleted and the same subtree was added again as a modified subtree
currentSubTreeList.Add(st.GUID, st);
}
else
{
if (null == oldSubTree.AstNodes)
{
// The ast list for this subtree is null
// This is due to the liverunner being passed an empty astlist, such as a codeblock with no content
// Populate this subtree with the current ast contents
oldSubTree.AstNodes = modifiedASTList;
currentSubTreeList[st.GUID] = oldSubTree;
}
else
{
var unmodifiedASTs = GetUnmodifiedASTList(oldSubTree.AstNodes, st.AstNodes);
if (st.ForceExecution)
{
// Get the cached AST and append it to the changeSet
csData.ForceExecuteASTList.AddRange(unmodifiedASTs);
}
// Update the cached AST to reflect the change
List<AssociativeNode> newCachedASTList = new List<AssociativeNode>();
// Get all the unmodified ASTs and append them to the cached ast list
newCachedASTList.AddRange(unmodifiedASTs);
// Append all the modified ASTs to the cached ast list
newCachedASTList.AddRange(modifiedASTList);
// ================================================================================
// Get a list of functions that were removed
// This is the list of functions that exist in oldSubTree.AstNodes and no longer exist in st.AstNodes
// This will passed to the changeset applier to handle removed functions in the VM
// ================================================================================
IEnumerable<AssociativeNode> removedFunctions = oldSubTree.AstNodes.Where(f => f is FunctionDefinitionNode && !st.AstNodes.Contains(f));
csData.RemovedFunctionDefNodesFromModification.AddRange(removedFunctions);
st.AstNodes.Clear();
st.AstNodes.AddRange(newCachedASTList);
currentSubTreeList[st.GUID] = st;
}
}
}
private IEnumerable<AssociativeNode> GetDeltaAstListModified(List<Subtree> modifiedSubTrees)
{
var deltaAstList = new List<AssociativeNode>();
csData.RemovedBinaryNodesFromModification = new List<AssociativeNode>();
csData.ModifiedNodesForRuntimeSetValue = new List<AssociativeNode>();
csData.RemovedFunctionDefNodesFromModification = new List<AssociativeNode>();
csData.ModifiedFunctions = new List<AssociativeNode>();
csData.ForceExecuteASTList = new List<AssociativeNode>();
csData.ModifiedNestedLangBlock = new List<AssociativeNode>();
if (modifiedSubTrees == null)
{
return deltaAstList;
}
//Redefinition of input nodes can only be processed when all the modified nodes are input nodes.
var redefinitionAllowed = true;
foreach (var modifiedSubTree in modifiedSubTrees)
{
if (!modifiedSubTree.IsInput)
{
redefinitionAllowed = false;
break;
}
}
for (int n = 0; n < modifiedSubTrees.Count(); ++n)
{
var modifiedSubTree = modifiedSubTrees[n];
if (modifiedSubTree.AstNodes == null)
{
continue;
}
if (modifiedSubTree.DeltaComputation)
{
// Get modified statements
List<AssociativeNode> modifiedInputAST;
var modifiedASTList = GetModifiedNodes(modifiedSubTree, redefinitionAllowed, out modifiedInputAST);
csData.ModifiedNodesForRuntimeSetValue.AddRange(modifiedInputAST);
modifiedSubTree.ModifiedAstNodes.Clear();
modifiedSubTree.ModifiedAstNodes.AddRange(modifiedASTList);
modifiedSubTree.ModifiedAstNodes.AddRange(modifiedInputAST);
deltaAstList.AddRange(modifiedASTList);
foreach (AssociativeNode node in modifiedASTList)
{
var bnode = node as BinaryExpressionNode;
if (bnode != null)
{
bnode.guid = modifiedSubTrees[n].GUID;
bnode.IsInputExpression = modifiedSubTrees[n].IsInput;
}
SetNestedLanguageBlockASTGuids(modifiedSubTree.GUID, new List<ProtoCore.AST.Node>() { bnode });
}
// Handle modified primitives
foreach (AssociativeNode node in modifiedInputAST)
{
var bnode = node as BinaryExpressionNode;
Validity.Assert(bnode != null);
bnode.guid = modifiedSubTrees[n].GUID;
bnode.IsInputExpression = true;
}
UpdateCachedASTList(modifiedSubTree, modifiedSubTree.ModifiedAstNodes);
}
else
{
// No delta computation.
// Right now it is only disabled for code block node, but we may
// completely disable it. Details about why doing so:
// https://github.com/DynamoDS/Dynamo/pull/7282
Subtree oldSubTree;
List<AssociativeNode> deletedExpressions = new List<AssociativeNode>();
if (currentSubTreeList.TryGetValue(modifiedSubTree.GUID, out oldSubTree) && oldSubTree.AstNodes != null)
{
csData.RemovedFunctionDefNodesFromModification.AddRange(oldSubTree.AstNodes.Where(f => f is FunctionDefinitionNode));
deletedExpressions.AddRange(oldSubTree.AstNodes);
var nullNodes = BuildNullAssignments(deletedExpressions, modifiedSubTree.GUID);
deltaAstList.AddRange(nullNodes);
}
currentSubTreeList.Remove(modifiedSubTree.GUID);
core.BuildStatus.ClearWarningsForGraph(modifiedSubTree.GUID);
runtimeCore.RuntimeStatus.ClearWarningsForGraph(modifiedSubTree.GUID);
csData.DeletedBinaryExprASTNodes.AddRange(deletedExpressions);
currentSubTreeList.Add(modifiedSubTree.GUID, modifiedSubTree);
deltaAstList.AddRange(modifiedSubTree.AstNodes);
foreach (AssociativeNode node in modifiedSubTree.AstNodes)
{
var bnode = node as BinaryExpressionNode;
if (bnode != null)
{
bnode.guid = modifiedSubTree.GUID;
}
SetNestedLanguageBlockASTGuids(modifiedSubTree.GUID, new List<ProtoCore.AST.Node>() { bnode });
}
var modifiedFunctions = modifiedSubTree.AstNodes.Where(f => f is FunctionDefinitionNode);
csData.ModifiedFunctions.AddRange(modifiedFunctions);
}
}
return deltaAstList;
}
public List<AssociativeNode> GetDeltaASTList(GraphSyncData syncData)
{
csData = new ChangeSetData();
var finalDeltaAstList = new List<AssociativeNode>();
var deletedDeltaAsts = GetDeltaAstListDeleted(syncData.DeletedSubtrees);
finalDeltaAstList.AddRange(deletedDeltaAsts);
var addedDeltaAsts = GetDeltaAstListAdded(syncData.AddedSubtrees);
finalDeltaAstList.AddRange(addedDeltaAsts);
var modifiedDeltaAsts = GetDeltaAstListModified(syncData.ModifiedSubtrees);
finalDeltaAstList.AddRange(modifiedDeltaAsts);
csData.ContainsDeltaAST = finalDeltaAstList.Any();
return finalDeltaAstList;
}
/// <summary>
///
/// Handle instances of redefining the lhs of an expression
/// Given:
/// a = p.x -> b = p.x
/// In such a scenario, the new expression 'b = p.x' must inherit the previous expression id of a = p.x
/// </summary>
/// <param name="newNode"></param>
/// <param name="cachedASTList"></param>
private void HandleRedefinedLHS(BinaryExpressionNode newNode, List<AssociativeNode> cachedASTList)
{
//
// Note that after SSA is applied, the expression:
// a = p.x
// transforms to:
// t0 = p
// t1 = t0.x
// a = t1
//
// And the expression:
// b = p.x
// transforms to:
// t0 = p
// t1 = t0.x
// b = t1
//
// As such we only need to update the expression id of 'b = t1' to inherit the expression id of 'a = t1'
//
if (null != newNode)
{
IdentifierNode rnode = newNode.RightNode as IdentifierNode;
if (null != rnode)
{
foreach (AssociativeNode prevNode in cachedASTList)
{
BinaryExpressionNode prevBinaryNode = prevNode as BinaryExpressionNode;
if (null != prevBinaryNode)
{
IdentifierNode prevIdent = prevBinaryNode.LeftNode as IdentifierNode;
if (null != prevIdent)
{
if (prevIdent.Equals(rnode))
{
newNode.InheritID(prevBinaryNode.ID);
newNode.ExpressionUID = prevBinaryNode.ExpressionUID;
}
}
}
}
}
}
}
/// <summary>
/// Returns the only the modified nodes from the subtree by checking of the previous cached instance
/// </summary>
/// <param name="subtree"></param>
/// <returns></returns>
private List<AssociativeNode> GetModifiedNodes(Subtree subtree, bool redefinitionAllowed, out List<AssociativeNode> modifiedInputAST)
{
modifiedInputAST = new List<AssociativeNode>();
Subtree st;
if (!currentSubTreeList.TryGetValue(subtree.GUID, out st) || st.AstNodes == null)
{
// If the subtree was not cached, it means the cache was delted
// This means the current subtree is all modified
return subtree.AstNodes;
}
// We want to process only modified statements
// If the AST is identical to an existing AST in the same GUID, it means it was not modified
var modifiedASTList = new List<AssociativeNode>();
foreach (AssociativeNode node in subtree.AstNodes)
{
// Check if node exists in the prev AST list
bool nodeFound = false;
foreach (AssociativeNode prevNode in st.AstNodes)
{
if (prevNode.Equals(node))