示例#1
0
        public void Initialize(MyProjectRunner projectRunner)
        {
            try
            {
                projectRunner.OpenProject(BrainFileName, strict: true);

                List <MyNode> brainUnitNodes = projectRunner.Filter(node => (node.GetType().Name == "BrainUnitNode"));
                if (brainUnitNodes.Count != 1)
                {
                    throw new InvalidTestException("Exactly 1 occurrence of BrainUnitNode required.");  // TODO: allow more
                }
                m_brainUnitNode = brainUnitNodes[0];

                PropertyInfo maxStepCountProperty = m_brainUnitNode.GetType().GetProperty("MaxStepCount", typeof(int));
                MaxStepCount = (int)maxStepCountProperty.GetValue(m_brainUnitNode);

                PropertyInfo inspectIntervalProperty = m_brainUnitNode.GetType().GetProperty("InspectInterval", typeof(int));
                InspectInterval = (int)inspectIntervalProperty.GetValue(m_brainUnitNode);

                PropertyInfo expectedToFailProperty = m_brainUnitNode.GetType().GetProperty("ExpectedToFail", typeof(bool));
                ExpectedToFail = (bool)expectedToFailProperty.GetValue(m_brainUnitNode);
            }
            catch (Exception e)
            {
                MyLog.ERROR.WriteLine(
                    "Failed to instantiate test {0}: {1}", Path.GetFileName(BrainFileName), e.Message);
                throw;
            }
        }
示例#2
0
        protected MyMemoryBlock <float> GetMemBlock(int nodeId, string blockName)
        {
            MyNode                n     = Project.GetNodeById(nodeId);
            PropertyInfo          mem   = n.GetType().GetProperty(blockName);
            MyMemoryBlock <float> block = mem.GetValue(n, null) as MyMemoryBlock <float>;

            return(block);
        }
示例#3
0
        public static MyNodeView CreateNodeView(MyNode node, GraphControl owner)
        {
            MyNodeView nodeView = CreateNodeView(node.GetType(), owner);

            nodeView.Node = node;

            return(nodeView);
        }
示例#4
0
        private void UpdateTitleAndButtons()
        {
            if (Target is MyNode)
            {
                MyNode node = Target as MyNode;
                nodeNameTextBox.Rtf = @"{\rtf1\ansi \b " + node.Name + @"\b0  - " + node.GetType().Name + "}";
            }
            else if (Target is MyAbstractObserver)
            {
                MyAbstractObserver observer = Target as MyAbstractObserver;
                nodeNameTextBox.Rtf = @"{\rtf1\ansi \b " + observer.TargetIdentifier + @"\b0  - " + observer.GetType().Name + "}";

                snapshotButton.Checked = observer.AutosaveSnapshop;
            }
            else
            {
                nodeNameTextBox.Rtf = "";
            }

            CanEdit            = m_mainForm.SimulationHandler.State == MySimulationHandler.SimulationState.STOPPED;
            helpButton.Enabled = Target is MyWorkingNode || Target is MyAbstractObserver;

            snapshotButton.Enabled = Target is MyAbstractObserver;

            if (Target is MyWorkingNode)
            {
                saveNodeDataButton.Enabled = true;
                saveNodeDataButton.Checked = (Target as MyWorkingNode).SaveOnStop;

                loadNodeDataButton.Enabled = true;
                loadNodeDataButton.Checked = (Target as MyWorkingNode).LoadOnStart;

                clearDataButton.Enabled = true;
            }
            else
            {
                saveNodeDataButton.Enabled = false;
                saveNodeDataButton.Checked = false;

                loadNodeDataButton.Enabled = false;
                loadNodeDataButton.Checked = false;

                clearDataButton.Enabled = false;
            }
        }
示例#5
0
        private void UpdateTitleAndButtons()
        {
            if (HasMultipleTargets)
            {
                nodeNameTextBox.Rtf = @"{\rtf1\ansi \b " + Targets?.Length + @" \b0 nodes selected.}";
            }
            else if (Target is MyNode)
            {
                MyNode node = Target as MyNode;
                nodeNameTextBox.Rtf = @"{\rtf1\ansi \b " + node.Name + @"\b0  - " + node.GetType().Name + "}";
            }
            else if (Target is MyAbstractObserver)
            {
                MyAbstractObserver observer = Target as MyAbstractObserver;
                nodeNameTextBox.Rtf = @"{\rtf1\ansi \b " + observer.TargetIdentifier + @"\b0  - " + observer.GetType().Name + "}";

                snapshotButton.Checked = observer.AutosaveSnapshop;
            }
            else
            {
                nodeNameTextBox.Rtf = "";
            }

            CanEdit = m_mainForm.SimulationHandler.State == MySimulationHandler.SimulationState.STOPPED;

            // TODO(Premek): Allow help for multiple nodes of the same type.
            helpButton.Enabled = !HasMultipleTargets && (Target is MyWorkingNode || Target is MyAbstractObserver);

            snapshotButton.Enabled = !HasMultipleTargets && (Target is MyAbstractObserver);

            // TODO(Premek): Allow to set SaveOnStop / LoadOnStart for multiple nodes.
            var workingNode   = !HasMultipleTargets ? (Target as MyWorkingNode) : null;
            var isWorkingNode = (workingNode != null);

            saveNodeDataButton.Enabled = isWorkingNode;
            saveNodeDataButton.Checked = workingNode?.SaveOnStop ?? false;

            loadNodeDataButton.Enabled = isWorkingNode;
            loadNodeDataButton.Checked = workingNode?.LoadOnStart ?? false;

            clearDataButton.Enabled = isWorkingNode;

            dashboardButton.Enabled = !HasMultipleTargets && (Target is MyNode);
        }
示例#6
0
        private object InvokeNodeMethod(string name)
        {
            if (m_brainUnitNode == null)
            {
                throw new InvalidOperationException("Test not initialized, BrainUnitNode instance not set.");
            }

            MethodInfo checkMethod = m_brainUnitNode.GetType().GetMethod(name);

            try
            {
                return(checkMethod.Invoke(m_brainUnitNode, new object[0]));
            }
            catch (TargetInvocationException e)
            {
                Exception innerException = e.InnerException ?? e;
                throw innerException;  // TODO: recover the original stack trace (or at least file and line)
            }
        }
        private string GenerateVariablesCode()
        {
            StringBuilder defs = new StringBuilder();
            StringBuilder init = new StringBuilder();

            init.AppendLine("public static void __InitGeneratedVariables(MyCSharpNodeGroup owner) { ");

            if (GenerateVariables)
            {
                foreach (string nodeIdentifier in m_nodeVariables.Keys)
                {
                    MyNode node         = GetChildNodeById(m_nodeVariables[nodeIdentifier]);
                    string nodeTypeName = node.GetType().FullName.Replace('+', '.');

                    if (node != null)
                    {
                        defs.AppendLine("static " + nodeTypeName + " " + nodeIdentifier + ";");
                        init.AppendLine(nodeIdentifier + " = (" + nodeTypeName + ")owner.GetChildNodeById(" + node.Id + ");");
                    }
                }

                foreach (string taskIdentifier in m_taskVariables.Keys)
                {
                    MyWorkingNode wNode = (MyWorkingNode)GetChildNodeById(m_taskVariables[taskIdentifier].Item1);

                    MyTask task = wNode.GetTaskByPropertyName(m_taskVariables[taskIdentifier].Item2);

                    if (task != null)
                    {
                        string taskTypeName = task.GetType().FullName.Replace('+', '.');
                        defs.AppendLine("static " + taskTypeName + " " + taskIdentifier + ";");
                        init.AppendLine(taskIdentifier + " = (" + taskTypeName + ")((MyWorkingNode)owner.GetChildNodeById(" + wNode.Id + ")).GetTaskByPropertyName(\"" + m_taskVariables[taskIdentifier].Item2 + "\");");
                    }
                }
            }

            init.AppendLine("}");

            return(defs.ToString() + init.ToString());
        }
示例#8
0
        public static MyNodeView CreateNodeView(MyNode node, GraphControl owner)
        {
            MyNodeView nodeView = CreateNodeView(node.GetType(), owner);
            nodeView.Node = node;

            return nodeView;
        }
 public static string GetNodeFolder(MyNode node)
 {
     return(node.GetType().Name + "_" + node.Id);
 }
 public static string GetNodeFolder(MyNode node)
 {
     return node.GetType().Name + "_" + node.Id;
 }
示例#11
0
 public static DashboardNodeProperty CreateNodeProperty(MyNode node, string propertyName)
 {
     // This is a node property holding a task. Task groups and simple tasks have to be distinguished.
     return(new DashboardNodeProperty(node, node.GetType().GetProperty(propertyName)));
 }
        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;
            }
        }
 public static DashboardNodeProperty CreateNodeProperty(MyNode node, string propertyName)
 {
     // This is a node property holding a task. Task groups and simple tasks have to be distinguished.
     return new DashboardNodeProperty(node, node.GetType().GetProperty(propertyName));
 }