class TaskJuggler::Limits::Limit

This class implements a mechanism that can be used to limit certain events within a certain time period. It supports an upper and a lower limit.

Attributes

interval[R]
name[R]
resource[RW]
upper[R]

Public Class Methods

new(name, interval, period, value, upper, resource) click to toggle source

To create a new Limit object, the Interval interval and the period duration (period in seconds) must be specified. This creates a counter for each period within the overall interval. value is the value of the limit. upper specifies whether the limit is an upper or lower limit. The limit can also be restricted to certain a Resource specified by resource.

# File lib/taskjuggler/Limits.rb, line 37
def initialize(name, interval, period, value, upper, resource)
  @name = name
  @interval = interval
  @period = period
  @value = value
  @upper = upper
  @resource = resource

  # To avoid multiple resets of untouched scoreboards we keep this dirty
  # flag. It's set whenever a counter is increased.
  @dirty = true
  reset
end

Public Instance Methods

copy() click to toggle source

Returns a deep copy of the class instance.

# File lib/taskjuggler/Limits.rb, line 52
def copy
  Limit.new(@name, @interval, @period, @value, @upper, @resource)
end
dec(index, resource) click to toggle source

Decrease the counter if the index matches the @interval. The relationship between @resource and resource is described below. @r \ r nil y nil inc inc

x         -    if x==y inc else -
# File lib/taskjuggler/Limits.rb, line 91
def dec(index, resource)
  if @interval.contains?(index) &&
     (@resource.nil? || @resource == resource)
    # The condition is met, decrement the counter for the interval.
    @dirty = true
    @scoreboard[idxToSbIdx(index)] -= 1
  end
end
inc(index, resource) click to toggle source

Increase the counter if the index matches the @interval. The relationship between @resource and resource is described below. @r \ r nil y nil inc inc

x         -    if x==y inc else -
# File lib/taskjuggler/Limits.rb, line 77
def inc(index, resource)
  if @interval.contains?(index) &&
     (@resource.nil? || @resource == resource)
    # The condition is met, increment the counter for the interval.
    @dirty = true
    @scoreboard[idxToSbIdx(index)] += 1
  end
end
ok?(index, upper, resource) click to toggle source

Returns true if the counter for the time slot specified by index or all counters are within the limit. If upper is true, only upper limits are checked. If not, only lower limits are checked. The dependency between resource and @resource is described in the matrix below: @r \ r nil y nil test true

x        true  if x==y test else true
# File lib/taskjuggler/Limits.rb, line 108
def ok?(index, upper, resource)
  # if @upper does not match or the provided resource does not match,
  # we can ignore this limit.
  return true if @upper != upper || (@resource && @resource != resource)

  if index.nil?
    # No index given. We need to check all counters.
    @scoreboard.each do |i|
      return false if @upper ? i >= @value : i < @value
    end
    return true
  else
    # If the index is outside the interval we don't have to check
    # anything. Everything is ok.
    return true if !@interval.contains?(index)

    sbVal = @scoreboard[idxToSbIdx(index)]
    return @upper ? (sbVal < @value) : (sbVal >= @value)
  end
end
reset(index = nil) click to toggle source

This function can be used to reset the counter for a specific period specified by index or to reset all counters.

# File lib/taskjuggler/Limits.rb, line 58
def reset(index = nil)
  return unless @dirty

  if index.nil?
    @scoreboard = Scoreboard.new(@interval.startDate, @interval.endDate,
                                 @period, 0)
  else
    return unless @interval.contains?(index)
    # The scoreboard may be just a subset of the @interval period.
    @scoreboard[idxToSbIdx(index)] = 0
  end
  @dirty = false
end