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, #tree, #vocabulary

Instance Method Summary collapse

Methods inherited from Definition

#all_bindings_in_clauses, #build_all_steps, #build_step, #build_variables, #source

Constructor Details

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

Returns a new instance of Constraint.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/activefacts/cql/compiler/constraint.rb', line 49

def initialize context_note, enforcement, clauses_lists = []
  if context_note.is_a?(Treetop::Runtime::SyntaxNode) && !context_note.empty?
    context_note = context_note.empty? ? nil : context_note.ast
  else
    context_note = nil  # Perhaps a context note got attached to one of the clauses. Steal it.
    clauses_lists.detect do |clauses_list|
      if c = clauses_list.last.context_note
        context_note = c
        clauses_list.last.context_note = nil
      end
    end
  end
  @context_note = context_note
  @enforcement = enforcement
  @clauses_lists = clauses_lists
end

Instance Method Details

#bind_clauses(extra = []) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/activefacts/cql/compiler/constraint.rb', line 75

def bind_clauses extra = []
  @context = CompilationContext.new(@vocabulary)
  @context.left_contraction_allowed = true

  @context.bind @clauses_lists, extra
  @clauses_lists.map do |clauses_list|
    @context.left_contractable_clause = nil # Don't contract outside this set of clauses
    clauses_list.each do |clause| 
      fact_type = clause.match_existing_fact_type @context
      raise "Unrecognised fact type #{clause.inspect} in #{self.class}" unless fact_type
      raise "Negated fact type #{clause.inspect} in #{self.class} is not yet supported" if clause.certainty == false
    end
  end

  # Any constrained roles will be first identified here.
  # This means that they can't introduce role names.
  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 query (with all steps) for
  # each query list, and build an array of the bindings that are involved in the steps.
  @bindings_by_list =
    @clauses_lists.map do |clauses_list|
      all_bindings_in_clauses(clauses_list)
    end

  warn_ignored_queries
end

#common_bindingsObject



180
181
182
183
184
# File 'lib/activefacts/cql/compiler/constraint.rb', line 180

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



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

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

#loose_bindObject



147
148
149
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
# File 'lib/activefacts/cql/compiler/constraint.rb', line 147

def loose_bind
  # Apply loose binding over applicable @roles:
  trace :binding, "Check for loose bindings on #{@roles.size} roles in #{self.class.name}" do
    @roles.each do |ref|
      if ref.binding.refs.size < @clauses_lists.size+1
        trace :binding, "Insufficient bindings for #{ref.inspect} (#{ref.binding.refs.size}, expected #{@clauses_lists.size+1}), attempting loose binding" do
          @clauses_lists.each do |clauses_list|
            candidates = []
            next if clauses_list.
              detect do |clause|
                trace :binding, "Checking #{clause.inspect}"
                clause.refs.
                  detect do |vr|
                    already_bound = vr.binding == ref.binding
                    if !already_bound && vr.player == ref.player
                      candidates << vr
                    end
                    already_bound
                  end
              end
            trace :binding, "Attempting loose binding for #{ref.inspect} in #{clauses_list.inspect}, from the following candidates: #{candidates.inspect}"

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

#loose_bind_wherever_possibleObject



114
115
116
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
# File 'lib/activefacts/cql/compiler/constraint.rb', line 114

def loose_bind_wherever_possible
  # Apply loose binding over applicable roles:
  trace :binding, "Loose binding on #{self.class.name}" do
    @clauses_lists.each do |clauses_list|
      clauses_list.each do |clause|
        clause.refs.each_with_index do |ref, i|
          next if ref.binding.refs.size > 1
#                  if clause.side_effects && !clause.side_effects.role_side_effects[i].residual_adjectives
#                    trace :binding, "Discounting #{ref.inspect} as needing loose binding because it has no residual_adjectives"
#                    next
#                  end
          # This ref didn't match any other ref. Have a scout around for a suitable partner
          candidates = @context.bindings.
            select do |key, binding|
              binding.player == ref.binding.player and
                binding != ref.binding and
                binding.role_name == 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 clause
                !binding.refs.detect{|vr|
                  x = vr.clause == clause
                  # puts "Discounting binding #{binding.inspect} as a match for #{ref.inspect} because it's already bound to a player in #{ref.clause.inspect}" if x
                  x
                }
            end.map{|k,b| b}
          next if candidates.size != 1  # Fail
          trace :binding, "Loose binding #{ref.inspect} to #{candidates[0].inspect}"
          ref.rebind_to(@context, candidates[0].refs[0])
        end
      end
    end
  end
end

#loose_bindingObject



71
72
73
# File 'lib/activefacts/cql/compiler/constraint.rb', line 71

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

#to_sObject



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

def to_s
  "#{self.class.name.sub(/.*::/,'')}" + (@clauses_lists.size > 0 ? " over #{@clauses_lists.inspect}" : '')
end

#warn_ignored_queriesObject



104
105
106
107
108
109
110
111
112
# File 'lib/activefacts/cql/compiler/constraint.rb', line 104

def warn_ignored_queries
  # Warn about ignored queries
  @clauses_lists.each do |clauses_list|
    fact_types = clauses_list.map{|clauses| (rr = clauses.refs[0].role_ref) && rr.role.fact_type}.compact.uniq
    if fact_types.size > 1
      raise "------->>>> join ignored in #{self.class}: #{fact_types.map{|ft| ft.preferred_reading.expand}*' and '}"
    end
  end
end