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

Methods inherited from Component

#data_type, #depth, #fork_to_new_parent, #in_foreign_key, #in_primary_index, #is_auto_assigned, #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_roleObject



1949
1950
1951
# File 'lib/activefacts/metamodel/extensions.rb', line 1949

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

#commentObject



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

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



1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
# File 'lib/activefacts/metamodel/extensions.rb', line 1934

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

#inspectObject



1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
# File 'lib/activefacts/metamodel/extensions.rb', line 1847

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

#inspect_readingObject



1843
1844
1845
# File 'lib/activefacts/metamodel/extensions.rb', line 1843

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

#is_mandatoryObject



1958
1959
1960
# File 'lib/activefacts/metamodel/extensions.rb', line 1958

def is_mandatory
  parent_role.is_mandatory
end

#is_preferred_directionObject



1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
# File 'lib/activefacts/metamodel/extensions.rb', line 1882

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_absorptionObject



1878
1879
1880
# File 'lib/activefacts/metamodel/extensions.rb', line 1878

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

#is_supertype_absorptionObject



1874
1875
1876
# File 'lib/activefacts/metamodel/extensions.rb', line 1874

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

#is_type_inheritanceObject



1870
1871
1872
# File 'lib/activefacts/metamodel/extensions.rb', line 1870

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

#path_mandatoryObject



1962
1963
1964
# File 'lib/activefacts/metamodel/extensions.rb', line 1962

def path_mandatory
  is_mandatory && parent.path_mandatory
end

#show_traceObject



1860
1861
1862
1863
1864
1865
1866
1867
1868
# File 'lib/activefacts/metamodel/extensions.rb', line 1860

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



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

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

#validate_reverse(&report) ⇒ Object



129
130
131
132
133
134
# File 'lib/activefacts/metamodel/validate/composition.rb', line 129

def validate_reverse &report
  reverse = forward_absorption || reverse_absorption
  return unless reverse
  report.call(self, "Opposite absorption's child role #{reverse.child_role.name} should match parent role #{parent_role.name}") unless reverse.child_role == parent_role
  report.call(self, "Opposite absorption's parent role #{reverse.parent_role.name} should match child role #{child_role.name}") unless reverse.parent_role == child_role
end

#value_constraintsObject



1953
1954
1955
1956
# File 'lib/activefacts/metamodel/extensions.rb', line 1953

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