forked from DynamoDS/Dynamo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEngineController.cs
More file actions
689 lines (590 loc) · 25 KB
/
Copy pathEngineController.cs
File metadata and controls
689 lines (590 loc) · 25 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
using System;
using System.Collections.Generic;
using System.Linq;
using Dynamo.Engine.CodeCompletion;
using Dynamo.Engine.CodeGeneration;
using Dynamo.Engine.NodeToCode;
using Dynamo.Engine.Profiling;
using Dynamo.Graph.Nodes;
using Dynamo.Graph.Workspaces;
using Dynamo.Logging;
using Dynamo.Scheduler;
using Dynamo.Utilities;
using ProtoCore.AST.AssociativeAST;
using ProtoCore.DSASM.Mirror;
using ProtoCore.Mirror;
using ProtoCore.Utils;
using ProtoScript.Runners;
using BuildWarning = ProtoCore.BuildData.WarningEntry;
using RuntimeWarning = ProtoCore.Runtime.WarningEntry;
using RuntimeInfo = ProtoCore.Runtime.InfoEntry;
namespace Dynamo.Engine
{
/// <summary>
/// This is a delegate used in AstBuilt event.
/// </summary>
/// <param name="sender">EngineController</param>
/// <param name="e">CompiledEventArgs (include node GUID and list of AST nodes)</param>
public delegate void AstBuiltEventHandler(object sender, CompiledEventArgs e);
/// <summary>
/// A controller to coordinate the interactions between some DesignScript
/// sub components like library management, live runner and so on.
/// </summary>
public class EngineController : LogSourceBase, IAstNodeContainer, IDisposable
{
/// <summary>
/// This event is fired when a node has been compiled.
/// </summary>
public event AstBuiltEventHandler AstBuilt;
/// <summary>
/// The event notifies client that the VMLibraries have been reset and the VM is now ready to run the new code.
/// </summary>
internal static event Action VMLibrariesReset;
/// <summary>
/// Dynamo version in which the current workspace was last created or modified.
/// </summary>
internal Version CurrentWorkspaceVersion { get; set; }
/// <summary>
/// This event is fired when <see cref="UpdateGraphAsyncTask"/> is completed.
/// </summary>
internal event Action<TraceReconciliationEventArgs> TraceReconcliationComplete;
private void OnTraceReconciliationComplete(TraceReconciliationEventArgs e)
{
if (TraceReconcliationComplete != null)
{
TraceReconcliationComplete(e);
}
}
private readonly LiveRunnerServices liveRunnerServices;
private readonly LibraryServices libraryServices;
private CodeCompletionServices codeCompletionServices;
private readonly AstBuilder astBuilder;
private SyncDataManager syncDataManager;
private readonly Queue<GraphSyncData> graphSyncDataQueue = new Queue<GraphSyncData>();
private readonly Queue<List<Guid>> previewGraphQueue = new Queue<List<Guid>>();
/// <summary>
/// Bool value indicates if every action should be logged. Used in debug mode.
/// </summary>
public bool VerboseLogging;
private readonly Object macroMutex = new Object();
/// <summary>
/// Reference to Compilation service. This compiles Input / Output node.
/// </summary>
public static CompilationServices CompilationServices;
/// <summary>
/// Returns DesignScript core.
/// </summary>
public ProtoCore.Core LiveRunnerCore
{
get
{
return liveRunnerServices.Core;
}
}
/// <summary>
/// Returns DesignScript runtime core.
/// </summary>
public ProtoCore.RuntimeCore LiveRunnerRuntimeCore
{
get
{
return liveRunnerServices.RuntimeCore;
}
}
/// <summary>
/// Returns library service instance.
/// </summary>
public LibraryServices LibraryServices
{
get { return libraryServices; }
}
internal CodeCompletionServices CodeCompletionServices
{
get { return codeCompletionServices; }
}
internal ProfilingSession ProfilingSession
{
get { return astBuilder.ProfilingSession; }
}
/// <summary>
/// Returns information about time spent compiling and executing nodes.
/// </summary>
public IProfilingExecutionTimeData ExecutionTimeData
{
get { return ProfilingSession.ProfilingData; }
}
/// <summary>
/// A property defining whether the EngineController has been disposed or not.
/// This is a conservative field, as there should only be one owner of a valid
/// EngineController or not.
/// </summary>
public bool IsDisposed { get; private set; }
/// <summary>
/// This function creates EngineController
/// </summary>
/// <param name="libraryServices"> LibraryServices manages builtin libraries and imported libraries.</param>
/// <param name="geometryFactoryFileName">Path to LibG</param>
/// <param name="verboseLogging">Bool value, if set to true, enables verbose logging</param>
public EngineController(LibraryServices libraryServices, string geometryFactoryFileName, bool verboseLogging)
{
this.libraryServices = libraryServices;
libraryServices.LibraryLoaded += LibraryLoaded;
CompilationServices = new CompilationServices(libraryServices);
codeCompletionServices = new CodeCompletionServices(libraryServices.LibraryManagementCore);
liveRunnerServices = new LiveRunnerServices(this, geometryFactoryFileName);
OnLibraryLoaded();
astBuilder = new AstBuilder(this);
syncDataManager = new SyncDataManager();
VerboseLogging = verboseLogging;
CurrentWorkspaceVersion = AssemblyHelper.GetDynamoVersion();
}
/// <summary>
/// Disposes EngineController.
/// </summary>
public void Dispose()
{
// This flag must be set immediately
IsDisposed = true;
libraryServices.LibraryLoaded -= LibraryLoaded;
liveRunnerServices.Dispose();
codeCompletionServices = null;
}
/// <summary>
/// Enables or disables profiling depending on the given argument
/// </summary>
/// <param name="enable">Indicates enabling or disabling of profiling.</param>
/// <param name="workspace">The workspace to enable or disable profiling for.</param>
/// <param name="nodes">The list of nodes to enable or disable profiling for.</param>
public void EnableProfiling(bool enable, HomeWorkspaceModel workspace, IEnumerable<NodeModel> nodes)
{
Validity.Assert(workspace != null, "Workspace cannot be null");
Validity.Assert(nodes != null, "Node list cannot be null");
if (enable)
{
if (astBuilder.ProfilingSession == null)
{
astBuilder.ProfilingSession = new ProfilingSession();
}
}
else
{
astBuilder.ProfilingSession.Dispose();
astBuilder.ProfilingSession = null;
}
workspace.MarkNodesAsModifiedAndRequestRun(nodes, true);
}
#region Function Groups
/// <summary>
/// Import Library
/// </summary>
/// <param name="library"></param>
internal void ImportLibrary(string library)
{
LibraryServices.ImportLibrary(library);
}
#endregion
#region Value queries
/// <summary>
/// Returns runtime mirror for variable.
/// </summary>
/// <param name="variableName">Unique ID of AST node</param>
/// <returns>RuntimeMirror object that reflects status of a single designscript variable</returns>
public RuntimeMirror GetMirror(string variableName)
{
lock (macroMutex)
{
RuntimeMirror mirror = null;
try
{
mirror = liveRunnerServices.GetMirror(variableName, VerboseLogging);
}
catch (SymbolNotFoundException)
{
// The variable hasn't been defined yet. Just skip it.
}
catch (Exception ex)
{
Log(string.Format(Properties.Resources.FailedToGetMirrorVariable, variableName,
ex.Message));
}
return mirror;
}
}
#endregion
/// <summary>
/// This method is called on the main thread from UpdateGraphAsyncTask
/// to generate GraphSyncData for a list of updated nodes.
/// </summary>
/// <param name="nodes"></param>
/// <param name="updatedNodes">The list of all updated nodes.</param>
/// <param name="verboseLogging"></param>
/// <returns>This method returns true if GraphSyncData is generated from
/// the list of updated nodes. If updatedNodes is empty or does not
/// result in any GraphSyncData, then this method returns false.</returns>
internal GraphSyncData ComputeSyncData(IEnumerable<NodeModel> nodes, IEnumerable<NodeModel> updatedNodes, bool verboseLogging)
{
if (updatedNodes == null)
return null;
var activeNodes = updatedNodes.Where(n => !n.IsInErrorState);
if (activeNodes.Any())
{
astBuilder.CompileToAstNodes(activeNodes, CompilationContext.DeltaExecution, verboseLogging);
}
if (!VerifyGraphSyncData(nodes) || ((graphSyncDataQueue.Count <= 0)))
{
return null;
}
return graphSyncDataQueue.Dequeue();
}
/// <summary>
/// This is called on the main thread from PreviewGraphSyncData
/// to generate the list of node id's that will be executed on the next run
/// </summary>
/// <param name="updatedNodes">The updated nodes.</param>
/// <param name="verboseLogging"></param>
/// <returns>This method returns the list of all reachable node id's from the given
/// updated nodes</returns>
internal List<Guid> PreviewGraphSyncData(IEnumerable<NodeModel> updatedNodes, bool verboseLogging)
{
if (updatedNodes == null)
return null;
var tempSyncDataManager = syncDataManager.Clone();
var activeNodes = updatedNodes.Where(n => n.State != ElementState.Error);
if (activeNodes.Any())
{
astBuilder.CompileToAstNodes(activeNodes, CompilationContext.PreviewGraph, verboseLogging);
}
GraphSyncData graphSyncdata = syncDataManager.GetSyncData();
List<Guid> previewGraphData = this.liveRunnerServices.PreviewGraph(graphSyncdata, verboseLogging);
syncDataManager = tempSyncDataManager;
lock (previewGraphQueue)
{
previewGraphQueue.Enqueue(previewGraphData);
}
return previewGraphQueue.Dequeue();
}
/// <summary>
/// Returns true if there are graph sync data in the queue waiting to be executed.
/// </summary>
/// <returns></returns>
public bool HasPendingGraphSyncData
{
get
{
lock (macroMutex)
{
lock (graphSyncDataQueue)
{
return graphSyncDataQueue.Count > 0;
}
}
}
}
private readonly Queue<GraphSyncData> pendingCustomNodeSyncData = new Queue<GraphSyncData>();
/// <summary>
/// Generate graph sync data based on the input Dynamo custom node information.
/// Returns false if all nodes are clean.
/// </summary>
/// <param name="nodes"></param>
/// <param name="definition"></param>
/// <param name="verboseLogging"></param>
/// <returns></returns>
internal bool GenerateGraphSyncDataForCustomNode(IEnumerable<NodeModel> nodes, CustomNodeDefinition definition, bool verboseLogging)
{
lock (macroMutex)
{
// Any graph updates through the scheduler no longer store their
// GraphSyncData in 'graphSyncDataQueue' (any such entry will be
// withdrawn from the queue and get associated with an AsyncTask.
// This check is to ensure that such case does not exist.
//
if (graphSyncDataQueue.Count > 0)
{
throw new InvalidOperationException(
"'graphSyncDataQueue' is not empty");
}
astBuilder.CompileCustomNodeDefinition(
definition.FunctionId,
definition.ReturnKeys,
definition.FunctionName,
definition.FunctionBody,
definition.OutputNodes,
definition.Parameters,
verboseLogging);
if (!VerifyGraphSyncData(nodes) || (graphSyncDataQueue.Count == 0))
return false;
// GraphSyncData objects accumulated through the compilation above
// will be stored in 'pendingCustomNodeSyncData'. Entries in this
// queue will be used to update custom node graph prior to updating
// the graph for the home workspace.
//
while (graphSyncDataQueue.Count > 0)
{
var graphSyncData = graphSyncDataQueue.Dequeue();
pendingCustomNodeSyncData.Enqueue(graphSyncData);
}
return true;
}
}
/// <summary>
/// DynamoModel calls this method prior to scheduling a graph update for
/// the home workspace. This method is called to schedule custom node
/// compilation since the home workspace update may depend on it. Any
/// updates to a CustomNodeDefinition will cause GraphSyncData to be added
/// to "pendingCustomNodeSyncData" queue.
/// </summary>
/// <param name="scheduler">The scheduler on which custom node compilation
/// task can be scheduled.</param>
///
internal void ProcessPendingCustomNodeSyncData(IScheduler scheduler)
{
while (pendingCustomNodeSyncData.Count > 0)
{
var initParams = new CompileCustomNodeParams
{
SyncData = pendingCustomNodeSyncData.Dequeue(),
EngineController = this
};
var compileTask = new CompileCustomNodeAsyncTask(scheduler);
if (compileTask.Initialize(initParams))
scheduler.ScheduleForExecution(compileTask);
}
}
private bool VerifyGraphSyncData(IEnumerable<NodeModel> nodes)
{
GraphSyncData graphSyncdata = syncDataManager.GetSyncData();
syncDataManager.ResetStates();
var reExecuteNodesIds = new HashSet<Guid>(
nodes.Where(n => n.NeedsForceExecution)
.Select(n => n.GUID));
var codeBlockNodes = new HashSet<Guid>(
nodes.Where(n => n is CodeBlockNodeModel).Select(n => n.GUID));
if (reExecuteNodesIds.Any() || codeBlockNodes.Any())
{
for (int i = 0; i < graphSyncdata.ModifiedSubtrees.Count; ++i)
{
var st = graphSyncdata.ModifiedSubtrees[i];
var forceExecution = reExecuteNodesIds.Contains(st.GUID);
var isCodeBlockNode = codeBlockNodes.Contains(st.GUID);
if (forceExecution || isCodeBlockNode)
{
Subtree newSt = new Subtree(st.AstNodes, st.GUID);
newSt.ForceExecution = forceExecution;
newSt.DeltaComputation = !isCodeBlockNode;
graphSyncdata.ModifiedSubtrees[i] = newSt;
}
}
}
foreach (var node in nodes)
{
if (!node.IsInputNode) continue;
//We also don't want any nodes that do have input ports or where derived from custom nodes.
if (node.InPorts.Any() || node.IsCustomFunction) continue;
// Only one or the other of the two lists, Added or Modified, will match the node GUID if they do.
bool isAdded = false;
for (int i = 0; i < graphSyncdata.AddedSubtrees.Count; i++)
{
if (graphSyncdata.AddedSubtrees[i].GUID == node.GUID)
{
graphSyncdata.AddedSubtrees[i] = new Subtree(graphSyncdata.AddedSubtrees[i])
{
IsInput = true
};
isAdded = true;
break;
}
}
if (isAdded) continue;
for (int i = 0; i < graphSyncdata.ModifiedSubtrees.Count; i++)
{
if (graphSyncdata.ModifiedSubtrees[i].GUID == node.GUID)
{
graphSyncdata.ModifiedSubtrees[i] = new Subtree(graphSyncdata.ModifiedSubtrees[i])
{
IsInput = true
};
break;
}
}
}
if (graphSyncdata.AddedSubtrees.Any() || graphSyncdata.ModifiedSubtrees.Any() || graphSyncdata.DeletedSubtrees.Any())
{
lock (graphSyncDataQueue)
{
graphSyncDataQueue.Enqueue(graphSyncdata);
}
return true;
}
return false;
}
/// <summary>
/// This method is called by UpdateGraphAsyncTask in the context of
/// ISchedulerThread to kick start an update through LiveRunner.
/// </summary>
/// <param name="graphSyncData">The GraphSyncData that was generated by
/// a prior call to ComputeSyncData at the time UpdateGraphAsyncTask was
/// scheduled.</param>
///
internal void UpdateGraphImmediate(GraphSyncData graphSyncData)
{
// NOTE: We will not attempt to catch any unhandled exception from
// within the execution. Such exception, if any, will be caught by
// DynamoScheduler.ProcessTaskInternal.
liveRunnerServices.UpdateGraph(graphSyncData, VerboseLogging);
}
internal IDictionary<Guid, List<BuildWarning>> GetBuildWarnings()
{
return liveRunnerServices.GetBuildWarnings();
}
internal IDictionary<Guid, List<RuntimeWarning>> GetRuntimeWarnings()
{
return liveRunnerServices.GetRuntimeWarnings();
}
internal IDictionary<Guid, List<RuntimeInfo>> GetRuntimeInfos()
{
return liveRunnerServices.GetRuntimeInfos();
}
internal IEnumerable<Guid> GetExecutedAstGuids(Guid sessionID)
{
return liveRunnerServices.GetExecutedAstGuids(sessionID);
}
internal void RemoveRecordedAstGuidsForSession(Guid sessionID)
{
liveRunnerServices.RemoveRecordedAstGuidsForSession(sessionID);
}
internal void ReconcileTraceDataAndNotify()
{
if (this.IsDisposed)
{
throw new ObjectDisposedException("EngineController");
}
var callsiteToOrphanMap = new Dictionary<Guid, List<string>>();
foreach (var cs in liveRunnerServices.RuntimeCore.RuntimeData.CallsiteCache.Values)
{
var orphanedSerializables = cs.GetOrphanedSerializables().ToList();
if (callsiteToOrphanMap.ContainsKey(cs.CallSiteID))
{
callsiteToOrphanMap[cs.CallSiteID].AddRange(orphanedSerializables);
}
else
{
callsiteToOrphanMap.Add(cs.CallSiteID, orphanedSerializables);
}
}
OnTraceReconciliationComplete(new TraceReconciliationEventArgs(callsiteToOrphanMap));
}
private void OnLibraryLoaded()
{
liveRunnerServices.ReloadAllLibraries(libraryServices.ImportedLibraries);
VMLibrariesReset?.Invoke();
}
/// <summary>
/// LibraryLoaded event handler.
/// </summary>
private void LibraryLoaded(object sender, LibraryServices.LibraryLoadedEventArgs e)
{
if (e.LibraryPaths.Any())
{
OnLibraryLoaded();
}
}
internal event EventHandler RequestCustomNodeRegistration;
internal void OnRequestCustomNodeRegistration()
{
RequestCustomNodeRegistration?.Invoke(this, EventArgs.Empty);
}
#region Implement IAstNodeContainer interface
/// <summary>
/// This class represents the intermediate state (Compiling state), when a nodemodel is compiled to AST nodes.
/// </summary>
/// <param name="nodeGuid">Node unique ID</param>
public void OnCompiling(Guid nodeGuid)
{
syncDataManager.MarkForAdding(nodeGuid);
}
/// <summary>
/// This class represents a state after a nodemodel is compiled to AST nodes.
/// </summary>
/// <param name="nodeGuid">Node unique ID</param>
/// <param name="astNodes">Resulting AST nodes</param>
public void OnCompiled(Guid nodeGuid, IEnumerable<AssociativeNode> astNodes)
{
var associativeNodes = astNodes as IList<AssociativeNode> ?? astNodes.ToList();
foreach (var astNode in associativeNodes)
syncDataManager.AddNode(nodeGuid, astNode);
if (AstBuilt != null)
AstBuilt(this, new CompiledEventArgs(nodeGuid, associativeNodes));
}
#endregion
/// <summary>
/// NodeDeleted event handler.
/// </summary>
/// <param name="node"></param>
public void NodeDeleted(NodeModel node)
{
syncDataManager.DeleteNodes(node.GUID);
}
/// <summary>
/// When a node is in freeze state, the node and its dependencies are
/// deleted from AST
/// </summary>
/// <param name="node">The node.</param>
internal void DeleteFrozenNodesFromAST(NodeModel node)
{
HashSet<NodeModel> gathered = new HashSet<NodeModel>();
node.GetDownstreamNodes(node, gathered);
foreach (var iNode in gathered)
{
syncDataManager.DeleteNodes(iNode.GUID);
}
}
#region Node2Code
internal NodeToCodeResult ConvertNodesToCode(IEnumerable<NodeModel> graph, IEnumerable<NodeModel> nodes, INamingProvider namingProvider = null)
{
return NodeToCodeCompiler.NodeToCode(libraryServices.LibraryManagementCore, graph, nodes, namingProvider);
}
#endregion
}
/// <summary>
/// This class is used to precompile code block node.
/// Also it's used as helper for resolving code in Input and Output nodes.
/// </summary>
public class CompilationServices
{
private ProtoCore.Core compilationCore;
private readonly Dictionary<string, string> priorNames;
/// <summary>
/// Creates CompilationServices.
/// </summary>
/// <param name="libraryServices">Copilation core</param>
public CompilationServices(LibraryServices libraryServices)
{
compilationCore = libraryServices.LibraryManagementCore;
priorNames = libraryServices.GetPriorNames();
}
/// <summary>
/// Pre-compiles Design script code in code block node.
/// </summary>
/// <param name="parseParams">Container for compilation related parameters</param>
/// <returns>true if code compilation succeeds, false otherwise</returns>
internal bool PreCompileCodeBlock(ParseParam parseParams)
{
return CompilerUtils.PreCompileCodeBlock(compilationCore, parseParams, priorNames);
}
}
internal class TraceReconciliationEventArgs : EventArgs
{
/// <summary>
/// A list of string items.
/// </summary>
public Dictionary<Guid, List<string>> CallsiteToOrphanMap { get; private set; }
public TraceReconciliationEventArgs(Dictionary<Guid, List<string>> callsiteToOrphanMap)
{
CallsiteToOrphanMap = callsiteToOrphanMap;
}
}
public interface ITraceReconciliationProcessor
{
void PostTraceReconciliation(Dictionary<Guid, List<string>> orphanedSerializables);
}
}