private void DoRollback(IResourceRequest request) { // ReSharper disable once PossibleUnintendedReferenceComparison (Intended reference comparison.) if (request.ResourceObtained != this) { throw new ResourceMismatchException(request, this, ResourceMismatchException.MismatchType.UnReserve); } Substance addBackIn = (Substance)((Substance)m_material).MaterialType.CreateMass(request.QuantityObtained, m_material.Temperature); // ReSharper disable once ConditionIsAlwaysTrueOrFalse Substance material = (Substance)m_material; //if ( material != null ) { material.Add(addBackIn); /*} else if ( m_material is Mixture ) { * ((Mixture)m_material).AddMaterial(addBackIn); * } else { * Debug.Assert(false,"Unknown IMaterial type : " + m_material.GetType().Name); * }*/ request.ResourceObtained = null; request.ResourceObtainedFrom = null; request.QuantityObtained = 0; }
public void TestMaterialSpecifications() { Model model = new Model("Test Model", Guid.NewGuid()); BasicReactionSupporter brs = new BasicReactionSupporter(); InitializeForTesting(brs); Guid city = Guid.NewGuid(); Guid dist = Guid.NewGuid(); Guid perr = Guid.NewGuid(); MaterialType waterType = (MaterialType)brs.MyMaterialCatalog["Water"]; Substance w1 = (Substance)waterType.CreateMass(100, 37); w1.SetMaterialSpec(city, 100); Debug.WriteLine("\r\nCreating 100 kg of City Water (" + city + ")."); DumpMaterialSpecs(w1); Substance w2 = (Substance)waterType.CreateMass(40, 37); w2.SetMaterialSpec(dist, 40); Debug.WriteLine("\r\nCreating 40 kg of Distilled Water (" + dist + ")."); DumpMaterialSpecs(w2); Debug.WriteLine("\r\nAdding city water to distilled water."); w1.Add(w2); DumpMaterialSpecs(w1); Debug.WriteLine("\r\nRemoving 60 kg of the blended water."); Substance w3 = w1.Remove(60); DumpMaterialSpecs(w1); DumpMaterialSpecs(w3); Debug.WriteLine("\r\nCloning the blended water."); Substance w4 = (Substance)w3.Clone(); DumpMaterialSpecs(w4); Debug.WriteLine("\r\nPurifying the cloned blended water."); w4.ConvertMaterialSpec(dist, city); Debug.WriteLine("Purified clone:"); DumpMaterialSpecs(w4); Debug.WriteLine("...and original water:"); DumpMaterialSpecs(w3); double amtDistilled = w1.GetMaterialSpec(dist); Debug.WriteLine("\r\nThere are " + amtDistilled + " kg of distilled water."); w1.ConvertMaterialSpec(dist, city); Debug.WriteLine("\r\nConverting distilled water to city water."); DumpMaterialSpecs(w1); }
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)); }