89572862f35f0abeb6b80e0413b0f1a249ba91b6.svn-base 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using iBemsDataService.Controllers.Bems.Common;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Web;
  6. namespace iBemsDataService.Controllers.Bems.Formula
  7. {
  8. public class ParameterBox
  9. {
  10. public DateTime DateTime { get ; set; }
  11. Box[] boxes;
  12. public ParameterBox( CalculationParameter[] parameters )
  13. {
  14. this.boxes = new Box[parameters.Length];
  15. for( int i = 0 ; i < parameters.Length ; i++ )
  16. {
  17. var box = this.boxes[i] = new Box();
  18. box.parameter = parameters[i];
  19. }
  20. }
  21. public bool NextStage()
  22. {
  23. for( var i = 0 ; i < this.boxes.Length ; i++ )
  24. {
  25. var box = this.boxes[i];
  26. box.Set = false;
  27. if( box.Next() == false )
  28. {
  29. return false;
  30. }
  31. }
  32. return true;
  33. }
  34. public bool Sync()
  35. {
  36. if (this.boxes == null || this.boxes.Length == 0) return false;
  37. var value = this.boxes[0].GetValue();
  38. if( value == null )
  39. return false;
  40. var dateTime = value.DateTime;
  41. Loop:
  42. for( var i = 0 ; i < this.boxes.Length ; i++ )
  43. {
  44. var box = this.boxes[i];
  45. if( box.Set )
  46. {
  47. continue;
  48. }
  49. switch( box.Sync( dateTime ) )
  50. {
  51. case -1: // 없다. 더이상 계산이 필요 없다.
  52. return false;
  53. case 0: // 동일한 것이 있다.
  54. box.Set = true;
  55. break;
  56. case 1: // 초과한 시간이 있다.
  57. dateTime = box.GetValue().DateTime;
  58. goto Loop;
  59. }
  60. }
  61. this.DateTime = dateTime;
  62. return true;
  63. }
  64. class Box
  65. {
  66. public enum IndexAtDateTimeResult
  67. {
  68. };
  69. public bool Set { get; set; }
  70. public int currentIndex = 0;
  71. public CalculationParameter parameter;
  72. public IndexAtDateTimeResult Result { get; set; }
  73. public Box()
  74. {
  75. Set = false;
  76. }
  77. public bool Next()
  78. {
  79. this.currentIndex++;
  80. return (this.currentIndex < parameter.Values.Length);
  81. }
  82. public int Sync( DateTime dateTime )
  83. {
  84. var values = parameter.Values;
  85. if( values != null )
  86. {
  87. for( var i = currentIndex ; i < values.Length ; i++ )
  88. {
  89. if( values[i].DateTime == dateTime )
  90. {
  91. this.currentIndex = i;
  92. this.Setup();
  93. return 0;
  94. }
  95. if( dateTime < values[i].DateTime )
  96. {
  97. this.currentIndex = i;
  98. return 1;
  99. }
  100. }
  101. }
  102. return -1;
  103. }
  104. public CalculationValue GetValue()
  105. {
  106. if( parameter.Values == null )
  107. return null;
  108. if( parameter.Values.Length <= currentIndex )
  109. return null;
  110. return parameter.Values[currentIndex];
  111. }
  112. public void Setup()
  113. {
  114. this.Set = true;
  115. this.parameter.SetValueFromIndex( currentIndex );
  116. }
  117. }
  118. }
  119. }