Example #1
0
        public void TestChangeNotifications()
        {
            Model model = new Model("Test Model", Guid.NewGuid());
            BasicReactionSupporter brs = new BasicReactionSupporter();

            InitializeForTesting(brs);

            MaterialType waterType = (MaterialType)brs.MyMaterialCatalog["Water"];

            string    results = string.Empty;
            Substance w1      = (Substance)waterType.CreateMass(100, 37);

            w1.MaterialChanged += new MaterialChangeListener(delegate(IMaterial material, MaterialChangeType type) {
                results += string.Format("{0} changed in {1}\r\n", material, type);
            });

            w1.Temperature = 85.0;
            w1.Add((Substance)waterType.CreateMass(100, 37));
            Assert.IsTrue(results.Equals(RESULT1));

            results = string.Empty;
            w1.SuspendChangeEvents();
            w1.Temperature = 95.0;
            w1.Add((Substance)waterType.CreateMass(100, 37));
            w1.ResumeChangeEvents(false);
            Assert.IsTrue(results.Equals(string.Empty));

            results = string.Empty;
            w1.SuspendChangeEvents();
            w1.Temperature = 95.0;
            w1.Add((Substance)waterType.CreateMass(100, 37));
            w1.ResumeChangeEvents(true);
            Assert.IsTrue(results.Equals(RESULT2));

            // NOW, SAME TEST, BUT ON A MIXTURE INSTEAD.
            MaterialType acetoneType = (MaterialType)brs.MyMaterialCatalog["Acetone"];

            Mixture m1 = new Mixture("My Goo");

            m1.MaterialChanged += new MaterialChangeListener(delegate(IMaterial material, MaterialChangeType type) {
                results += string.Format("{0} changed in {1}\r\n", material, type);
            });

            results = string.Empty;
            m1.AddMaterial((Substance)waterType.CreateMass(100, 37));
            m1.AddMaterial((Substance)acetoneType.CreateMass(100, 45));
            Assert.IsTrue(results.Equals(RESULT3));

            results = string.Empty;
            m1.SuspendChangeEvents();
            m1.AddMaterial((Substance)waterType.CreateMass(100, 99));
            m1.AddMaterial((Substance)acetoneType.CreateMass(100, 99));
            Assert.IsTrue(results.Equals(string.Empty));
            m1.ResumeChangeEvents(false);
            Assert.IsTrue(results.Equals(string.Empty));

            results = string.Empty;
            m1.SuspendChangeEvents();
            m1.AddMaterial((Substance)waterType.CreateMass(100, 99));
            m1.AddMaterial((Substance)acetoneType.CreateMass(100, 99));
            Assert.IsTrue(results.Equals(string.Empty));
            m1.ResumeChangeEvents(true);
            Assert.IsTrue(results.Equals(RESULT4));
        }
Example #2
0
        public void TestMaterialTransferrer()
        {
            #region Create and initialize the model
            Model model = new Model("Test Model", Guid.NewGuid());
            BasicReactionSupporter brs = new BasicReactionSupporter();
            InitializeForTesting(brs);
            #endregion Create and initialize the model

            #region Create handles to material types
            MaterialType potassiumSulfateType = (MaterialType)brs.MyMaterialCatalog["Potassium Sulfate"];
            MaterialType acetoneType          = (MaterialType)brs.MyMaterialCatalog["Acetone"];
            MaterialType hexaneType           = (MaterialType)brs.MyMaterialCatalog["Hexane"];
            MaterialType waterType            = (MaterialType)brs.MyMaterialCatalog["Water"];
            #endregion Create handles to material types

            #region Create the source mixture
            Mixture from = new Mixture("From");
            from.AddMaterial(potassiumSulfateType.CreateMass(100, 37));
            from.AddMaterial(acetoneType.CreateMass(100, 37));
            from.AddMaterial(hexaneType.CreateMass(100, 37));
            #endregion Create the source mixture

            #region Create the destination mixture
            Mixture to = new Mixture("To");
            to.AddMaterial(waterType.CreateMass(55, 50));
            #endregion Create the destination mixture

            #region Create the mixture that will act as the exemplar
            Mixture what = new Mixture("What");
            what.AddMaterial(potassiumSulfateType.CreateMass(25, 37));
            what.AddMaterial(acetoneType.CreateMass(35, 37));
            what.AddMaterial(hexaneType.CreateMass(45, 37));
            #endregion Create the mixture that will act as the exemplar

            TimeSpan            duration = TimeSpan.FromHours(2.0);
            MaterialTransferrer mxfr     = new MaterialTransferrer(model, ref from, ref to, what, duration);

            // Create the start event for the transfer.
            DateTime startAt = new DateTime(2009, 2, 23, 3, 0, 0);
            model.Executive.RequestEvent(delegate(IExecutive exec, object userData) { mxfr.Start(); }, startAt, 0.0, null);

            // Create a pause-til-start event.
            model.Executive.RequestEvent(delegate(IExecutive exec, object userData) {
                Console.WriteLine("Waiting for start.");
                mxfr.BlockTilStart();
                Console.WriteLine("Start has occurred!");
            }, startAt - TimeSpan.FromHours(2.0), 0.0, null, ExecEventType.Detachable);

            // Create a pause-til-done event.
            model.Executive.RequestEvent(delegate(IExecutive exec, object userData) {
                Console.WriteLine("Waiting for finish.");
                mxfr.BlockTilDone();
                Console.WriteLine("Finish has occurred!");
            }, startAt - TimeSpan.FromHours(2.0), 0.0, null, ExecEventType.Detachable);


            // Create a series of (unrelated) events that will force updates during the transfer.
            DateTime update = startAt;
            while (update <= (startAt + duration))
            {
                model.Executive.RequestEvent(delegate(IExecutive exec, object userData) { Console.WriteLine("{2} : From = {0}\r\nTo = {1}", from, to, exec.Now); }, update, 0.0, null);
                update += TimeSpan.FromMinutes(20.0);
            }

            model.Start();
        }
Example #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MaterialResourceRequest"/> class.
 /// </summary>
 /// <param name="byWhom">The identity of the entity making the reqest.</param>
 /// <param name="mt">The Material Type being requested.</param>
 /// <param name="quantity">The quantity of Material being requested.</param>
 /// <param name="direction">The <see cref="MaterialResourceRequest.Direction "/> of the request - Augment or Deplete.</param>
 public MaterialResourceRequest(IHasIdentity byWhom, MaterialType mt, double quantity, Direction direction)
     : this(byWhom, mt, null, quantity, direction)
 {
 }
Example #4
0
 public MaterialResourceItem(IModel model, MaterialType mt, double initialQuantity, double initialTemp, double initialCapacity, Guid guid)
     : this(model, "Material Resource Item : " + mt.Name, guid, mt, initialQuantity, initialTemp, initialCapacity, null)
 {
 }
Example #5
0
        /// <summary>
        /// Removes the specified mass from this substance.
        /// </summary>
        /// <param name="mass">The mass.</param>
        /// <returns>Substance.</returns>
        /// <exception cref="WriteProtectionViolationException">Fired if there is a write lock on the substance.</exception>
        public virtual Substance Remove(double mass)
        {
            if (!m_writeLock.IsWritable)
            {
                throw new WriteProtectionViolationException(this, m_writeLock);
            }

            Substance s = (Substance)MaterialType.CreateMass(mass, Temperature);

            if (m_mass == float.MaxValue)
            {
                if (m_materialSpecs != null)
                {
                    // First figure out the total mass...
                    double totalMass = m_materialSpecs.Cast <DictionaryEntry>().Sum(de => (double)de.Value);

                    if (totalMass > 0.0)
                    {
                        foreach (DictionaryEntry de in m_materialSpecs)
                        {
                            if (s.m_materialSpecs == null)
                            {
                                s.m_materialSpecs = new Hashtable();
                            }
                            s.m_materialSpecs.Add(de.Key, mass * ((double)de.Value) / totalMass);
                        }
                    }
                }
                return(s);
            }
            else
            {
                double    pctRemoved = mass / m_mass;
                Hashtable ht         = new Hashtable();
                if (m_materialSpecs != null)
                {
                    foreach (DictionaryEntry de in m_materialSpecs)
                    {
                        if (s.m_materialSpecs == null)
                        {
                            s.m_materialSpecs = new Hashtable();
                        }
                        if (s.m_materialSpecs.ContainsKey(de.Key))
                        {
                            s.m_materialSpecs[de.Key] = pctRemoved * ((double)de.Value);
                        }
                        else
                        {
                            s.m_materialSpecs.Add(de.Key, pctRemoved * ((double)de.Value));
                        }
                        if (pctRemoved < 1.0)
                        {
                            ht.Add(de.Key, (1.0 - pctRemoved) * ((double)de.Value));
                        }
                    }
                }
                m_materialSpecs = ht;
            }

            double have  = Energy;
            double minus = s.Energy;

            m_mass -= s.Mass; // Have to set Mass before setting energy, since energy->temp requires mass.
            Energy  = (have - minus);

            m_ssh.ReportChange();
            MaterialChanged?.Invoke(this, MaterialChangeType.Contents);
            return(s);
        }
Example #6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MaterialResourceRequest"/> class.
 /// </summary>
 /// <param name="mt">The Material Type being requested.</param>
 /// <param name="quantity">The quantity of Material being requested.</param>
 /// <param name="direction">The <see cref="MaterialResourceRequest.Direction "/> of the request - Augment or Deplete.</param>
 public MaterialResourceRequest(MaterialType mt, double quantity, Direction direction)
     : this(null, mt, null, quantity, direction)
 {
 }
Example #7
0
 /// <summary>
 /// Determines whether this MaterialCatalog contains the specified MaterialType.
 /// Different instances of Material Types are considered equal if they have the same name and guid.
 /// </summary>
 /// <param name="mt">The MaterialType instance.</param>
 /// <returns><c>true</c> if this MaterialCatalog contains the specified MaterialType; otherwise, <c>false</c>.</returns>
 public bool Contains(MaterialType mt)
 {
     return(m_materialTypesByGuid.ContainsValue(mt) && m_materialTypesByName.ContainsValue(mt));
 }
Example #8
0
 /// <summary>
 /// Adds a conduit that allows this resource manager to draw from, and push to, another resource manager
 /// that handles a specified material. The other resource manager is seen as providing a source for the
 /// material if this resource manager cannot fulfill a request, and provides a destination to absorb any
 /// more material than can be absorbed by a material resource item in this resource manager.
 /// </summary>
 /// <param name="secondary">The resource manager that provides over/underflow support.</param>
 /// <param name="mt">The material type for which this conduit applies.</param>
 public void AddConduit(IResourceManager secondary, MaterialType mt)
 {
     m_conduits.Add(mt, secondary);
 }
Example #9
0
 private static bool LiquidOrUnknownOnly(MaterialType mt)
 {
     return(mt.STPState.Equals(MaterialState.Liquid) || mt.STPState.Equals(MaterialState.Unknown));
 }
Example #10
0
 private static bool LiquidOnly(MaterialType mt)
 {
     return(mt.STPState.Equals(MaterialState.Liquid));
 }
Example #11
0
 private static bool AcceptAll(MaterialType mt)
 {
     return(true);
 }
Example #12
0
 /// <summary>
 /// Defines a reactant in this reaction.
 /// </summary>
 /// <param name="material">The material type that is to be the reactant.</param>
 /// <param name="mass">The mass of reactant that participates in one unit of the reaction.</param>
 public void AddReactant(MaterialType material, double mass)
 {
     m_reactants.Add(new ReactionParticipant(material, mass));
     IdentifyCatalysts();
 }
Example #13
0
 /// <summary>
 /// Reconstitutes this object from the specified XmlSerializationContext.
 /// </summary>
 /// <param name="xmlsc">The specified XmlSerializationContext.</param>
 public void DeserializeFrom(XmlSerializationContext xmlsc)
 {
     m_type = (MaterialType)xmlsc.LoadObject("MaterialType");
     m_mass = (double)xmlsc.LoadObject("Mass");
 }