/Source/HydroModeler/Utilities/OpenMI/sdk/Wrapper/LinearConversionDataOperation.cs

# · C# · 224 lines · 121 code · 28 blank · 75 comment · 11 complexity · 4a7774febe16a3b520af97675739ec7c MD5 · raw file

  1. #region Copyright
  2. /*
  3. * Copyright (c) 2005,2006,2007, OpenMI Association
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * * Neither the name of the OpenMI Association nor the
  14. * names of its contributors may be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY "OpenMI Association" ``AS IS'' AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL "OpenMI Association" BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #endregion
  29. using System;
  30. using OpenMI.Standard;
  31. using Oatc.OpenMI.Sdk.Backbone;
  32. namespace Oatc.OpenMI.Sdk.Wrapper
  33. {
  34. /// <summary>
  35. /// The LinearDataOperation class is an implementation of the IDataOperation interface.
  36. /// The LinearDataOperation can make linear conversion on ScalarSets. The ax+b type of operations.
  37. /// </summary>
  38. public class LinearConversionDataOperation : IDataOperation, ICloneable
  39. {
  40. Oatc.OpenMI.Sdk.Backbone.Argument[] _arguments;
  41. bool _isActivated;
  42. double _a;
  43. double _b;
  44. /// <summary>
  45. /// Constructor
  46. /// </summary>
  47. public LinearConversionDataOperation()
  48. {
  49. _arguments = new Oatc.OpenMI.Sdk.Backbone.Argument[3];
  50. _arguments[0] = new Oatc.OpenMI.Sdk.Backbone.Argument();
  51. _arguments[0].Description = "Parameter A. Used in conversion: A*x + B";
  52. _arguments[0].Key = "Type";
  53. _arguments[0].Value = "Linear Conversion";
  54. _arguments[0].ReadOnly = true;
  55. _arguments[1] = new Oatc.OpenMI.Sdk.Backbone.Argument();
  56. _arguments[1].Description = "Parameter A. Used in conversion: A*x + B";
  57. _arguments[1].Key = "A";
  58. _arguments[1].Value = "1.0";
  59. _arguments[1].ReadOnly = false;
  60. _arguments[2] = new Oatc.OpenMI.Sdk.Backbone.Argument();
  61. _arguments[2].Description = "Parameter B. Used in conversion: A*x + B";
  62. _arguments[2].Key = "B";
  63. _arguments[2].Value = "0.0";
  64. _arguments[2].ReadOnly = false;
  65. _isActivated = false;
  66. }
  67. #region IDataOperation Members
  68. /// <summary>
  69. /// DataOperation ID. In this class always "Linear Conversions" (is hardcoded)
  70. /// </summary>
  71. public string ID
  72. {
  73. get
  74. {
  75. return "Linear Conversion";
  76. }
  77. }
  78. /// <summary>
  79. /// The linear dataoperation is valid for any input and output exchange items and can be combined with any other
  80. /// dataopertion, consequently this method always return true.
  81. /// See also documentation for : OpenMI.Standard.IDataOperation for details
  82. /// </summary>
  83. /// <param name="inputExchangeItem">inputExchangeItem</param>
  84. /// <param name="outputExchangeItem">outputExchangeItem</param>
  85. /// <param name="SelectedDataOperations">SelectedDataOperations</param>
  86. /// <returns></returns>
  87. public bool IsValid(IInputExchangeItem inputExchangeItem, IOutputExchangeItem outputExchangeItem, IDataOperation[] SelectedDataOperations)
  88. {
  89. return true;
  90. }
  91. /// <summary>
  92. /// Number of dataoperation arguments. For the Linear dataoperation this number is always 3 (coefficient a, offset b and description text)
  93. /// </summary>
  94. public int ArgumentCount
  95. {
  96. get
  97. {
  98. return _arguments.Length;
  99. }
  100. }
  101. /// <summary>
  102. /// Initialises the data operation. Nothing is done for the Linear dataoperation
  103. /// </summary>
  104. /// <param name="properties">arguments</param>
  105. public void Initialize(IArgument[] properties)
  106. {
  107. }
  108. /// <summary>
  109. /// Returns the arguments for the Linear Dataoperation
  110. /// </summary>
  111. /// <param name="argumentIndex">Argument index</param>
  112. /// <returns></returns>
  113. public IArgument GetArgument(int argumentIndex)
  114. {
  115. return (IArgument) _arguments[argumentIndex];
  116. }
  117. #endregion
  118. /// <summary>
  119. /// The prepare method should be called before the PerformDataOperation. This method is
  120. /// not part of the OpenMI.Standard.IDataOperation interface. This method will convert
  121. /// the arguments which originally are defined as strings to doubles and subsequently assign
  122. /// these values to private field variables. The prepare method is introduced for performance
  123. /// reasons.
  124. /// </summary>
  125. public void Prepare()
  126. {
  127. bool argumentAWasFound = false;
  128. bool argumentBWasFound = false;
  129. _isActivated = true;
  130. for (int i = 0; i < this._arguments.Length; i++)
  131. {
  132. if (_arguments[i].Key == "A")
  133. {
  134. _a = Convert.ToDouble(_arguments[i].Value);
  135. argumentAWasFound = true;
  136. }
  137. if (_arguments[i].Key == "B")
  138. {
  139. _b = Convert.ToDouble(_arguments[i].Value);
  140. argumentBWasFound = true;
  141. }
  142. }
  143. if (!argumentAWasFound || !argumentBWasFound)
  144. {
  145. throw new Exception("Missing argument in data operation: \"Linear Conversion\"");
  146. }
  147. }
  148. /// <summary>
  149. /// The ValueSet is converted. This method does not support VectorSet, so if the ValueSet is a Vectorset
  150. /// an exception will be thrown. The parameters passed in this method is not used, since all needed information
  151. /// is already assigned in the Prepare method.
  152. /// </summary>
  153. /// <param name="values">argumens but not used in this method</param>
  154. /// <returns>The converted ValueSet</returns>
  155. public IValueSet PerformDataOperation(IValueSet values)
  156. {
  157. if (_isActivated)
  158. {
  159. if (!(values is IScalarSet))
  160. {
  161. throw new Exception("The Oatc.OpenMI.Sdk.Wrapper packages only supports ScalarSets (Not VectorSets)");
  162. }
  163. double[] x = new double[values.Count];
  164. for (int i = 0; i < values.Count; i++)
  165. {
  166. x[i] = ((IScalarSet) values).GetScalar(i) * _a + _b;
  167. }
  168. return ((IValueSet) new ScalarSet(x));
  169. }
  170. return values; // return the values unchanged.
  171. }
  172. #region ICloneable Members
  173. /// <summary>
  174. /// Creates a copy of the current instance.
  175. /// </summary>
  176. /// <returns>Copy of the instance</returns>
  177. public object Clone()
  178. {
  179. LinearConversionDataOperation clone = new LinearConversionDataOperation();
  180. clone._a = _a;
  181. clone._b = _b;
  182. clone._isActivated = _isActivated;
  183. clone._arguments = new Argument[ArgumentCount];
  184. for(int i=0; i<ArgumentCount; i++)
  185. {
  186. clone._arguments[i] = new Oatc.OpenMI.Sdk.Backbone.Argument();
  187. clone._arguments[i].Description = _arguments[i].Description;
  188. clone._arguments[i].Key = _arguments[i].Key;
  189. clone._arguments[i].ReadOnly = _arguments[i].ReadOnly;
  190. clone._arguments[i].Value = _arguments[i].Value;
  191. }
  192. return clone;
  193. }
  194. #endregion
  195. }
  196. }