Class: ActiveFacts::Metamodel::Role

Inherits:
Object
  • Object
show all
Defined in:
lib/activefacts/rmap/tables.rb

Overview

EntityType class

Instance Method Summary collapse

Instance Method Details

#role_typeObject

:nodoc:



118
119
120
121
122
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
# File 'lib/activefacts/rmap/tables.rb', line 118

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