Class: ActiveFacts::CQL::Compiler::Constraint
- Inherits:
-
Definition
- Object
- Definition
- ActiveFacts::CQL::Compiler::Constraint
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_readings ⇒ Object
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 } 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
@bindings_by_list =
@readings_lists.map do |readings_list|
all_bindings_in_readings(readings_list)
end
warn_ignored_joins
end
|
#common_bindings ⇒ Object
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
|
#compile ⇒ Object
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_roles ⇒ Object
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
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_possible ⇒ Object
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
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
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 !binding.refs.detect{|rr|
x = rr.reading == reading
x
}
end.map{|k,b| b}
next if candidates.size != 1 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_binding ⇒ Object
66
67
68
|
# File 'lib/activefacts/cql/compiler/constraint.rb', line 66
def loose_binding
end
|
#warn_ignored_joins ⇒ Object
107
108
109
110
111
112
113
114
115
|
# File 'lib/activefacts/cql/compiler/constraint.rb', line 107
def warn_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
|