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, #tree, #vocabulary
Instance Method Summary
collapse
Methods inherited from Definition
#all_bindings_in_clauses, #assert_literal_value, #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 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 = []
@context = CompilationContext.new(@vocabulary)
@context.left_contraction_allowed = true
@context.bind @clauses_lists,
@clauses_lists.map do |clauses_list|
@context.left_contractable_clause = nil 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
loose_binding
@bindings_by_list =
@clauses_lists.map do |clauses_list|
all_bindings_in_clauses(clauses_list)
end
warn_ignored_queries
end
|
#common_bindings ⇒ Object
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
|
#compile ⇒ Object
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_bind ⇒ Object
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
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_possible ⇒ Object
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
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
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 !binding.refs.detect{|vr|
x = vr.clause == clause
x
}
end.map{|k,b| b}
next if candidates.size != 1 trace :binding, "Loose binding #{ref.inspect} to #{candidates[0].inspect}"
ref.rebind_to(@context, candidates[0].refs[0])
end
end
end
end
end
|
#loose_binding ⇒ Object
71
72
73
|
# File 'lib/activefacts/cql/compiler/constraint.rb', line 71
def loose_binding
end
|
#to_s ⇒ Object
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_queries ⇒ Object
104
105
106
107
108
109
110
111
112
|
# File 'lib/activefacts/cql/compiler/constraint.rb', line 104
def warn_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
|