Module: ActiveFacts::Metamodel

Defined in:
lib/activefacts/persistence/index.rb,
lib/activefacts/generate/cql.rb,
lib/activefacts/mapping/rails.rb,
lib/activefacts/persistence/tables.rb,
lib/activefacts/persistence/columns.rb,
lib/activefacts/vocabulary/metamodel.rb,
lib/activefacts/persistence/reference.rb,
lib/activefacts/vocabulary/extensions.rb,
lib/activefacts/vocabulary/verbaliser.rb,
lib/activefacts/generate/helpers/rails.rb,
lib/activefacts/persistence/foreignkey.rb,
lib/activefacts/generate/transform/surrogate.rb

Overview

:nodoc:

Defined Under Namespace

Classes: Adjective, Agent, AgentName, Aggregate, AggregateCode, Aggregation, Agreement, AllowedRange, AlternativeSet, Assimilation, Bound, Coefficient, Concept, Constraint, ConstraintShape, ContextAccordingTo, ContextAgreedBy, ContextNote, ContextNoteKind, Date, Denominator, Derivation, Diagram, Discussion, DisplayRoleNamesSetting, DomainObjectType, Enforcement, EnforcementCode, EntityType, EphemeraURL, Exponent, Facet, FacetRestriction, Fact, FactType, FactTypeShape, Frequency, Guid, ImplicationRule, ImplicationRuleName, ImplicitBooleanValueType, Instance, Length, LinkFactType, Literal, Location, ModelNoteShape, Name, Numerator, ORMDiagram, ObjectType, ObjectTypeShape, ObjectifiedFactTypeNameShape, Offset, Ordinal, Play, Population, PresenceConstraint, Pronoun, Query, Reading, ReadingShape, RegularExpression, RingConstraint, RingConstraintShape, RingType, Role, RoleDisplay, RoleNameShape, RoleRef, RoleSequence, RoleValue, RotationSetting, Scale, SetComparisonConstraint, SetComparisonRoles, SetConstraint, SetEqualityConstraint, SetExclusionConstraint, Shape, Step, Subscript, SubsetConstraint, Text, TransactionPhase, TypeInheritance, Unit, Value, ValueConstraint, ValueConstraintShape, ValueRange, ValueType, Variable, Verbaliser, Vocabulary, X, Y

Class Method Summary collapse

Class Method Details

.plays_over(roles, options = :both) ⇒ Object

Some queries must be over the proximate roles, some over the counterpart roles. Return the common superclass of the appropriate roles, and the actual roles



915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
# File 'lib/activefacts/vocabulary/extensions.rb', line 915

def self.plays_over roles, options = :both   # Or :proximate, :counterpart
  # If we can stay inside this objectified FT, there's no query:
  roles = Array(roles)  # To be safe, in case we get a role collection proxy
  return nil if roles.size == 1 or
    options != :counterpart && roles.map{|role| role.fact_type}.uniq.size == 1
  proximate_sups, counterpart_sups, obj_sups, counterpart_roles, objectification_roles =
    *roles.inject(nil) do |d_c_o, role|
      object_type = role.object_type
      fact_type = role.fact_type

      proximate_role_supertypes = object_type.supertypes_transitive

      # A role in an objectified fact type may indicate either the objectification or the counterpart player.
      # This could be ambiguous. Figure out both and prefer the counterpart over the objectification.
      counterpart_role_supertypes =
        if fact_type.all_role.size > 2
          possible_roles = fact_type.all_role.select{|r| d_c_o && d_c_o[1].include?(r.object_type) }
          if possible_roles.size == 1 # Only one candidate matches the types of the possible variables
            counterpart_role = possible_roles[0]
            d_c_o[1]  # No change
          else
            # puts "#{constraint_type} #{name}: Awkward, try counterpart-role query on a >2ary '#{fact_type.default_reading}'"
            # Try all roles; hopefully we don't have two roles with a matching candidate here:
            # Find which role is compatible with the existing supertypes, if any
            if d_c_o
              st = nil
              counterpart_role =
                fact_type.all_role.detect{|r| ((st = r.object_type.supertypes_transitive) & d_c_o[1]).size > 0}
              st
            else
              counterpart_role = nil  # This can't work, we don't have any basis for a decision (must be objectification)
              []
            end
            #fact_type.all_role.map{|r| r.object_type.supertypes_transitive}.flatten.uniq
          end
        else
          # Get the supertypes of the counterpart role (care with unaries):
          ftr = role.fact_type.all_role.to_a
          (counterpart_role = ftr[0] == role ? ftr[-1] : ftr[0]).object_type.supertypes_transitive
        end

      if fact_type.entity_type
        objectification_role_supertypes =
          fact_type.entity_type.supertypes_transitive+object_type.supertypes_transitive
        objectification_role = role.link_fact_type.all_role.single # Find the phantom role here
      else
        objectification_role_supertypes = counterpart_role_supertypes
        objectification_role = counterpart_role
      end

      if !d_c_o
        d_c_o = [proximate_role_supertypes, counterpart_role_supertypes, objectification_role_supertypes, [counterpart_role], [objectification_role]]
        #puts "role player supertypes starts #{d_c_o.map{|dco| dco.map(&:name).inspect}*' or '}"
      else
        #puts "continues #{[proximate_role_supertypes, counterpart_role_supertypes, objectification_role_supertypes]map{|dco| dco.map(&:name).inspect}*' or '}"
        d_c_o[0] &= proximate_role_supertypes
        d_c_o[1] &= counterpart_role_supertypes
        d_c_o[2] &= objectification_role_supertypes
        d_c_o[3] << (counterpart_role || objectification_role)
        d_c_o[4] << (objectification_role || counterpart_role)
      end
      d_c_o
    end # inject

  # Discount a subtype step over an object type that's not a player here,
  # if we can use an objectification step to an object type that is:
  if counterpart_sups.size > 0 && obj_sups.size > 0 && counterpart_sups[0] != obj_sups[0]
    debug :query, "ambiguous query, could be over #{counterpart_sups[0].name} or #{obj_sups[0].name}"
    if !roles.detect{|r| r.object_type == counterpart_sups[0]} and roles.detect{|r| r.object_type == obj_sups[0]}
      debug :query, "discounting #{counterpart_sups[0].name} in favour of direct objectification"
      counterpart_sups = []
    end
  end

  # Choose the first entry in the first non-empty supertypes list:
  if options != :counterpart && proximate_sups[0]
    [ proximate_sups[0], roles ]
  elsif !counterpart_sups.empty?
    [ counterpart_sups[0], counterpart_roles ]
  else
    [ obj_sups[0], objectification_roles ]
  end
end