/// <summary> /// Parses <see cref="WindDirection"/> objects from the <paramref name="reader"/>. /// </summary> /// <param name="reader">The database reader.</param> /// <exception cref="HydraRingFileParserException">Thrown when the read <see cref="IllustrationPointsDatabaseConstants.WindDirectionAngle"/> /// is <see cref="DBNull"/> or when no governing wind direction is found.</exception> private void ParseWindDirections(HydraRingDatabaseReader reader) { windDirections = new Dictionary <int, WindDirection>(); foreach (Dictionary <string, object> readWindDirection in GetIterator(reader)) { int key = Convert.ToInt32(readWindDirection[IllustrationPointsDatabaseConstants.WindDirectionId]); string name = Convert.ToString(readWindDirection[IllustrationPointsDatabaseConstants.WindDirectionName]); double angle = ConvertToDouble(readWindDirection[IllustrationPointsDatabaseConstants.WindDirectionAngle], IllustrationPointsDatabaseConstants.WindDirectionAngle); bool isGoverning = Convert.ToBoolean(readWindDirection[IllustrationPointsDatabaseConstants.IsGoverning]); var windDirection = new WindDirection(name, angle); windDirections[key] = windDirection; if (isGoverning) { governingWindDirection = windDirection; } } if (governingWindDirection == null) { throw new HydraRingFileParserException(Resources.IllustrationPointsParser_Parse_No_governing_wind_direction_found); } }
/// <summary> /// Proceeds <paramref name="reader"/> to the next result in the data set. /// </summary> /// <param name="reader">The database reader.</param> /// <exception cref="HydraRingFileParserException">Thrown there was no other result in the data set.</exception> private static void ProceedOrThrow(HydraRingDatabaseReader reader) { if (!reader.NextResult()) { throw new HydraRingFileParserException(Resources.IllustrationPointsParser_Parse_Could_not_read_illustration_point_data); } }
public void Parse(string workingDirectory, int sectionId) { string query = string.Concat( IllustrationPointQueries.ClosingSituations, IllustrationPointQueries.WindDirections, IllustrationPointQueries.SubMechanisms, IllustrationPointQueries.FaultTrees, IllustrationPointQueries.GeneralAlphaValues, IllustrationPointQueries.GeneralBetaValues, IllustrationPointQueries.FaultTreeAlphaValues, IllustrationPointQueries.FaultTreeBetaValues, IllustrationPointQueries.SubMechanismAlphaValues, IllustrationPointQueries.SubMechanismBetaValues, IllustrationPointQueries.SubMechanismIllustrationPointResults, IllustrationPointQueries.RecursiveFaultTree); try { using (var reader = new HydraRingDatabaseReader(workingDirectory, query, sectionId)) { ParseResultsFromReader(reader); } } catch (SQLiteException e) { throw new HydraRingFileParserException(Resources.IllustrationPointsParser_Parse_Could_not_read_illustration_point_data, e); } }
/// <summary> /// Parses the illustration point database results. /// </summary> /// <param name="reader">The database reader.</param> /// <exception cref="HydraRingFileParserException">Thrown when parsing the results from <paramref name="reader"/>.</exception> private void ParseResultsFromReader(HydraRingDatabaseReader reader) { ParseClosingSituations(reader); ProceedOrThrow(reader); ParseWindDirections(reader); ProceedOrThrow(reader); ParseSubMechanisms(reader); ProceedOrThrow(reader); ParseFaultTrees(reader); ProceedOrThrow(reader); ParseGeneralAlphaValues(reader); ProceedOrThrow(reader); ParseGeneralBetaValue(reader); ProceedOrThrow(reader); ParseFaultTreeAlphaValues(reader); ProceedOrThrow(reader); ParseFaultTreeBetaValues(reader); ProceedOrThrow(reader); ParseSubMechanismAlphaValues(reader); ProceedOrThrow(reader); ParseSubMechanismBetaValues(reader); ProceedOrThrow(reader); ParseSubMechanismResults(reader); ProceedOrThrow(reader); Dictionary <WindDirectionClosingSituation, IllustrationPointTreeNode> rootIllustrationPoints = ParseFaultTree(reader) ?? GetSubMechanismAsRootIllustrationPoint(); Output = new GeneralResult(beta, governingWindDirection, stochasts, rootIllustrationPoints); }
/// <summary> /// Parses <see cref="SubMechanismIllustrationPointStochast"/> objects from the <paramref name="reader"/>. /// </summary> /// <param name="reader">The database reader.</param> /// <exception cref="HydraRingFileParserException">Thrown when the read <see cref="IllustrationPointsDatabaseConstants.Duration"/>, /// <see cref="IllustrationPointsDatabaseConstants.AlphaValue"/>, or <see cref="IllustrationPointsDatabaseConstants.Realization"/> /// is <see cref="DBNull"/>.</exception> private void ParseSubMechanismAlphaValues(HydraRingDatabaseReader reader) { foreach (Dictionary <string, object> readSubMechanismAlphaValue in GetIterator(reader)) { int subMechanismId = Convert.ToInt32(readSubMechanismAlphaValue[IllustrationPointsDatabaseConstants.SubMechanismId]); int windDirectionId = Convert.ToInt32(readSubMechanismAlphaValue[IllustrationPointsDatabaseConstants.WindDirectionId]); int closingSituationid = Convert.ToInt32(readSubMechanismAlphaValue[IllustrationPointsDatabaseConstants.ClosingSituationId]); string name = Convert.ToString(readSubMechanismAlphaValue[IllustrationPointsDatabaseConstants.StochastName]); string unit = Convert.ToString(readSubMechanismAlphaValue[IllustrationPointsDatabaseConstants.IllustrationPointUnit]); double duration = ConvertToDouble(readSubMechanismAlphaValue[IllustrationPointsDatabaseConstants.Duration], IllustrationPointsDatabaseConstants.Duration); double alpha = ConvertToDouble(readSubMechanismAlphaValue[IllustrationPointsDatabaseConstants.AlphaValue], IllustrationPointsDatabaseConstants.AlphaValue); double realization = ConvertToDouble(readSubMechanismAlphaValue[IllustrationPointsDatabaseConstants.Realization], IllustrationPointsDatabaseConstants.Realization); var key = new ThreeKeyIndex(windDirectionId, closingSituationid, subMechanismId); if (!subMechanismStochasts.ContainsKey(key)) { subMechanismStochasts[key] = new List <SubMechanismIllustrationPointStochast>(); } subMechanismStochasts[key].Add(new SubMechanismIllustrationPointStochast(name, unit, duration, alpha, realization)); } }
/// <summary> /// Parses <see cref="Stochast"/> objects from the <paramref name="reader"/>. /// </summary> /// <param name="reader">The database reader.</param> /// <exception cref="HydraRingFileParserException">Thrown when the read <see cref="IllustrationPointsDatabaseConstants.Duration"/> /// or <see cref="IllustrationPointsDatabaseConstants.AlphaValue"/> is <see cref="DBNull"/>.</exception> private void ParseGeneralAlphaValues(HydraRingDatabaseReader reader) { stochasts = GetIterator(reader).Select(a => { string name = Convert.ToString(a[IllustrationPointsDatabaseConstants.StochastName]); double duration = ConvertToDouble(a[IllustrationPointsDatabaseConstants.Duration], IllustrationPointsDatabaseConstants.Duration); double alpha = ConvertToDouble(a[IllustrationPointsDatabaseConstants.AlphaValue], IllustrationPointsDatabaseConstants.AlphaValue); return(new Stochast(name, duration, alpha)); }).ToArray(); }
private static IEnumerable <Dictionary <string, object> > GetIterator(HydraRingDatabaseReader reader) { Dictionary <string, object> nextLine = reader.ReadLine(); while (nextLine != null) { yield return(nextLine); nextLine = reader.ReadLine(); } }
public void Constructor_ExpectedValues() { // Setup string directory = Path.Combine(testDirectory, validDatabase); // Call using (var reader = new HydraRingDatabaseReader(directory, "", 1)) { // Assert Assert.IsInstanceOf <IDisposable>(reader); } }
public void ReadLine_EmptyDatabase_ReturnsNull() { // Setup string directory = Path.Combine(testDirectory, emptyDatabase); using (var reader = new HydraRingDatabaseReader(directory, query, 1)) { // Call Dictionary <string, object> result = reader.ReadLine(); // Assert Assert.IsNull(result); } }
public void NextResult_EmptyDatabase_ReturnsFalse() { // Setup string directory = Path.Combine(testDirectory, emptyDatabase); using (var reader = new HydraRingDatabaseReader(directory, query, 1)) { // Call bool couldGetNextResult = reader.NextResult(); // Assert Assert.IsFalse(couldGetNextResult); } }
public void NextResult_MultipleResult_ReturnsTrue() { // Setup string directory = Path.Combine(testDirectory, validDatabase); using (var reader = new HydraRingDatabaseReader(directory, query + query, 1)) { // Call bool couldGetNextResult = reader.NextResult(); // Assert Assert.IsTrue(couldGetNextResult); } }
/// <summary> /// Parses beta values from the <paramref name="reader"/>. /// </summary> /// <param name="reader">The database reader.</param> /// <exception cref="HydraRingFileParserException">Thrown when: /// <list type="bullet"> /// <item>The read <see cref="IllustrationPointsDatabaseConstants.BetaValue"/> is <see cref="DBNull"/>;</item> /// <item>Multiple values for beta of illustration point found.</item> /// </list> /// </exception> private void ParseGeneralBetaValue(HydraRingDatabaseReader reader) { Dictionary <string, object>[] betaValues = GetIterator(reader).ToArray(); if (betaValues.Length > 1) { throw new HydraRingFileParserException(Resources.IllustrationPointsParser_Parse_Multiple_values_for_beta_of_illustration_point_found); } if (betaValues.Length == 0) { throw new HydraRingFileParserException(Resources.IllustrationPointsParser_Parse_No_values_for_beta_of_illustration_point_found); } beta = ConvertToDouble(betaValues[0][IllustrationPointsDatabaseConstants.BetaValue], IllustrationPointsDatabaseConstants.BetaValue); }
/// <summary> /// Parses fault tree beta values from the <paramref name="reader"/>. /// </summary> /// <param name="reader">The database reader.</param> /// <exception cref="HydraRingFileParserException">Thrown when: /// <list type="bullet"> /// <item>The read <see cref="IllustrationPointsDatabaseConstants.BetaValue"/> is <see cref="DBNull"/>;</item> /// <item>Multiple values for beta of illustration point found.</item> /// </list> /// </exception> private void ParseFaultTreeBetaValues(HydraRingDatabaseReader reader) { foreach (Dictionary <string, object> readFaultTreeBetaValue in GetIterator(reader)) { int faultTreeId = Convert.ToInt32(readFaultTreeBetaValue[IllustrationPointsDatabaseConstants.FaultTreeId]); int windDirectionId = Convert.ToInt32(readFaultTreeBetaValue[IllustrationPointsDatabaseConstants.WindDirectionId]); int closingSituationid = Convert.ToInt32(readFaultTreeBetaValue[IllustrationPointsDatabaseConstants.ClosingSituationId]); double faultTreeBeta = ConvertToDouble(readFaultTreeBetaValue[IllustrationPointsDatabaseConstants.BetaValue], IllustrationPointsDatabaseConstants.BetaValue); var threeKeyIndex = new ThreeKeyIndex(windDirectionId, closingSituationid, faultTreeId); if (faultTreeBetaValues.ContainsKey(threeKeyIndex)) { throw new HydraRingFileParserException(Resources.IllustrationPointsParser_Parse_Multiple_values_for_beta_of_illustration_point_found); } faultTreeBetaValues[threeKeyIndex] = faultTreeBeta; } }
/// <summary> /// Parses the Hydra-Ring output database. /// </summary> /// <param name="workingDirectory">The path to the directory which contains /// the output of the Hydra-Ring calculation.</param> /// <param name="query">The query to perform when reading the database.</param> /// <param name="sectionId">The section id to get the output for.</param> /// <param name="exceptionMessage">The exception message when there is no result.</param> /// <returns>A <see cref="Dictionary{TKey,TValue}"/> with the key of the column and the value.</returns> /// <exception cref="ArgumentNullException">Thrown when any input parameter is <c>null</c>.</exception> /// <exception cref="HydraRingFileParserException">Thrown when the reader encounters an error while /// reading the database.</exception> public static Dictionary <string, object> ReadSingleLine(string workingDirectory, string query, int sectionId, string exceptionMessage) { ValidateParameters(workingDirectory, query, exceptionMessage); try { using (var reader = new HydraRingDatabaseReader(workingDirectory, query, sectionId)) { return(ReadLineFromReader(exceptionMessage, reader)); } } catch (SQLiteException e) { throw new HydraRingFileParserException(Resources.Parse_Cannot_read_result_in_output_file, e); } }
/// <summary> /// Parses <see cref="IllustrationPointResult"/> objects from the <paramref name="reader"/>. /// </summary> /// <param name="reader">The database reader.</param> /// <exception cref="HydraRingFileParserException">Thrown when the read <see cref="IllustrationPointsDatabaseConstants.IllustrationPointResultValue"/> /// is <see cref="DBNull"/>.</exception> private void ParseSubMechanismResults(HydraRingDatabaseReader reader) { foreach (Dictionary <string, object> readSubMechanismResult in GetIterator(reader)) { int subMechanismId = Convert.ToInt32(readSubMechanismResult[IllustrationPointsDatabaseConstants.SubMechanismId]); int windDirectionId = Convert.ToInt32(readSubMechanismResult[IllustrationPointsDatabaseConstants.WindDirectionId]); int closingSituationid = Convert.ToInt32(readSubMechanismResult[IllustrationPointsDatabaseConstants.ClosingSituationId]); string description = Convert.ToString(readSubMechanismResult[IllustrationPointsDatabaseConstants.IllustrationPointResultDescription]); string unit = Convert.ToString(readSubMechanismResult[IllustrationPointsDatabaseConstants.IllustrationPointUnit]); double value = ConvertToDouble(readSubMechanismResult[IllustrationPointsDatabaseConstants.IllustrationPointResultValue], IllustrationPointsDatabaseConstants.IllustrationPointResultValue); var key = new ThreeKeyIndex(windDirectionId, closingSituationid, subMechanismId); if (!subMechanismResults.ContainsKey(key)) { subMechanismResults[key] = new List <IllustrationPointResult>(); } subMechanismResults[key].Add(new IllustrationPointResult(description, unit, value)); } }
/// <summary> /// Parses <see cref="Stochast"/> objects from the <paramref name="reader"/>. /// </summary> /// <param name="reader">The database reader.</param> /// <exception cref="HydraRingFileParserException">Thrown when the read <see cref="IllustrationPointsDatabaseConstants.AlphaValue"/> /// or <see cref="IllustrationPointsDatabaseConstants.Duration"/> is <see cref="DBNull"/>.</exception> private void ParseFaultTreeAlphaValues(HydraRingDatabaseReader reader) { foreach (Dictionary <string, object> readFaultTreeAlphaValue in GetIterator(reader)) { int faultTreeId = Convert.ToInt32(readFaultTreeAlphaValue[IllustrationPointsDatabaseConstants.FaultTreeId]); int windDirectionId = Convert.ToInt32(readFaultTreeAlphaValue[IllustrationPointsDatabaseConstants.WindDirectionId]); int closingSituationid = Convert.ToInt32(readFaultTreeAlphaValue[IllustrationPointsDatabaseConstants.ClosingSituationId]); string name = Convert.ToString(readFaultTreeAlphaValue[IllustrationPointsDatabaseConstants.StochastName]); double duration = ConvertToDouble(readFaultTreeAlphaValue[IllustrationPointsDatabaseConstants.Duration], IllustrationPointsDatabaseConstants.Duration); double alpha = ConvertToDouble(readFaultTreeAlphaValue[IllustrationPointsDatabaseConstants.AlphaValue], IllustrationPointsDatabaseConstants.AlphaValue); var key = new ThreeKeyIndex(windDirectionId, closingSituationid, faultTreeId); if (!faultTreeStochasts.ContainsKey(key)) { faultTreeStochasts[key] = new List <Stochast>(); } faultTreeStochasts[key].Add(new Stochast(name, duration, alpha)); } }
private Dictionary <WindDirectionClosingSituation, IllustrationPointTreeNode> ParseFaultTree(HydraRingDatabaseReader reader) { IEnumerable <Tuple <int, WindDirection, int, string> > windDirectionClosingSituations = GetAllWindDirectionClosingSituationCombinations(); Dictionary <string, object>[] readFaultTrees = GetIterator(reader).ToArray(); if (readFaultTrees.Length > 0) { IEnumerable <Tuple <int?, int, Type, CombinationType> > results = CreateResultTuples(readFaultTrees); var rootIllustrationPoints = new Dictionary <WindDirectionClosingSituation, IllustrationPointTreeNode>(); foreach (Tuple <int, WindDirection, int, string> windDirectionClosingSituation in windDirectionClosingSituations) { Tuple <int?, int, Type, CombinationType> root = results.Single(r => !r.Item1.HasValue); IllustrationPointTreeNode illustrationPointTreeNode = BuildFaultTree(windDirectionClosingSituation, root.Item2, root.Item4, results); if (illustrationPointTreeNode != null) { rootIllustrationPoints[CreateFaultTreeKey(windDirectionClosingSituation)] = illustrationPointTreeNode; } } return(rootIllustrationPoints); } return(null); }
private void ParseClosingSituations(HydraRingDatabaseReader reader) { closingSituations = GetIterator(reader).ToDictionary( r => Convert.ToInt32(r[IllustrationPointsDatabaseConstants.ClosingSituationId]), r => Convert.ToString(r[IllustrationPointsDatabaseConstants.ClosingSituationName])); }
private void ParseFaultTrees(HydraRingDatabaseReader reader) { faultTrees = GetIterator(reader).ToDictionary( r => Convert.ToInt32(r[IllustrationPointsDatabaseConstants.FaultTreeId]), r => Convert.ToString(r[IllustrationPointsDatabaseConstants.FaultTreeName])); }
/// <summary> /// Tries to read a result from the reader and throws an exception if no row could be read. /// </summary> /// <param name="exceptionMessage">The message to use in the exception when reading fails.</param> /// <param name="reader">The reader to read a row from.</param> /// <returns>A single row from the reader.</returns> /// <exception cref="HydraRingFileParserException">Thrown when no row could be read from the /// <paramref name="reader"/>.</exception> private static Dictionary <string, object> ReadLineFromReader(string exceptionMessage, HydraRingDatabaseReader reader) { Dictionary <string, object> result = reader.ReadLine(); if (result != null) { return(result); } throw new HydraRingFileParserException(exceptionMessage); }
private void ParseSubMechanisms(HydraRingDatabaseReader reader) { subMechanisms = GetIterator(reader).ToDictionary( r => Convert.ToInt32(r[IllustrationPointsDatabaseConstants.SubMechanismId]), r => Convert.ToString(r[IllustrationPointsDatabaseConstants.SubMechanismName])); }