Class: TaskJuggler::ShiftAssignments
- Includes:
- ObjectSpace
- Defined in:
- lib/taskjuggler/ShiftAssignments.rb
Overview
This class manages a list of ShiftAssignment elements. The intervals of the assignments must not overlap.
Since it is fairly costly to determine the onShift and onLeave values for a given date we use a scoreboard to cache all computed values. Changes to the assigment set invalidate the cache again.
To optimize memory usage and computation time the Scoreboard objects for similar ShiftAssignments are shared.
Scoreboard may be nil or a bit vector encoded as an Integer nil: Value has not been determined yet. Bit 0: 0: No assignment
1: Has assignement
Bit 1: 0: Work time (as defined by working hours)
1: No work time (as defined by working hours)
Bit 2 - 5: 0: No holiday or leave time
1: Public holiday (holiday)
2: Annual leave
3: Special leave
4: Sick leave
5: unpaid leave
6: blocked for other projects
7 - 15: Reserved
Bit 6 - 7: Reserved Bit 8: 0: No global override
1: Override global setting
Constant Summary collapse
- @@scoreboards =
This class is sharing the Scoreboard instances for ShiftAssignments that have identical assignment data. This class variable holds a Hash with records for each unique Scoreboard. A record is an array of references to the owning ShiftAssignments objects and a reference to the Scoreboard object.
{}
Instance Attribute Summary collapse
-
#assignments ⇒ Object
readonly
Returns the value of attribute assignments.
-
#project ⇒ Object
Returns the value of attribute project.
Class Method Summary collapse
Instance Method Summary collapse
-
#addAssignment(shiftAssignment) ⇒ Object
Add a new assignment to the list.
-
#assigned?(idx) ⇒ Boolean
Returns true if any of the defined shift periods overlaps with the date or interval specified by idx.
-
#collectTimeOffIntervals(iv, minDuration) ⇒ Object
Return a list of intervals that lay within iv and are at least minDuration long and contain no working time.
-
#getSbSlot(idx) ⇒ Object
This function returns the entry in the scoreboard that corresponds to idx.
- #hashKey ⇒ Object
-
#initialize(sa = nil) ⇒ ShiftAssignments
constructor
A new instance of ShiftAssignments.
-
#onLeave?(idx) ⇒ Boolean
Returns true if any of the defined shift periods contains the date specified by the scoreboard index idx and if the shift has a leave defined for the date.
-
#onShift?(idx) ⇒ Boolean
Returns true if any of the defined shift periods contains the date specified by the scoreboard index idx and the shift has working hours defined for that date.
-
#timeOff?(idx) ⇒ Boolean
Returns true if any of the defined shift periods contains the date specified by the scoreboard index idx and the shift has a leave defined or all off hours defined for that date.
-
#to_s ⇒ Object
This function is primarily used for debugging purposes.
Constructor Details
#initialize(sa = nil) ⇒ ShiftAssignments
Returns a new instance of ShiftAssignments.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/taskjuggler/ShiftAssignments.rb', line 113 def initialize(sa = nil) define_finalizer(self, self.class.method(:deleteScoreboard).to_proc) # An Array of ShiftAssignment objects. @assignments = [] # A String that uniquely identifies the content of this ShiftAssignment # object. @hashKey = nil if sa # A ShiftAssignments object was passed to the contructor. We create a # deep copy of it. @project = sa.project sa.assignments.each do |assignment| @assignments << assignment.copy end # Create a new ScoreBoard or share one with a ShiftAssignments object # that has the same set of shift assignments. @scoreboard = newScoreboard else @project = nil @scoreboard = nil end end |
Instance Attribute Details
#assignments ⇒ Object (readonly)
Returns the value of attribute assignments.
104 105 106 |
# File 'lib/taskjuggler/ShiftAssignments.rb', line 104 def assignments @assignments end |
#project ⇒ Object
Returns the value of attribute project.
103 104 105 |
# File 'lib/taskjuggler/ShiftAssignments.rb', line 103 def project @project end |
Class Method Details
.sbClear ⇒ Object
220 221 222 |
# File 'lib/taskjuggler/ShiftAssignments.rb', line 220 def ShiftAssignments.sbClear @@scoreboards = {} end |
.scoreboards ⇒ Object
216 217 218 |
# File 'lib/taskjuggler/ShiftAssignments.rb', line 216 def ShiftAssignments.scoreboards @@scoreboards end |
Instance Method Details
#addAssignment(shiftAssignment) ⇒ Object
Add a new assignment to the list. In case there was no overlap the function returns true. Otherwise false.
141 142 143 144 145 146 147 148 |
# File 'lib/taskjuggler/ShiftAssignments.rb', line 141 def addAssignment(shiftAssignment) # Make sure we don't insert overlapping assignments. return false if overlaps?(shiftAssignment.interval) @assignments << shiftAssignment @scoreboard = newScoreboard true end |
#assigned?(idx) ⇒ Boolean
Returns true if any of the defined shift periods overlaps with the date or interval specified by idx.
183 184 185 |
# File 'lib/taskjuggler/ShiftAssignments.rb', line 183 def assigned?(idx) (getSbSlot(idx) & 1) == 1 end |
#collectTimeOffIntervals(iv, minDuration) ⇒ Object
Return a list of intervals that lay within iv and are at least minDuration long and contain no working time.
210 211 212 213 214 |
# File 'lib/taskjuggler/ShiftAssignments.rb', line 210 def collectTimeOffIntervals(iv, minDuration) @scoreboard.collectIntervals(iv, minDuration) do |val| (val & 0x3E) != 0 end end |
#getSbSlot(idx) ⇒ Object
This function returns the entry in the scoreboard that corresponds to idx. If the slot has not yet been determined, it’s calculated first.
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 177 178 179 |
# File 'lib/taskjuggler/ShiftAssignments.rb', line 152 def getSbSlot(idx) # Check if we have a value already for this slot. return @scoreboard[idx] unless @scoreboard[idx].nil? date = @scoreboard.idxToDate(idx) # If not, compute it. @assignments.each do |sa| next unless sa.assigned?(date) # Mark the slot as 'assigned'. Meaning, the rest of the bits are valid # for this time slot. @scoreboard[idx] = 1 # Set bit 1 if the shift is not active @scoreboard[idx] |= 1 << 1 unless sa.onShift?(date) # Set bits 2 - 5 to 1 if it's a leave slot. @scoreboard[idx] |= 1 << 3 if sa.onLeave?(date) # Set the 8th bit if the shift replaces global leaves. @scoreboard[idx] |= 1 << 8 if sa.replace?(date) return @scoreboard[idx] end # The slot is not covered by any assignment. @scoreboard[idx] = 0 end |
#hashKey ⇒ Object
241 242 243 244 245 246 247 248 |
# File 'lib/taskjuggler/ShiftAssignments.rb', line 241 def hashKey @hashKey if @hashKey @hashKey = "#{@project.object_id}|" @assignments.sort! { |a, b| a.interval.start <=> b.interval.start } @assignments.each { |a| @hashKey += a.hashKey + '||' } @hashKey end |
#onLeave?(idx) ⇒ Boolean
Returns true if any of the defined shift periods contains the date specified by the scoreboard index idx and if the shift has a leave defined for the date.
204 205 206 |
# File 'lib/taskjuggler/ShiftAssignments.rb', line 204 def onLeave?(idx) (getSbSlot(idx) & 0x3C) != 0 end |
#onShift?(idx) ⇒ Boolean
Returns true if any of the defined shift periods contains the date specified by the scoreboard index idx and the shift has working hours defined for that date.
190 191 192 |
# File 'lib/taskjuggler/ShiftAssignments.rb', line 190 def onShift?(idx) (getSbSlot(idx) & (1 << 1)) == 0 end |
#timeOff?(idx) ⇒ Boolean
Returns true if any of the defined shift periods contains the date specified by the scoreboard index idx and the shift has a leave defined or all off hours defined for that date.
197 198 199 |
# File 'lib/taskjuggler/ShiftAssignments.rb', line 197 def timeOff?(idx) (getSbSlot(idx) & 0x3E) != 0 end |
#to_s ⇒ Object
This function is primarily used for debugging purposes.
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/taskjuggler/ShiftAssignments.rb', line 225 def to_s return '' if @assignments.empty? out = "shifts " first = true @assignments.each do |sa| if first first = false else out += ', ' end out += sa.to_s end out end |