PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Branches/0.9.5-DeltaShell/src/Common/DelftTools.Functions/Conversion/ConvertedVariable.cs

#
C# | 398 lines | 308 code | 53 blank | 37 comment | 27 complexity | 370af9c887726cc44c1f9c32184b799a MD5 | raw file
Possible License(s): LGPL-2.1
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using DelftTools.Functions.Filters;
  6. using DelftTools.Functions.Generic;
  7. using DelftTools.Units;
  8. using DelftTools.Utils.Aop.NotifyPropertyChange;
  9. namespace DelftTools.Functions.Conversion
  10. {
  11. /// <summary>
  12. /// Convert a certain variable within a variable (for example x(t) convert t-t')
  13. /// Here x is variable, t is sourcevariable, t' is target variable
  14. /// </summary>
  15. /// <typeparam name="TVariable">Type of resulting variable</typeparam>
  16. /// <typeparam name="TTarget">Type of converted variable</typeparam>
  17. /// <typeparam name="TSource">Type of source variable</typeparam>
  18. public class ConvertedVariable<TVariable, TTarget, TSource> : ConvertedFunction<TTarget, TSource>, IVariable<TVariable>
  19. where TTarget : IComparable where TSource : IComparable where TVariable : IComparable
  20. {
  21. public ConvertedVariable(IVariable parent,IVariable<TSource> variableToConvert, Func<TTarget, TSource> toSource, Func<TSource, TTarget> toTarget)
  22. :base(parent,variableToConvert,toSource,toTarget)
  23. {
  24. if (variableToConvert.Arguments.Count > 0)
  25. throw new NotImplementedException("Conversion of non-arguments not supported yet");
  26. //convertedVariable = this;
  27. }
  28. public void SetValues<T>(IEnumerable<T> values, params IVariableFilter[] filters)
  29. {
  30. //convert values to parent values
  31. IEnumerable<TTarget> targetValues = (IEnumerable<TTarget>)values;
  32. Parent.SetValues(targetValues.Select(s=>toSource(s)),ConvertFilters(filters));
  33. }
  34. public new IVariable Parent
  35. {
  36. get
  37. {
  38. return (IVariable) base.Parent;
  39. }
  40. set
  41. {
  42. base.Parent = value;
  43. }
  44. }
  45. public IMultiDimensionalArray<TVariable> GetValues(params IVariableFilter[] filters)
  46. {
  47. return GetValues<TVariable>();
  48. }
  49. IVariable<TVariable> IVariable<TVariable>.Clone()
  50. {
  51. throw new NotImplementedException();
  52. }
  53. //TODO: get this addvalues mess out.
  54. public void AddValues<T>(IEnumerable<T> values)
  55. {
  56. throw new NotImplementedException();
  57. }
  58. [NoNotifyPropertyChange]
  59. public bool GenerateUniqueValueForDefaultValue { get; set; }
  60. public InterpolationType InterpolationType { get; set; }
  61. public ExtrapolationType ExtrapolationType { get; set; }
  62. public NextValueGenerator<TVariable> NextValueGenerator
  63. {
  64. get; set;
  65. }
  66. object IVariable.MaxValue
  67. {
  68. get { return MaxValue; }
  69. }
  70. public IMultiDimensionalArray<TVariable> AllValues
  71. {
  72. get { return Values; }
  73. }
  74. object IVariable.MinValue
  75. {
  76. get { return MinValue; }
  77. }
  78. public TVariable MinValue
  79. {
  80. get
  81. {
  82. if ((variableToConvert == Parent) && typeof(TVariable) == typeof(TTarget))
  83. {
  84. //no nice cast here :(
  85. return (TVariable)(object)toTarget((TSource)Parent.MinValue);
  86. }
  87. return (TVariable)Parent.MinValue;
  88. }
  89. }
  90. public TVariable MaxValue
  91. {
  92. get
  93. {
  94. if ((variableToConvert == Parent) && typeof(TVariable) == typeof(TTarget))
  95. {
  96. //no nice cast here :(
  97. return (TVariable)(object)toTarget((TSource)Parent.MaxValue);
  98. }
  99. return (TVariable)Parent.MaxValue;
  100. }
  101. }
  102. public void AddValues<T>(IEnumerable<TVariable> values)
  103. {
  104. IEnumerable<TTarget> targetValues = (IEnumerable<TTarget>)values;
  105. Parent.AddValues(targetValues.Select(s => toSource(s)));
  106. }
  107. public void AddValues(IEnumerable values)
  108. {
  109. //TODO redirect parent?
  110. IEnumerable<TTarget> targetValues = (IEnumerable<TTarget>)values;
  111. Parent.AddValues(targetValues.Select(s => toSource(s)));
  112. }
  113. public IMultiDimensionalArray CreateStorageArray()
  114. {
  115. return new MultiDimensionalArray<TTarget>();
  116. }
  117. public IMultiDimensionalArray CreateStorageArray(IMultiDimensionalArray values)
  118. {
  119. return new MultiDimensionalArray<TTarget>(values.Cast<TTarget>().ToList(), values.Shape);
  120. }
  121. public bool IsAutoSorted
  122. {
  123. get
  124. {
  125. if (variableToConvert == null)
  126. {
  127. return true;//a wild guess :(
  128. }
  129. return variableToConvert.IsAutoSorted;
  130. }
  131. set { throw new NotImplementedException(); }
  132. }
  133. /*
  134. public TVariable MinValue
  135. {
  136. get { throw new NotImplementedException(); }
  137. }
  138. public TVariable MaxValue
  139. {
  140. get { throw new NotImplementedException(); }
  141. }
  142. object IVariable.MaxValue
  143. {
  144. get { return MaxValue; }
  145. }
  146. object IVariable.MinValue
  147. {
  148. get { return MinValue; }
  149. }
  150. */
  151. public override IMultiDimensionalArray<T> GetValues<T>(params IVariableFilter[] filters)
  152. {
  153. return (IMultiDimensionalArray<T>) base.GetValues(filters);
  154. }
  155. private IVariableFilter[] ConvertFilters(IEnumerable<IVariableFilter> filters)
  156. {
  157. //rewrite variable value filter to the domain of the source.
  158. IList<IVariableFilter> filterList = new List<IVariableFilter>();
  159. foreach (var filter in filters)
  160. {
  161. //TODO: rewrite to IEnumerable etc
  162. if (filter is IVariableValueFilter && filter.Variable == convertedVariable)
  163. {
  164. var variableValueFilter = filter as IVariableValueFilter;
  165. IList values = new List<TSource>();
  166. foreach (TTarget obj in variableValueFilter.Values)
  167. {
  168. values.Add(toSource(obj));
  169. }
  170. filterList.Add(variableToConvert.CreateValuesFilter(values));
  171. }
  172. else
  173. {
  174. filterList.Add(filter);
  175. }
  176. }
  177. return filterList.ToArray();
  178. }
  179. private IUnit unit;
  180. public virtual IUnit Unit
  181. {
  182. get
  183. {
  184. return Parent != null ? Parent.Unit : unit;
  185. }
  186. set
  187. {
  188. if (Parent == null)
  189. {
  190. unit = value;
  191. }
  192. else
  193. {
  194. throw new Exception("Can't set unit for a filtered function.");
  195. }
  196. }
  197. }
  198. public Type ValueType
  199. {
  200. get
  201. {
  202. if (variableToConvert == Parent)
  203. return typeof(TTarget);
  204. return Parent.ValueType;
  205. }
  206. set
  207. {
  208. throw new NotImplementedException();
  209. }
  210. }
  211. public TVariable DefaultValue
  212. {
  213. get
  214. {
  215. if ((variableToConvert == Parent) && typeof(TVariable) == typeof(TTarget))
  216. {
  217. //no nice cast here :(
  218. return (TVariable)(object)toTarget((TSource) Parent.DefaultValue);
  219. }
  220. return (TVariable) Parent.DefaultValue;
  221. }
  222. set { throw new NotImplementedException(); }
  223. }
  224. public IList<TVariable> NoDataValues
  225. {
  226. get;
  227. set;
  228. }
  229. public virtual object NoDataValue
  230. {
  231. get
  232. {
  233. return NoDataValues.Count != 0 ? (object)NoDataValues[0] : null;
  234. }
  235. set
  236. {
  237. NoDataValues.Clear();
  238. NoDataValues.Add((TVariable)value);
  239. }
  240. }
  241. public IMultiDimensionalArray<TVariable> Values
  242. {
  243. get
  244. {
  245. return GetValues<TVariable>();
  246. }
  247. set
  248. {
  249. SetValues(value);
  250. }
  251. }
  252. object IMeasurable.DefaultValue
  253. {
  254. get ; set;
  255. }
  256. object IMeasurable.MinValidValue { get; set; }
  257. object IMeasurable.MaxValidValue { get; set; }
  258. IMeasurable IMeasurable.Clone()
  259. {
  260. return Clone();
  261. }
  262. IMultiDimensionalArray IVariable.Values
  263. {
  264. get
  265. {
  266. return base.GetValues();
  267. }
  268. set
  269. {
  270. SetValues((IEnumerable<TVariable>) value);
  271. }
  272. }
  273. public object DefaultStep
  274. {
  275. get; set;
  276. }
  277. IList IVariable.NoDataValues
  278. {
  279. get;
  280. set;
  281. }
  282. public IDictionary<string, string> Attributes
  283. {
  284. get;
  285. set;
  286. }
  287. public IVariable Filter(params IVariableFilter[] filters)
  288. {
  289. return (IVariable)base.Filter(filters);
  290. }
  291. public int FixedSize
  292. {
  293. get
  294. {
  295. if (Parent != null)
  296. {
  297. return Parent.FixedSize;
  298. }
  299. return 0;
  300. }
  301. set { Parent.FixedSize = value; }
  302. }
  303. public bool IsFixedSize
  304. {
  305. get { return Parent.IsFixedSize; }
  306. }
  307. public IVariable Clone()
  308. {
  309. throw new NotImplementedException();
  310. }
  311. #region IMeasurable Members
  312. public virtual string DisplayName
  313. {
  314. get { return string.Format("{0} [{1}]", Name, ((Unit != null) ? Unit.Symbol : "-")); }
  315. }
  316. #endregion
  317. public IVariableValueFilter CreateValueFilter(object value)
  318. {
  319. return new VariableValueFilter<TVariable>(this, new[] { (TVariable)value });
  320. }
  321. public IVariableValueFilter CreateValuesFilter(IEnumerable values)
  322. {
  323. return new VariableValueFilter<TVariable>(this, values.Cast<TVariable>());
  324. }
  325. public IMultiDimensionalArray CachedValues
  326. {
  327. get;
  328. set;
  329. }
  330. public bool AllowSetInterpolationType { get; set; }
  331. public bool AllowSetExtrapolationType { get; set; }
  332. public bool SkipUniqueValuesCheck
  333. {
  334. get { throw new NotImplementedException(); }
  335. set { throw new NotImplementedException(); }
  336. }
  337. public void CopyFrom(object source)
  338. {
  339. throw new NotImplementedException();
  340. }
  341. }
  342. }