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



30
31
32
# File 'lib/activefacts/vocabulary/extensions.rb', line 30

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

#is_mandatoryObject



45
46
47
48
49
50
51
52
53
# File 'lib/activefacts/vocabulary/extensions.rb', line 45

def is_mandatory
  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
    }
  }
end

#preferred_referenceObject

Return the RoleRef to this role from its fact type’s preferred_reading



56
57
58
# File 'lib/activefacts/vocabulary/extensions.rb', line 56

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

#role_typeObject

:nodoc:



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

def role_type
  # TypeInheritance roles are always 1:1
  if TypeInheritance === fact_type
    return concept == 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?



35
36
37
38
39
40
41
42
43
# File 'lib/activefacts/vocabulary/extensions.rb', line 35

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
    }
  }
end