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, #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 = []
@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
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
|
#compile ⇒ Object
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_bind ⇒ Object
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
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_possible ⇒ Object
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
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
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 debug :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
63
64
65
|
# File 'lib/activefacts/cql/compiler/constraint.rb', line 63
def loose_binding
end
|
#to_s ⇒ Object
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_queries ⇒ Object
96
97
98
99
100
101
102
103
104
|
# File 'lib/activefacts/cql/compiler/constraint.rb', line 96
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
|