-
Notifications
You must be signed in to change notification settings - Fork 670
Expand file tree
/
Copy pathCustomNodeCompatibilityMutator.cs
More file actions
395 lines (330 loc) · 15.7 KB
/
CustomNodeCompatibilityMutator.cs
File metadata and controls
395 lines (330 loc) · 15.7 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using Autodesk.DesignScript.Runtime;
using Dynamo.Configuration;
using Dynamo.Engine;
using Dynamo.Graph.Connectors;
using Dynamo.Graph.Nodes;
using Dynamo.Graph.Nodes.CustomNodes;
using Dynamo.Models;
using Dynamo.Utilities;
using Dynamo.ViewModels;
namespace Dynamo.TestInfrastructure
{
[MutationTest("CustomNodeCompatibilityMutator")]
class CustomNodeCompatibilityMutator : AbstractMutator
{
public CustomNodeCompatibilityMutator(DynamoViewModel viewModel)
: base(viewModel)
{
}
public override Type GetNodeType()
{
return typeof(Function);
}
public override int NumberOfLaunches
{
get { return 1; }
}
public override bool RunTest(NodeModel node, EngineController engine, StreamWriter writer)
{
bool pass = false;
var types = LoadAllTypesFromDynamoAssemblies();
foreach (Type type in types)
{
string nodeName = GetName(type);
var firstNodeConnectors = node.AllConnectors.ToList();
double coordinatesX = node.X;
double coordinatesY = node.Y;
if (!string.IsNullOrEmpty(nodeName))
{
DynamoViewModel.UIDispatcher.Invoke(new Action(() =>
{
var newNode = type.GetDefaultConstructor<NodeModel>()();
DynamoModel.CreateNodeCommand createCommand =
new DynamoModel.CreateNodeCommand(
newNode, coordinatesX, coordinatesY, false, false);
DynamoViewModel.ExecuteCommand(createCommand);
}));
var valueMap = new Dictionary<Guid, String>();
foreach (ConnectorModel connector in firstNodeConnectors)
{
Guid guid = connector.Start.Owner.GUID;
Object data = connector.Start.Owner.GetValue(0, engine).Data;
String val = data != null ? data.ToString() : "null";
valueMap.Add(guid, val);
writer.WriteLine(guid + " :: " + val);
writer.Flush();
}
int numberOfUndosNeeded = Mutate(node);
Thread.Sleep(100);
writer.WriteLine("### - Beginning undo");
for (int iUndo = 0; iUndo < numberOfUndosNeeded; iUndo++)
{
DynamoViewModel.UIDispatcher.Invoke(new Action(() =>
{
DynamoModel.UndoRedoCommand undoCommand =
new DynamoModel.UndoRedoCommand(DynamoModel.UndoRedoCommand.Operation.Undo);
DynamoViewModel.ExecuteCommand(undoCommand);
}));
}
Thread.Sleep(100);
writer.WriteLine("### - undo complete");
writer.Flush();
ExecuteAndWait();
writer.WriteLine("### - Beginning test of CustomNode");
if (node.OutPorts.Count > 0)
{
try
{
NodeModel nodeAfterUndo =
DynamoViewModel.Model.CurrentWorkspace.Nodes.FirstOrDefault(
(t) => (t.GUID == node.GUID));
if (nodeAfterUndo != null)
{
var firstNodeConnectorsAfterUndo = nodeAfterUndo.AllConnectors.ToList();
foreach (ConnectorModel connector in firstNodeConnectors)
{
Guid guid = connector.Start.Owner.GUID;
Object data = connector.Start.Owner.GetValue(0, engine).Data;
String val = data != null ? data.ToString() : "null";
if (valueMap[guid] != val)
{
writer.WriteLine("!!!!!!!!!!! - test of CustomNode is failed");
writer.WriteLine(node.GUID);
writer.WriteLine("Was: " + val);
writer.WriteLine("Should have been: " + valueMap[guid]);
writer.Flush();
return pass;
}
}
}
}
catch (Exception)
{
writer.WriteLine("!!!!!!!!!!! - test of CustomNode is failed");
writer.Flush();
return pass;
}
}
writer.WriteLine("### - test of CustomNode complete");
writer.Flush();
}
}
return pass = true;
}
public override int Mutate(NodeModel node)
{
NodeModel lastNode = DynamoModel.CurrentWorkspace.Nodes.Last();
if (lastNode.OutPorts.Count > 0)
{
DynamoViewModel.UIDispatcher.Invoke(new Action(() =>
{
DynamoModel.MakeConnectionCommand connectCmd1 =
new DynamoModel.MakeConnectionCommand(lastNode.GUID, 0, PortType.Output,
DynamoModel.MakeConnectionCommand.Mode.Begin);
DynamoModel.MakeConnectionCommand connectCmd2 =
new DynamoModel.MakeConnectionCommand(node.GUID, 0, PortType.Input,
DynamoModel.MakeConnectionCommand.Mode.End);
DynamoViewModel.ExecuteCommand(connectCmd1);
DynamoViewModel.ExecuteCommand(connectCmd2);
}));
}
else
{
DynamoViewModel.UIDispatcher.Invoke(new Action(() =>
{
DynamoModel.MakeConnectionCommand connectCmd1 =
new DynamoModel.MakeConnectionCommand(node.GUID, 0, PortType.Output,
DynamoModel.MakeConnectionCommand.Mode.Begin);
DynamoModel.MakeConnectionCommand connectCmd2 =
new DynamoModel.MakeConnectionCommand(lastNode.GUID, 0, PortType.Input,
DynamoModel.MakeConnectionCommand.Mode.End);
DynamoViewModel.ExecuteCommand(connectCmd1);
DynamoViewModel.ExecuteCommand(connectCmd2);
}));
}
return 5;
}
#region Auxiliary methods
public string GetName(Type type)
{
string name = string.Empty;
var excludedTypeNames = new List<string>() { "Code Block", "Custom Node", "Compose Functions", "List.ForEach", "Build Sublists", "Apply Function" };
var attribs = type.GetCustomAttributes(typeof(NodeNameAttribute), false);
var attrs = type.GetCustomAttributes(typeof(IsVisibleInDynamoLibraryAttribute), true);
if (attribs.Length > 0)
{
if (!excludedTypeNames.Contains((attribs[0] as NodeNameAttribute).Name))
name = (attribs[0] as NodeNameAttribute).Name;
if ((attrs != null) && attrs.Any())
{
var isVisibleAttr = attrs[0] as IsVisibleInDynamoLibraryAttribute;
if (null != isVisibleAttr && isVisibleAttr.Visible == false)
{
name = string.Empty;
}
}
}
return name;
}
public List<Type> LoadAllTypesFromDynamoAssemblies()
{
var nodeTypes = new List<Type>();
var loadedAssemblyNames = new HashSet<string>();
var allLoadedAssembliesByPath = new Dictionary<string, Assembly>();
var allLoadedAssemblies = new Dictionary<string, Assembly>();
// cache the loaded assembly information
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
if (assembly.IsDynamic)
continue;
try
{
allLoadedAssembliesByPath[assembly.Location] = assembly;
allLoadedAssemblies[assembly.FullName] = assembly;
}
catch { }
}
// find all the dlls registered in all search paths
// and concatenate with all dlls in the current directory
var nodes = DynamoModel.PathManager.NodeDirectories;
var allDynamoAssemblyPaths = nodes.SelectMany((path) =>
{
return (Directory.GetFiles(path, "*.dll", SearchOption.TopDirectoryOnly));
}).ToList();
// add the core assembly to get things like code block nodes and watches.
var mainExecPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
allDynamoAssemblyPaths.Add(Path.Combine(mainExecPath, "DynamoCore.dll"));
var resolver = new ResolveEventHandler(delegate(object sender, ResolveEventArgs args)
{
Assembly result;
allLoadedAssemblies.TryGetValue(args.Name, out result);
return result;
});
foreach (var assemblyPath in allDynamoAssemblyPaths)
{
var fn = Path.GetFileName(assemblyPath);
if (fn == null)
continue;
// if the assembly has already been loaded, then
// skip it, otherwise cache it.
if (loadedAssemblyNames.Contains(fn))
continue;
loadedAssemblyNames.Add(fn);
if (allLoadedAssembliesByPath.ContainsKey(assemblyPath))
{
List<Type> types = LoadNodesFromAssembly(allLoadedAssembliesByPath[assemblyPath]);
nodeTypes.AddRange(types);
}
else
{
try
{
var assembly = Dynamo.Utilities.AssemblyHelper.LoadInALCFrom(assemblyPath);
allLoadedAssemblies[assembly.GetName().Name] = assembly;
List<Type> types = LoadNodesFromAssembly(assembly);
nodeTypes.AddRange(types);
}
catch (Exception e)
{
DynamoViewModel.Model.Logger.Log(e);
}
}
}
return nodeTypes;
}
private List<Type> LoadNodesFromAssembly(Assembly assembly)
{
if (assembly == null)
throw new ArgumentNullException("assembly");
var searchViewModel = DynamoViewModel.Model.SearchModel;
var types = new List<Type>();
try
{
var loadedTypes = assembly.GetTypes();
foreach (var t in loadedTypes)
{
//only load types that are in the right namespace, are not abstract
//and have the elementname attribute
var attribs = t.GetCustomAttributes(typeof(NodeNameAttribute), false);
var isDeprecated = t.GetCustomAttributes(typeof(NodeDeprecatedAttribute), true).Any();
var isMetaNode = t.GetCustomAttributes(typeof(IsMetaNodeAttribute), false).Any();
var isDSCompatible = t.GetCustomAttributes(typeof(IsDesignScriptCompatibleAttribute), true).Any();
bool isHidden = false;
var attrs = t.GetCustomAttributes(typeof(IsVisibleInDynamoLibraryAttribute), true);
if (null != attrs && attrs.Any())
{
var isVisibleAttr = attrs[0] as IsVisibleInDynamoLibraryAttribute;
if (null != isVisibleAttr && isVisibleAttr.Visible == false)
{
isHidden = true;
}
}
if (!NodeModelAssemblyLoader.IsNodeSubType(t) &&
t.Namespace != "Dynamo.Graph.Nodes") /*&& attribs.Length > 0*/
continue;
//if we are running in revit (or any context other than NONE)
//use the DoNotLoadOnPlatforms attribute,
//if available, to discern whether we should load this type
if (!DynamoViewModel.Model.Context.Equals(Context.NONE))
{
object[] platformExclusionAttribs =
t.GetCustomAttributes(typeof(DoNotLoadOnPlatformsAttribute), false);
if (platformExclusionAttribs.Length > 0)
{
string[] exclusions =
(platformExclusionAttribs[0] as DoNotLoadOnPlatformsAttribute).Values;
//if the attribute's values contain the context stored on the controller
//then skip loading this type.
if (exclusions.Reverse().Any(e => e.Contains(DynamoViewModel.Model.Context)))
continue;
//utility was late for Vasari release,
//but could be available with after-post RevitAPI.dll
if (t.Name.Equals("dynSkinCurveLoops"))
{
MethodInfo[] specialTypeStaticMethods =
t.GetMethods(BindingFlags.Static | BindingFlags.Public);
const string nameOfMethodCreate = "noSkinSolidMethod";
bool exclude = true;
foreach (MethodInfo m in specialTypeStaticMethods)
{
if (m.Name == nameOfMethodCreate)
{
var argsM = new object[0];
exclude = (bool)m.Invoke(null, argsM);
break;
}
}
if (exclude)
continue;
}
}
}
string typeName;
if (attribs.Length > 0 && !isDeprecated &&
!isMetaNode && isDSCompatible && !isHidden)
{
//searchViewModel.Add(t);
typeName = (attribs[0] as NodeNameAttribute).Name;
}
else
typeName = t.Name;
types.Add(t);
var data = new TypeLoadData(t);
}
}
catch (Exception e)
{
DynamoViewModel.Model.Logger.Log("Could not load types.");
DynamoViewModel.Model.Logger.Log(e);
}
return types;
}
#endregion
}
}