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 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 146 146: def index 147: i = 0 148: @items.each do |p| 149: p.set('index', i += 1) 150: end 151: end
Return the Array index of item or nil.
# File lib/PropertyList.rb, line 140 140: def itemIndex(item) 141: @items.index(item) 142: 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 105 105: def sort! 106: if treeMode? 107: # Tree sorting is somewhat complex. It will be based on the 'tree' 108: # attribute of the PropertyTreeNodes but we have to update them first 109: # based on the other sorting criteria. 110: 111: # Remove the tree sorting mode first. 112: sc = @sortingCriteria.delete_at(0) 113: su = @sortingUp.delete_at(0) 114: si = @scenarioIdx.delete_at(0) 115: @sortingLevels -= 1 116: 117: # Sort the list based on the rest of the modes. 118: sortInternal 119: # The update the 'index' attributes of the PropertyTreeNodes. 120: index 121: # An then the 'tree' attributes. 122: indexTree 123: 124: # Restore the 'tree' sorting mode again. 125: @sortingCriteria.insert(0, sc) 126: @sortingUp.insert(0, su) 127: @scenarioIdx.insert(0, si) 128: @sortingLevels += 1 129: 130: # Sort again, now based on the updated 'tree' attributes. 131: sortInternal 132: else 133: sortInternal 134: end 135: # Update indexes. 136: index 137: 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 100 100: def treeMode? 101: @sortingLevels > 0 && @sortingCriteria[0] == 'tree' 102: end
Append a new sorting level to the existing levels.
# File lib/PropertyList.rb, line 168 168: def addSortingCriteria(criteria, up, scIdx) 169: unless @propertySet.knownAttribute?(criteria) || 170: @propertySet.hasQuery?(criteria, scIdx) 171: raise "Unknown attribute #{criteria} used for sorting criterium" 172: end 173: if scIdx == 1 174: if @propertySet.scenarioSpecific?(criteria) 175: raise "Attribute #{criteria} is scenario specific." + 176: "You must specify a scenario id." 177: end 178: else 179: if @propertySet.project.scenario(scIdx).nil? 180: raise "Unknown scenario index #{scIdx} used." 181: end 182: if !@propertySet.scenarioSpecific?(criteria) 183: raise "Attribute #{criteria} is not scenario specific" 184: end 185: end 186: @sortingCriteria.push(criteria) 187: @sortingUp.push(up) 188: @scenarioIdx.push(scIdx) 189: @sortingLevels += 1 190: end
Update the ‘tree’ indicies that are needed for the ‘tree’ sorting mode.
# File lib/PropertyList.rb, line 193 193: def indexTree 194: @items.each do |property| 195: # The indicies are an Array if the 'index' attributes for this 196: # property and all its parents. 197: treeIdcs = property.getIndicies 198: # Now convert them to a String. 199: tree = '' 200: treeIdcs.each do |idx| 201: # Prefix the level index with zeros so that we always have a 6 202: # digit long String. 6 digits should be large enough for all 203: # real-world projects. 204: tree += idx.to_s.rjust(6, '0') 205: end 206: property.set('tree', tree) 207: end 208: end
# File lib/PropertyList.rb, line 210 210: def sortInternal 211: @items.sort! do |a, b| 212: res = 0 213: @sortingLevels.times do |i| 214: if @query 215: # In case we have a Query reference, we get the two values with this 216: # query. 217: @query.scenarioIdx = @scenarioIdx[i] < 0 ? nil : @scenarioIdx[i] 218: @query.attributeId = @sortingCriteria[i] 219: 220: @query.property = a 221: @query.process 222: aVal = @query.to_sort 223: 224: @query.property = b 225: @query.process 226: bVal = @query.to_sort 227: else 228: # In case we don't have a query, we use the static mechanism. 229: # If the scenario index is negative we have a non-scenario-specific 230: # attribute. 231: if @scenarioIdx[i] < 0 232: if @sortingCriteria[i] == 'id' 233: aVal = a.fullId 234: bVal = b.fullId 235: else 236: aVal = a.get(@sortingCriteria[i]) 237: bVal = b.get(@sortingCriteria[i]) 238: end 239: else 240: aVal = a[@sortingCriteria[i], @scenarioIdx[i]] 241: bVal = b[@sortingCriteria[i], @scenarioIdx[i]] 242: end 243: end 244: res = aVal <=> bVal 245: # Invert the result if we have to sort in decreasing order. 246: res = -res unless @sortingUp[i] 247: # If the two elements are equal on this compare level we try the next 248: # level. 249: break if res != 0 250: end 251: res 252: end 253: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.