Method: ActiveFacts::CQL::Compiler::FactType#verify_matching_roles

Defined in:
lib/activefacts/cql/compiler/fact_type.rb

#verify_matching_rolesObject



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/activefacts/cql/compiler/fact_type.rb', line 346

def verify_matching_roles
  refs_by_clause_and_key = {}
  clauses_by_refs =
    @clauses.inject({}) do |hash, clause|
      keys = clause.refs.map do |ref|
        key = ref.key.compact
        refs_by_clause_and_key[[clause, key]] = ref
        key
      end.sort_by{|a| a.map{|k|k.to_s}}
      raise "Fact types may not have duplicate roles" if keys.uniq.size < keys.size
      (hash[keys] ||= []) << clause
      hash
    end

  if clauses_by_refs.size != 1 and @conditions.empty?
    # Attempt loose binding here; it might merge some Compiler::References to share the same Variables
    variants = clauses_by_refs.keys
    (clauses_by_refs.size-1).downto(1) do |m|   # Start with the last one
      0.upto(m-1) do |l|                              # Try to rebind onto any lower one
        common = variants[m]&variants[l]
        clauses_l = clauses_by_refs[variants[l]]
        clauses_m = clauses_by_refs[variants[m]]
        l_keys = variants[l]-common
        m_keys = variants[m]-common
        trace :binding, "Try to collapse variant #{m} onto #{l}; diffs are #{l_keys.inspect} -> #{m_keys.inspect}"
        rebindings = 0
        l_keys.each_with_index do |l_key, i|
          # Find possible rebinding candidates; there must be exactly one.
          candidates = []
          (0...m_keys.size).each do |j|
            m_key = m_keys[j]
            l_ref = refs_by_clause_and_key[[clauses_l[0], l_key]]
            m_ref = refs_by_clause_and_key[[clauses_m[0], m_key]]
            trace :binding, "Can we match #{l_ref.inspect} (#{i}) with #{m_ref.inspect} (#{j})?"
            next if m_ref.player != l_ref.player
            if has_more_adjectives(m_ref, l_ref)
              trace :binding, "can rebind #{m_ref.inspect} to #{l_ref.inspect}"
              candidates << [m_ref, l_ref]
            elsif has_more_adjectives(l_ref, m_ref)
              trace :binding, "can rebind #{l_ref.inspect} to #{m_ref.inspect}"
              candidates << [l_ref, m_ref]
            end
          end

          # trace :binding, "found #{candidates.size} rebinding candidates for this role"
          trace :binding, "rebinding is ambiguous so not attempted" if candidates.size > 1
          if (candidates.size == 1)
            candidates[0][0].rebind_to(@context, candidates[0][1])
            rebindings += 1
          end

        end
        if (rebindings == l_keys.size)
          # Successfully rebound this fact type
          trace :binding, "Successfully rebound clauses #{clauses_l.map{|r|r.inspect}*'; '} on to #{clauses_m.map{|r|r.inspect}*'; '}"
          break
        else
          # No point continuing, we failed on this one.
          raise "All readings in a fact type definition must have matching role players, compare (#{
              clauses_by_refs.keys.map do |keys|
                keys.map{|key| key*'-' }*", "
              end*") with ("
            })"
        end

      end
    end
  # else all clauses already matched
  end
end