Skip to content

Commit

Permalink
refactor, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
aparajit-pratap committed Nov 22, 2023
1 parent 2d96ffd commit 97d5987
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 173 deletions.
5 changes: 5 additions & 0 deletions src/DynamoCore/Engine/EngineController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Dynamo.Graph.Workspaces;
using Dynamo.Logging;
using Dynamo.Scheduler;
using Dynamo.Utilities;
using ProtoCore.AST.AssociativeAST;
using ProtoCore.DSASM.Mirror;
using ProtoCore.Mirror;
Expand Down Expand Up @@ -44,6 +45,8 @@ public class EngineController : LogSourceBase, IAstNodeContainer, IDisposable
/// </summary>
internal static event Action VMLibrariesReset;

internal Version WorkspaceVersion { get; set; }

/// <summary>
/// This event is fired when <see cref="UpdateGraphAsyncTask"/> is completed.
/// </summary>
Expand Down Expand Up @@ -153,6 +156,8 @@ public EngineController(LibraryServices libraryServices, string geometryFactoryF
syncDataManager = new SyncDataManager();

VerboseLogging = verboseLogging;

WorkspaceVersion = AssemblyHelper.GetDynamoVersion();
}

/// <summary>
Expand Down
18 changes: 0 additions & 18 deletions src/DynamoCore/Graph/Workspaces/CustomNodeWorkspaceModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,23 +315,5 @@ public override void Save(string newPath, bool isBackup = false, EngineControlle

base.Save(newPath, isBackup, engine);
}

[Obsolete("Method will be deprecated in Dynamo 3.0.")]
protected override bool PopulateXmlDocument(XmlDocument document)
{
if (!base.PopulateXmlDocument(document))
return false;

var root = document.DocumentElement;
if (root == null)
return false;

var guid = CustomNodeDefinition != null ? CustomNodeDefinition.FunctionId : Guid.NewGuid();
root.SetAttribute("ID", guid.ToString());
root.SetAttribute("Description", Description);
root.SetAttribute("Category", Category);

return true;
}
}
}
19 changes: 1 addition & 18 deletions src/DynamoCore/Graph/Workspaces/HomeWorkspaceModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -664,24 +664,7 @@ internal void StopPeriodicEvaluation()
}

#endregion

[Obsolete("Method will be deprecated in Dynamo 3.0.")]
protected override bool PopulateXmlDocument(XmlDocument document)
{
if (!base.PopulateXmlDocument(document))
return false;

var root = document.DocumentElement;
if (root == null)
return false;

root.SetAttribute("RunType", RunSettings.RunType.ToString());
root.SetAttribute("RunPeriod", RunSettings.RunPeriod.ToString(CultureInfo.InvariantCulture));
root.SetAttribute("HasRunWithoutCrash", HasRunWithoutCrash.ToString(CultureInfo.InvariantCulture));

return true;
}


private void PulseMakerRunStarted()
{
var nodesToUpdate = Nodes.Where(n => n.CanUpdatePeriodically);
Expand Down
43 changes: 26 additions & 17 deletions src/DynamoCore/Graph/Workspaces/SerializationConverters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Dynamo.Library;
using Dynamo.Linting;
using Dynamo.Logging;
using Dynamo.Models;
using Dynamo.Properties;
using Dynamo.Scheduler;
using Dynamo.Utilities;
Expand Down Expand Up @@ -679,30 +680,38 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
#region Restore trace data
// Trace Data
Dictionary<Guid, List<CallSite.RawTraceData>> loadedTraceData = new Dictionary<Guid, List<CallSite.RawTraceData>>();
bool containsTraceData = false;
bool containsLegacyTraceData = false;
// Restore trace data if bindings are present in json
if (obj["Bindings"] != null && obj["Bindings"].Children().Count() > 0)
{
containsTraceData = true;

JEnumerable<JToken> bindings = obj["Bindings"].Children();
var wrc = (WorkspaceReadConverter)serializer.Converters.First(c => c is WorkspaceReadConverter);

// Iterate through bindings to extract nodeID's and bindingData (callsiteId & traceData)
foreach (JToken entity in bindings)
if (wrc.engine.WorkspaceVersion < new Version(3, 0, 0))
{
Guid nodeId = Guid.Parse(entity["NodeId"].ToString());
string bindingString = entity["Binding"].ToString();

// Key(callsiteId) : Value(traceData)
Dictionary<string, string> bindingData = JsonConvert.DeserializeObject<Dictionary<string, string>>(bindingString);
List<CallSite.RawTraceData> callsiteTraceData = new List<CallSite.RawTraceData>();
containsLegacyTraceData = true;
}
else
{
JEnumerable<JToken> bindings = obj["Bindings"].Children();

foreach (KeyValuePair<string, string> pair in bindingData)
// Iterate through bindings to extract nodeID's and bindingData (callsiteId & traceData)
foreach (JToken entity in bindings)
{
callsiteTraceData.Add(new CallSite.RawTraceData(pair.Key, pair.Value));
}
Guid nodeId = Guid.Parse(entity["NodeId"].ToString());
string bindingString = entity["Binding"].ToString();

loadedTraceData.Add(nodeId, callsiteTraceData);
// Key(callsiteId) : Value(traceData)
Dictionary<string, string> bindingData =
JsonConvert.DeserializeObject<Dictionary<string, string>>(bindingString);
List<CallSite.RawTraceData> callsiteTraceData = new List<CallSite.RawTraceData>();

foreach (KeyValuePair<string, string> pair in bindingData)
{
callsiteTraceData.Add(new CallSite.RawTraceData(pair.Key, pair.Value));
}

loadedTraceData.Add(nodeId, callsiteTraceData);
}
}
}
#endregion
Expand Down Expand Up @@ -751,7 +760,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
if (obj.TryGetValue(nameof(WorkspaceModel.Author), StringComparison.OrdinalIgnoreCase, out JToken author))
ws.Author = author.ToString();

ws.ContainsTraceData = containsTraceData;
ws.ContainsLegacyTraceData = containsLegacyTraceData;

return ws;
}
Expand Down
94 changes: 1 addition & 93 deletions src/DynamoCore/Graph/Workspaces/WorkspaceModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ internal int CurrentPasteOffset
}
}

internal bool ContainsTraceData { get; set; }
internal bool ContainsLegacyTraceData { get; set; }

internal bool ScaleFactorChanged = false;

Expand Down Expand Up @@ -1282,9 +1282,6 @@ public Rect2D Rect
get { return new Rect2D(x, y, width, height); }
}

//TODO(Steve): This probably isn't needed inside of WorkspaceModel -- MAGN-5714
internal Version WorkspaceVersion { get; set; }

/// <summary>
/// Implements <see cref="ILocatable.CenterX"/> property.
/// </summary>
Expand Down Expand Up @@ -1374,7 +1371,6 @@ protected WorkspaceModel(
IsReadOnly = DynamoUtilities.PathHelper.IsReadOnlyPath(fileName);
LastSaved = DateTime.Now;

WorkspaceVersion = AssemblyHelper.GetDynamoVersion();
undoRecorder = new UndoRedoRecorder(this);

NodeFactory = factory;
Expand Down Expand Up @@ -2064,94 +2060,6 @@ private void SerializeElementResolver(XmlDocument xmlDoc)
root.AppendChild(mapElement);
}

[Obsolete("Method will be deprecated in Dynamo 3.0.")]
protected virtual bool PopulateXmlDocument(XmlDocument xmlDoc)
{
try
{
var root = xmlDoc.DocumentElement;
root.SetAttribute("Version", WorkspaceVersion.ToString());
root.SetAttribute("X", X.ToString(CultureInfo.InvariantCulture));
root.SetAttribute("Y", Y.ToString(CultureInfo.InvariantCulture));
root.SetAttribute("ScaleFactor", ScaleFactor.ToString(CultureInfo.InvariantCulture));
root.SetAttribute("Name", Name);
root.SetAttribute("Description", Description);

SerializeElementResolver(xmlDoc);

var elementList = xmlDoc.CreateElement("Elements");
//write the root element
root.AppendChild(elementList);

foreach (var dynEl in Nodes.Select(el => el.Serialize(xmlDoc, SaveContext.Save)))
elementList.AppendChild(dynEl);

//write only the output connectors
var connectorList = xmlDoc.CreateElement("Connectors");
//write the root element
root.AppendChild(connectorList);

foreach (var el in Nodes)
{
foreach (var port in el.OutPorts)
{
foreach (
var c in
port.Connectors.Where(c => c.Start != null && c.End != null))
{
var connector = xmlDoc.CreateElement(c.GetType().ToString());
connectorList.AppendChild(connector);
connector.SetAttribute("start", c.Start.Owner.GUID.ToString());
connector.SetAttribute("start_index", c.Start.Index.ToString());
connector.SetAttribute("end", c.End.Owner.GUID.ToString());
connector.SetAttribute("end_index", c.End.Index.ToString());
connector.SetAttribute(nameof(ConnectorModel.IsHidden), c.IsHidden.ToString());

if (c.End.PortType == PortType.Input)
connector.SetAttribute("portType", "0");
}
}
}

//save the notes
var noteList = xmlDoc.CreateElement("Notes"); //write the root element
root.AppendChild(noteList);
foreach (var n in Notes)
{
var note = n.Serialize(xmlDoc, SaveContext.Save);
noteList.AppendChild(note);
}

//save the annotation
var annotationList = xmlDoc.CreateElement("Annotations");
root.AppendChild(annotationList);
foreach (var n in annotations)
{
var annotation = n.Serialize(xmlDoc, SaveContext.Save);
annotationList.AppendChild(annotation);
}

//save the presets into the dyn file as a seperate element on the root
var presetsElement = xmlDoc.CreateElement("Presets");
root.AppendChild(presetsElement);
foreach (var preset in Presets)
{
var presetState = preset.Serialize(xmlDoc, SaveContext.Save);
presetsElement.AppendChild(presetState);
}

OnSaving(xmlDoc);

return true;
}
catch (Exception ex)
{
Log(ex.Message);
Log(ex.StackTrace);
return false;
}
}

internal void SendModelEvent(Guid modelGuid, string eventName, int value)
{
var retrievedModel = GetModelInternal(modelGuid);
Expand Down
18 changes: 0 additions & 18 deletions src/DynamoCore/Migration/Migration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -517,24 +517,6 @@ internal static Version VersionFromString(string version)
return new Version(ver.Major, ver.Minor, ver.Build, 0);
}

/// <summary>
/// Call this method to obtain the version of current WorkspaceModel.
/// Note that the revision number is dropped as both "0.7.0.1234"
/// should be treated as the same version as "0.7.0.5678", and no file
/// migration should take place.
/// </summary>
/// <param name="workspace">The WorkspaceModel to get the Version from.
/// </param>
/// <returns>Returns the Version object representing the workspace
/// version with the revision set to 0.</returns>
///
internal static Version VersionFromWorkspace(WorkspaceModel workspace)
{
// Ignore revision number.
var ver = workspace.WorkspaceVersion;
return new Version(ver.Major, ver.Minor, ver.Build, 0);
}

/// <summary>
/// Call this method to determine if migration should take place
/// for the input DYN/DYF file based on the given version numbers.
Expand Down
15 changes: 7 additions & 8 deletions src/DynamoCore/Models/DynamoModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2323,6 +2323,10 @@ private bool OpenJsonFile(
var currentHomeSpace = Workspaces.OfType<HomeWorkspaceModel>().FirstOrDefault();
currentHomeSpace.UndefineCBNFunctionDefinitions();

// This is to handle the case of opening a JSON file that does not have a version string
EngineController.WorkspaceVersion = dynamoPreferences.Version ==
null ? AssemblyHelper.GetDynamoVersion() : new Version(dynamoPreferences.Version);

// TODO, QNTM-1108: WorkspaceModel.FromJson does not check a schema and so will not fail as long
// as the fileContents are valid JSON, regardless of if all required data is present or not
workspace = WorkspaceModel.FromJson(
Expand All @@ -2339,15 +2343,10 @@ private bool OpenJsonFile(
workspace.FileName = string.IsNullOrEmpty(filePath) ? "" : filePath;
workspace.FromJsonGraphId = string.IsNullOrEmpty(filePath) ? WorkspaceModel.ComputeGraphIdFromJson(fileContents) : "";
workspace.ScaleFactor = dynamoPreferences.ScaleFactor;


// This is to handle the case of opening a JSON file that does not have a version string
workspace.WorkspaceVersion = dynamoPreferences.Version ==
null ? AssemblyHelper.GetDynamoVersion() : new Version(dynamoPreferences.Version);


if (!IsTestMode)
{
if (workspace.ContainsTraceData && workspace.WorkspaceVersion < new Version(3, 0, 0))
if (workspace.ContainsLegacyTraceData)
{
OnRequestNotification(Resources.LegacyTraceDataWarning, true);
}
Expand Down Expand Up @@ -3215,7 +3214,7 @@ public void ClearCurrentWorkspace()
//don't save the file path
CurrentWorkspace.FileName = "";
CurrentWorkspace.HasUnsavedChanges = false;
CurrentWorkspace.WorkspaceVersion = AssemblyHelper.GetDynamoVersion();
EngineController.WorkspaceVersion = AssemblyHelper.GetDynamoVersion();

this.LinterManager?.SetDefaultLinter();

Expand Down
Loading

0 comments on commit 97d5987

Please sign in to comment.