Exemplo n.º 1
0
        public PlacingNewStream(UI.ControlPalette sender, DrawingCanvas canvas, StreamType streamType)
        {
            // Quick error check on parameters
            if (null == sender || null == canvas)
            {
                throw new ArgumentNullException();
            }
            if (StreamType.Chemical != streamType && StreamType.Heat != streamType)
            {
                // To save us from problems with future changes
                throw new NotImplementedException();
            }

            m_palette = sender;
            m_canvas  = canvas;

            // Create the stream (data structure, not control). Take care to pick an ID that's not
            // already in use.
            int newID;

            do
            {
                newID = AbstractStream.GetNextUID();
            }while (canvas.GetWorkspace().StreamExists(newID));
            if (StreamType.Chemical == streamType)
            {
                m_stream = new ChemicalStream(newID);
            }
            else
            {
                m_stream = new HeatStream(newID);
            }

            // Create the table
            m_stream.PropertiesTable = new StreamPropertiesTable(m_stream);

            // Create the default row if we have a heat stream. There is no default row
            // for chemical streams
            if (StreamType.Heat == streamType)
            {
                // Add a default row if it's not already there
                if (0 == m_stream.PropertiesTable.RowCount)
                {
                    m_stream.PropertiesTable.AddNewRow();
                }

                // Choose a default label
                m_stream.PropertiesTable.Rows[0].Label = "Q" + m_stream.Id.ToString();

                // Select default units
                (m_stream.PropertiesTable.Rows[0] as HeatStreamData).SelectedUnits =
                    HeatStreamData.EnergyUnitOptions[0];

                // Flag it as not renamed by the user yet
                m_stream.PropertiesTable.Rows[0].UserHasRenamed = false;
            }

            // Create the source placement icon and add it to the canvas
            m_sourcePlacementIcon = new Image();
            Uri         uri = new Uri("/UI/Icons/pu_source.png", UriKind.Relative);
            ImageSource img = new System.Windows.Media.Imaging.BitmapImage(uri);

            m_sourcePlacementIcon.SetValue(Image.SourceProperty, img);
            m_canvas.AddNewChild(m_sourcePlacementIcon);
            m_sourcePlacementIcon.SetValue(Canvas.ZIndexProperty, 4);
        }
Exemplo n.º 2
0
        public void TestSaveLoad()
        {
            int       i;
            Workspace ws1  = new Workspace();
            Random    rand = new Random();

            // Add a random number of process units
            List <int> puIDs = new List <int>();
            int        numPU = rand.Next(25);

            for (i = 0; i < numPU; i++)
            {
                AbstractProcessUnit pu = new Mixer();
                ws1.AddProcessUnit(pu);
                puIDs.Add(pu.Id);
            }

            // Add a random number of chemical streams
            int numStreams = rand.Next(10);

            for (i = 0; i < numStreams; i++)
            {
                AbstractStream stream = new ChemicalStream(AbstractStream.GetNextUID());
                ws1.AddStream(stream);

                // Don't forget that the properties table needs to be created separately
                stream.PropertiesTable = new StreamPropertiesTable(stream);

                // 50% chance of connecting a destination (attempting a connect that is)
                if (0 == (rand.Next() % 2))
                {
                    AbstractProcessUnit pu = ws1.ProcessUnits[rand.Next(ws1.ProcessUnitCount)];
                    if (pu.AttachIncomingStream(stream))
                    {
                        stream.Destination = pu;
                    }
                }
            }

            // Save the workspace to a memory stream
            MemoryStream ms = new MemoryStream();

            ws1.Save(ms, "TEST_VersionNA");

            // Load to a new workspace
            Workspace ws2 = new Workspace();

            ws2.Load(ms);

            // Make sure the number of process units and streams match
            Assert.IsTrue(numPU == ws2.ProcessUnitCount,
                          "Number of process units between saved document (" + numPU.ToString() +
                          ") and loaded document (" + ws2.ProcessUnitCount.ToString() + ") do not match");
            Assert.IsTrue(numStreams == ws2.Streams.Count,
                          "Number of streams between saved document (" + numStreams.ToString() +
                          ") and loaded document (" + ws2.Streams.Count.ToString() + ") do not match");

            // Test that the incoming/outgoing streams for process units match
            foreach (AbstractProcessUnit pu1 in ws1.ProcessUnits)
            {
                AbstractProcessUnit pu2 = ws2.GetProcessUnit(pu1.Id);
                Assert.IsNotNull(pu2,
                                 "Process unit with ID=" + pu1.Id.ToString() + " from workspace 1 (saved) was not " +
                                 "found in workspace 2 (loaded).");

                // For now just compare outoging stream count
                Assert.IsTrue(pu1.OutgoingStreamCount == pu2.OutgoingStreamCount,
                              "Mis-match outgoing stream count");
            }
        }