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_steps, #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
# 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 = context_note.empty? ? nil : context_note.ast
  end
  @context_note = context_note
  @enforcement = enforcement
  @clauses_lists = clauses_lists
end

Instance Method Details

#bind_clauses(extra = []) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/activefacts/cql/compiler/constraint.rb', line 67

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



172
173
174
175
176
# File 'lib/activefacts/cql/compiler/constraint.rb', line 172

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



58
59
60
61
# File 'lib/activefacts/cql/compiler/constraint.rb', line 58

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

#loose_bindObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/activefacts/cql/compiler/constraint.rb', line 139

def loose_bind
  # Apply loose binding over applicable @roles:
  debug :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
        debug :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|
                debug :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
            debug :binding, "Attempting loose binding for #{ref.inspect} in #{clauses_list.inspect}, from the following candidates: #{candidates.inspect}"

            if candidates.size == 1
              debug :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



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/activefacts/cql/compiler/constraint.rb', line 106

def loose_bind_wherever_possible
  # Apply loose binding over applicable roles:
  debug :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
#                    debug :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
          debug :binding, "Loose binding #{ref.inspect} to #{candidates[0].inspect}"
          ref.rebind_to(@context, candidates[0].refs[0])
        end
      end
    end
  end
end

#loose_bindingObject



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

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

#to_sObject



178
179
180
# File 'lib/activefacts/cql/compiler/constraint.rb', line 178

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

#warn_ignored_queriesObject



96
97
98
99
100
101
102
103
104
# File 'lib/activefacts/cql/compiler/constraint.rb', line 96

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