[299] | 1 | # rm::data_structures::item_structure
|
---|
| 2 | # ItemStructure module
|
---|
| 3 | # http://www.openehr.org/uml/release-1.0.1/Browsable/_9_0_76d0249_1109346709572_859750_3810Report.html
|
---|
| 4 | # refs #54
|
---|
| 5 | require 'set'
|
---|
| 6 | include OpenEHR::RM::DataStructures
|
---|
| 7 | module OpenEHR
|
---|
| 8 | module RM
|
---|
| 9 | module DataStructures
|
---|
| 10 | module ItemStructure
|
---|
| 11 | class ItemStructure < DataStructure
|
---|
| 12 | end
|
---|
| 13 |
|
---|
| 14 | class ItemSingle < ItemStructure
|
---|
| 15 | attr_reader :item
|
---|
| 16 |
|
---|
| 17 | def initialize(args = {})
|
---|
| 18 | super(args)
|
---|
| 19 | self.item = args[:item]
|
---|
| 20 | end
|
---|
| 21 |
|
---|
| 22 | def as_hierarchy
|
---|
| 23 | return @item
|
---|
| 24 | end
|
---|
| 25 |
|
---|
| 26 | def item=(item)
|
---|
| 27 | raise ArgumentError, 'item is mandatory' if item.nil?
|
---|
| 28 | @item = item
|
---|
| 29 | end
|
---|
| 30 | end
|
---|
| 31 |
|
---|
| 32 | class ItemList < ItemStructure
|
---|
| 33 | attr_accessor :items
|
---|
| 34 |
|
---|
| 35 | def initialize(args = {})
|
---|
| 36 | super(args)
|
---|
| 37 | self.items = args[:items]
|
---|
| 38 | end
|
---|
| 39 |
|
---|
| 40 | def item_count
|
---|
| 41 | unless @items.nil?
|
---|
| 42 | return @items.size
|
---|
| 43 | else
|
---|
| 44 | return 0
|
---|
| 45 | end
|
---|
| 46 | end
|
---|
| 47 |
|
---|
| 48 | def names
|
---|
| 49 | return @items.collect{|item| item.name}
|
---|
| 50 | end
|
---|
| 51 |
|
---|
| 52 | def named_item(a_name)
|
---|
| 53 | @items.each do |item|
|
---|
| 54 | return item if item.name.value == a_name
|
---|
| 55 | end
|
---|
[369] | 56 | return nil
|
---|
[299] | 57 | end
|
---|
| 58 |
|
---|
| 59 | def ith_item(i)
|
---|
| 60 | raise ArgumentError, 'index invalid' if i <= 0
|
---|
| 61 | return @items[i - 1]
|
---|
| 62 | end
|
---|
| 63 |
|
---|
| 64 | def as_hierarchy
|
---|
| 65 | return Cluster.new(:name => @name,
|
---|
| 66 | :archetype_node_id => @archetype_node_id,
|
---|
| 67 | :items => @items)
|
---|
| 68 | end
|
---|
| 69 | end
|
---|
| 70 |
|
---|
| 71 | class ItemTable < ItemStructure
|
---|
| 72 | attr_accessor :rows
|
---|
| 73 |
|
---|
| 74 | def initialize(args = {})
|
---|
| 75 | super(args)
|
---|
| 76 | self.rows = args[:rows]
|
---|
| 77 | end
|
---|
| 78 |
|
---|
| 79 | def row_count
|
---|
| 80 | if @rows.nil?
|
---|
| 81 | return 0
|
---|
| 82 | else
|
---|
| 83 | return @rows.size
|
---|
| 84 | end
|
---|
| 85 | end
|
---|
| 86 |
|
---|
| 87 | def column_count
|
---|
| 88 | if @rows.nil?
|
---|
| 89 | return 0
|
---|
| 90 | else
|
---|
| 91 | return @rows[0].items.count
|
---|
| 92 | end
|
---|
| 93 | end
|
---|
| 94 |
|
---|
| 95 | def row_names
|
---|
| 96 | if @rows.nil?
|
---|
| 97 | return []
|
---|
| 98 | else
|
---|
| 99 | return @rows.collect{|r| r.name}
|
---|
| 100 | end
|
---|
| 101 | end
|
---|
| 102 |
|
---|
| 103 | def column_names
|
---|
| 104 | if @rows.nil?
|
---|
| 105 | return []
|
---|
| 106 | else
|
---|
| 107 | return @rows[0].items.collect{|i| i.name}
|
---|
| 108 | end
|
---|
| 109 | end
|
---|
| 110 |
|
---|
| 111 | def ith_row(i)
|
---|
| 112 | raise ArgumentError, 'invalid index' if i<=0 or i>@rows.size
|
---|
[369] | 113 | return @rows[i - 1]
|
---|
[299] | 114 | end
|
---|
| 115 |
|
---|
| 116 | def has_row_with_name?(key)
|
---|
| 117 | raise ArgumentError, 'invalid argument' if key.nil? or key.empty?
|
---|
| 118 | @rows.each do |row|
|
---|
| 119 | return true if row.items[0].name.value == key
|
---|
| 120 | end
|
---|
| 121 | return false
|
---|
| 122 | end
|
---|
| 123 |
|
---|
| 124 | def has_column_with_name?(key)
|
---|
| 125 | raise ArgumentError, 'invalid argument' if key.nil? or key.empty?
|
---|
| 126 | self.column_names.each do |name|
|
---|
| 127 | return true if name.value == key
|
---|
| 128 | end
|
---|
| 129 | return false
|
---|
| 130 | end
|
---|
| 131 |
|
---|
| 132 | def named_row(key)
|
---|
| 133 | raise ArgumentError, 'invalid argument' unless has_row_with_name?(key)
|
---|
| 134 | @rows.each do |row|
|
---|
| 135 | return row if row.items[0].name.value == key
|
---|
| 136 | end
|
---|
| 137 | end
|
---|
| 138 |
|
---|
| 139 | def has_row_with_key?(keys)
|
---|
| 140 | keys.each do |key|
|
---|
| 141 | @rows.each do |row|
|
---|
| 142 | return true if row.items[0].name.value == key
|
---|
| 143 | end
|
---|
| 144 | end
|
---|
| 145 | return false
|
---|
| 146 | end
|
---|
| 147 |
|
---|
| 148 | def row_with_key(keys)
|
---|
| 149 | unless has_row_with_key?(keys)
|
---|
| 150 | raise ArgumentError, 'no row for key'
|
---|
| 151 | end
|
---|
| 152 | keys.each do |key|
|
---|
| 153 | @rows.each do |row|
|
---|
| 154 | return row if row.items[0].name.value == key
|
---|
| 155 | end
|
---|
| 156 | end
|
---|
| 157 | end
|
---|
| 158 |
|
---|
| 159 | def element_at_cell_ij(i,j)
|
---|
| 160 | return @rows[i-1].items[j-1]
|
---|
| 161 | end
|
---|
| 162 |
|
---|
| 163 | def element_at_named_cell(row_key, column_key)
|
---|
| 164 | i,j=0,0
|
---|
| 165 | @rows[0].items.each do |c|
|
---|
| 166 | break if c.name.value == column_key
|
---|
| 167 | i+=1
|
---|
| 168 | end
|
---|
| 169 | @rows.each do |row|
|
---|
| 170 | break if row.name.value == row_key
|
---|
| 171 | j+=1
|
---|
| 172 | end
|
---|
| 173 | return element_at_cell_ij(i,j)
|
---|
| 174 | end
|
---|
| 175 |
|
---|
| 176 | def as_hierarchy
|
---|
| 177 | return @rows[0]
|
---|
| 178 | end
|
---|
| 179 | end
|
---|
| 180 |
|
---|
| 181 | class ItemTree < ItemStructure
|
---|
| 182 | attr_accessor :items
|
---|
| 183 |
|
---|
| 184 | def initialize(args ={ })
|
---|
| 185 | super(args)
|
---|
| 186 | self.items = args[:items]
|
---|
| 187 | end
|
---|
[369] | 188 |
|
---|
| 189 | def has_element_path?(path)
|
---|
| 190 | paths = [ ]
|
---|
| 191 | @items.each do |item|
|
---|
| 192 | paths << item.archetype_node_id
|
---|
| 193 | end
|
---|
| 194 | return paths.include? path
|
---|
| 195 | end
|
---|
| 196 |
|
---|
| 197 | def element_at_path(path)
|
---|
| 198 | @items.each do |item|
|
---|
| 199 | return item if item.archetype_node_id == path
|
---|
| 200 | end
|
---|
| 201 | return nil
|
---|
| 202 | end
|
---|
| 203 |
|
---|
| 204 | def as_hierarchy
|
---|
| 205 | return Cluster.new(:name => @name,
|
---|
| 206 | :archetype_node_id => @archetype_node_id,
|
---|
| 207 | :items => @items)
|
---|
| 208 | end
|
---|
[299] | 209 | end
|
---|
| 210 | end # of ItemStructure
|
---|
| 211 | end # of DataStructures
|
---|
| 212 | end # of RM
|
---|
| 213 | end # of OpenEHR
|
---|