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



153
154
155
# File 'lib/activefacts/vocabulary/extensions.rb', line 153

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.



185
186
187
188
189
190
191
192
193
194
# File 'lib/activefacts/vocabulary/extensions.rb', line 185

def is_functional
  fact_type.entity_type or
  fact_type.all_role.size != 2 or
    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

#is_mandatoryObject



168
169
170
171
172
173
174
175
176
177
# File 'lib/activefacts/vocabulary/extensions.rb', line 168

def is_mandatory
  link_fact_type ||
  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

#preferred_referenceObject



179
180
181
# File 'lib/activefacts/vocabulary/extensions.rb', line 179

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
# 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

  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?



158
159
160
161
162
163
164
165
166
# File 'lib/activefacts/vocabulary/extensions.rb', line 158

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