Module: ActiveFacts::Metamodel

Defined in:
lib/activefacts/metamodel/version.rb,
lib/activefacts/metamodel/datatypes.rb,
lib/activefacts/metamodel/metamodel.rb,
lib/activefacts/metamodel/extensions.rb,
lib/activefacts/metamodel/validate/composition.rb

Defined Under Namespace

Classes: Absorption, AccessPath, Adjective, Agent, AgentName, Aggregate, AggregateCode, Aggregation, Agreement, AllowedRange, AlternativeSet, Annotation, Assimilation, Bound, Coefficient, Component, ComponentShape, Composite, CompositeGroup, Composition, CompoundMatching, Concept, ConceptAnnotation, Constraint, ConstraintShape, ContextAccordingTo, ContextAgreedBy, ContextNote, ContextNoteKind, DataType, Date, Denominator, Derivation, Description, Diagram, DiscriminatedRole, Discriminator, DisplayRoleNamesSetting, DomainObjectType, Enforcement, EnforcementCode, EntityType, EphemeraURL, Exponent, Expression, ExpressionObjectRef, ExpressionType, Fact, FactType, FactTypeShape, ForeignKey, ForeignKeyField, Frequency, FullAbsorption, Guid, ImplicationRule, ImplicationRuleName, Import, Index, IndexField, Indicator, Injection, Instance, LeafConstraint, Length, LinkFactType, Literal, LiteralString, LocalConstraint, Location, Mapping, MirrorRole, ModelNoteShape, Name, Nesting, NestingMode, Numerator, ORMDiagram, ObjectType, ObjectTypeShape, ObjectifiedFactTypeNameShape, Offset, Operator, Ordinal, Play, Population, PresenceConstraint, Pronoun, Query, Reading, ReadingShape, RegularExpression, RingConstraint, RingConstraintShape, RingType, Role, RoleDisplay, RoleNameShape, RoleRef, RoleSequence, RoleValue, RotationSetting, Scale, Scoping, SetComparisonConstraint, SetComparisonRoles, SetConstraint, SetEqualityConstraint, SetExclusionConstraint, Shape, SimpleMatching, SpanningConstraint, Step, Subscript, SubsetConstraint, SurrogateKey, TemporalMapping, Text, Topic, TransactionPhase, TransformMatching, TransformRule, TransformTargetRef, TypeInheritance, Unit, ValidFrom, Value, ValueConstraint, ValueConstraintShape, ValueField, ValueRange, ValueType, ValueTypeParameter, ValueTypeParameterRestriction, Variable, VersionNumber, VersionPattern, Vocabulary, X, Y

Constant Summary collapse

VERSION =
"1.9.20"

Class Method Summary collapse

Class Method Details

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

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



1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
# File 'lib/activefacts/metamodel/extensions.rb', line 1438

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
        # Find the objectification role here:
        return nil unless role.link_fact_type   # REVISIT: Link Fact Types are missing; happens from half-baked surrogate transform
        objectification_role = role.link_fact_type.all_role.detect{|r| !r.is_a?(MirrorRole)}
      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]
    trace :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]}
      trace :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