コード例 #1
0
        public static WorkspaceModel WorkspaceFromJSON(string file)
        {
            string json = File.ReadAllText(file);
            //this amazing little portion constructs a DYN from JSON.
            var wm = WorkspaceModel.FromJson(json, theDAM.DynView.Model.LibraryServices,
                                             null,
                                             null,
                                             theDAM.DynView.Model.NodeFactory,
                                             true,
                                             true,
                                             theDAM.DynView.Model.CustomNodeManager);

            return(wm);
        }
コード例 #2
0
        public void ReadConverterDoesNotThrowWithNullEngineAndScheduler()
        {
            CurrentDynamoModel.AddHomeWorkspace();
            var json = CurrentDynamoModel.CurrentWorkspace.ToJson(null);

            Assert.DoesNotThrow(() =>
            {
                WorkspaceModel.FromJson(
                    json, this.CurrentDynamoModel.LibraryServices,
                    null,
                    null,
                    this.CurrentDynamoModel.NodeFactory,
                    true,
                    true,
                    this.CurrentDynamoModel.CustomNodeManager);
            });
        }
コード例 #3
0
        private bool InitializeCustomNode(
            WorkspaceInfo workspaceInfo,
            XmlDocument xmlDoc,
            out CustomNodeWorkspaceModel workspace)
        {
            // Add custom node definition firstly so that a recursive
            // custom node won't recursively load itself.
            SetPreloadFunctionDefinition(Guid.Parse(workspaceInfo.ID));
            string jsonDoc;
            CustomNodeWorkspaceModel newWorkspace = null;

            if (xmlDoc is XmlDocument)
            {
                var nodeGraph = NodeGraph.LoadGraphFromXml(xmlDoc, nodeFactory);
                newWorkspace = new CustomNodeWorkspaceModel(
                    nodeFactory,
                    nodeGraph.Nodes,
                    nodeGraph.Notes,
                    nodeGraph.Annotations,
                    nodeGraph.Presets,
                    nodeGraph.ElementResolver,
                    workspaceInfo);
            }
            else
            {
                Exception ex;
                if (DynamoUtilities.PathHelper.isValidJson(workspaceInfo.FileName, out jsonDoc, out ex))
                {
                    //we pass null for engine and scheduler as apparently the custom node constructor doesn't need them.
                    newWorkspace          = (CustomNodeWorkspaceModel)WorkspaceModel.FromJson(jsonDoc, this.libraryServices, null, null, nodeFactory, true, this);
                    newWorkspace.FileName = workspaceInfo.FileName;
                    newWorkspace.Category = workspaceInfo.Category;
                    // Mark the custom node workspace as having no changes - when we set the category on the above line
                    // this marks the workspace as changed.
                    newWorkspace.HasUnsavedChanges = false;
                }
            }

            RegisterCustomNodeWorkspace(newWorkspace);
            workspace = newWorkspace;
            return(true);
        }
コード例 #4
0
        private void DoWorkspaceOpenAndCompare(string filePath, string dirName,
                                               Func <DynamoModel, string, string> saveFunction,
                                               Action <serializationTestUtils.WorkspaceComparisonData, serializationTestUtils.WorkspaceComparisonData, Dictionary <Guid, String> > workspaceCompareFunction,
                                               Action <serializationTestUtils.WorkspaceComparisonData, string, TimeSpan, Dictionary <Guid, string> > workspaceDataSaveFunction)
        {
            var openPath = filePath;

            if (bannedTests.Any(t => filePath.Contains(t)))
            {
                Assert.Inconclusive("Skipping test known to kill the test framework...");
            }

            OpenModel(openPath);

            var model = CurrentDynamoModel;
            var ws1   = model.CurrentWorkspace;

            ws1.Description = "TestDescription";

            var dummyNodes = ws1.Nodes.Where(n => n is DummyNode);

            if (dummyNodes.Any())
            {
                Assert.Inconclusive("The Workspace contains dummy nodes for: " + string.Join(",", dummyNodes.Select(n => n.Name).ToArray()));
            }

            var cbnErrorNodes = ws1.Nodes.Where(n => n is CodeBlockNodeModel && n.State == ElementState.Error);

            if (cbnErrorNodes.Any())
            {
                Assert.Inconclusive("The Workspace contains code block nodes in error state due to which rest " +
                                    "of the graph will not execute; skipping test ...");
            }

            if (((HomeWorkspaceModel)ws1).RunSettings.RunType == Models.RunType.Manual)
            {
                RunCurrentModel();
            }

            var wcd1 = new serializationTestUtils.WorkspaceComparisonData(ws1, CurrentDynamoModel.EngineController);

            var dirPath = Path.Combine(Path.GetTempPath(), dirName);

            if (!Directory.Exists(dirPath))
            {
                Directory.CreateDirectory(dirPath);
            }
            var fi           = new FileInfo(filePath);
            var filePathBase = dirPath + @"\" + Path.GetFileNameWithoutExtension(fi.Name);

            serializationTestUtils.ConvertCurrentWorkspaceToDesignScriptAndSave(filePathBase, CurrentDynamoModel);

            string json = saveFunction(model, filePathBase);

            workspaceDataSaveFunction(wcd1, filePathBase, lastExecutionDuration, modelsGuidToIdMap);

            lastExecutionDuration = new TimeSpan();

            var ws2 = WorkspaceModel.FromJson(json, model.LibraryServices,
                                              model.EngineController, model.Scheduler, model.NodeFactory, DynamoModel.IsTestMode, false,
                                              model.CustomNodeManager);

            if (ws2 is CustomNodeWorkspaceModel)
            {
                model.AddCustomNodeWorkspace((CustomNodeWorkspaceModel)ws2);
            }

            foreach (var c in ws2.Connectors)
            {
                Assert.NotNull(c.Start.Owner, "The node is not set for the start of connector " + c.GUID + ". The end node is " + c.End.Owner + ".");
                Assert.NotNull(c.End.Owner, "The node is not set for the end of connector " + c.GUID + ". The start node is " + c.Start.Owner + ".");
            }

            // The following logic is taken from the DynamoModel.Open method.
            // It assumes a single home workspace model. So, we remove all
            // others, before adding a new one.
            if (ws2 is HomeWorkspaceModel)
            {
                var currentHomeSpaces = model.Workspaces.OfType <HomeWorkspaceModel>().ToList();
                if (currentHomeSpaces.Any())
                {
                    var end = ws2 is HomeWorkspaceModel ? 0 : 1;

                    for (var i = currentHomeSpaces.Count - 1; i >= end; i--)
                    {
                        model.RemoveWorkspace(currentHomeSpaces[i]);
                    }
                }

                model.AddWorkspace(ws2);

                var hws = ws2 as HomeWorkspaceModel;
                if (hws != null)
                {
                    model.ResetEngine();

                    if (hws.RunSettings.RunType == RunType.Periodic)
                    {
                        hws.StartPeriodicEvaluation();
                    }
                }

                model.CurrentWorkspace = ws2;
            }

            Assert.NotNull(ws2);

            dummyNodes = ws2.Nodes.Where(n => n is DummyNode);
            if (dummyNodes.Any())
            {
                Assert.Inconclusive("The Workspace contains dummy nodes for: " + string.Join(",", dummyNodes.Select(n => n.Name).ToArray()));
            }

            var wcd2 = new serializationTestUtils.WorkspaceComparisonData(ws2, CurrentDynamoModel.EngineController);

            workspaceCompareFunction(wcd1, wcd2, modelsGuidToIdMap);

            var functionNodes = ws2.Nodes.Where(n => n is Function).Cast <Function>();

            if (functionNodes.Any())
            {
                Assert.True(functionNodes.All(n => CurrentDynamoModel.CustomNodeManager.LoadedDefinitions.Contains(n.Definition)));
            }

            foreach (var c in ws2.Connectors)
            {
                Assert.NotNull(c.Start.Owner);
                Assert.NotNull(c.End.Owner);
                Assert.True(ws2.Nodes.Contains(c.Start.Owner));
                Assert.True(ws2.Nodes.Contains(c.End.Owner));
            }

            //assert that the inputs in the saved json file are the same as those we can gather from the
            //graph at runtime - because we don't deserialize these directly we check the json itself.
            //Use load vs parse to preserve date time strings.
            var jsonReader = new JsonTextReader(new StringReader(json));

            jsonReader.DateParseHandling = DateParseHandling.None;
            var jObject = JObject.Load(jsonReader);

            var jToken  = jObject["Inputs"];
            var inputs  = jToken.ToArray().Select(x => x.ToObject <NodeInputData>()).ToList();
            var inputs2 = ws1.Nodes.Where(x => x.IsSetAsInput == true && x.InputData != null).Select(input => input.InputData).ToList();

            //inputs2 might come from a WS with non guids, so we need to replace the ids with guids if they exist in the map
            foreach (var input in inputs2)
            {
                if (modelsGuidToIdMap.ContainsKey(input.Id))
                {
                    input.Id = GuidUtility.Create(GuidUtility.UrlNamespace, modelsGuidToIdMap[input.Id]);
                }
            }
            Assert.IsTrue(inputs.SequenceEqual(inputs2));

            //assert that the outputs in the saved json file are the same as those we can gather from the
            //graph at runtime - because we don't deserialize these directly we check the json itself.
            var jTokenOutput = jObject["Outputs"];
            var outputs      = jTokenOutput.ToArray().Select(x => x.ToObject <NodeOutputData>()).ToList();
            var outputs2     = ws1.Nodes.Where(x => x.IsSetAsOutput == true && x.OutputData != null).Select(output => output.OutputData).ToList();

            //Outputs2 might come from a WS with non guids, so we need to replace the ids with guids if they exist in the map
            foreach (var output in outputs2)
            {
                if (modelsGuidToIdMap.ContainsKey(output.Id))
                {
                    output.Id = GuidUtility.Create(GuidUtility.UrlNamespace, modelsGuidToIdMap[output.Id]);
                }
            }
            Assert.IsTrue(outputs.SequenceEqual(outputs2));
        }
コード例 #5
0
        private void DoWorkspaceOpenAndCompare(string filePath,
                                               Func <DynamoModel, string, string> saveFunction,
                                               Action <WorkspaceComparisonData, WorkspaceComparisonData> workspaceCompareFunction)
        {
            var openPath = filePath;

            if (bannedTests.Any(t => filePath.Contains(t)))
            {
                Assert.Inconclusive("Skipping test known to kill the test framework...");
            }

            OpenModel(openPath);

            var model = CurrentDynamoModel;
            var ws1   = model.CurrentWorkspace;

            ws1.Description = "TestDescription";

            var dummyNodes = ws1.Nodes.Where(n => n is DummyNode);

            if (dummyNodes.Any())
            {
                Assert.Inconclusive("The Workspace contains dummy nodes for: " + string.Join(",", dummyNodes.Select(n => n.Name).ToArray()));
            }

            var cbnErrorNodes = ws1.Nodes.Where(n => n is CodeBlockNodeModel && n.State == ElementState.Error);

            if (cbnErrorNodes.Any())
            {
                Assert.Inconclusive("The Workspace contains code block nodes in error state due to which rest " +
                                    "of the graph will not execute; skipping test ...");
            }

            if (((HomeWorkspaceModel)ws1).RunSettings.RunType == Models.RunType.Manual)
            {
                RunCurrentModel();
            }

            var wcd1 = new WorkspaceComparisonData(ws1, CurrentDynamoModel.EngineController);

            var dirPath = Path.Combine(Path.GetTempPath(), "json");

            if (!Directory.Exists(dirPath))
            {
                Directory.CreateDirectory(dirPath);
            }
            var fi           = new FileInfo(filePath);
            var filePathBase = dirPath + @"\" + Path.GetFileNameWithoutExtension(fi.Name);

            ConvertCurrentWorkspaceToDesignScriptAndSave(filePathBase);

            string json = saveFunction(model, filePathBase);

            SaveWorkspaceComparisonData(wcd1, filePathBase, lastExecutionDuration);

            lastExecutionDuration = new TimeSpan();

            var ws2 = WorkspaceModel.FromJson(json, model.LibraryServices,
                                              model.EngineController, model.Scheduler, model.NodeFactory, DynamoModel.IsTestMode, false,
                                              model.CustomNodeManager);

            if (ws2 is CustomNodeWorkspaceModel)
            {
                model.AddCustomNodeWorkspace((CustomNodeWorkspaceModel)ws2);
            }

            foreach (var c in ws2.Connectors)
            {
                Assert.NotNull(c.Start.Owner, "The node is not set for the start of connector " + c.GUID + ". The end node is " + c.End.Owner + ".");
                Assert.NotNull(c.End.Owner, "The node is not set for the end of connector " + c.GUID + ". The start node is " + c.Start.Owner + ".");
            }

            // The following logic is taken from the DynamoModel.Open method.
            // It assumes a single home workspace model. So, we remove all
            // others, before adding a new one.
            if (ws2 is HomeWorkspaceModel)
            {
                var currentHomeSpaces = model.Workspaces.OfType <HomeWorkspaceModel>().ToList();
                if (currentHomeSpaces.Any())
                {
                    var end = ws2 is HomeWorkspaceModel ? 0 : 1;

                    for (var i = currentHomeSpaces.Count - 1; i >= end; i--)
                    {
                        model.RemoveWorkspace(currentHomeSpaces[i]);
                    }
                }

                model.AddWorkspace(ws2);

                var hws = ws2 as HomeWorkspaceModel;
                if (hws != null)
                {
                    model.ResetEngine();

                    if (hws.RunSettings.RunType == RunType.Periodic)
                    {
                        hws.StartPeriodicEvaluation();
                    }
                }

                model.CurrentWorkspace = ws2;
            }

            Assert.NotNull(ws2);

            dummyNodes = ws2.Nodes.Where(n => n is DummyNode);
            if (dummyNodes.Any())
            {
                Assert.Inconclusive("The Workspace contains dummy nodes for: " + string.Join(",", dummyNodes.Select(n => n.Name).ToArray()));
            }

            var wcd2 = new WorkspaceComparisonData(ws2, CurrentDynamoModel.EngineController);

            workspaceCompareFunction(wcd1, wcd2);

            var functionNodes = ws2.Nodes.Where(n => n is Function).Cast <Function>();

            if (functionNodes.Any())
            {
                Assert.True(functionNodes.All(n => CurrentDynamoModel.CustomNodeManager.LoadedDefinitions.Contains(n.Definition)));
            }

            foreach (var c in ws2.Connectors)
            {
                Assert.NotNull(c.Start.Owner);
                Assert.NotNull(c.End.Owner);
                Assert.True(ws2.Nodes.Contains(c.Start.Owner));
                Assert.True(ws2.Nodes.Contains(c.End.Owner));
            }
        }