Esempio n. 1
0
        /// <summary>
        /// Loads project from file
        /// </summary>
        /// <param name="path">Path to .brain/.brainz file</param>
        public void OpenProject(string path)
        {
            MyLog.INFO.WriteLine("Loading project: " + path);

            string content;

            try
            {
                string newProjectName = MyProject.MakeNameFromPath(path);

                content = ProjectLoader.LoadProject(path,
                                                    MyMemoryBlockSerializer.GetTempStorage(newProjectName));

                using (MyMemoryManager.Backup backup = MyMemoryManager.GetBackup())
                {
                    Project = MyProject.Deserialize(content, Path.GetDirectoryName(path));
                    backup.Forget();
                }

                Project.FileName = path;
            }
            catch (Exception e)
            {
                MyLog.ERROR.WriteLine("Project loading failed: " + e.Message);
                throw;
            }
        }
Esempio n. 2
0
        private bool OpenProject(string fileName)
        {
            MyLog.INFO.WriteLine("--------------");
            MyLog.INFO.WriteLine("Loading project: " + fileName);
            try
            {
                TextReader reader  = new StreamReader(fileName);
                string     content = reader.ReadToEnd();
                reader.Close();

                Project = MyProject.Deserialize(content, Path.GetDirectoryName(fileName));

                Properties.Settings.Default.LastProject = fileName;
                saveFileDialog.FileName      = fileName;
                m_savedProjectRepresentation = content;

                CloseAllGraphLayouts();
                CloseAllObservers();

                CreateNetworkView();
                OpenGraphLayout(Project.Network);

                if (Project.World != null)
                {
                    SelectWorldInWorldList(Project.World);
                }

                if (Project.Observers != null)
                {
                    foreach (MyAbstractObserver observer in Project.Observers)
                    {
                        observer.RestoreTargetFromIdentifier(Project);

                        if (observer.GenericTarget != null)
                        {
                            ShowObserverView(observer);
                        }
                    }
                }
                Project.Observers = null;

                exportStateButton.Enabled = MyMemoryBlockSerializer.TempDataExists(Project);
                clearDataButton.Enabled   = exportStateButton.Enabled;

                Text = TITLE_TEXT + " - " + Project.Name;
                return(true);
            }
            catch (Exception e)
            {
                MyLog.ERROR.WriteLine("Project loading failed: " + e.Message);
                return(false);
            }
        }
Esempio n. 3
0
        private bool ImportProject(string fileName, bool showObservers = false)
        {
            MyLog.INFO.WriteLine("Importing project: " + fileName);
            try
            {
                TextReader reader  = new StreamReader(fileName);
                string     content = reader.ReadToEnd();
                reader.Close();

                MyProject importedProject = MyProject.Deserialize(content, Path.GetDirectoryName(fileName));

                //offset all imported nodes
                float maxY = NetworkView.Desktop.GetContentBounds().Bottom;
                foreach (var node in importedProject.Network.Children)
                {
                    node.Location.Y += maxY + 10.0f;
                }

                if (showObservers && importedProject.Observers != null)
                {
                    foreach (MyAbstractObserver observer in importedProject.Observers)
                    {
                        observer.RestoreTargetFromIdentifier(importedProject);

                        if (observer.GenericTarget != null)
                        {
                            ShowObserverView(observer);
                        }
                    }
                }

                Project.Network.AppendGroupContent(importedProject.Network);

                if (showObservers && importedProject.Observers != null)
                {
                    foreach (MyAbstractObserver observer in importedProject.Observers)
                    {
                        observer.UpdateTargetIdentifier();
                    }
                }

                NetworkView.ReloadContent();
                NetworkView.Desktop.ZoomToBounds();

                return(true);
            }
            catch (Exception e)
            {
                MyLog.ERROR.WriteLine("Project import failed: " + e.Message);
                return(false);
            }
        }
Esempio n. 4
0
        public void SerializesAndDeserializesCorrectly()
        {
            // I.e. deserialized(serialized(PROJECT)) should equal PROJECT
            string tmpPath = Path.GetTempPath();

            MyConfiguration.SetupModuleSearchPath();

            MyConfiguration.LoadModules();

            MyConfiguration.KnownNodes.Add(typeof(TestNode), new MyNodeConfig());

            var project = new MyProject();

            project.Network      = project.CreateNode <MyNetwork>();
            project.Network.Name = "Network";
            project.CreateWorld(typeof(MyTestingWorld));
            project.Name = "test";
            var node = project.CreateNode <TestNode>();

            project.Network.AddChild(node);
            project.Restore();

            string    serialized          = project.Serialize(tmpPath);
            MyProject deserializedProject = MyProject.Deserialize(serialized, tmpPath);

            // MaxDifferences = 20 - A magic number. It shows more than one difference in the log.
            // There should eventually be zero differences, so this number can be arbitrary. Adjust as needed.
            // Observers are ignored - there are none, and the (de)serialization mechanism works with them in a special way.
            var compareLogic =
                new CompareLogic(new ComparisonConfig
            {
                MaxDifferences  = 20,
                MembersToIgnore = new List <string> {
                    "Observers"
                }
            });
            ComparisonResult result = compareLogic.Compare(project, deserializedProject);

            m_output.WriteLine(result.DifferencesString);

            Assert.True(result.AreEqual);
        }
        public void FileCanBeReadWhenSimulationIsNotRunning()
        {
            string       directory = Path.GetFullPath(@"Data\");
            const string fileName  = "csv_file_test.brain";

            m_outputFileFullPath = Path.GetTempFileName();
            var outputFileName      = Path.GetFileName(m_outputFileFullPath);
            var outputFileDirectory = Path.GetDirectoryName(m_outputFileFullPath);

            string projectPath = directory + fileName;

            var simulation = TypeMap.GetInstance <MySimulation>();

            // TODO(HonzaS): This should not be required!
            // The referenced assemblies get loaded only if a Type is required here. But since the serializer
            // is just looking for types by name, it doesn't force the runtime to load all of the assemblies.
            // In this case, we would miss the BasicNodes if the following line was deleted.
            // Two solutions: 1) Use the Managed Extensibility Framework or 2) load all referenced assemblies
            // explicitely (as a part of the BS testing framework).
            var csvNode = new MyCsvFileWriterNode();

            MyProject project = MyProject.Deserialize(File.ReadAllText(projectPath), Path.GetDirectoryName(projectPath));

            var handler = new MySimulationHandler(simulation)
            {
                Project = project
            };

            // The CSV node
            MyNode node = project.Network.GetChildNodeById(6);

            PropertyInfo fileNameProperty = node.GetType().GetProperty("OutputFile", BindingFlags.Instance | BindingFlags.Public);

            fileNameProperty.SetValue(node, outputFileName);

            PropertyInfo directoryProperty = node.GetType().GetProperty("OutputDirectory", BindingFlags.Instance | BindingFlags.Public);

            directoryProperty.SetValue(node, outputFileDirectory);

            handler.UpdateMemoryModel();

            handler.StateChanged += StateChanged;

            try
            {
                handler.StartSimulation();
                m_continueEvent.WaitOne();

                Assert.Throws <IOException>(() =>
                {
                    File.Open(m_outputFileFullPath, FileMode.Open, FileAccess.ReadWrite);
                });

                // Every time we change simulation state, the StateChanged method gets notified.
                // We're changing the state several times here and the StateChanged methods checks that
                // the file that the Csv node uses is available for writing.

                // First, go through start->pause->stop.
                handler.PauseSimulation();
                m_continueEvent.WaitOne();

                handler.StopSimulation();
                m_continueEvent.WaitOne();

                // Now, try start->stop only.
                handler.StartSimulation();
                m_continueEvent.WaitOne();

                handler.StopSimulation();
                m_continueEvent.WaitOne();

                // The file should have been successfully opened three times - every time the simulation was paused or stopped.
                Assert.Equal(3, m_openCount);
            }
            finally
            {
                handler.Finish();
                File.Delete(m_outputFileFullPath);
                m_outputFileFullPath = null;
            }
        }