Class: ActiveFacts::Metamodel::EntityType
- Includes:
- ActiveFacts
- Defined in:
- lib/activefacts/persistence/columns.rb,
lib/activefacts/persistence/tables.rb,
lib/activefacts/vocabulary/metamodel.rb,
lib/activefacts/persistence/reference.rb,
lib/activefacts/vocabulary/extensions.rb
Overview
The EntityType class is defined in the metamodel; full documentation is not generated. This section shows the features relevant to relational Persistence.
Constant Summary
Constants included from ActiveFacts
Instance Attribute Summary
Attributes inherited from Concept
Instance Method Summary collapse
-
#absorbed_via ⇒ Object
A Reference from an entity type that fully absorbs this one.
-
#absorbed_via=(r) ⇒ Object
:nodoc:.
-
#all_columns(excluded_supertypes) ⇒ Object
When absorbing this EntityType, what columns must be absorbed? This must be a fresh copy, because the columns will have References prepended.
- #all_supertype_inheritance ⇒ Object
-
#identifier_columns ⇒ Object
The identifier_columns for an EntityType are the columns that result from the identifying roles.
-
#identifying_supertype ⇒ Object
A subtype does not have a identifying_supertype if it defines its own identifier.
-
#is_auto_assigned ⇒ Object
:nodoc:.
-
#is_table ⇒ Object
Returns true if this EntityType is a table.
-
#populate_references ⇒ Object
:nodoc:.
- #preferred_identifier ⇒ Object
-
#reference_columns(excluded_supertypes) ⇒ Object
When creating a foreign key to this EntityType, what columns must we include (the identifier columns)? This must be a fresh copy, because the columns will have References prepended.
-
#subtypes ⇒ Object
An array of all direct subtypes:.
-
#supertypes ⇒ Object
An array all direct supertypes.
-
#supertypes_transitive ⇒ Object
An array of self followed by all supertypes in order:.
Methods included from ActiveFacts
Methods inherited from Concept
#all_absorbed_foreign_key_reference_path, #clear_indices, #clear_references, #columns, #definitely_not_table, #definitely_table, #foreign_keys, #has_references, #indices, #populate_columns, #populate_indices, #populate_reference, #probably_not_table, #probably_table, #references_from, #references_to, #show_tabular
Instance Method Details
#absorbed_via ⇒ Object
A Reference from an entity type that fully absorbs this one
60 |
# File 'lib/activefacts/persistence/tables.rb', line 60 def absorbed_via; @absorbed_via; end |
#absorbed_via=(r) ⇒ Object
:nodoc:
61 62 63 |
# File 'lib/activefacts/persistence/tables.rb', line 61 def absorbed_via=(r) #:nodoc: @absorbed_via = r end |
#all_columns(excluded_supertypes) ⇒ Object
When absorbing this EntityType, what columns must be absorbed? This must be a fresh copy, because the columns will have References prepended.
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/activefacts/persistence/columns.rb', line 298 def all_columns(excluded_supertypes) #:nodoc: debug :columns, "All Columns for #{name}" do columns = [] sups = supertypes references_from.sort_by do |ref| # Put supertypes first, in order, then non-subtype references, then subtypes, otherwise retaining their order: sups.index(ref.to) || (!ref.fact_type.is_a?(TypeInheritance) && references_from.size+references_from.index(ref)) || references_from.size*2+references_from.index(ref) end.each do |ref| debug :columns, "Columns absorbed via #{ref}" do if (ref.role_type == :supertype) if excluded_supertypes[ref.to] debug :columns, "Exclude #{ref.to.name}, we already inherited it" next end next if (ref.to.absorbed_via != ref) excluded_supertypes[ref.to] = true columns += ref.columns(excluded_supertypes) else columns += ref.columns({}) end end end columns end end |
#all_supertype_inheritance ⇒ Object
270 271 272 273 274 |
# File 'lib/activefacts/vocabulary/extensions.rb', line 270 def all_supertype_inheritance all_type_inheritance_as_subtype.sort_by{|ti| [ti.provides_identification ? 0 : 1, ti.supertype.name] } end |
#identifier_columns ⇒ Object
The identifier_columns for an EntityType are the columns that result from the identifying roles
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/activefacts/persistence/columns.rb', line 255 def identifier_columns debug :columns, "Identifier Columns for #{name}" do if absorbed_via and # If this is a subtype that has its own identification, use that. (all_type_inheritance_as_subtype.size == 0 || all_type_inheritance_as_subtype.detect{|ti| ti.provides_identification }) return absorbed_via.from.identifier_columns end preferred_identifier.role_sequence.all_role_ref.map do |role_ref| ref = references_from.detect {|ref| ref.to_role == role_ref.role} columns.select{|column| column.references[0] == ref} end.flatten end end |
#identifying_supertype ⇒ Object
A subtype does not have a identifying_supertype if it defines its own identifier
292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/activefacts/vocabulary/extensions.rb', line 292 def debug "Looking for identifying_supertype of #{name}" all_type_inheritance_as_subtype.detect{|ti| debug "considering supertype #{ti.supertype.name}" next unless ti.provides_identification debug "found identifying supertype of #{name}, it's #{ti.supertype.name}" return ti.supertype } debug "Failed to find identifying supertype of #{name}" return nil end |
#is_auto_assigned ⇒ Object
:nodoc:
65 66 67 |
# File 'lib/activefacts/persistence/tables.rb', line 65 def is_auto_assigned #:nodoc: false end |
#is_table ⇒ Object
Returns true if this EntityType is a table
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/activefacts/persistence/tables.rb', line 70 def is_table return @is_table if @is_table != nil # We already make a guess or decision @tentative = false # Always a table if marked so if is_independent debug :absorption, "EntityType #{name} is declared independent" return @is_table = true end # Always a table if nowhere else to go, and has no one-to-ones that might flip: if references_to.empty? and !references_from.detect{|ref| ref.role_type == :one_one } debug :absorption, "EntityType #{name} is independent as it has nowhere to go" return @is_table = true end # Subtypes are not a table unless partitioned or separate # REVISIT: Support partitioned subtypes here if (!supertypes.empty?) av = all_supertype_inheritance[0] return @is_table = false end # If the preferred_identifier includes an auto_assigned ValueType # and this object is absorbed in more than one place, we need a table # to manage the auto-assignment. if references_to.size > 1 and preferred_identifier.role_sequence.all_role_ref.detect {|rr| next false unless rr.role.concept.is_a? ValueType rr.role.concept.is_auto_assigned } debug :absorption, "#{name} has an auto-assigned counter in its ID, so must be a table" @tentative = false return @is_table = true end @tentative = true @is_table = true end |
#populate_references ⇒ Object
:nodoc:
319 320 321 322 323 324 325 326 327 |
# File 'lib/activefacts/persistence/reference.rb', line 319 def populate_references #:nodoc: if fact_type && fact_type.all_role.size > 1 # NOT: fact_type.all_role.each do |role| # Place roles in the preferred order instead: fact_type.preferred_reading.role_sequence.all_role_ref.map(&:role).each do |role| populate_reference role # Objectified fact role, handled specially end end super end |
#preferred_identifier ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/activefacts/vocabulary/extensions.rb', line 123 def preferred_identifier if fact_type # For a nested fact type, the PI is a unique constraint over N or N-1 roles fact_roles = Array(fact_type.all_role) debug :pi, "Looking for PI on nested fact type #{name}" do pi = catch :pi do fact_roles[0,2].each{|r| # Try the first two roles of the fact type, that's enough r.all_role_ref.map{|rr| # All role sequences that reference this role role_sequence = rr.role_sequence # The role sequence is only interesting if it cover only this fact's roles # or roles of the objectification next if role_sequence.all_role_ref.size < fact_roles.size-1 # Not enough roles next if role_sequence.all_role_ref.size > fact_roles.size # Too many roles next if role_sequence.all_role_ref.detect do |rsr| if (of = rsr.role.fact_type) != fact_type case of.all_role.size when 1 # A unary FT must be played by the objectification of this fact type next rsr.role.concept != fact_type.entity_type when 2 # A binary FT must have the objectification of this FT as the other player other_role = (of.all_role-[rsr.role])[0] next other_role.concept != fact_type.entity_type else next true # A role in a ternary (or higher) cannot be usd in our identifier end end rsr.role.fact_type != fact_type end # This role sequence is a candidate pc = role_sequence.all_presence_constraint.detect{|c| c.max_frequency == 1 && c.is_preferred_identifier } throw :pi, pc if pc } } throw :pi, nil end debug :pi, "Got PI #{pi.name||pi.object_id} for nested #{name}" if pi debug :pi, "Looking for PI on entity that nests this fact" unless pi raise "Oops, pi for nested fact is #{pi.class}" unless !pi || PresenceConstraint === pi return pi if pi end end debug :pi, "Looking for PI for ordinary entity #{name} with #{all_role.size} roles:" do debug :pi, "Roles are in fact types #{all_role.map{|r| r.fact_type.describe(r)}*", "}" pi = catch :pi do all_supertypes = supertypes_transitive debug :pi, "PI roles must be played by one of #{all_supertypes.map(&:name)*", "}" if all_supertypes.size > 1 all_role.each{|role| next unless role.unique || fact_type ftroles = Array(role.fact_type.all_role) # Skip roles in ternary and higher fact types, they're objectified, and in unaries, they can't identify us. next if ftroles.size != 2 debug :pi, "Considering role in #{role.fact_type.describe(role)}" # Find the related role which must be included in any PI: # Note this works with unary fact types: pi_role = ftroles[ftroles[0] != role ? 0 : -1] next if ftroles.size == 2 && pi_role.concept == self debug :pi, " Considering #{pi_role.concept.name} as a PI role" # If this is an identifying role, the PI is a PC whose role_sequence spans the role. # Walk through all role_sequences that span this role, and test each: pi_role.all_role_ref.each{|rr| role_sequence = rr.role_sequence # A role sequence that includes a possible role debug :pi, " Considering role sequence #{role_sequence.describe}" # All roles in this role_sequence must be in fact types which # (apart from that role) only have roles played by the original # entity type or a supertype. #debug :pi, " All supertypes #{all_supertypes.map{|st| "#{st.object_id}=>#{st.name}"}*", "}" if role_sequence.all_role_ref.detect{|rsr| fact_type = rsr.role.fact_type debug :pi, " Role Sequence touches #{fact_type.describe(pi_role)}" fact_type_roles = fact_type.all_role debug :pi, " residual is #{fact_type_roles.map{|r| r.concept.name}.inspect} minus #{rsr.role.concept.name}" residual_roles = fact_type_roles-[rsr.role] residual_roles.detect{|rfr| debug :pi, " Checking residual role #{rfr.concept.object_id}=>#{rfr.concept.name}" # This next line looks right, but breaks things. Find out what and why: # !rfr.unique or !all_supertypes.include?(rfr.concept) } } debug :pi, " Discounting this role_sequence because it includes alien roles" next end # Any presence constraint over this role sequence is a candidate rr.role_sequence.all_presence_constraint.detect{|pc| # Found it! if pc.is_preferred_identifier debug :pi, "found PI #{pc.name||pc.object_id}, is_preferred_identifier=#{pc.is_preferred_identifier.inspect} over #{pc.role_sequence.describe}" throw :pi, pc end } } } throw :pi, nil end raise "Oops, pi for entity is #{pi.class}" if pi && !(PresenceConstraint === pi) debug :pi, "Got PI #{pi.name||pi.object_id} for #{name}" if pi if !pi if (supertype = ) # This shouldn't happen now, as an identifying supertype is connected by a fact type # that has a uniqueness constraint marked as the preferred identifier. #debug :pi, "PI not found for #{name}, looking in supertype #{supertype.name}" #pi = supertype.preferred_identifier #return nil elsif fact_type fact_type.all_role.each{|role| role.all_role_ref.each{|role_ref| # Discount role sequences that contain roles not in this fact type: next if role_ref.role_sequence.all_role_ref.detect{|rr| rr.role.fact_type != fact_type } role_ref.role_sequence.all_presence_constraint.each{|pc| next unless pc.is_preferred_identifier and pc.max_frequency == 1 pi = pc break } break if pi } break if pi } else debug :pi, "No PI found for #{name}" end end raise "No PI found for #{name}" unless pi pi end end |
#reference_columns(excluded_supertypes) ⇒ Object
When creating a foreign key to this EntityType, what columns must we include (the identifier columns)? This must be a fresh copy, because the columns will have References prepended
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/activefacts/persistence/columns.rb', line 274 def reference_columns(excluded_supertypes) #:nodoc: debug :columns, "Reference Columns for #{name}" do if absorbed_via and # If this is a subtype that has its own identification, use that. (all_type_inheritance_as_subtype.size == 0 || all_type_inheritance_as_subtype.detect{|ti| ti.provides_identification }) return absorbed_via.from.reference_columns(excluded_supertypes) end # REVISIT: Should have built preferred_identifier_references preferred_identifier.role_sequence.all_role_ref.map do |role_ref| # REVISIT: Should index references by to_role: ref = references_from.detect {|ref| ref.to_role == role_ref.role} raise "reference for role #{role.describe} not found on #{name} in #{references_from.size} references:\n\t#{references_from.map(&:to_s)*"\n\t"}" unless ref ref.columns({}) end.flatten end end |
#subtypes ⇒ Object
An array of all direct subtypes:
265 266 267 268 |
# File 'lib/activefacts/vocabulary/extensions.rb', line 265 def subtypes # REVISIT: There's no sorting here. Should there be? all_type_inheritance_as_supertype.map{|ti| ti.subtype } end |
#supertypes ⇒ Object
An array all direct supertypes
277 278 279 280 281 |
# File 'lib/activefacts/vocabulary/extensions.rb', line 277 def supertypes all_supertype_inheritance.map{|ti| ti.supertype } end |
#supertypes_transitive ⇒ Object
An array of self followed by all supertypes in order:
284 285 286 287 288 289 |
# File 'lib/activefacts/vocabulary/extensions.rb', line 284 def supertypes_transitive ([self] + all_type_inheritance_as_subtype.map{|ti| # debug ti.class.roles.verbalise; exit ti.supertype.supertypes_transitive }).flatten.uniq end |