/
CyPhyMdaoAddOn.cs
541 lines (482 loc) · 19.4 KB
/
CyPhyMdaoAddOn.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
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using GME.CSharp;
using GME.MGA;
using GME;
using GME.MGA.Core;
using System.Windows.Forms;
using System.Linq;
using GME.MGA.Meta;
namespace CyPhyMdaoAddOn
{
[Guid(ComponentConfig.guid),
ProgId(ComponentConfig.progID),
ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]
public class CyPhyMdaoAddOnAddon : IMgaComponentEx, IGMEVersionInfo, IMgaEventSink, IDisposable
{
private const string BADGE_COMMON_STYLE = "block:inline;display: inline;padding-left: 6px;margin-left: 6px;padding-right: 6px; margin-right: 6px; width: 60px; text-align: center;";
private const string ERROR_BADGE = "<div style=\"" + BADGE_COMMON_STYLE +
" background-color: rgb(255, 145, 145);color: rgb(255, 0, 0);\">Error</div>";
private const string WARNING_BADGE = "<div style=\"" + BADGE_COMMON_STYLE +
" background-color: rgb(255, 255, 79);color: rgb(255, 112, 0);\">Warning</div>";
private const string INFO_BADGE = "<div style=\"" + BADGE_COMMON_STYLE +
" background-color: rgb(205, 227, 255);color: rgb(0, 51, 255);\">Info</div>";
private MgaAddOn addon;
private bool componentEnabled = true;
private GMEConsole GMEConsole { get; set; }
// Event handlers for addons
#region MgaEventSink members
public void GlobalEvent(globalevent_enum @event)
{
if (@event == globalevent_enum.GLOBALEVENT_CLOSE_PROJECT)
{
Marshal.FinalReleaseComObject(addon);
addon = null;
}
else if (@event == globalevent_enum.APPEVENT_XML_IMPORT_BEGIN)
{
isXMLImportInProgress = true;
}
else if (@event == globalevent_enum.APPEVENT_XML_IMPORT_END)
{
isXMLImportInProgress = false;
}
if (!componentEnabled)
{
return;
}
//TODO: Handle global events
//MessageBox.Show(@event.ToString());
}
/// <summary>
/// Called when an FCO or folder changes
/// </summary>
/// <param name="subject">the object the event(s) happened to</param>
/// <param name="eventMask">objectevent_enum values ORed together</param>
/// <param name="param">extra information provided for cetertain event types</param>
public void ObjectEvent(MgaObject subject, uint eventMask, object param)
{
if (!componentEnabled)
{
return;
}
if (subject.HasReadOnlyAccess())
{
return;
}
if (GMEConsole == null)
{
GMEConsole = GMEConsole.CreateFromProject(subject.Project);
}
// Warning: Only those events are received that you have subscribed for by setting ComponentConfig.eventMask
uint uOBJEVENT_CREATED = 0;
unchecked { uOBJEVENT_CREATED = (uint)objectevent_enum.OBJEVENT_CREATED; }
uint uOBJEVENT_ATTR = (uint)objectevent_enum.OBJEVENT_ATTR;
uint uOBJEVENT_NEWCHILD = (uint)objectevent_enum.OBJEVENT_NEWCHILD;
uint uOBJEVENT_LOSTCHILD = (uint)objectevent_enum.OBJEVENT_LOSTCHILD;
uint uOBJEVENT_OPENMODEL = (uint)objectevent_enum.OBJEVENT_OPENMODEL;
uint uOBJEVENT_DESTROYED = (uint)objectevent_enum.OBJEVENT_DESTROYED;
if ((eventMask & uOBJEVENT_CREATED) != 0)
{
if (subject.IsLibObject || subject.IsWritable == false)
return;
if (subject.MetaBase.Name == "TestBenchRef")
{
if (subject as MgaReference != null)
{
UpdateColor((subject as MgaReference).Referred);
}
}
else if (subject.MetaBase.Name == "Optimizer")
{
(subject as MgaFCO).RegistryValue["portLabelLength"] = "0";
}
else if (subject.MetaBase.Name == "VariableSweep")
{
(subject as MgaFCO).RegistryValue["dstStyle"] = "arrow";
(subject as MgaFCO).RegistryValue["color"] = "0xff80c0";
}
else if (subject.MetaBase.Name == "ObjectiveMapping")
{
(subject as MgaFCO).RegistryValue["dstStyle"] = "arrow";
(subject as MgaFCO).RegistryValue["color"] = "0x7cadde";
}
}
else if ((eventMask & uOBJEVENT_ATTR) != 0)
{
this.DisplayPCCIterations(subject, true);
if ((eventMask & uOBJEVENT_DESTROYED) == 0)
{
this.SwapParameterStudyCode(subject, param);
}
if (subject.MetaBase.Name == "ValueFlow")
{
// set the src custom formula's name to the specified name
// in the ValueFlow attribute field
MgaSimpleConnection ValueFlow = subject as MgaSimpleConnection;
var src = ValueFlow.Src;
if (src.Meta.Name == "CustomFormula")
{
foreach (MgaAttribute attr in ValueFlow.Attributes)
{
if (attr.Meta.Name == "FormulaVariableName")
{
if (string.IsNullOrEmpty(attr.StringValue) == false)
{
src.Name = attr.StringValue;
break;
}
}
}
}
}
else if (subject.MetaBase.Name == "TestBench")
{
// Excel import helper
MgaModel testBench = subject as MgaModel;
// call color helper
UpdateColor(testBench as MgaFCO);
}
}
else if ((eventMask & uOBJEVENT_NEWCHILD) != 0 || (eventMask & uOBJEVENT_LOSTCHILD) != 0)
{
this.DisplayPCCIterations(subject, false);
}
else if ((eventMask & uOBJEVENT_OPENMODEL) != 0)
{
}
//MessageBox.Show(eventMask.ToString());
}
private void SwapParameterStudyCode(MgaObject subject, object param)
{
if (isXMLImportInProgress)
{
return;
}
string reg_name = "/code_by_type";
if (subject.MetaBase.Name != "ParameterStudy")
{
return;
}
MgaFCO fco = (MgaFCO)subject;
if (param is object[])
{
object[] attrs = (object[])param;
string prev_DOEType = null;
string prev_Code = null;
for (int i = 0; i + 1< attrs.Length; i += 2)
{
if (attrs[i] is string && attrs[i + 1] is string)
{
string name = (string)attrs[i];
string prev_value = (string)attrs[i + 1];
string prefix = "ATTR:";
if (name.StartsWith(prefix))
{
name = name.Substring(prefix.Length);
if (name == "DOEType")
{
prev_DOEType = prev_value;
}
if (name == "Code")
{
prev_Code = prev_value;
}
}
}
}
Dictionary<string, string> codeByType = new Dictionary<string, string>();
try
{
codeByType = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(fco.GetRegistryValueDisp(reg_name) ?? "{}");
}
catch (Newtonsoft.Json.JsonException ex)
{
}
if (prev_Code != null && prev_DOEType == null)
{
codeByType[fco.GetStrAttrByNameDisp("DOEType")] = fco.GetStrAttrByNameDisp("Code");
}
if (prev_DOEType != null)
{
codeByType[prev_DOEType] = prev_Code ?? fco.GetStrAttrByNameDisp("Code");
string Code;
if (codeByType.TryGetValue(fco.GetStrAttrByNameDisp("DOEType"), out Code)
&& Code != fco.GetStrAttrByNameDisp("Code"))
{
fco.SetStrAttrByNameDisp("Code", Code);
}
}
string serialized = Newtonsoft.Json.JsonConvert.SerializeObject(codeByType);
if (serialized != fco.GetRegistryValueDisp(reg_name))
{
fco.SetRegistryValueDisp(reg_name, serialized);
}
}
}
private void DisplayPCCIterations(MgaObject subject, bool attributeChanged)
{
List<string> kinds = new List<string>()
{
"PCCDriver",
"PCCParameterBeta",
"PCCParameterUniform",
"PCCParameterLNormal",
"PCCParameterNormal"
};
if (kinds.Contains(subject.MetaBase.Name) == false)
{
return;
}
MgaModel PCCDriver = null;
if (attributeChanged && subject.MetaBase.Name == "PCCDriver")
{
// TODO: Compute and
PCCDriver = subject as MgaModel;
}
else if (attributeChanged == false && subject.MetaBase.Name == "PCCDriver")
{
// Compute and prin
//GME.MGA.Meta.objtype_enum type;
//MgaObject parent;
//(subject as MgaFCO).GetParent(out parent, out type);
PCCDriver = subject as MgaModel;
}
else
{
return;
}
//Compute and print PCCDriver
var nbrOfParameters = PCCDriver.ChildFCOs.Cast<MgaFCO>().Where(x => kinds.Contains(x.Meta.Name)).Count();
var saMethod = PCCDriver.StrAttrByName["PCC_SA_Methods"];
var upMethod = PCCDriver.StrAttrByName["PCC_UP_Methods"];
GMEConsole.Out.WriteLine("{2}Iterations : UP : {0}, SA : {1}",
this.GetNumberOfIterations(upMethod, nbrOfParameters),
this.GetNumberOfIterations(saMethod, nbrOfParameters),
INFO_BADGE);
}
private void UpdateColor(MgaFCO subject)
{
// default values
string shadowThickness = "12";
string itemShadowCast = "true";
string shadowColor = "0x666666";
string color = "0xc0c0c0";
string borderColor = "0x000000";
if (subject != null)
{
if (subject.Meta.Name == "TestBench")
{
string Type = subject.StrAttrByName["Type"];
if (Type == "Excel")
{
color = "0x98fb98";
borderColor = "0x005500";
}
else if (Type == "DynSim")
{
color = "0x7d9ec0";
borderColor = "0x00008b";
}
else if (Type == "SimpleCalc")
{
color = "0xffad5b";
borderColor = "0xa52a00";
}
}
subject.RegistryValue["color"] = color;
subject.RegistryValue["borderColor"] = borderColor;
subject.RegistryValue["shadowThickness"] = shadowThickness;
subject.RegistryValue["itemShadowCast"] = itemShadowCast;
subject.RegistryValue["shadowColor"] = shadowColor;
}
}
private int GetNumberOfIterations(string method, int nbrOfInputs, int fixedIterations = 1280)
{
if (nbrOfInputs == 0)
{
return 0;
}
int nodeDefault = 5;
int MI = 4;
switch (method)
{
case "UP_MCS": // 1
return fixedIterations;
case "UP_TS": // 2
return 1 + 2 * nbrOfInputs;
case "UP_MPP": // 3
return -1;
case "UP_FFNI": // 4
return (int)Math.Pow(5, nbrOfInputs) * nbrOfInputs;
case "UP_UDR": // 5
return nodeDefault * nbrOfInputs + 1;
case "UP_PCE": // 6
return (int)Math.Pow(5, nbrOfInputs);
case "SA_SOBOL": // 7
return 2 * fixedIterations + nbrOfInputs * fixedIterations;
case "SA_FAST": // 9
int OMAX = 0;
switch (nbrOfInputs)
{
case 1:
return -1;
case 2:
OMAX = 9;
break;
case 3:
OMAX = 15;
break;
default:
OMAX = this.OMEGA[nbrOfInputs - 1];
for (int i = 1; i < nbrOfInputs; i++)
{
OMAX = OMAX + this.DN[nbrOfInputs - 1 - i];
}
break;
}
return 2 * MI * OMAX + 1;
case "SA_EFAST": // 10:
int OMi = (int)Math.Floor((double)((fixedIterations - 1) / (2 * MI) / nbrOfInputs));
return nbrOfInputs * (2 * MI * OMi + 1);
}
return 0;
}
public readonly int[] OMEGA = {0, 3, 1, 5, 11, 1, 17, 23, 19, 25, 41, 31,
23, 87, 67, 73, 85, 143, 149, 99, 119, 237, 267, 283, 151, 385, 157,
215, 449, 163, 337, 253, 375, 441, 673, 773, 875, 873, 587, 849, 623,
637, 891, 943, 1171, 1225, 1335, 1725, 1663, 2019};
public readonly int[] DN = { 4, 8, 6, 10, 20, 22, 32, 40, 38, 26, 56, 62,
46, 76, 96, 60, 86, 126, 134, 112, 92, 128, 154, 196, 34, 416, 106, 208,
328, 198, 382, 88, 348, 186, 140, 170, 284, 568, 302, 438, 410, 248, 448,
388, 596, 216, 100, 488, 166, 0 };
#endregion
#region IMgaComponentEx Members
public void Initialize(MgaProject p)
{
// Creating addon
p.CreateAddOn(this, out addon);
// Setting event mask (see ComponentConfig.eventMask)
unchecked
{
addon.EventMask = (uint)ComponentConfig.eventMask;
}
}
public void InvokeEx(MgaProject project, MgaFCO currentobj, MgaFCOs selectedobjs, int param)
{
throw new NotImplementedException(); // Not called by addon
}
#region Component Information
public string ComponentName
{
get { return GetType().Name; }
}
public string ComponentProgID
{
get
{
return ComponentConfig.progID;
}
}
public componenttype_enum ComponentType
{
get { return ComponentConfig.componentType; }
}
public string Paradigm
{
get { return ComponentConfig.paradigmName; }
}
#endregion
#region Enabling
bool enabled = true;
public void Enable(bool newval)
{
enabled = newval;
}
#endregion
#region Interactive Mode
protected bool interactiveMode = true;
public bool InteractiveMode
{
get
{
return interactiveMode;
}
set
{
interactiveMode = value;
}
}
#endregion
#region Custom Parameters
SortedDictionary<string, object> componentParameters = null;
private bool isXMLImportInProgress = false;
public object get_ComponentParameter(string Name)
{
if (Name == "type")
return "csharp";
if (Name == "path")
return GetType().Assembly.Location;
if (Name == "fullname")
return GetType().FullName;
object value;
if (componentParameters != null && componentParameters.TryGetValue(Name, out value))
{
return value;
}
return null;
}
public void set_ComponentParameter(string Name, object pVal)
{
if (componentParameters == null)
{
componentParameters = new SortedDictionary<string, object>();
}
componentParameters[Name] = pVal;
}
#endregion
#region Unused Methods
// Old interface, it is never called for MgaComponentEx interfaces
public void Invoke(MgaProject Project, MgaFCOs selectedobjs, int param)
{
throw new NotImplementedException();
}
// Not used by GME
public void ObjectsInvokeEx(MgaProject Project, MgaObject currentobj, MgaObjects selectedobjs, int param)
{
throw new NotImplementedException();
}
#endregion
#endregion
#region IMgaVersionInfo Members
public GMEInterfaceVersion_enum version
{
get { return GMEInterfaceVersion_enum.GMEInterfaceVersion_Current; }
}
#endregion
#region Registration Helpers
[ComRegisterFunctionAttribute]
public static void GMERegister(Type t)
{
Registrar.RegisterComponentsInGMERegistry();
}
[ComUnregisterFunctionAttribute]
public static void GMEUnRegister(Type t)
{
Registrar.UnregisterComponentsInGMERegistry();
}
#endregion
public void Dispose()
{
if (addon != null)
{
addon.Destroy();
addon = null;
}
}
}
}