The PropertyList is a utility class that can be used to hold a list of properties. It’s derived from an Array, so it can hold the properties in a well defined order. The order can be determined by an arbitrary number of sorting levels. A sorting level specifies an attribute who’s value should be used for sorting, a scenario index if necessary and the sorting direction (up/down). All nodes in the PropertyList must belong to the same PropertySet.
A PropertyList is always bound to a certain PropertySet. All properties in the list must be of that set.
# File lib/PropertyList.rb, line 30 30: def initialize(arg, copyItems = true) 31: @items = copyItems ? arg.to_ary : [] 32: if arg.is_a?(PropertySet) 33: # Create a PropertyList from the given PropertySet. 34: @propertySet = arg 35: # To keep the list sorted, we may have to access Property attributes. 36: # Pre-scheduling, we can only use static attributes. Post-scheduling, 37: # we can include dynamic attributes as well. This query template will 38: # be used to query attributes when it has been set. Otherwise the list 39: # can only be sorted by static attributes. 40: @query = nil 41: resetSorting 42: addSortingCriteria('seqno', true, 1) 43: sort! 44: else 45: # Create a PropertyList from a given other PropertyList. 46: @propertySet = arg.propertySet 47: @query = arg.query ? arg.query.dup : nil 48: @sortingLevels = arg.sortingLevels 49: @sortingCriteria = arg.sortingCriteria.dup 50: @sortingUp = arg.sortingUp.dup 51: @scenarioIdx = arg.scenarioIdx.dup 52: end 53: end
Append a new sorting level to the existing levels.
# File lib/PropertyList.rb, line 98 98: def addSortingCriteria(criteria, up, scIdx) 99: unless @propertySet.knownAttribute?(criteria) || 100: @propertySet.hasQuery?(criteria, scIdx) 101: raise TjException.new, "Unknown attribute #{criteria} used for " + 102: 'sorting criterium' 103: end 104: if scIdx == 1 105: if @propertySet.scenarioSpecific?(criteria) 106: raise TjException.new, "Attribute #{criteria} is scenario specific." + 107: "You must specify a scenario id." 108: end 109: else 110: if @propertySet.project.scenario(scIdx).nil? 111: raise TjException.new, "Unknown scenario index #{scIdx} used." 112: end 113: if !@propertySet.scenarioSpecific?(criteria) 114: raise TjException.new, "Attribute #{criteria} is not scenario specific" 115: end 116: end 117: @sortingCriteria.push(criteria) 118: @sortingUp.push(up) 119: @scenarioIdx.push(scIdx) 120: @sortingLevels += 1 121: end
Append another Array of PropertyTreeNodes or a PropertyList to this. The list will be sorted again.
# File lib/PropertyList.rb, line 84 84: def append(list) 85: if $DEBUG 86: list.each do |node| 87: unless node.propertySet == @propertySet 88: raise "Fatal Error: All nodes must belong to the same PropertySet." 89: end 90: end 91: end 92: 93: @items.concat(list) 94: sort! 95: end
This function sets the index attribute of all the properties in the list. The index starts with 0 and increases for each property.
# File lib/PropertyList.rb, line 172 172: def index 173: i = 0 174: @items.each do |p| 175: p.set('index', i += 1) 176: end 177: end
Return the Array index of item or nil.
# File lib/PropertyList.rb, line 166 166: def itemIndex(item) 167: @items.index(item) 168: end
This class should be a derived class of Array. But since it re-defines sort!() and still needs to call Array::sort!() I took a different route. All missing methods will be propagated to the @items Array.
# File lib/PropertyList.rb, line 58 58: def method_missing(func, *args, &block) 59: @items.method(func).call(*args, &block) 60: end
Clear all sorting levels.
# File lib/PropertyList.rb, line 75 75: def resetSorting 76: @sortingLevels = 0 77: @sortingCriteria = [] 78: @sortingUp = [] 79: @scenarioIdx = [] 80: end
Set all sorting levels as Array of triplets.
# File lib/PropertyList.rb, line 67 67: def setSorting(modes) 68: resetSorting 69: modes.each do |mode| 70: addSortingCriteria(*mode) 71: end 72: end
Sort the properties according to the currently defined sorting criteria.
# File lib/PropertyList.rb, line 131 131: def sort! 132: if treeMode? 133: # Tree sorting is somewhat complex. It will be based on the 'tree' 134: # attribute of the PropertyTreeNodes but we have to update them first 135: # based on the other sorting criteria. 136: 137: # Remove the tree sorting mode first. 138: sc = @sortingCriteria.delete_at(0) 139: su = @sortingUp.delete_at(0) 140: si = @scenarioIdx.delete_at(0) 141: @sortingLevels -= 1 142: 143: # Sort the list based on the rest of the modes. 144: sortInternal 145: # The update the 'index' attributes of the PropertyTreeNodes. 146: index 147: # An then the 'tree' attributes. 148: indexTree 149: 150: # Restore the 'tree' sorting mode again. 151: @sortingCriteria.insert(0, sc) 152: @sortingUp.insert(0, su) 153: @scenarioIdx.insert(0, si) 154: @sortingLevels += 1 155: 156: # Sort again, now based on the updated 'tree' attributes. 157: sortInternal 158: else 159: sortInternal 160: end 161: # Update indexes. 162: index 163: end
# File lib/PropertyList.rb, line 62 62: def to_ary 63: @items.dup 64: end
If the first sorting level is ‘tree’ the breakdown structure of the list is preserved. This is a somewhat special mode and this function returns true if the mode is set.
# File lib/PropertyList.rb, line 126 126: def treeMode? 127: @sortingLevels > 0 && @sortingCriteria[0] == 'tree' 128: end
Update the ‘tree’ indicies that are needed for the ‘tree’ sorting mode.
# File lib/PropertyList.rb, line 194 194: def indexTree 195: @items.each do |property| 196: # The indicies are an Array if the 'index' attributes for this 197: # property and all its parents. 198: treeIdcs = property.getIndicies 199: # Now convert them to a String. 200: tree = '' 201: treeIdcs.each do |idx| 202: # Prefix the level index with zeros so that we always have a 6 203: # digit long String. 6 digits should be large enough for all 204: # real-world projects. 205: tree += idx.to_s.rjust(6, '0') 206: end 207: property.set('tree', tree) 208: end 209: end
# File lib/PropertyList.rb, line 211 211: def sortInternal 212: @items.sort! do |a, b| 213: res = 0 214: @sortingLevels.times do |i| 215: if @query 216: # In case we have a Query reference, we get the two values with this 217: # query. 218: @query.scenarioIdx = @scenarioIdx[i] < 0 ? nil : @scenarioIdx[i] 219: @query.attributeId = @sortingCriteria[i] 220: 221: @query.property = a 222: @query.process 223: aVal = @query.to_sort 224: 225: @query.property = b 226: @query.process 227: bVal = @query.to_sort 228: else 229: # In case we don't have a query, we use the static mechanism. 230: # If the scenario index is negative we have a non-scenario-specific 231: # attribute. 232: if @scenarioIdx[i] < 0 233: if @sortingCriteria[i] == 'id' 234: aVal = a.fullId 235: bVal = b.fullId 236: else 237: aVal = a.get(@sortingCriteria[i]) 238: bVal = b.get(@sortingCriteria[i]) 239: end 240: else 241: aVal = a[@sortingCriteria[i], @scenarioIdx[i]] 242: bVal = b[@sortingCriteria[i], @scenarioIdx[i]] 243: end 244: end 245: res = aVal <=> bVal 246: # Invert the result if we have to sort in decreasing order. 247: res = -res unless @sortingUp[i] 248: # If the two elements are equal on this compare level we try the next 249: # level. 250: break if res != 0 251: end 252: res 253: end 254: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.