Class: ActiveFacts::Metamodel::Role

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

Direct Known Subclasses

MirrorRole

Instance Method Summary collapse

Instance Method Details

#all_constraintObject

return an array of all the constraints on this role (not including ValueConstraint on a ValueType player)



382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/activefacts/metamodel/extensions.rb', line 382

def all_constraint
  (
    Array(role_value_constraint) +
    all_role_ref.to_a.flat_map do |rr|
      rr.role_sequence.all_presence_constraint.to_a +
      rr.role_sequence.all_subset_constraint_as_superset_role_sequence +
      rr.role_sequence.all_subset_constraint_as_subset_role_sequence +
      rr.role_sequence.all_set_comparison_roles.map(&:set_comparison_constraint)
    end +
    all_ring_constraint.to_a +
    all_ring_constraint_as_other_role.to_a
  ).uniq
end

#base_roleObject

Mirror Role defines this, but it’s more convenient not to have to type-check. A Role that’s not a Mirror Role is its own base role.



274
275
276
# File 'lib/activefacts/metamodel/extensions.rb', line 274

def base_role
  self
end

#counterpartObject



370
371
372
373
374
375
376
377
378
379
# File 'lib/activefacts/metamodel/extensions.rb', line 370

def counterpart
  case fact_type.all_role.size
  when 1
    self
  when 2
    (fact_type.all_role.to_a-[self])[0]
  else
    nil # raise "counterpart roles are undefined in n-ary fact types"
  end
end

#describe(highlight = nil) ⇒ Object



278
279
280
# File 'lib/activefacts/metamodel/extensions.rb', line 278

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.



299
300
301
302
303
304
305
# File 'lib/activefacts/metamodel/extensions.rb', line 299

def is_functional
  return true if fact_type.is_a?(LinkFactType) # Handle objectification roles

  fact_type.entity_type or
  fact_type.all_role.size != 2 or
  uniqueness_constraint
end

#is_identifyingObject



319
320
321
# File 'lib/activefacts/metamodel/extensions.rb', line 319

def is_identifying
  uc = uniqueness_constraint and uc.is_preferred_identifier
end


358
359
360
# File 'lib/activefacts/metamodel/extensions.rb', line 358

def is_link_role
  fact_type.is_a?(LinkFactType)
end

#is_mandatoryObject



282
283
284
285
286
287
288
289
290
291
# File 'lib/activefacts/metamodel/extensions.rb', line 282

def is_mandatory
  return true if fact_type.is_a?(LinkFactType) # Handle objectification roles
  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_mirror_roleObject



362
363
364
# File 'lib/activefacts/metamodel/extensions.rb', line 362

def is_mirror_role
  is_a?(MirrorRole)
end

#is_objectification_roleObject



366
367
368
# File 'lib/activefacts/metamodel/extensions.rb', line 366

def is_objectification_role
  is_link_role && !is_mirror_role
end

#is_uniqueObject

Is there are internal uniqueness constraint on this role only?



324
325
326
327
328
329
# File 'lib/activefacts/metamodel/extensions.rb', line 324

def is_unique
  return true if fact_type.is_a?(LinkFactType) or         # Handle objectification roles
    fact_type.all_role.size == 1                          # and unary roles

  uniqueness_constraint ? true : false
end

#nameObject



335
336
337
338
339
340
341
# File 'lib/activefacts/metamodel/extensions.rb', line 335

def name
  role_name or
  is_mirror_role && base_role.role_name or
  fact_type.is_unary && unary_name or
  String::Words.new(preferred_reference.role_name nil).capwords*' ' or
  object_type.name
end

#preferred_referenceObject



293
294
295
# File 'lib/activefacts/metamodel/extensions.rb', line 293

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

#unary_nameObject



343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/activefacts/metamodel/extensions.rb', line 343

def unary_name
  fact_type.
  preferred_reading.
  text.
  gsub(/(.*)\{[0-9]\}(.*)/) do
    if $1.empty? or $2.empty?
      "#{$1} #{$2}"
    else
      "#{$1} #{object_type.name} #{$2}"
    end
  end.
  words.
  titlewords*' '
end

#uniqueObject



331
332
333
# File 'lib/activefacts/metamodel/extensions.rb', line 331

def unique
  raise "REVISIT: unique is deprecated. Call is_unique instead"
end

#uniqueness_constraintObject

Find any internal uniqueness constraint on this role only



308
309
310
311
312
313
314
315
316
317
# File 'lib/activefacts/metamodel/extensions.rb', line 308

def uniqueness_constraint
  base_role.all_role_ref.detect{|rr|
    rs = rr.role_sequence
    rs.all_role_ref.size == 1 and
      rs.all_presence_constraint.detect do |pc|
        return pc if pc.max_frequency == 1 and !pc.enforcement   # Alethic uniqueness constraint
      end
  }
  nil
end