forked from DynamoDS/Dynamo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefineData.cs
More file actions
302 lines (256 loc) · 10.7 KB
/
Copy pathDefineData.cs
File metadata and controls
302 lines (256 loc) · 10.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Serialization;
using DSCore;
using Dynamo.Graph;
using Dynamo.Graph.Nodes;
using Newtonsoft.Json;
using ProtoCore.AST.AssociativeAST;
using VMDataBridge;
using static DSCore.Data;
namespace CoreNodeModels
{
[NodeName("DefineData")]
[NodeDescription(nameof(Properties.Resources.DefineDataDescription), typeof(Properties.Resources))]
[NodeCategory("Core.Data")]
[InPortNames(">")]
[InPortTypes("var[]..[]")]
[InPortDescriptions(typeof(Properties.Resources), nameof(Properties.Resources.DefineDataInputTooltip))]
[OutPortNames(">")]
[OutPortTypes("var[]..[]")]
[OutPortDescriptions(typeof(Properties.Resources), nameof(Properties.Resources.DefineDataOutputTooltip))]
[IsDesignScriptCompatible]
[AlsoKnownAs("Data.DefineData")]
public class DefineData : DSDropDownBase, IValueSchemaProvider
{
/// <inheritdoc/>
[JsonIgnore]
public string ValueTypeId
{
get
{
if (SelectedIndex < 0 || SelectedIndex >= Items.Count)
return SelectedString;
return (Items[SelectedIndex].Item as Data.DataNodeDynamoType)?.TypeId ?? SelectedString;
}
}
/// <inheritdoc/>
[JsonIgnore]
public bool IsListValue => IsList;
private bool isAutoMode = true; // default start with auto-detect 'on'
private bool isList;
private string displayValue = Properties.Resources.DefineDataDisplayValueMessage;
/// <summary>
/// The IsAutoMode property enables the node to automatically validate and process input data.
/// AutoMode = true: the node checks input types for serialization compatibility, supports single values and non-nested lists,
/// and distinguishes between homogeneous and certain heterogeneous collections through inheritance.
/// Invalid or unsupported data types result in error messages,
/// while successful validation updates node properties and UI elements to reflect the processed data.
/// AutoMode = false: the node enters a manual processing mode,
/// where it strictly validates that the TypeID and Context predefined on the node match the attached input data.
/// Mismatches in expected data types or contexts—such as receiving a list instead of a single item,
/// or input data not matching the specified TypeID—result in errors, without automatically adjusting node settings.
/// This manual mode maintains the node's current configurations, ensuring an output is passed only when valid data is processed,
/// and retains the node's state in warning without resetting selections for invalid data.
/// </summary>
[JsonProperty]
public bool IsAutoMode
{
get => isAutoMode;
set
{
isAutoMode = value;
OnNodeModified();
RaisePropertyChanged(nameof(IsAutoMode));
}
}
/// <summary>
/// IsList property defines if the input is of a type ArrayList.
/// The node supports only non-nested lists of homogeneous or heterogenous collections through inheritance
/// </summary>
[JsonProperty]
public bool IsList
{
get => isList;
set
{
isList = value;
OnNodeModified();
RaisePropertyChanged(nameof(IsList));
}
}
/// <summary>
/// This is a mediator property handling the displayed value on the dropdown
/// </summary>
///
[JsonProperty]
public string DisplayValue
{
get => displayValue;
set
{
if (displayValue != value)
{
displayValue = value;
RaisePropertyChanged(nameof(DisplayValue));
}
}
}
[JsonIgnore]
public override bool IsInputNode => true;
private string value = "";
[JsonProperty("InputValue")]
public virtual string Value
{
get
{
return value;
}
set
{
if (Equals(this.value, null) || !this.value.Equals(value))
{
this.value = value ?? "";
MarkNodeAsModified();
RaisePropertyChanged(nameof(Value));
}
}
}
/// <summary>
/// Construct a new DefineData Dropdown Menu node
/// </summary>
public DefineData() : base(">")
{
PropertyChanged += OnPropertyChanged;
foreach (var dataType in Data.DataNodeDynamoTypeList)
{
var displayName = dataType.Name;
var value = dataType;
Items.Add(new DynamoDropDownItem(displayName, value));
}
SelectedIndex = 0;
}
[JsonConstructor]
private DefineData(IEnumerable<PortModel> inPorts, IEnumerable<PortModel> outPorts) : base(">", inPorts, outPorts)
{
RegisterAllPorts();
PropertyChanged += OnPropertyChanged;
}
private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
}
protected override void OnBuilt()
{
base.OnBuilt();
DataBridge.Instance.RegisterCallback(GUID.ToString(), DataBridgeCallback);
}
public override void Dispose()
{
PropertyChanged -= OnPropertyChanged;
base.Dispose();
DataBridge.Instance.UnregisterCallback(GUID.ToString());
}
public override IEnumerable<AssociativeNode> BuildOutputAst(List<AssociativeNode> inputAstNodes)
{
var resultAst = new List<AssociativeNode>();
// function call inputs - reference to the function, and the function arguments coming from the inputs
// the object to be (type) evaluated
// the expected datatype
// if the input is an ArrayList or not
var function = new Func<object, string, bool, bool, string, Dictionary<string, object>>(EvaluateDefineDataNode);
var functionInputs = new List<AssociativeNode> {
inputAstNodes[0],
AstFactory.BuildStringNode((Items[SelectedIndex].Item as Data.DataNodeDynamoType).Type.ToString()),
AstFactory.BuildBooleanNode(IsList),
AstFactory.BuildBooleanNode(IsAutoMode),
AstFactory.BuildStringNode(Value)
};
var functionCall = AstFactory.BuildFunctionCall(function, functionInputs);
var functionCallIdentifier = AstFactory.BuildIdentifier(GUID + "_func");
resultAst.Add(AstFactory.BuildAssignment(functionCallIdentifier, functionCall));
// Next add the first key value pair to the output port
var safeExtractDictionaryValue = new Func<Dictionary<string, object>, string, object>(DSCore.Data.SafeExtractDictionaryValue);
var getFirstKey = AstFactory.BuildFunctionCall(safeExtractDictionaryValue,
[functionCallIdentifier, AstFactory.BuildStringNode(">")]);
resultAst.Add(AstFactory.BuildAssignment(GetAstIdentifierForOutputIndex(0), getFirstKey));
// Second get the key value pair to pass to the databridge callback
var getSecondKey = AstFactory.BuildFunctionCall(safeExtractDictionaryValue,
[functionCallIdentifier, AstFactory.BuildStringNode("Validation")]);
resultAst.Add(AstFactory.BuildAssignment(
AstFactory.BuildIdentifier(GUID + "_db"),
DataBridge.GenerateBridgeDataAst(GUID.ToString(), getSecondKey)));
return resultAst;
}
private void DataBridgeCallback(object data)
{
//Todo If the playerValue is not empty string then we can change the UI to reflect the value is coming from the player
//Todo if the function call throws we don't get back to DatabridgeCallback. Not sure if we need to handle this case
//Now we reset this value to empty string so that the next time a value is set from upstream nodes we can know that it is not coming from the player
value = "";
if (data == null)
{
if (IsAutoMode)
{
DisplayValue = string.Empty; // show blank if we are in locked mode (as we cannot interact with the node)
}
else
{
DisplayValue = SelectedString;
}
return;
}
// If data is not null
(bool IsValid, bool UpdateList, DataNodeDynamoType InputType) = (ValueTuple<bool, bool, DataNodeDynamoType>)data;
if (IsAutoMode)
{
if (UpdateList)
{
IsList = !IsList;
}
if (InputType != null)
{
if (!IsValid)
{
// Assign to the correct value, if the object was of supported type
var index = Items.IndexOf(Items.First(i => i.Name.Equals(InputType.Name)));
SelectedIndex = index;
}
if (!DisplayValue.Equals(InputType.Name))
{
DisplayValue = InputType.Name;
}
}
}
else
{
DisplayValue = SelectedString;
}
}
protected override SelectionState PopulateItemsCore(string currentSelection)
{
Items.Clear();
foreach (var dataType in Data.DataNodeDynamoTypeList)
{
var displayName = dataType.Name;
var value = dataType;
Items.Add(new DynamoDropDownItem(displayName, value));
}
SelectedIndex = 0;
return SelectionState.Restore;
}
protected override bool UpdateValueCore(UpdateValueParams updateValueParams)
{
string name = updateValueParams.PropertyName;
string value = updateValueParams.PropertyValue;
switch (name)
{
case "Value":
Value = value;
return true; // UpdateValueCore handled.
}
return base.UpdateValueCore(updateValueParams);
}
}
}