Class: ActiveFacts::CQL::Compiler::Constraint

Inherits:
Definition
  • Object
show all
Defined in:
lib/activefacts/cql/compiler/constraint.rb

Instance Attribute Summary

Attributes inherited from Definition

#constellation, #source, #vocabulary

Instance Method Summary collapse

Constructor Details

#initialize(context_note, enforcement, readings_lists = []) ⇒ Constraint

Returns a new instance of Constraint.



55
56
57
58
59
# File 'lib/activefacts/cql/compiler/constraint.rb', line 55

def initialize context_note, enforcement, readings_lists = []
  @context_note = context_note
  @enforcement = enforcement
  @readings_lists = readings_lists
end

Instance Method Details

#all_bindings_in_readings(readings) ⇒ Object

Return the unique array of all bindings in these readings, including in objectification joins



71
72
73
74
75
76
77
78
79
# File 'lib/activefacts/cql/compiler/constraint.rb', line 71

def all_bindings_in_readings readings
  readings.map do |reading|
    reading.role_refs.map do |rr|
      [rr.binding] + (rr.objectification_join ? all_bindings_in_readings(rr.objectification_join) : [])
    end
  end.
    flatten.
    uniq
end

#bind_readingsObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/activefacts/cql/compiler/constraint.rb', line 81

def bind_readings
  @context = CompilationContext.new(@vocabulary)

  @readings_lists.map do |readings_list|
    readings_list.each{ |reading| reading.identify_players_with_role_name(@context) }
    readings_list.each{ |reading| reading.identify_other_players(@context) }
    readings_list.each{ |reading| reading.bind_roles @context }  # Create the Compiler::Bindings
    readings_list.each do |reading| 
      fact_type = reading.match_existing_fact_type @context
      raise "Unrecognised fact type #{reading.inspect} in #{self.class}" unless fact_type
    end
  end

  loose_binding

  # Ok, we have bound all players by subscript/role_name, by adjectives, and by loose binding,
  # and matched all the fact types that matter. Now assemble a join (with all join steps) for
  # each join list, and build an array of the bindings that are involved in the join steps.
  @bindings_by_list =
    @readings_lists.map do |readings_list|
      all_bindings_in_readings(readings_list)
    end

  warn_ignored_joins
end

#common_bindingsObject



185
186
187
188
189
# File 'lib/activefacts/cql/compiler/constraint.rb', line 185

def common_bindings
  @common_bindings ||= @bindings_by_list[1..-1].inject(@bindings_by_list[0]) { |r, b| r & b }
  raise "#{self.class} must cover some of the same roles, see #{@bindings_by_list.inspect}" unless @common_bindings.size > 0
  @common_bindings
end

#compileObject



61
62
63
64
# File 'lib/activefacts/cql/compiler/constraint.rb', line 61

def compile
  @context_note.compile @constellation, @constraint if @context_note
  @constraint
end

#loose_bind_rolesObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/activefacts/cql/compiler/constraint.rb', line 150

def loose_bind_roles
  # Apply loose binding over applicable @roles:
  debug :binding, "Check for loose bindings on #{@roles.size} roles in #{self.class.name}" do
    @roles.each do |role_ref|
      role_ref.identify_player @context
      role_ref.bind @context
      if role_ref.binding.refs.size < @readings_lists.size+1
        debug :binding, "Insufficient bindings for #{role_ref.inspect} (#{role_ref.binding.refs.size}, expected #{@readings_lists.size+1}), attempting loose binding" do
          @readings_lists.each do |readings_list|
            candidates = []
            next if readings_list.
              detect do |reading|
                debug :binding, "Checking #{reading.inspect}"
                reading.role_refs.
                  detect do |rr|
                    already_bound = rr.binding == role_ref.binding
                    if !already_bound && rr.player == role_ref.player
                      candidates << rr
                    end
                    already_bound
                  end
              end
            debug :binding, "Attempting loose binding for #{role_ref.inspect} in #{readings_list.inspect}, from the following candidates: #{candidates.inspect}"

            if candidates.size == 1
              debug :binding, "Rebinding #{candidates[0].inspect} to #{role_ref.inspect}"
              candidates[0].rebind_to(@context, role_ref)
            end
          end
        end
      end
    end
  end
end

#loose_bind_wherever_possibleObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/activefacts/cql/compiler/constraint.rb', line 117

def loose_bind_wherever_possible
  # Apply loose binding over applicable roles:
  debug :binding, "Loose binding on #{self.class.name}" do
    @readings_lists.each do |readings_list|
      readings_list.each do |reading|
        reading.role_refs.each_with_index do |role_ref, i|
          next if role_ref.binding.refs.size > 1
#                  if reading.side_effects && !reading.side_effects.role_side_effects[i].residual_adjectives
#                    debug :binding, "Discounting #{role_ref.inspect} as needing loose binding because it has no residual_adjectives"
#                    next
#                  end
          # This role_ref didn't match any other role_ref. Have a scout around for a suitable partner
          candidates = @context.bindings.
            select do |key, binding|
              binding.player == role_ref.binding.player and
                binding != role_ref.binding and
                binding.role_name == role_ref.binding.role_name and  # Both will be nil if they match
                # REVISIT: Don't bind to a binding with a role occurrence in the same reading
                !binding.refs.detect{|rr|
                  x = rr.reading == reading
                  # puts "Discounting binding #{binding.inspect} as a match for #{role_ref.inspect} because it's already bound to a player in #{role_ref.reading.inspect}" if x
                  x
                }
            end.map{|k,b| b}
          next if candidates.size != 1  # Fail
          debug :binding, "Loose binding #{role_ref.inspect} to #{candidates[0].inspect}"
          role_ref.rebind_to(@context, candidates[0].refs[0])
        end
      end
    end
  end
end

#loose_bindingObject



66
67
68
# File 'lib/activefacts/cql/compiler/constraint.rb', line 66

def loose_binding
  # Override for constraint types that need loose binding (same role player matching with different adjectives)
end

#warn_ignored_joinsObject



107
108
109
110
111
112
113
114
115
# File 'lib/activefacts/cql/compiler/constraint.rb', line 107

def warn_ignored_joins
  # Warn about ignored joins
  @readings_lists.each do |readings_list|
    fact_types = readings_list.map{|join| join.role_refs[0].role_ref.role.fact_type}.uniq
    if fact_types.size > 1
      puts "------->>>> Join ignored in #{self.class}: #{fact_types.map{|ft| ft.preferred_reading.expand}*' and '}"
    end
  end
end