Class: TaskJuggler::Scoreboard
Overview
Scoreboard objects are instrumental during the scheduling process. The project time span is divided into discrete time slots by the scheduling resolution. This class models the resulting time slots with an array that spans from project start o project end. Each slot has an index start with 0 at the project start.
Instance Attribute Summary collapse
-
#endDate ⇒ Object
readonly
Returns the value of attribute endDate.
-
#resolution ⇒ Object
readonly
Returns the value of attribute resolution.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#startDate ⇒ Object
readonly
Returns the value of attribute startDate.
Instance Method Summary collapse
-
#[](idx) ⇒ Object
Get the value at index idx.
-
#[]=(idx, value) ⇒ Object
Set the value at index idx.
-
#clear(initVal = nil) ⇒ Object
Erase all values and set them to nil or a new initial value.
-
#collect! ⇒ Object
Assign result of block to each element.
-
#collectIntervals(iv, minDuration) ⇒ Object
Return a list of intervals that describe a contiguous part of the scoreboard that contains only the values that yield true for the passed block.
-
#dateToIdx(date, forceIntoProject = true) ⇒ Object
Converts a date to the corresponding scoreboard index.
-
#each(startIdx = 0, endIdx = @size) ⇒ Object
Iterate over all scoreboard entries.
-
#each_index ⇒ Object
Iterate over all scoreboard entries by index.
-
#get(date) ⇒ Object
Get the value corresponding to date.
-
#idxToDate(idx, forceIntoProject = false) ⇒ Object
Converts a scroreboard index to the corresponding date.
-
#initialize(startDate, endDate, resolution, initVal = nil) ⇒ Scoreboard
constructor
Create the scoreboard based on the the given startDate, endDate and timing resolution.
- #inspect ⇒ Object
-
#set(date, value) ⇒ Object
Set the value corresponding to date.
Constructor Details
#initialize(startDate, endDate, resolution, initVal = nil) ⇒ Scoreboard
Create the scoreboard based on the the given startDate, endDate and timing resolution. The resolution must be specified in seconds. Optionally you can provide an initial value for the scoreboard cells.
31 32 33 34 35 36 37 |
# File 'lib/taskjuggler/Scoreboard.rb', line 31 def initialize(startDate, endDate, resolution, initVal = nil) @startDate = startDate @endDate = endDate @resolution = resolution @size = ((endDate - startDate) / resolution).ceil + 1 clear(initVal) end |
Instance Attribute Details
#endDate ⇒ Object (readonly)
Returns the value of attribute endDate.
26 27 28 |
# File 'lib/taskjuggler/Scoreboard.rb', line 26 def endDate @endDate end |
#resolution ⇒ Object (readonly)
Returns the value of attribute resolution.
26 27 28 |
# File 'lib/taskjuggler/Scoreboard.rb', line 26 def resolution @resolution end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
26 27 28 |
# File 'lib/taskjuggler/Scoreboard.rb', line 26 def size @size end |
#startDate ⇒ Object (readonly)
Returns the value of attribute startDate.
26 27 28 |
# File 'lib/taskjuggler/Scoreboard.rb', line 26 def startDate @startDate end |
Instance Method Details
#[](idx) ⇒ Object
Get the value at index idx.
98 99 100 |
# File 'lib/taskjuggler/Scoreboard.rb', line 98 def [](idx) @sb[idx] end |
#[]=(idx, value) ⇒ Object
Set the value at index idx.
103 104 105 |
# File 'lib/taskjuggler/Scoreboard.rb', line 103 def []=(idx, value) @sb[idx] = value end |
#clear(initVal = nil) ⇒ Object
Erase all values and set them to nil or a new initial value.
40 41 42 |
# File 'lib/taskjuggler/Scoreboard.rb', line 40 def clear(initVal = nil) @sb = Array.new(@size, initVal) end |
#collect! ⇒ Object
Assign result of block to each element.
93 94 95 |
# File 'lib/taskjuggler/Scoreboard.rb', line 93 def collect! @sb.collect! { |x| yield x } end |
#collectIntervals(iv, minDuration) ⇒ Object
Return a list of intervals that describe a contiguous part of the scoreboard that contains only the values that yield true for the passed block. The intervals must be within the interval described by iv and must be at least minDuration long. The return value is an IntervalList.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/taskjuggler/Scoreboard.rb', line 122 def collectIntervals(iv, minDuration) # Determine the start and stop index for the scoreboard search. We save # the original values for later use as well. startIdx = sIdx = dateToIdx(iv.start) endIdx = eIdx = dateToIdx(iv.end) # Convert the minDuration into number of slots. minDuration /= @resolution minDuration = 1 if minDuration <= 0 # Expand the interval with the minDuration to both sides. This will # reduce the failure to detect intervals at the iv boundary. However, # this will not prevent undetected intervals at the project time frame # boundaries. startIdx -= minDuration startIdx = 0 if startIdx < 0 endIdx += minDuration endIdx = @size - 1 if endIdx > @size - 1 # This is collects the resulting intervals. intervals = IntervalList.new # The duration counter for the currently analyzed interval and the start # index. duration = start = 0 idx = startIdx loop do # Check whether the scoreboard slot matches any of the target values # and we have not yet reached the last slot. if yield(@sb[idx]) && idx < endIdx # If so, save the start position if this is the first slot and start # counting the matching slots. start = idx if start == 0 duration += 1 else # If we don't have a match or are at the end of the interval, check # if we've just finished a matching interval. if duration > 0 if duration >= minDuration # Make sure that all intervals are within the originally # requested Interval. start = sIdx if start < sIdx idx = eIdx if idx > eIdx intervals << TimeInterval.new(idxToDate(start), idxToDate(idx)) end duration = start = 0 end end break if (idx += 1) > endIdx end intervals end |
#dateToIdx(date, forceIntoProject = true) ⇒ Object
Converts a date to the corresponding scoreboard index. You can optionally sanitize the date by forcing it into the project time span.
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/taskjuggler/Scoreboard.rb', line 58 def dateToIdx(date, forceIntoProject = true) idx = ((date - @startDate) / @resolution).to_i if forceIntoProject return 0 if idx < 0 return @size - 1 if idx >= @size elsif (idx < 0 || idx >= @size) raise "Date #{date} is out of project time range " + "(#{@startDate} - #{@endDate})" end idx end |
#each(startIdx = 0, endIdx = @size) ⇒ Object
Iterate over all scoreboard entries.
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/taskjuggler/Scoreboard.rb', line 73 def each(startIdx = 0, endIdx = @size) if startIdx != 0 || endIdx != @size startIdx.upto(endIdx - 1) do |i| yield @sb[i] end else @sb.each do |entry| yield entry end end end |
#each_index ⇒ Object
Iterate over all scoreboard entries by index.
86 87 88 89 90 |
# File 'lib/taskjuggler/Scoreboard.rb', line 86 def each_index @sb.each_index do |index| yield index end end |
#get(date) ⇒ Object
Get the value corresponding to date.
108 109 110 |
# File 'lib/taskjuggler/Scoreboard.rb', line 108 def get(date) @sb[dateToIdx(date)] end |
#idxToDate(idx, forceIntoProject = false) ⇒ Object
Converts a scroreboard index to the corresponding date. You can optionally sanitize the idx value by forcing it into the project range.
46 47 48 49 50 51 52 53 54 |
# File 'lib/taskjuggler/Scoreboard.rb', line 46 def idxToDate(idx, forceIntoProject = false) if forceIntoProject return @startDate if kdx < 0 return @endDate if @size - 1 if idx >= @size elsif idx < 0 || idx >= @size raise "Index #{idx} is out of scoreboard range (#{size - 1})" end @startDate + idx * @resolution end |
#inspect ⇒ Object
178 179 180 181 182 183 184 185 |
# File 'lib/taskjuggler/Scoreboard.rb', line 178 def inspect s = +'' 0.upto(@sb.length - 1) do |i| s << "#{idxToDate(i)}: #{@sb[i]}" end s end |
#set(date, value) ⇒ Object
Set the value corresponding to date.
113 114 115 |
# File 'lib/taskjuggler/Scoreboard.rb', line 113 def set(date, value) @sb[dateToIdx(date)] = value end |