/// <summary> /// Creates a new instance of <see cref="FaultTreeIllustrationPoint"/> based on the /// information of <paramref name="hydraRingFaultTreeIllustrationPoint"/>. /// </summary> /// <param name="hydraRingFaultTreeIllustrationPoint">The <see cref="HydraRingFaultTreeIllustrationPoint"/> /// to base the <see cref="FaultTreeIllustrationPoint"/> to create on.</param> /// <returns>The newly created <see cref="FaultTreeIllustrationPoint"/>.</returns> /// <exception cref="ArgumentNullException">Thrown when <paramref name="hydraRingFaultTreeIllustrationPoint"/> /// is <c>null</c>.</exception> /// <exception cref="IllustrationPointConversionException">Thrown when the <paramref name="hydraRingFaultTreeIllustrationPoint"/> /// cannot be converted to a <see cref="FaultTreeIllustrationPoint"/>.</exception> public static FaultTreeIllustrationPoint Convert(HydraRingFaultTreeIllustrationPoint hydraRingFaultTreeIllustrationPoint) { if (hydraRingFaultTreeIllustrationPoint == null) { throw new ArgumentNullException(nameof(hydraRingFaultTreeIllustrationPoint)); } try { CombinationType combinationType = CombinationTypeConverter.Convert(hydraRingFaultTreeIllustrationPoint.CombinationType); IEnumerable <Stochast> stochasts = hydraRingFaultTreeIllustrationPoint.Stochasts .Select(StochastConverter.Convert) .ToArray(); return(new FaultTreeIllustrationPoint(hydraRingFaultTreeIllustrationPoint.Name, hydraRingFaultTreeIllustrationPoint.Beta, stochasts, combinationType)); } catch (Exception e) when(e is NotSupportedException || e is InvalidEnumArgumentException) { string errorMessage = $"Could not convert the {typeof(HydraRingCombinationType)} into a {typeof(CombinationType)}."; throw new IllustrationPointConversionException(errorMessage, e); } }
public void Convert_ValidArguments_ExpectedProperties() { // Setup var random = new Random(21); var hydraRingStochast = new HydraRingStochast("hydraRingStochast", random.NextDouble(), random.NextDouble()); var hydraRingFaultTreeIllustrationPoint = new HydraRingFaultTreeIllustrationPoint("name", random.NextDouble(), new[] { hydraRingStochast }, HydraRingCombinationType.And); // Call FaultTreeIllustrationPoint faultTreeIllustrationPoint = FaultTreeIllustrationPointConverter.Convert(hydraRingFaultTreeIllustrationPoint); // Assert Assert.AreEqual(hydraRingFaultTreeIllustrationPoint.Beta, faultTreeIllustrationPoint.Beta, faultTreeIllustrationPoint.Beta.GetAccuracy()); Assert.AreEqual(hydraRingFaultTreeIllustrationPoint.Name, faultTreeIllustrationPoint.Name); Assert.AreEqual(CombinationType.And, faultTreeIllustrationPoint.CombinationType); Stochast stochast = faultTreeIllustrationPoint.Stochasts.Single(); Assert.AreEqual(hydraRingStochast.Alpha, stochast.Alpha, stochast.Alpha.GetAccuracy()); Assert.AreEqual(hydraRingStochast.Duration, stochast.Duration, stochast.Duration.GetAccuracy()); Assert.AreEqual(hydraRingStochast.Name, stochast.Name); }
public void ConvertToGeneralResultTopLevelFaultTreeIllustrationPoint_HydraRingGeneralResultWithFaultTreeIllustrationPointsOnly_ExpectedProperties() { // Setup var random = new Random(21); const string closingSituation = "Closing situation"; var hydraRingWindDirection = new HydraRingWindDirection("SSE", random.NextDouble()); var hydraRingWindDirectionClosingSituation = new HydraRingWindDirectionClosingSituation( hydraRingWindDirection, closingSituation); var hydraRingIllustrationPoint = new HydraRingFaultTreeIllustrationPoint( "IllustrationPoint", random.NextDouble(), Enumerable.Empty <HydraRingStochast>(), HydraRingCombinationType.Or); var hydraRingIllustrationTreeNode = new HydraRingIllustrationPointTreeNode(hydraRingIllustrationPoint); var governingHydraRingWindDirection = new HydraRingWindDirection("Name", random.NextDouble()); var hydraRingGeneralResult = new HydraRingGeneralResult( random.NextDouble(), governingHydraRingWindDirection, Enumerable.Empty <HydraRingStochast>(), new Dictionary <HydraRingWindDirectionClosingSituation, HydraRingIllustrationPointTreeNode> { { hydraRingWindDirectionClosingSituation, hydraRingIllustrationTreeNode } }); // Call GeneralResult <TopLevelFaultTreeIllustrationPoint> generalResult = GeneralResultConverter.ConvertToGeneralResultTopLevelFaultTreeIllustrationPoint(hydraRingGeneralResult); // Assert WindDirection generalResultGoverningWindDirection = generalResult.GoverningWindDirection; AssertWindDirection(governingHydraRingWindDirection, generalResultGoverningWindDirection); TopLevelFaultTreeIllustrationPoint topLevelFaultTreeIllustrationPoint = generalResult.TopLevelIllustrationPoints.Single(); AssertWindDirection(hydraRingWindDirection, topLevelFaultTreeIllustrationPoint.WindDirection); Assert.AreEqual(closingSituation, topLevelFaultTreeIllustrationPoint.ClosingSituation); IllustrationPointNode faultTreeIllustrationPoint = topLevelFaultTreeIllustrationPoint.FaultTreeNodeRoot; CollectionAssert.IsEmpty(faultTreeIllustrationPoint.Children); var faultTreeIllustrationPointData = (FaultTreeIllustrationPoint)faultTreeIllustrationPoint.Data; CollectionAssert.IsEmpty(faultTreeIllustrationPointData.Stochasts); Assert.AreEqual(hydraRingIllustrationPoint.Name, faultTreeIllustrationPointData.Name); Assert.AreEqual(hydraRingIllustrationPoint.Beta, faultTreeIllustrationPointData.Beta, faultTreeIllustrationPointData.Beta.GetAccuracy()); Assert.AreEqual(CombinationType.Or, faultTreeIllustrationPointData.CombinationType); }
public void Convert_ValidHydraRingFaultTreeIllustrationPointWithoutChildren_ExpectedProperties() { // Setup var random = new Random(21); var hydraRingWindDirection = new HydraRingWindDirection("random name", random.NextDouble()); var hydraRingWindDirectionClosingSituation = new HydraRingWindDirectionClosingSituation(hydraRingWindDirection, "random closing situation"); var hydraRingStochast = new HydraRingStochast("random stochast", random.NextDouble(), random.NextDouble()); var hydraRingFaultTreeIllustrationPoint = new HydraRingFaultTreeIllustrationPoint("fault tree", random.NextDouble(), new[] { hydraRingStochast }, HydraRingCombinationType.And); var treeNode = new HydraRingIllustrationPointTreeNode(hydraRingFaultTreeIllustrationPoint); // Call TopLevelFaultTreeIllustrationPoint topLevelIllustrationPoint = TopLevelFaultTreeIllustrationPointConverter.Convert(hydraRingWindDirectionClosingSituation, treeNode); // Assert WindDirection windDirection = topLevelIllustrationPoint.WindDirection; Assert.AreEqual(hydraRingWindDirection.Angle, windDirection.Angle, windDirection.Angle.GetAccuracy()); Assert.AreEqual(hydraRingWindDirection.Name, windDirection.Name); Assert.AreEqual(hydraRingWindDirectionClosingSituation.ClosingSituation, topLevelIllustrationPoint.ClosingSituation); IllustrationPointNode illustrationPointNode = topLevelIllustrationPoint.FaultTreeNodeRoot; CollectionAssert.IsEmpty(illustrationPointNode.Children); var illustrationPointData = (FaultTreeIllustrationPoint)illustrationPointNode.Data; Assert.AreEqual(hydraRingFaultTreeIllustrationPoint.Name, illustrationPointData.Name); Assert.AreEqual(hydraRingFaultTreeIllustrationPoint.Beta, illustrationPointData.Beta, illustrationPointData.Beta.GetAccuracy()); Assert.AreEqual(CombinationType.And, illustrationPointData.CombinationType); Stochast stochast = illustrationPointData.Stochasts.Single(); Assert.AreEqual(hydraRingStochast.Alpha, stochast.Alpha, stochast.Alpha.GetAccuracy()); Assert.AreEqual(hydraRingStochast.Duration, stochast.Duration, stochast.Duration.GetAccuracy()); Assert.AreEqual(hydraRingStochast.Name, stochast.Name); }
public void Convert_HydraRingWindDirectionClosingSituationNull_ThrowsArgumentNullException() { // Setup var hydraRingFaultTreeIllustrationPoint = new HydraRingFaultTreeIllustrationPoint("fault tree", double.NaN, Enumerable.Empty <HydraRingStochast>(), HydraRingCombinationType.And); var treeNode = new HydraRingIllustrationPointTreeNode(hydraRingFaultTreeIllustrationPoint); // Call TestDelegate call = () => TopLevelFaultTreeIllustrationPointConverter.Convert(null, treeNode); // Assert var exception = Assert.Throws <ArgumentNullException>(call); Assert.AreEqual("hydraRingWindDirectionClosingSituation", exception.ParamName); }
public void Convert_InvalidHydraRingCombinationType_ThrowsIllustrationPointConversionException() { // Setup var hydraRingFaultTreeIllustrationPoint = new HydraRingFaultTreeIllustrationPoint( "name", new Random(210).NextDouble(), Enumerable.Empty <HydraRingStochast>(), (HydraRingCombinationType)999999); // Call TestDelegate call = () => FaultTreeIllustrationPointConverter.Convert(hydraRingFaultTreeIllustrationPoint); // Assert var exception = Assert.Throws <IllustrationPointConversionException>(call); string expectedMessage = $"Could not convert the {typeof(HydraRingCombinationType)} into a {typeof(CombinationType)}."; Assert.AreEqual(expectedMessage, exception.Message); Assert.IsInstanceOf <InvalidEnumArgumentException>(exception.InnerException); }
public void ConvertToGeneralResultTopLevelSubMechanismIllustrationPoint_HydraRingGeneralResultWithFaultTreeIllustrationPointsOnly_ThrowsIllustrationPointConversionException() { // Setup var random = new Random(21); const string closingSituation = "Closing situation"; var hydraRingWindDirection = new HydraRingWindDirection("SSE", random.NextDouble()); var hydraRingWindDirectionClosingSituation = new HydraRingWindDirectionClosingSituation( hydraRingWindDirection, closingSituation); var hydraRingIllustrationPoint = new HydraRingFaultTreeIllustrationPoint( "IllustrationPoint", random.NextDouble(), Enumerable.Empty <HydraRingStochast>(), random.NextEnumValue <HydraRingCombinationType>()); var hydraRingIllustrationTreeNode = new HydraRingIllustrationPointTreeNode(hydraRingIllustrationPoint); var governingHydraRingWindDirection = new HydraRingWindDirection("Name", random.NextDouble()); var hydraRingGeneralResult = new HydraRingGeneralResult( random.NextDouble(), governingHydraRingWindDirection, Enumerable.Empty <HydraRingStochast>(), new Dictionary <HydraRingWindDirectionClosingSituation, HydraRingIllustrationPointTreeNode> { { hydraRingWindDirectionClosingSituation, hydraRingIllustrationTreeNode } }); // Call TestDelegate call = () => GeneralResultConverter.ConvertToGeneralResultTopLevelSubMechanismIllustrationPoint(hydraRingGeneralResult); // Assert var exception = Assert.Throws <IllustrationPointConversionException>(call); string expectedMessage = $"Expected a fault tree node with data of type {typeof(HydraRingSubMechanismIllustrationPoint)} as root, " + $"but got {hydraRingIllustrationPoint.GetType()}"; Assert.AreEqual(expectedMessage, exception.Message); }
public void Convert_ValidHydraRingFaultTreeIllustrationPointWithChildren_ExpectedProperties() { // Setup var random = new Random(21); var hydraRingWindDirection = new HydraRingWindDirection("random name", random.NextDouble()); var hydraRingWindDirectionClosingSituation = new HydraRingWindDirectionClosingSituation(hydraRingWindDirection, "random closing situation"); var hydraRingStochast = new HydraRingStochast("random stochast", random.NextDouble(), random.NextDouble()); var hydraRingFaultTreeIllustrationPointRoot = new HydraRingFaultTreeIllustrationPoint("fault tree root", random.NextDouble(), new[] { hydraRingStochast }, HydraRingCombinationType.And); var hydraRingFaultTreeIllustrationPointChildOne = new HydraRingFaultTreeIllustrationPoint("fault tree child one", random.NextDouble(), new[] { hydraRingStochast }, HydraRingCombinationType.Or); var hydraRingFaultTreeIllustrationPointChildTwo = new HydraRingSubMechanismIllustrationPoint("fault tree child two", Enumerable.Empty <HydraRingSubMechanismIllustrationPointStochast>(), Enumerable.Empty <HydraRingIllustrationPointResult>(), random.NextDouble()); var treeNodeRoot = new HydraRingIllustrationPointTreeNode(hydraRingFaultTreeIllustrationPointRoot); treeNodeRoot.SetChildren(new[] { new HydraRingIllustrationPointTreeNode(hydraRingFaultTreeIllustrationPointChildOne), new HydraRingIllustrationPointTreeNode(hydraRingFaultTreeIllustrationPointChildTwo) }); // Call TopLevelFaultTreeIllustrationPoint topLevelIllustrationPoint = TopLevelFaultTreeIllustrationPointConverter.Convert(hydraRingWindDirectionClosingSituation, treeNodeRoot); // Assert WindDirection windDirection = topLevelIllustrationPoint.WindDirection; Assert.AreEqual(hydraRingWindDirection.Angle, windDirection.Angle, windDirection.Angle.GetAccuracy()); Assert.AreEqual(hydraRingWindDirection.Name, windDirection.Name); Assert.AreEqual(hydraRingWindDirectionClosingSituation.ClosingSituation, topLevelIllustrationPoint.ClosingSituation); IllustrationPointNode illustrationPoint = topLevelIllustrationPoint.FaultTreeNodeRoot; var illustrationPointData = (FaultTreeIllustrationPoint)illustrationPoint.Data; Assert.AreEqual(hydraRingFaultTreeIllustrationPointRoot.Name, illustrationPointData.Name); Assert.AreEqual(hydraRingFaultTreeIllustrationPointRoot.Beta, illustrationPointData.Beta, illustrationPointData.Beta.GetAccuracy()); Assert.AreEqual(CombinationType.And, illustrationPointData.CombinationType); Stochast stochast = illustrationPointData.Stochasts.Single(); Assert.AreEqual(hydraRingStochast.Alpha, stochast.Alpha, stochast.Alpha.GetAccuracy()); Assert.AreEqual(hydraRingStochast.Duration, stochast.Duration, stochast.Duration.GetAccuracy()); Assert.AreEqual(hydraRingStochast.Name, stochast.Name); Assert.AreEqual(treeNodeRoot.Children.Count(), illustrationPoint.Children.Count()); IllustrationPointNode[] children = illustrationPoint.Children.ToArray(); CollectionAssert.IsEmpty(children[0].Children); CollectionAssert.IsEmpty(children[1].Children); var childOne = (FaultTreeIllustrationPoint)children[0].Data; Stochast childStochast = illustrationPointData.Stochasts.Single(); Assert.AreEqual(hydraRingStochast.Alpha, childStochast.Alpha, childStochast.Alpha.GetAccuracy()); Assert.AreEqual(hydraRingStochast.Duration, childStochast.Duration, childStochast.Duration.GetAccuracy()); Assert.AreEqual(hydraRingStochast.Name, childStochast.Name); Assert.AreEqual(hydraRingFaultTreeIllustrationPointChildOne.Name, childOne.Name); Assert.AreEqual(hydraRingFaultTreeIllustrationPointChildOne.Beta, childOne.Beta, childOne.Beta.GetAccuracy()); Assert.AreEqual(CombinationType.Or, childOne.CombinationType); var childTwo = (SubMechanismIllustrationPoint)children[1].Data; CollectionAssert.IsEmpty(childTwo.Stochasts); CollectionAssert.IsEmpty(childTwo.IllustrationPointResults); Assert.AreEqual(hydraRingFaultTreeIllustrationPointChildTwo.Name, childTwo.Name); Assert.AreEqual(hydraRingFaultTreeIllustrationPointChildTwo.Beta, childTwo.Beta, childTwo.Beta.GetAccuracy()); }