Class: ActiveFacts::Metamodel::Absorption

Inherits:
Mapping show all
Defined in:
lib/activefacts/metamodel/metamodel.rb,
lib/activefacts/metamodel/extensions.rb,
lib/activefacts/metamodel/validate/composition.rb

Constant Summary

Constants inherited from Component

Component::RANK_DISCRIMINATOR, Component::RANK_FOREIGN, Component::RANK_IDENT, Component::RANK_INDICATOR, Component::RANK_INJECTION, Component::RANK_MANDATORY, Component::RANK_MULTIPLE, Component::RANK_NON_MANDATORY, Component::RANK_SCOPING, Component::RANK_SUBTYPE, Component::RANK_SUPER, Component::RANK_SURROGATE, Component::RANK_VALUE

Instance Method Summary collapse

Methods inherited from Mapping

#all_leaf, #is_auto_assigned, #re_rank, #root, #validate_members, #validate_reverse

Methods inherited from Component

#data_type, #depth, #fork_to_new_parent, #in_foreign_key, #in_natural_index, #in_primary_index, #is_auto_assigned, #is_in_primary, #leaves, #narrow_value_constraint, #parent_entity_type, #path, #primary_index_components, #rank_key, #rank_kind, #rank_path, #root, #uncache_rank_key

Instance Method Details

#all_role ⇒ Object



1981
1982
1983
# File 'lib/activefacts/metamodel/extensions.rb', line 1981

def all_role
  ([child_role, parent_role] + all_nesting.map(&:index_role)).flat_map{|role| [role, role.base_role]}.uniq
end

#comment ⇒ Object



1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
# File 'lib/activefacts/metamodel/extensions.rb', line 1998

def comment
  return '' unless parent
  prefix = parent.comment
  reading = parent_role.fact_type.reading_preferably_starting_with_role(parent_role).expand([], false)
  maybe = parent_role.is_mandatory ? '' : 'maybe '
  parent_name = parent.name
  if prefix[(-parent_name.size-1)..-1] == ' '+parent_name && reading[0..parent_name.size] == parent_name+' '
    prefix+' that ' + maybe + reading[parent_name.size+1..-1]
  else
    (prefix.empty? ? '' : prefix+' and ') + maybe + reading
  end
end

#flip! ⇒ Object



1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
# File 'lib/activefacts/metamodel/extensions.rb', line 1966

def flip!
  raise "REVISIT: Need to flip FullAbsorption on #{inspect}" if full_absorption or reverse_mapping && reverse_mapping.full_absorption or forward_mapping && forward_mapping.full_absorption
  if (other = forward_mapping)
    # We point at them - make them point at us instead
    self.forward_mapping = nil
    self.reverse_mapping = other
  elsif (other = reverse_mapping)
    # They point at us - make us point at them instead
    self.reverse_mapping = nil
    self.forward_mapping = other
  else
    raise "Absorption cannot be flipped as it has no reverse"
  end
end

#inspect ⇒ Object



1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
# File 'lib/activefacts/metamodel/extensions.rb', line 1871

def inspect
  "#{super}#{full_absorption ? ' (full)' : ''
  } in #{inspect_reason}#{
    # If we have a related forward absorption, we're by definition a reverse absorption
    if forward_mapping
      ' (reverse)'
     else
        # If we have a related reverse absorption, we're by definition a forward absorption
        reverse_mapping ? ' (forward)' : ''
    end
  }"
end

#inspect_reason ⇒ Object



1867
1868
1869
# File 'lib/activefacts/metamodel/extensions.rb', line 1867

def inspect_reason
  parent_role.fact_type.reading_preferably_starting_with_role(parent_role).expand.inspect
end

#is_mandatory ⇒ Object



1990
1991
1992
# File 'lib/activefacts/metamodel/extensions.rb', line 1990

def is_mandatory
  parent_role.is_mandatory
end

#is_one_to_one ⇒ Object



1910
1911
1912
# File 'lib/activefacts/metamodel/extensions.rb', line 1910

def is_one_to_one
  parent_role.is_unique and child_role.is_unique
end

#is_partitioned_here ⇒ Object



1906
1907
1908
# File 'lib/activefacts/metamodel/extensions.rb', line 1906

def is_partitioned_here
  (ft = child_role.fact_type).is_a?(TypeInheritance) && ft.assimilation == 'partitioned'
end

#is_preferred_direction ⇒ Object



1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
# File 'lib/activefacts/metamodel/extensions.rb', line 1914

def is_preferred_direction
  return child_role.is_mirror_role if child_role.is_mirror_role != parent_role.is_mirror_role

  # Prefer to absorb the one into the many:
  p_un = parent_role.is_unique
  c_un = child_role.is_unique
  return p_un if p_un != c_un

  # Prefer to absorb a subtype into the supertype (opposite if separate or partitioned)
  if (ti = child_role.fact_type).is_a?(TypeInheritance)
    is_subtype = child_role == ti.subtype_role  # Supertype absorbing subtype
    subtype = ti.subtype_role.object_type       # Subtype doesn't want to be absorbed?
    # REVISIT: We need fewer ways to say this:
    child_separate = ["separate", "partitioned"].include?(ti.assimilation) ||
      subtype.is_independent ||
      subtype.concept.all_concept_annotation.detect{|ca| ca.mapping_annotation == 'separate'}
    return !is_subtype != !child_separate
  end

  if p_un && c_un
    # Prefer to absorb a ValueType into an EntityType rather than the other way around:
    pvt = parent_role.object_type.is_a?(ActiveFacts::Metamodel::ValueType)
    cvt = child_role.object_type.is_a?(ActiveFacts::Metamodel::ValueType)
    return cvt if pvt != cvt

    if !pvt
      # Force the decision if one EntityType identifies another
      return true if child_role.base_role.is_identifying  # Parent is identified by child role, correct
      return false if parent_role.base_role.is_identifying # Child is identified by parent role, incorrect
    end

    # Primary absorption absorbs the object playing the mandatory role into the non-mandatory:
    return child_role.is_mandatory if !parent_role.is_mandatory != !child_role.is_mandatory
  end

  if parent_role.object_type.is_a?(ActiveFacts::Metamodel::EntityType) &&
       child_role.object_type.is_a?(ActiveFacts::Metamodel::EntityType)
    # Prefer to absorb an identifying element into the EntityType it identifies
    return true if parent_role.object_type.preferred_identifier.
      role_sequence.all_role_ref.map(&:role).detect{|r|
        r.object_type == child_role.object_type
      }
    return false if child_role.object_type.preferred_identifier.
      role_sequence.all_role_ref.map(&:role).detect{|r|
        r.object_type == parent_role.object_type
      }
  end

  # For stability, absorb a later-named role into an earlier-named one:
  return parent_role.name < child_role.name
end

#is_subtype_absorption ⇒ Object



1902
1903
1904
# File 'lib/activefacts/metamodel/extensions.rb', line 1902

def is_subtype_absorption
  is_type_inheritance && parent_role.fact_type.supertype == object_type
end

#is_supertype_absorption ⇒ Object



1898
1899
1900
# File 'lib/activefacts/metamodel/extensions.rb', line 1898

def is_supertype_absorption
  is_type_inheritance && child_role.fact_type.supertype == object_type
end

#is_type_inheritance ⇒ Object



1894
1895
1896
# File 'lib/activefacts/metamodel/extensions.rb', line 1894

def is_type_inheritance
  child_role.fact_type.is_a?(TypeInheritance) && child_role.fact_type
end

#path_mandatory ⇒ Object



1994
1995
1996
# File 'lib/activefacts/metamodel/extensions.rb', line 1994

def path_mandatory
  is_mandatory && parent.path_mandatory
end

#show_trace ⇒ Object



1884
1885
1886
1887
1888
1889
1890
1891
1892
# File 'lib/activefacts/metamodel/extensions.rb', line 1884

def show_trace
  super() do
    if nesting_mode || all_nesting.size > 0
      trace :composition, "Nested using #{nesting_mode || 'unspecified'} mode" do
        all_nesting.sort_by(&:ordinal).each(&:show_trace)
      end
    end
  end
end

#validate_nesting(&report) ⇒ Object



135
136
137
138
139
140
141
# File 'lib/activefacts/metamodel/validate/composition.rb', line 135

def validate_nesting &report
  report.call(self, "REVISIT: Unexpected and unchecked Nesting")
  report.call(self, "Nesting Mode must be specified") unless self.nesting_mode
  # REVISIT: Nesting names must be unique
  # REVISIT: Nesting roles must be played by...
  # REVISIT: Nesting roles must be value types
end

#value_constraints ⇒ Object



1985
1986
1987
1988
# File 'lib/activefacts/metamodel/extensions.rb', line 1985

def value_constraints
  return [] unless object_type.is_a?(ValueType)
  object_type.supertypes_transitive.flat_map{|vt| Array(vt.value_constraint)}
end