Class: ActiveFacts::Metamodel::Role

Inherits:
Object
  • Object
show all
Defined in:
lib/activefacts/persistence/tables.rb,
lib/activefacts/vocabulary/metamodel.rb,
lib/activefacts/vocabulary/extensions.rb

Overview

EntityType class

Instance Method Summary collapse

Instance Method Details

#describe(highlight = nil) ⇒ Object



276
277
278
# File 'lib/activefacts/vocabulary/extensions.rb', line 276

def describe(highlight = nil)
  object_type.name + (self == highlight ? "*" : "")
end

#is_functionalObject

Return true if this role is functional (has only one instance wrt its player) A role in an objectified fact type is deemed to refer to the implicit role of the objectification.



308
309
310
311
312
# File 'lib/activefacts/vocabulary/extensions.rb', line 308

def is_functional
  fact_type.entity_type or
  fact_type.all_role.size != 2 or
  is_unique
end

#is_mandatoryObject



291
292
293
294
295
296
297
298
299
300
# File 'lib/activefacts/vocabulary/extensions.rb', line 291

def is_mandatory
  return fact_type.implying_role.is_mandatory if fact_type.is_a?(LinkFactType)
  all_role_ref.detect{|rr|
    rs = rr.role_sequence
    rs.all_role_ref.size == 1 and
    rs.all_presence_constraint.detect{|pc|
      pc.min_frequency and pc.min_frequency >= 1 and pc.is_mandatory
    }
  } ? true : false
end

#is_uniqueObject



314
315
316
317
318
319
320
321
# File 'lib/activefacts/vocabulary/extensions.rb', line 314

def is_unique
  all_role_ref.detect do |rr|
    rr.role_sequence.all_role_ref.size == 1 and
      rr.role_sequence.all_presence_constraint.detect do |pc|
        pc.max_frequency == 1 and !pc.enforcement   # Alethic uniqueness constraint
      end
  end
end

#nameObject



323
324
325
# File 'lib/activefacts/vocabulary/extensions.rb', line 323

def name
  role_name || object_type.name
end

#preferred_referenceObject



302
303
304
# File 'lib/activefacts/vocabulary/extensions.rb', line 302

def preferred_reference
  fact_type.preferred_reading.role_sequence.all_role_ref.detect{|rr| rr.role == self }
end

#role_typeObject

:nodoc:



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
# File 'lib/activefacts/persistence/tables.rb', line 128

def role_type
  # TypeInheritance roles are always 1:1
  if TypeInheritance === fact_type
    return object_type == fact_type.supertype ? :supertype : :subtype
  end

  # Always N:1 if unary:
  return :unary if fact_type.all_role.size == 1

  # List the UCs on this fact type:
  all_uniqueness_constraints =
    fact_type.all_role.map do |fact_role|
      fact_role.all_role_ref.map do |rr|
        rr.role_sequence.all_presence_constraint.select do |pc|
          pc.max_frequency == 1
        end
      end
    end.flatten.uniq

  to_1 =
    all_uniqueness_constraints.
      detect do |c|
          (rr = c.role_sequence.all_role_ref.single) and
          rr.role == self
      end
	# REVISIT: check mapping pragmas, e.g. by to_1.concept.all_concept_annotation.detect{|ca| ca.mapping_annotation == 'separate'}

  if fact_type.entity_type
    # This is a role in an objectified fact type
    from_1 = true
  else
    # It's to-1 if a UC exists over roles of this FT that doesn't cover this role:
    from_1 = all_uniqueness_constraints.detect{|uc|
      !uc.role_sequence.all_role_ref.detect{|rr| rr.role == self || rr.role.fact_type != fact_type}
    }
  end

  if from_1
    return to_1 ? :one_one : :one_many
  else
    return to_1 ? :many_one : :many_many
  end
end

#uniqueObject

Is there are internal uniqueness constraint on this role only?



281
282
283
284
285
286
287
288
289
# File 'lib/activefacts/vocabulary/extensions.rb', line 281

def unique
  all_role_ref.detect{|rr|
    rs = rr.role_sequence
    rs.all_role_ref.size == 1 and
    rs.all_presence_constraint.detect{|pc|
      pc.max_frequency == 1
    }
  } ? true : false
end